My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*
  2. temperature.c - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "temperature.h"
  26. #include "watchdog.h"
  27. #include "Sd2PinMap.h"
  28. //===========================================================================
  29. //=============================public variables============================
  30. //===========================================================================
  31. int target_temperature[EXTRUDERS] = { 0 };
  32. int target_temperature_bed = 0;
  33. int current_temperature_raw[EXTRUDERS] = { 0 };
  34. float current_temperature[EXTRUDERS] = { 0.0 };
  35. int current_temperature_bed_raw = 0;
  36. float current_temperature_bed = 0.0;
  37. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  38. int redundant_temperature_raw = 0;
  39. float redundant_temperature = 0.0;
  40. #endif
  41. #ifdef PIDTEMPBED
  42. float bedKp=DEFAULT_bedKp;
  43. float bedKi=(DEFAULT_bedKi*PID_dT);
  44. float bedKd=(DEFAULT_bedKd/PID_dT);
  45. #endif //PIDTEMPBED
  46. #ifdef FAN_SOFT_PWM
  47. unsigned char fanSpeedSoftPwm;
  48. #endif
  49. unsigned char soft_pwm_bed;
  50. #ifdef BABYSTEPPING
  51. volatile int babystepsTodo[3]={0,0,0};
  52. #endif
  53. #ifdef FILAMENT_SENSOR
  54. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  55. #endif
  56. //===========================================================================
  57. //=============================private variables============================
  58. //===========================================================================
  59. static volatile bool temp_meas_ready = false;
  60. #ifdef PIDTEMP
  61. //static cannot be external:
  62. static float temp_iState[EXTRUDERS] = { 0 };
  63. static float temp_dState[EXTRUDERS] = { 0 };
  64. static float pTerm[EXTRUDERS];
  65. static float iTerm[EXTRUDERS];
  66. static float dTerm[EXTRUDERS];
  67. //int output;
  68. static float pid_error[EXTRUDERS];
  69. static float temp_iState_min[EXTRUDERS];
  70. static float temp_iState_max[EXTRUDERS];
  71. // static float pid_input[EXTRUDERS];
  72. // static float pid_output[EXTRUDERS];
  73. static bool pid_reset[EXTRUDERS];
  74. #endif //PIDTEMP
  75. #ifdef PIDTEMPBED
  76. //static cannot be external:
  77. static float temp_iState_bed = { 0 };
  78. static float temp_dState_bed = { 0 };
  79. static float pTerm_bed;
  80. static float iTerm_bed;
  81. static float dTerm_bed;
  82. //int output;
  83. static float pid_error_bed;
  84. static float temp_iState_min_bed;
  85. static float temp_iState_max_bed;
  86. #else //PIDTEMPBED
  87. static unsigned long previous_millis_bed_heater;
  88. #endif //PIDTEMPBED
  89. static unsigned char soft_pwm[EXTRUDERS];
  90. #ifdef FAN_SOFT_PWM
  91. static unsigned char soft_pwm_fan;
  92. #endif
  93. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  94. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  95. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  96. static unsigned long extruder_autofan_last_check;
  97. #endif
  98. #if EXTRUDERS > 4
  99. # error Unsupported number of extruders
  100. #elif EXTRUDERS > 3
  101. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
  102. #elif EXTRUDERS > 2
  103. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3 }
  104. #elif EXTRUDERS > 1
  105. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2 }
  106. #else
  107. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
  108. #endif
  109. #ifdef PIDTEMP
  110. #ifdef PID_PARAMS_PER_EXTRUDER
  111. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp);
  112. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT);
  113. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT);
  114. #ifdef PID_ADD_EXTRUSION_RATE
  115. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc);
  116. #endif // PID_ADD_EXTRUSION_RATE
  117. #else //PID_PARAMS_PER_EXTRUDER
  118. float Kp = DEFAULT_Kp;
  119. float Ki = DEFAULT_Ki * PID_dT;
  120. float Kd = DEFAULT_Kd / PID_dT;
  121. #ifdef PID_ADD_EXTRUSION_RATE
  122. float Kc = DEFAULT_Kc;
  123. #endif // PID_ADD_EXTRUSION_RATE
  124. #endif // PID_PARAMS_PER_EXTRUDER
  125. #endif //PIDTEMP
  126. // Init min and max temp with extreme values to prevent false errors during startup
  127. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  128. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  129. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0, 0 );
  130. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 );
  131. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  132. #ifdef BED_MAXTEMP
  133. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  134. #endif
  135. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  136. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  137. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  138. #else
  139. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE, (void *)HEATER_3_TEMPTABLE );
  140. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN );
  141. #endif
  142. static float analog2temp(int raw, uint8_t e);
  143. static float analog2tempBed(int raw);
  144. static void updateTemperaturesFromRawValues();
  145. #ifdef WATCH_TEMP_PERIOD
  146. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  147. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  148. #endif //WATCH_TEMP_PERIOD
  149. #ifndef SOFT_PWM_SCALE
  150. #define SOFT_PWM_SCALE 0
  151. #endif
  152. #ifdef FILAMENT_SENSOR
  153. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  154. #endif
  155. //===========================================================================
  156. //============================= functions ============================
  157. //===========================================================================
  158. void PID_autotune(float temp, int extruder, int ncycles)
  159. {
  160. float input = 0.0;
  161. int cycles=0;
  162. bool heating = true;
  163. unsigned long temp_millis = millis();
  164. unsigned long t1=temp_millis;
  165. unsigned long t2=temp_millis;
  166. long t_high = 0;
  167. long t_low = 0;
  168. long bias, d;
  169. float Ku, Tu;
  170. float Kp, Ki, Kd;
  171. float max = 0, min = 10000;
  172. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  173. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  174. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
  175. (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
  176. unsigned long extruder_autofan_last_check = millis();
  177. #endif
  178. if ((extruder >= EXTRUDERS)
  179. #if (TEMP_BED_PIN <= -1)
  180. ||(extruder < 0)
  181. #endif
  182. ){
  183. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  184. return;
  185. }
  186. SERIAL_ECHOLN("PID Autotune start");
  187. disable_heater(); // switch off all heaters.
  188. if (extruder<0)
  189. {
  190. soft_pwm_bed = (MAX_BED_POWER)/2;
  191. bias = d = (MAX_BED_POWER)/2;
  192. }
  193. else
  194. {
  195. soft_pwm[extruder] = (PID_MAX)/2;
  196. bias = d = (PID_MAX)/2;
  197. }
  198. for(;;) {
  199. if(temp_meas_ready == true) { // temp sample ready
  200. updateTemperaturesFromRawValues();
  201. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  202. max=max(max,input);
  203. min=min(min,input);
  204. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  205. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  206. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
  207. (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
  208. if(millis() - extruder_autofan_last_check > 2500) {
  209. checkExtruderAutoFans();
  210. extruder_autofan_last_check = millis();
  211. }
  212. #endif
  213. if(heating == true && input > temp) {
  214. if(millis() - t2 > 5000) {
  215. heating=false;
  216. if (extruder<0)
  217. soft_pwm_bed = (bias - d) >> 1;
  218. else
  219. soft_pwm[extruder] = (bias - d) >> 1;
  220. t1=millis();
  221. t_high=t1 - t2;
  222. max=temp;
  223. }
  224. }
  225. if(heating == false && input < temp) {
  226. if(millis() - t1 > 5000) {
  227. heating=true;
  228. t2=millis();
  229. t_low=t2 - t1;
  230. if(cycles > 0) {
  231. bias += (d*(t_high - t_low))/(t_low + t_high);
  232. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  233. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  234. else d = bias;
  235. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  236. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  237. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  238. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  239. if(cycles > 2) {
  240. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  241. Tu = ((float)(t_low + t_high)/1000.0);
  242. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  243. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  244. Kp = 0.6*Ku;
  245. Ki = 2*Kp/Tu;
  246. Kd = Kp*Tu/8;
  247. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  248. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  249. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  250. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  251. /*
  252. Kp = 0.33*Ku;
  253. Ki = Kp/Tu;
  254. Kd = Kp*Tu/3;
  255. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  256. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  257. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  258. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  259. Kp = 0.2*Ku;
  260. Ki = 2*Kp/Tu;
  261. Kd = Kp*Tu/3;
  262. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  263. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  264. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  265. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  266. */
  267. }
  268. }
  269. if (extruder<0)
  270. soft_pwm_bed = (bias + d) >> 1;
  271. else
  272. soft_pwm[extruder] = (bias + d) >> 1;
  273. cycles++;
  274. min=temp;
  275. }
  276. }
  277. }
  278. if(input > (temp + 20)) {
  279. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  280. return;
  281. }
  282. if(millis() - temp_millis > 2000) {
  283. int p;
  284. if (extruder<0){
  285. p=soft_pwm_bed;
  286. SERIAL_PROTOCOLPGM("ok B:");
  287. }else{
  288. p=soft_pwm[extruder];
  289. SERIAL_PROTOCOLPGM("ok T:");
  290. }
  291. SERIAL_PROTOCOL(input);
  292. SERIAL_PROTOCOLPGM(" @:");
  293. SERIAL_PROTOCOLLN(p);
  294. temp_millis = millis();
  295. }
  296. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  297. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  298. return;
  299. }
  300. if(cycles > ncycles) {
  301. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  302. return;
  303. }
  304. lcd_update();
  305. }
  306. }
  307. void updatePID()
  308. {
  309. #ifdef PIDTEMP
  310. for(int e = 0; e < EXTRUDERS; e++) {
  311. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  312. }
  313. #endif
  314. #ifdef PIDTEMPBED
  315. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  316. #endif
  317. }
  318. int getHeaterPower(int heater) {
  319. if (heater<0)
  320. return soft_pwm_bed;
  321. return soft_pwm[heater];
  322. }
  323. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  324. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  325. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  326. #if defined(FAN_PIN) && FAN_PIN > -1
  327. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  328. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  329. #endif
  330. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  331. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  332. #endif
  333. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  334. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  335. #endif
  336. #endif
  337. void setExtruderAutoFanState(int pin, bool state)
  338. {
  339. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  340. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  341. pinMode(pin, OUTPUT);
  342. digitalWrite(pin, newFanSpeed);
  343. analogWrite(pin, newFanSpeed);
  344. }
  345. void checkExtruderAutoFans()
  346. {
  347. uint8_t fanState = 0;
  348. // which fan pins need to be turned on?
  349. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  350. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  351. fanState |= 1;
  352. #endif
  353. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  354. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  355. {
  356. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  357. fanState |= 1;
  358. else
  359. fanState |= 2;
  360. }
  361. #endif
  362. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  363. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  364. {
  365. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  366. fanState |= 1;
  367. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  368. fanState |= 2;
  369. else
  370. fanState |= 4;
  371. }
  372. #endif
  373. #if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
  374. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  375. {
  376. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  377. fanState |= 1;
  378. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  379. fanState |= 2;
  380. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  381. fanState |= 4;
  382. else
  383. fanState |= 8;
  384. }
  385. #endif
  386. // update extruder auto fan states
  387. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  388. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  389. #endif
  390. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  391. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  392. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  393. #endif
  394. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  395. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  396. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  397. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  398. #endif
  399. #if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
  400. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  401. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  402. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  403. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  404. #endif
  405. }
  406. #endif // any extruder auto fan pins set
  407. void manage_heater()
  408. {
  409. float pid_input;
  410. float pid_output;
  411. if(temp_meas_ready != true) //better readability
  412. return;
  413. updateTemperaturesFromRawValues();
  414. for(int e = 0; e < EXTRUDERS; e++)
  415. {
  416. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  417. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_RUNAWAY_PROTECTION_PERIOD, THERMAL_RUNAWAY_PROTECTION_HYSTERESIS);
  418. #endif
  419. #ifdef PIDTEMP
  420. pid_input = current_temperature[e];
  421. #ifndef PID_OPENLOOP
  422. pid_error[e] = target_temperature[e] - pid_input;
  423. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  424. pid_output = BANG_MAX;
  425. pid_reset[e] = true;
  426. }
  427. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  428. pid_output = 0;
  429. pid_reset[e] = true;
  430. }
  431. else {
  432. if(pid_reset[e] == true) {
  433. temp_iState[e] = 0.0;
  434. pid_reset[e] = false;
  435. }
  436. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  437. temp_iState[e] += pid_error[e];
  438. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  439. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  440. //K1 defined in Configuration.h in the PID settings
  441. #define K2 (1.0-K1)
  442. dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  443. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  444. if (pid_output > PID_MAX) {
  445. if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  446. pid_output=PID_MAX;
  447. } else if (pid_output < 0){
  448. if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  449. pid_output=0;
  450. }
  451. }
  452. temp_dState[e] = pid_input;
  453. #else
  454. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  455. #endif //PID_OPENLOOP
  456. #ifdef PID_DEBUG
  457. SERIAL_ECHO_START;
  458. SERIAL_ECHO(" PID_DEBUG ");
  459. SERIAL_ECHO(e);
  460. SERIAL_ECHO(": Input ");
  461. SERIAL_ECHO(pid_input);
  462. SERIAL_ECHO(" Output ");
  463. SERIAL_ECHO(pid_output);
  464. SERIAL_ECHO(" pTerm ");
  465. SERIAL_ECHO(pTerm[e]);
  466. SERIAL_ECHO(" iTerm ");
  467. SERIAL_ECHO(iTerm[e]);
  468. SERIAL_ECHO(" dTerm ");
  469. SERIAL_ECHOLN(dTerm[e]);
  470. #endif //PID_DEBUG
  471. #else /* PID off */
  472. pid_output = 0;
  473. if(current_temperature[e] < target_temperature[e]) {
  474. pid_output = PID_MAX;
  475. }
  476. #endif
  477. // Check if temperature is within the correct range
  478. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  479. {
  480. soft_pwm[e] = (int)pid_output >> 1;
  481. }
  482. else {
  483. soft_pwm[e] = 0;
  484. }
  485. #ifdef WATCH_TEMP_PERIOD
  486. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  487. {
  488. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  489. {
  490. setTargetHotend(0, e);
  491. LCD_MESSAGEPGM("Heating failed");
  492. SERIAL_ECHO_START;
  493. SERIAL_ECHOLN("Heating failed");
  494. }else{
  495. watchmillis[e] = 0;
  496. }
  497. }
  498. #endif
  499. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  500. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  501. disable_heater();
  502. if(IsStopped() == false) {
  503. SERIAL_ERROR_START;
  504. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  505. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  506. }
  507. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  508. Stop();
  509. #endif
  510. }
  511. #endif
  512. } // End extruder for loop
  513. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  514. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  515. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  516. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  517. {
  518. checkExtruderAutoFans();
  519. extruder_autofan_last_check = millis();
  520. }
  521. #endif
  522. #ifndef PIDTEMPBED
  523. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  524. return;
  525. previous_millis_bed_heater = millis();
  526. #endif
  527. #if TEMP_SENSOR_BED != 0
  528. #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
  529. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
  530. #endif
  531. #ifdef PIDTEMPBED
  532. pid_input = current_temperature_bed;
  533. #ifndef PID_OPENLOOP
  534. pid_error_bed = target_temperature_bed - pid_input;
  535. pTerm_bed = bedKp * pid_error_bed;
  536. temp_iState_bed += pid_error_bed;
  537. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  538. iTerm_bed = bedKi * temp_iState_bed;
  539. //K1 defined in Configuration.h in the PID settings
  540. #define K2 (1.0-K1)
  541. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  542. temp_dState_bed = pid_input;
  543. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  544. if (pid_output > MAX_BED_POWER) {
  545. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  546. pid_output=MAX_BED_POWER;
  547. } else if (pid_output < 0){
  548. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  549. pid_output=0;
  550. }
  551. #else
  552. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  553. #endif //PID_OPENLOOP
  554. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  555. {
  556. soft_pwm_bed = (int)pid_output >> 1;
  557. }
  558. else {
  559. soft_pwm_bed = 0;
  560. }
  561. #elif !defined(BED_LIMIT_SWITCHING)
  562. // Check if temperature is within the correct range
  563. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  564. {
  565. if(current_temperature_bed >= target_temperature_bed)
  566. {
  567. soft_pwm_bed = 0;
  568. }
  569. else
  570. {
  571. soft_pwm_bed = MAX_BED_POWER>>1;
  572. }
  573. }
  574. else
  575. {
  576. soft_pwm_bed = 0;
  577. WRITE(HEATER_BED_PIN,LOW);
  578. }
  579. #else //#ifdef BED_LIMIT_SWITCHING
  580. // Check if temperature is within the correct band
  581. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  582. {
  583. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  584. {
  585. soft_pwm_bed = 0;
  586. }
  587. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  588. {
  589. soft_pwm_bed = MAX_BED_POWER>>1;
  590. }
  591. }
  592. else
  593. {
  594. soft_pwm_bed = 0;
  595. WRITE(HEATER_BED_PIN,LOW);
  596. }
  597. #endif
  598. #endif
  599. //code for controlling the extruder rate based on the width sensor
  600. #ifdef FILAMENT_SENSOR
  601. if(filament_sensor)
  602. {
  603. meas_shift_index=delay_index1-meas_delay_cm;
  604. if(meas_shift_index<0)
  605. meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
  606. //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
  607. //then square it to get an area
  608. if(meas_shift_index<0)
  609. meas_shift_index=0;
  610. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  611. meas_shift_index=MAX_MEASUREMENT_DELAY;
  612. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  613. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  614. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  615. }
  616. #endif
  617. }
  618. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  619. // Derived from RepRap FiveD extruder::getTemperature()
  620. // For hot end temperature measurement.
  621. static float analog2temp(int raw, uint8_t e) {
  622. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  623. if(e > EXTRUDERS)
  624. #else
  625. if(e >= EXTRUDERS)
  626. #endif
  627. {
  628. SERIAL_ERROR_START;
  629. SERIAL_ERROR((int)e);
  630. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  631. kill();
  632. return 0.0;
  633. }
  634. #ifdef HEATER_0_USES_MAX6675
  635. if (e == 0)
  636. {
  637. return 0.25 * raw;
  638. }
  639. #endif
  640. if(heater_ttbl_map[e] != NULL)
  641. {
  642. float celsius = 0;
  643. uint8_t i;
  644. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  645. for (i=1; i<heater_ttbllen_map[e]; i++)
  646. {
  647. if (PGM_RD_W((*tt)[i][0]) > raw)
  648. {
  649. celsius = PGM_RD_W((*tt)[i-1][1]) +
  650. (raw - PGM_RD_W((*tt)[i-1][0])) *
  651. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  652. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  653. break;
  654. }
  655. }
  656. // Overflow: Set to last value in the table
  657. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  658. return celsius;
  659. }
  660. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  661. }
  662. // Derived from RepRap FiveD extruder::getTemperature()
  663. // For bed temperature measurement.
  664. static float analog2tempBed(int raw) {
  665. #ifdef BED_USES_THERMISTOR
  666. float celsius = 0;
  667. byte i;
  668. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  669. {
  670. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  671. {
  672. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  673. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  674. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  675. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  676. break;
  677. }
  678. }
  679. // Overflow: Set to last value in the table
  680. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  681. return celsius;
  682. #elif defined BED_USES_AD595
  683. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  684. #else
  685. return 0;
  686. #endif
  687. }
  688. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  689. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  690. static void updateTemperaturesFromRawValues()
  691. {
  692. for(uint8_t e=0;e<EXTRUDERS;e++)
  693. {
  694. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  695. }
  696. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  697. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  698. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  699. #endif
  700. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  701. filament_width_meas = analog2widthFil();
  702. #endif
  703. //Reset the watchdog after we know we have a temperature measurement.
  704. watchdog_reset();
  705. CRITICAL_SECTION_START;
  706. temp_meas_ready = false;
  707. CRITICAL_SECTION_END;
  708. }
  709. // For converting raw Filament Width to milimeters
  710. #ifdef FILAMENT_SENSOR
  711. float analog2widthFil() {
  712. return current_raw_filwidth/16383.0*5.0;
  713. //return current_raw_filwidth;
  714. }
  715. // For converting raw Filament Width to a ratio
  716. int widthFil_to_size_ratio() {
  717. float temp;
  718. temp=filament_width_meas;
  719. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  720. temp=filament_width_nominal; //assume sensor cut out
  721. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  722. temp= MEASURED_UPPER_LIMIT;
  723. return(filament_width_nominal/temp*100);
  724. }
  725. #endif
  726. void tp_init()
  727. {
  728. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  729. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  730. MCUCR=(1<<JTD);
  731. MCUCR=(1<<JTD);
  732. #endif
  733. // Finish init of mult extruder arrays
  734. for(int e = 0; e < EXTRUDERS; e++) {
  735. // populate with the first value
  736. maxttemp[e] = maxttemp[0];
  737. #ifdef PIDTEMP
  738. temp_iState_min[e] = 0.0;
  739. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  740. #endif //PIDTEMP
  741. #ifdef PIDTEMPBED
  742. temp_iState_min_bed = 0.0;
  743. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  744. #endif //PIDTEMPBED
  745. }
  746. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  747. SET_OUTPUT(HEATER_0_PIN);
  748. #endif
  749. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  750. SET_OUTPUT(HEATER_1_PIN);
  751. #endif
  752. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  753. SET_OUTPUT(HEATER_2_PIN);
  754. #endif
  755. #if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1)
  756. SET_OUTPUT(HEATER_3_PIN);
  757. #endif
  758. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  759. SET_OUTPUT(HEATER_BED_PIN);
  760. #endif
  761. #if defined(FAN_PIN) && (FAN_PIN > -1)
  762. SET_OUTPUT(FAN_PIN);
  763. #ifdef FAST_PWM_FAN
  764. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  765. #endif
  766. #ifdef FAN_SOFT_PWM
  767. soft_pwm_fan = fanSpeedSoftPwm / 2;
  768. #endif
  769. #endif
  770. #ifdef HEATER_0_USES_MAX6675
  771. #ifndef SDSUPPORT
  772. SET_OUTPUT(SCK_PIN);
  773. WRITE(SCK_PIN,0);
  774. SET_OUTPUT(MOSI_PIN);
  775. WRITE(MOSI_PIN,1);
  776. SET_INPUT(MISO_PIN);
  777. WRITE(MISO_PIN,1);
  778. #endif
  779. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  780. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  781. pinMode(SS_PIN, OUTPUT);
  782. digitalWrite(SS_PIN,0);
  783. pinMode(MAX6675_SS, OUTPUT);
  784. digitalWrite(MAX6675_SS,1);
  785. #endif
  786. // Set analog inputs
  787. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  788. DIDR0 = 0;
  789. #ifdef DIDR2
  790. DIDR2 = 0;
  791. #endif
  792. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  793. #if TEMP_0_PIN < 8
  794. DIDR0 |= 1 << TEMP_0_PIN;
  795. #else
  796. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  797. #endif
  798. #endif
  799. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  800. #if TEMP_1_PIN < 8
  801. DIDR0 |= 1<<TEMP_1_PIN;
  802. #else
  803. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  804. #endif
  805. #endif
  806. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  807. #if TEMP_2_PIN < 8
  808. DIDR0 |= 1 << TEMP_2_PIN;
  809. #else
  810. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  811. #endif
  812. #endif
  813. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  814. #if TEMP_3_PIN < 8
  815. DIDR0 |= 1 << TEMP_3_PIN;
  816. #else
  817. DIDR2 |= 1<<(TEMP_3_PIN - 8);
  818. #endif
  819. #endif
  820. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  821. #if TEMP_BED_PIN < 8
  822. DIDR0 |= 1<<TEMP_BED_PIN;
  823. #else
  824. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  825. #endif
  826. #endif
  827. //Added for Filament Sensor
  828. #ifdef FILAMENT_SENSOR
  829. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  830. #if FILWIDTH_PIN < 8
  831. DIDR0 |= 1<<FILWIDTH_PIN;
  832. #else
  833. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  834. #endif
  835. #endif
  836. #endif
  837. // Use timer0 for temperature measurement
  838. // Interleave temperature interrupt with millies interrupt
  839. OCR0B = 128;
  840. TIMSK0 |= (1<<OCIE0B);
  841. // Wait for temperature measurement to settle
  842. delay(250);
  843. #ifdef HEATER_0_MINTEMP
  844. minttemp[0] = HEATER_0_MINTEMP;
  845. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  846. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  847. minttemp_raw[0] += OVERSAMPLENR;
  848. #else
  849. minttemp_raw[0] -= OVERSAMPLENR;
  850. #endif
  851. }
  852. #endif //MINTEMP
  853. #ifdef HEATER_0_MAXTEMP
  854. maxttemp[0] = HEATER_0_MAXTEMP;
  855. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  856. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  857. maxttemp_raw[0] -= OVERSAMPLENR;
  858. #else
  859. maxttemp_raw[0] += OVERSAMPLENR;
  860. #endif
  861. }
  862. #endif //MAXTEMP
  863. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  864. minttemp[1] = HEATER_1_MINTEMP;
  865. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  866. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  867. minttemp_raw[1] += OVERSAMPLENR;
  868. #else
  869. minttemp_raw[1] -= OVERSAMPLENR;
  870. #endif
  871. }
  872. #endif // MINTEMP 1
  873. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  874. maxttemp[1] = HEATER_1_MAXTEMP;
  875. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  876. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  877. maxttemp_raw[1] -= OVERSAMPLENR;
  878. #else
  879. maxttemp_raw[1] += OVERSAMPLENR;
  880. #endif
  881. }
  882. #endif //MAXTEMP 1
  883. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  884. minttemp[2] = HEATER_2_MINTEMP;
  885. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  886. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  887. minttemp_raw[2] += OVERSAMPLENR;
  888. #else
  889. minttemp_raw[2] -= OVERSAMPLENR;
  890. #endif
  891. }
  892. #endif //MINTEMP 2
  893. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  894. maxttemp[2] = HEATER_2_MAXTEMP;
  895. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  896. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  897. maxttemp_raw[2] -= OVERSAMPLENR;
  898. #else
  899. maxttemp_raw[2] += OVERSAMPLENR;
  900. #endif
  901. }
  902. #endif //MAXTEMP 2
  903. #if (EXTRUDERS > 3) && defined(HEATER_3_MINTEMP)
  904. minttemp[3] = HEATER_3_MINTEMP;
  905. while(analog2temp(minttemp_raw[3], 3) < HEATER_3_MINTEMP) {
  906. #if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
  907. minttemp_raw[3] += OVERSAMPLENR;
  908. #else
  909. minttemp_raw[3] -= OVERSAMPLENR;
  910. #endif
  911. }
  912. #endif //MINTEMP 3
  913. #if (EXTRUDERS > 3) && defined(HEATER_3_MAXTEMP)
  914. maxttemp[3] = HEATER_3_MAXTEMP;
  915. while(analog2temp(maxttemp_raw[3], 3) > HEATER_3_MAXTEMP) {
  916. #if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
  917. maxttemp_raw[3] -= OVERSAMPLENR;
  918. #else
  919. maxttemp_raw[3] += OVERSAMPLENR;
  920. #endif
  921. }
  922. #endif // MAXTEMP 3
  923. #ifdef BED_MINTEMP
  924. /* No bed MINTEMP error implemented?!? */ /*
  925. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  926. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  927. bed_minttemp_raw += OVERSAMPLENR;
  928. #else
  929. bed_minttemp_raw -= OVERSAMPLENR;
  930. #endif
  931. }
  932. */
  933. #endif //BED_MINTEMP
  934. #ifdef BED_MAXTEMP
  935. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  936. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  937. bed_maxttemp_raw -= OVERSAMPLENR;
  938. #else
  939. bed_maxttemp_raw += OVERSAMPLENR;
  940. #endif
  941. }
  942. #endif //BED_MAXTEMP
  943. }
  944. void setWatch()
  945. {
  946. #ifdef WATCH_TEMP_PERIOD
  947. for (int e = 0; e < EXTRUDERS; e++)
  948. {
  949. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  950. {
  951. watch_start_temp[e] = degHotend(e);
  952. watchmillis[e] = millis();
  953. }
  954. }
  955. #endif
  956. }
  957. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  958. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  959. {
  960. /*
  961. SERIAL_ECHO_START;
  962. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  963. SERIAL_ECHO(heater_id);
  964. SERIAL_ECHO(" ; State:");
  965. SERIAL_ECHO(*state);
  966. SERIAL_ECHO(" ; Timer:");
  967. SERIAL_ECHO(*timer);
  968. SERIAL_ECHO(" ; Temperature:");
  969. SERIAL_ECHO(temperature);
  970. SERIAL_ECHO(" ; Target Temp:");
  971. SERIAL_ECHO(target_temperature);
  972. SERIAL_ECHOLN("");
  973. */
  974. if ((target_temperature == 0) || thermal_runaway)
  975. {
  976. *state = 0;
  977. *timer = 0;
  978. return;
  979. }
  980. switch (*state)
  981. {
  982. case 0: // "Heater Inactive" state
  983. if (target_temperature > 0) *state = 1;
  984. break;
  985. case 1: // "First Heating" state
  986. if (temperature >= target_temperature) *state = 2;
  987. break;
  988. case 2: // "Temperature Stable" state
  989. if (temperature >= (target_temperature - hysteresis_degc))
  990. {
  991. *timer = millis();
  992. }
  993. else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000)
  994. {
  995. SERIAL_ERROR_START;
  996. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  997. SERIAL_ERRORLN((int)heater_id);
  998. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  999. thermal_runaway = true;
  1000. while(1)
  1001. {
  1002. disable_heater();
  1003. disable_x();
  1004. disable_y();
  1005. disable_z();
  1006. disable_e0();
  1007. disable_e1();
  1008. disable_e2();
  1009. disable_e3();
  1010. manage_heater();
  1011. lcd_update();
  1012. }
  1013. }
  1014. break;
  1015. }
  1016. }
  1017. #endif
  1018. void disable_heater()
  1019. {
  1020. for(int i=0;i<EXTRUDERS;i++)
  1021. setTargetHotend(0,i);
  1022. setTargetBed(0);
  1023. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1024. target_temperature[0]=0;
  1025. soft_pwm[0]=0;
  1026. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1027. WRITE(HEATER_0_PIN,LOW);
  1028. #endif
  1029. #endif
  1030. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1031. target_temperature[1]=0;
  1032. soft_pwm[1]=0;
  1033. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1034. WRITE(HEATER_1_PIN,LOW);
  1035. #endif
  1036. #endif
  1037. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1038. target_temperature[2]=0;
  1039. soft_pwm[2]=0;
  1040. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1041. WRITE(HEATER_2_PIN,LOW);
  1042. #endif
  1043. #endif
  1044. #if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3
  1045. target_temperature[3]=0;
  1046. soft_pwm[3]=0;
  1047. #if defined(HEATER_3_PIN) && HEATER_3_PIN > -1
  1048. WRITE(HEATER_3_PIN,LOW);
  1049. #endif
  1050. #endif
  1051. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1052. target_temperature_bed=0;
  1053. soft_pwm_bed=0;
  1054. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1055. WRITE(HEATER_BED_PIN,LOW);
  1056. #endif
  1057. #endif
  1058. }
  1059. void max_temp_error(uint8_t e) {
  1060. disable_heater();
  1061. if(IsStopped() == false) {
  1062. SERIAL_ERROR_START;
  1063. SERIAL_ERRORLN((int)e);
  1064. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1065. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1066. }
  1067. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1068. Stop();
  1069. #endif
  1070. }
  1071. void min_temp_error(uint8_t e) {
  1072. disable_heater();
  1073. if(IsStopped() == false) {
  1074. SERIAL_ERROR_START;
  1075. SERIAL_ERRORLN((int)e);
  1076. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1077. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1078. }
  1079. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1080. Stop();
  1081. #endif
  1082. }
  1083. void bed_max_temp_error(void) {
  1084. #if HEATER_BED_PIN > -1
  1085. WRITE(HEATER_BED_PIN, 0);
  1086. #endif
  1087. if(IsStopped() == false) {
  1088. SERIAL_ERROR_START;
  1089. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  1090. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1091. }
  1092. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1093. Stop();
  1094. #endif
  1095. }
  1096. #ifdef HEATER_0_USES_MAX6675
  1097. #define MAX6675_HEAT_INTERVAL 250
  1098. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1099. int max6675_temp = 2000;
  1100. int read_max6675()
  1101. {
  1102. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1103. return max6675_temp;
  1104. max6675_previous_millis = millis();
  1105. max6675_temp = 0;
  1106. #ifdef PRR
  1107. PRR &= ~(1<<PRSPI);
  1108. #elif defined PRR0
  1109. PRR0 &= ~(1<<PRSPI);
  1110. #endif
  1111. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1112. // enable TT_MAX6675
  1113. WRITE(MAX6675_SS, 0);
  1114. // ensure 100ns delay - a bit extra is fine
  1115. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1116. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1117. // read MSB
  1118. SPDR = 0;
  1119. for (;(SPSR & (1<<SPIF)) == 0;);
  1120. max6675_temp = SPDR;
  1121. max6675_temp <<= 8;
  1122. // read LSB
  1123. SPDR = 0;
  1124. for (;(SPSR & (1<<SPIF)) == 0;);
  1125. max6675_temp |= SPDR;
  1126. // disable TT_MAX6675
  1127. WRITE(MAX6675_SS, 1);
  1128. if (max6675_temp & 4)
  1129. {
  1130. // thermocouple open
  1131. max6675_temp = 2000;
  1132. }
  1133. else
  1134. {
  1135. max6675_temp = max6675_temp >> 3;
  1136. }
  1137. return max6675_temp;
  1138. }
  1139. #endif
  1140. // Timer 0 is shared with millies
  1141. ISR(TIMER0_COMPB_vect)
  1142. {
  1143. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1144. static unsigned char temp_count = 0;
  1145. static unsigned long raw_temp_0_value = 0;
  1146. static unsigned long raw_temp_1_value = 0;
  1147. static unsigned long raw_temp_2_value = 0;
  1148. static unsigned long raw_temp_3_value = 0;
  1149. static unsigned long raw_temp_bed_value = 0;
  1150. static unsigned char temp_state = 12;
  1151. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1152. static unsigned char soft_pwm_0;
  1153. #ifdef SLOW_PWM_HEATERS
  1154. static unsigned char slow_pwm_count = 0;
  1155. static unsigned char state_heater_0 = 0;
  1156. static unsigned char state_timer_heater_0 = 0;
  1157. #endif
  1158. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1159. static unsigned char soft_pwm_1;
  1160. #ifdef SLOW_PWM_HEATERS
  1161. static unsigned char state_heater_1 = 0;
  1162. static unsigned char state_timer_heater_1 = 0;
  1163. #endif
  1164. #endif
  1165. #if EXTRUDERS > 2
  1166. static unsigned char soft_pwm_2;
  1167. #ifdef SLOW_PWM_HEATERS
  1168. static unsigned char state_heater_2 = 0;
  1169. static unsigned char state_timer_heater_2 = 0;
  1170. #endif
  1171. #endif
  1172. #if EXTRUDERS > 3
  1173. static unsigned char soft_pwm_3;
  1174. #ifdef SLOW_PWM_HEATERS
  1175. static unsigned char state_heater_3 = 0;
  1176. static unsigned char state_timer_heater_3 = 0;
  1177. #endif
  1178. #endif
  1179. #if HEATER_BED_PIN > -1
  1180. static unsigned char soft_pwm_b;
  1181. #ifdef SLOW_PWM_HEATERS
  1182. static unsigned char state_heater_b = 0;
  1183. static unsigned char state_timer_heater_b = 0;
  1184. #endif
  1185. #endif
  1186. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1187. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1188. #endif
  1189. #ifndef SLOW_PWM_HEATERS
  1190. /*
  1191. * standard PWM modulation
  1192. */
  1193. if(pwm_count == 0){
  1194. soft_pwm_0 = soft_pwm[0];
  1195. if(soft_pwm_0 > 0) {
  1196. WRITE(HEATER_0_PIN,1);
  1197. #ifdef HEATERS_PARALLEL
  1198. WRITE(HEATER_1_PIN,1);
  1199. #endif
  1200. } else WRITE(HEATER_0_PIN,0);
  1201. #if EXTRUDERS > 1
  1202. soft_pwm_1 = soft_pwm[1];
  1203. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1204. #endif
  1205. #if EXTRUDERS > 2
  1206. soft_pwm_2 = soft_pwm[2];
  1207. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1208. #endif
  1209. #if EXTRUDERS > 3
  1210. soft_pwm_3 = soft_pwm[3];
  1211. if(soft_pwm_3 > 0) WRITE(HEATER_3_PIN,1); else WRITE(HEATER_3_PIN,0);
  1212. #endif
  1213. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1214. soft_pwm_b = soft_pwm_bed;
  1215. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1216. #endif
  1217. #ifdef FAN_SOFT_PWM
  1218. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1219. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1220. #endif
  1221. }
  1222. if(soft_pwm_0 < pwm_count) {
  1223. WRITE(HEATER_0_PIN,0);
  1224. #ifdef HEATERS_PARALLEL
  1225. WRITE(HEATER_1_PIN,0);
  1226. #endif
  1227. }
  1228. #if EXTRUDERS > 1
  1229. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1230. #endif
  1231. #if EXTRUDERS > 2
  1232. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1233. #endif
  1234. #if EXTRUDERS > 3
  1235. if(soft_pwm_3 < pwm_count) WRITE(HEATER_3_PIN,0);
  1236. #endif
  1237. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1238. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1239. #endif
  1240. #ifdef FAN_SOFT_PWM
  1241. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1242. #endif
  1243. pwm_count += (1 << SOFT_PWM_SCALE);
  1244. pwm_count &= 0x7f;
  1245. #else //ifndef SLOW_PWM_HEATERS
  1246. /*
  1247. * SLOW PWM HEATERS
  1248. *
  1249. * for heaters drived by relay
  1250. */
  1251. #ifndef MIN_STATE_TIME
  1252. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1253. #endif
  1254. if (slow_pwm_count == 0) {
  1255. // EXTRUDER 0
  1256. soft_pwm_0 = soft_pwm[0];
  1257. if (soft_pwm_0 > 0) {
  1258. // turn ON heather only if the minimum time is up
  1259. if (state_timer_heater_0 == 0) {
  1260. // if change state set timer
  1261. if (state_heater_0 == 0) {
  1262. state_timer_heater_0 = MIN_STATE_TIME;
  1263. }
  1264. state_heater_0 = 1;
  1265. WRITE(HEATER_0_PIN, 1);
  1266. #ifdef HEATERS_PARALLEL
  1267. WRITE(HEATER_1_PIN, 1);
  1268. #endif
  1269. }
  1270. } else {
  1271. // turn OFF heather only if the minimum time is up
  1272. if (state_timer_heater_0 == 0) {
  1273. // if change state set timer
  1274. if (state_heater_0 == 1) {
  1275. state_timer_heater_0 = MIN_STATE_TIME;
  1276. }
  1277. state_heater_0 = 0;
  1278. WRITE(HEATER_0_PIN, 0);
  1279. #ifdef HEATERS_PARALLEL
  1280. WRITE(HEATER_1_PIN, 0);
  1281. #endif
  1282. }
  1283. }
  1284. #if EXTRUDERS > 1
  1285. // EXTRUDER 1
  1286. soft_pwm_1 = soft_pwm[1];
  1287. if (soft_pwm_1 > 0) {
  1288. // turn ON heather only if the minimum time is up
  1289. if (state_timer_heater_1 == 0) {
  1290. // if change state set timer
  1291. if (state_heater_1 == 0) {
  1292. state_timer_heater_1 = MIN_STATE_TIME;
  1293. }
  1294. state_heater_1 = 1;
  1295. WRITE(HEATER_1_PIN, 1);
  1296. }
  1297. } else {
  1298. // turn OFF heather only if the minimum time is up
  1299. if (state_timer_heater_1 == 0) {
  1300. // if change state set timer
  1301. if (state_heater_1 == 1) {
  1302. state_timer_heater_1 = MIN_STATE_TIME;
  1303. }
  1304. state_heater_1 = 0;
  1305. WRITE(HEATER_1_PIN, 0);
  1306. }
  1307. }
  1308. #endif
  1309. #if EXTRUDERS > 2
  1310. // EXTRUDER 2
  1311. soft_pwm_2 = soft_pwm[2];
  1312. if (soft_pwm_2 > 0) {
  1313. // turn ON heather only if the minimum time is up
  1314. if (state_timer_heater_2 == 0) {
  1315. // if change state set timer
  1316. if (state_heater_2 == 0) {
  1317. state_timer_heater_2 = MIN_STATE_TIME;
  1318. }
  1319. state_heater_2 = 1;
  1320. WRITE(HEATER_2_PIN, 1);
  1321. }
  1322. } else {
  1323. // turn OFF heather only if the minimum time is up
  1324. if (state_timer_heater_2 == 0) {
  1325. // if change state set timer
  1326. if (state_heater_2 == 1) {
  1327. state_timer_heater_2 = MIN_STATE_TIME;
  1328. }
  1329. state_heater_2 = 0;
  1330. WRITE(HEATER_2_PIN, 0);
  1331. }
  1332. }
  1333. #endif
  1334. #if EXTRUDERS > 3
  1335. // EXTRUDER 3
  1336. soft_pwm_3 = soft_pwm[3];
  1337. if (soft_pwm_3 > 0) {
  1338. // turn ON heather only if the minimum time is up
  1339. if (state_timer_heater_3 == 0) {
  1340. // if change state set timer
  1341. if (state_heater_3 == 0) {
  1342. state_timer_heater_3 = MIN_STATE_TIME;
  1343. }
  1344. state_heater_3 = 1;
  1345. WRITE(HEATER_3_PIN, 1);
  1346. }
  1347. } else {
  1348. // turn OFF heather only if the minimum time is up
  1349. if (state_timer_heater_3 == 0) {
  1350. // if change state set timer
  1351. if (state_heater_3 == 1) {
  1352. state_timer_heater_3 = MIN_STATE_TIME;
  1353. }
  1354. state_heater_3 = 0;
  1355. WRITE(HEATER_3_PIN, 0);
  1356. }
  1357. }
  1358. #endif
  1359. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1360. // BED
  1361. soft_pwm_b = soft_pwm_bed;
  1362. if (soft_pwm_b > 0) {
  1363. // turn ON heather only if the minimum time is up
  1364. if (state_timer_heater_b == 0) {
  1365. // if change state set timer
  1366. if (state_heater_b == 0) {
  1367. state_timer_heater_b = MIN_STATE_TIME;
  1368. }
  1369. state_heater_b = 1;
  1370. WRITE(HEATER_BED_PIN, 1);
  1371. }
  1372. } else {
  1373. // turn OFF heather only if the minimum time is up
  1374. if (state_timer_heater_b == 0) {
  1375. // if change state set timer
  1376. if (state_heater_b == 1) {
  1377. state_timer_heater_b = MIN_STATE_TIME;
  1378. }
  1379. state_heater_b = 0;
  1380. WRITE(HEATER_BED_PIN, 0);
  1381. }
  1382. }
  1383. #endif
  1384. } // if (slow_pwm_count == 0)
  1385. // EXTRUDER 0
  1386. if (soft_pwm_0 < slow_pwm_count) {
  1387. // turn OFF heather only if the minimum time is up
  1388. if (state_timer_heater_0 == 0) {
  1389. // if change state set timer
  1390. if (state_heater_0 == 1) {
  1391. state_timer_heater_0 = MIN_STATE_TIME;
  1392. }
  1393. state_heater_0 = 0;
  1394. WRITE(HEATER_0_PIN, 0);
  1395. #ifdef HEATERS_PARALLEL
  1396. WRITE(HEATER_1_PIN, 0);
  1397. #endif
  1398. }
  1399. }
  1400. #if EXTRUDERS > 1
  1401. // EXTRUDER 1
  1402. if (soft_pwm_1 < slow_pwm_count) {
  1403. // turn OFF heather only if the minimum time is up
  1404. if (state_timer_heater_1 == 0) {
  1405. // if change state set timer
  1406. if (state_heater_1 == 1) {
  1407. state_timer_heater_1 = MIN_STATE_TIME;
  1408. }
  1409. state_heater_1 = 0;
  1410. WRITE(HEATER_1_PIN, 0);
  1411. }
  1412. }
  1413. #endif
  1414. #if EXTRUDERS > 2
  1415. // EXTRUDER 2
  1416. if (soft_pwm_2 < slow_pwm_count) {
  1417. // turn OFF heather only if the minimum time is up
  1418. if (state_timer_heater_2 == 0) {
  1419. // if change state set timer
  1420. if (state_heater_2 == 1) {
  1421. state_timer_heater_2 = MIN_STATE_TIME;
  1422. }
  1423. state_heater_2 = 0;
  1424. WRITE(HEATER_2_PIN, 0);
  1425. }
  1426. }
  1427. #endif
  1428. #if EXTRUDERS > 3
  1429. // EXTRUDER 3
  1430. if (soft_pwm_3 < slow_pwm_count) {
  1431. // turn OFF heather only if the minimum time is up
  1432. if (state_timer_heater_3 == 0) {
  1433. // if change state set timer
  1434. if (state_heater_3 == 1) {
  1435. state_timer_heater_3 = MIN_STATE_TIME;
  1436. }
  1437. state_heater_3 = 0;
  1438. WRITE(HEATER_3_PIN, 0);
  1439. }
  1440. }
  1441. #endif
  1442. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1443. // BED
  1444. if (soft_pwm_b < slow_pwm_count) {
  1445. // turn OFF heather only if the minimum time is up
  1446. if (state_timer_heater_b == 0) {
  1447. // if change state set timer
  1448. if (state_heater_b == 1) {
  1449. state_timer_heater_b = MIN_STATE_TIME;
  1450. }
  1451. state_heater_b = 0;
  1452. WRITE(HEATER_BED_PIN, 0);
  1453. }
  1454. }
  1455. #endif
  1456. #ifdef FAN_SOFT_PWM
  1457. if (pwm_count == 0){
  1458. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1459. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1460. }
  1461. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1462. #endif
  1463. pwm_count += (1 << SOFT_PWM_SCALE);
  1464. pwm_count &= 0x7f;
  1465. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1466. if ((pwm_count % 64) == 0) {
  1467. slow_pwm_count++;
  1468. slow_pwm_count &= 0x7f;
  1469. // Extruder 0
  1470. if (state_timer_heater_0 > 0) {
  1471. state_timer_heater_0--;
  1472. }
  1473. #if EXTRUDERS > 1
  1474. // Extruder 1
  1475. if (state_timer_heater_1 > 0)
  1476. state_timer_heater_1--;
  1477. #endif
  1478. #if EXTRUDERS > 2
  1479. // Extruder 2
  1480. if (state_timer_heater_2 > 0)
  1481. state_timer_heater_2--;
  1482. #endif
  1483. #if EXTRUDERS > 3
  1484. // Extruder 3
  1485. if (state_timer_heater_3 > 0)
  1486. state_timer_heater_3--;
  1487. #endif
  1488. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1489. // Bed
  1490. if (state_timer_heater_b > 0)
  1491. state_timer_heater_b--;
  1492. #endif
  1493. } //if ((pwm_count % 64) == 0) {
  1494. #endif //ifndef SLOW_PWM_HEATERS
  1495. switch(temp_state) {
  1496. case 0: // Prepare TEMP_0
  1497. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1498. #if TEMP_0_PIN > 7
  1499. ADCSRB = 1<<MUX5;
  1500. #else
  1501. ADCSRB = 0;
  1502. #endif
  1503. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1504. ADCSRA |= 1<<ADSC; // Start conversion
  1505. #endif
  1506. lcd_buttons_update();
  1507. temp_state = 1;
  1508. break;
  1509. case 1: // Measure TEMP_0
  1510. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1511. raw_temp_0_value += ADC;
  1512. #endif
  1513. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1514. raw_temp_0_value = read_max6675();
  1515. #endif
  1516. temp_state = 2;
  1517. break;
  1518. case 2: // Prepare TEMP_BED
  1519. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1520. #if TEMP_BED_PIN > 7
  1521. ADCSRB = 1<<MUX5;
  1522. #else
  1523. ADCSRB = 0;
  1524. #endif
  1525. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1526. ADCSRA |= 1<<ADSC; // Start conversion
  1527. #endif
  1528. lcd_buttons_update();
  1529. temp_state = 3;
  1530. break;
  1531. case 3: // Measure TEMP_BED
  1532. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1533. raw_temp_bed_value += ADC;
  1534. #endif
  1535. temp_state = 4;
  1536. break;
  1537. case 4: // Prepare TEMP_1
  1538. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1539. #if TEMP_1_PIN > 7
  1540. ADCSRB = 1<<MUX5;
  1541. #else
  1542. ADCSRB = 0;
  1543. #endif
  1544. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1545. ADCSRA |= 1<<ADSC; // Start conversion
  1546. #endif
  1547. lcd_buttons_update();
  1548. temp_state = 5;
  1549. break;
  1550. case 5: // Measure TEMP_1
  1551. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1552. raw_temp_1_value += ADC;
  1553. #endif
  1554. temp_state = 6;
  1555. break;
  1556. case 6: // Prepare TEMP_2
  1557. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1558. #if TEMP_2_PIN > 7
  1559. ADCSRB = 1<<MUX5;
  1560. #else
  1561. ADCSRB = 0;
  1562. #endif
  1563. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1564. ADCSRA |= 1<<ADSC; // Start conversion
  1565. #endif
  1566. lcd_buttons_update();
  1567. temp_state = 7;
  1568. break;
  1569. case 7: // Measure TEMP_2
  1570. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1571. raw_temp_2_value += ADC;
  1572. #endif
  1573. temp_state = 8;
  1574. break;
  1575. case 8: // Prepare TEMP_3
  1576. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1577. #if TEMP_3_PIN > 7
  1578. ADCSRB = 1<<MUX5;
  1579. #else
  1580. ADCSRB = 0;
  1581. #endif
  1582. ADMUX = ((1 << REFS0) | (TEMP_3_PIN & 0x07));
  1583. ADCSRA |= 1<<ADSC; // Start conversion
  1584. #endif
  1585. lcd_buttons_update();
  1586. temp_state = 9;
  1587. break;
  1588. case 9: // Measure TEMP_3
  1589. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1590. raw_temp_3_value += ADC;
  1591. #endif
  1592. temp_state = 10; //change so that Filament Width is also measured
  1593. break;
  1594. case 10: //Prepare FILWIDTH
  1595. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1596. #if FILWIDTH_PIN>7
  1597. ADCSRB = 1<<MUX5;
  1598. #else
  1599. ADCSRB = 0;
  1600. #endif
  1601. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1602. ADCSRA |= 1<<ADSC; // Start conversion
  1603. #endif
  1604. lcd_buttons_update();
  1605. temp_state = 11;
  1606. break;
  1607. case 11: //Measure FILWIDTH
  1608. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1609. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1610. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1611. {
  1612. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1613. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1614. }
  1615. #endif
  1616. temp_state = 0;
  1617. temp_count++;
  1618. break;
  1619. case 12: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1620. temp_state = 0;
  1621. break;
  1622. // default:
  1623. // SERIAL_ERROR_START;
  1624. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1625. // break;
  1626. }
  1627. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1628. {
  1629. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1630. {
  1631. current_temperature_raw[0] = raw_temp_0_value;
  1632. #if EXTRUDERS > 1
  1633. current_temperature_raw[1] = raw_temp_1_value;
  1634. #endif
  1635. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1636. redundant_temperature_raw = raw_temp_1_value;
  1637. #endif
  1638. #if EXTRUDERS > 2
  1639. current_temperature_raw[2] = raw_temp_2_value;
  1640. #endif
  1641. #if EXTRUDERS > 3
  1642. current_temperature_raw[3] = raw_temp_3_value;
  1643. #endif
  1644. current_temperature_bed_raw = raw_temp_bed_value;
  1645. }
  1646. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1647. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1648. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1649. #endif
  1650. temp_meas_ready = true;
  1651. temp_count = 0;
  1652. raw_temp_0_value = 0;
  1653. raw_temp_1_value = 0;
  1654. raw_temp_2_value = 0;
  1655. raw_temp_3_value = 0;
  1656. raw_temp_bed_value = 0;
  1657. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1658. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1659. #else
  1660. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1661. #endif
  1662. max_temp_error(0);
  1663. }
  1664. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1665. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1666. #else
  1667. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1668. #endif
  1669. min_temp_error(0);
  1670. }
  1671. #if EXTRUDERS > 1
  1672. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1673. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1674. #else
  1675. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1676. #endif
  1677. max_temp_error(1);
  1678. }
  1679. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1680. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1681. #else
  1682. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1683. #endif
  1684. min_temp_error(1);
  1685. }
  1686. #endif
  1687. #if EXTRUDERS > 2
  1688. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1689. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1690. #else
  1691. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1692. #endif
  1693. max_temp_error(2);
  1694. }
  1695. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1696. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1697. #else
  1698. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1699. #endif
  1700. min_temp_error(2);
  1701. }
  1702. #endif
  1703. #if EXTRUDERS > 3
  1704. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1705. if(current_temperature_raw[3] <= maxttemp_raw[3]) {
  1706. #else
  1707. if(current_temperature_raw[3] >= maxttemp_raw[3]) {
  1708. #endif
  1709. max_temp_error(3);
  1710. }
  1711. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1712. if(current_temperature_raw[3] >= minttemp_raw[3]) {
  1713. #else
  1714. if(current_temperature_raw[3] <= minttemp_raw[3]) {
  1715. #endif
  1716. min_temp_error(3);
  1717. }
  1718. #endif
  1719. /* No bed MINTEMP error? */
  1720. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1721. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1722. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1723. #else
  1724. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1725. #endif
  1726. target_temperature_bed = 0;
  1727. bed_max_temp_error();
  1728. }
  1729. #endif
  1730. }
  1731. #ifdef BABYSTEPPING
  1732. for(uint8_t axis=0;axis<3;axis++)
  1733. {
  1734. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1735. if(curTodo>0)
  1736. {
  1737. babystep(axis,/*fwd*/true);
  1738. babystepsTodo[axis]--; //less to do next time
  1739. }
  1740. else
  1741. if(curTodo<0)
  1742. {
  1743. babystep(axis,/*fwd*/false);
  1744. babystepsTodo[axis]++; //less to do next time
  1745. }
  1746. }
  1747. #endif //BABYSTEPPING
  1748. }
  1749. #ifdef PIDTEMP
  1750. // Apply the scale factors to the PID values
  1751. float scalePID_i(float i)
  1752. {
  1753. return i*PID_dT;
  1754. }
  1755. float unscalePID_i(float i)
  1756. {
  1757. return i/PID_dT;
  1758. }
  1759. float scalePID_d(float d)
  1760. {
  1761. return d/PID_dT;
  1762. }
  1763. float unscalePID_d(float d)
  1764. {
  1765. return d*PID_dT;
  1766. }
  1767. #endif //PIDTEMP