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 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * temperature.cpp - temperature control
  24. */
  25. #include "Marlin.h"
  26. #include "ultralcd.h"
  27. #include "temperature.h"
  28. #include "language.h"
  29. #include "Sd2PinMap.h"
  30. #if ENABLED(USE_WATCHDOG)
  31. #include "watchdog.h"
  32. #endif
  33. #ifdef K1 // Defined in Configuration.h in the PID settings
  34. #define K2 (1.0-K1)
  35. #endif
  36. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  37. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  38. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  39. #else
  40. static void* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  41. static uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  42. #endif
  43. Temperature thermalManager;
  44. // public:
  45. int Temperature::current_temperature_raw[HOTENDS] = { 0 };
  46. float Temperature::current_temperature[HOTENDS] = { 0.0 };
  47. int Temperature::target_temperature[HOTENDS] = { 0 };
  48. int Temperature::current_temperature_bed_raw = 0;
  49. float Temperature::current_temperature_bed = 0.0;
  50. int Temperature::target_temperature_bed = 0;
  51. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  52. float Temperature::redundant_temperature = 0.0;
  53. #endif
  54. unsigned char Temperature::soft_pwm_bed;
  55. #if ENABLED(FAN_SOFT_PWM)
  56. unsigned char Temperature::fanSpeedSoftPwm[FAN_COUNT];
  57. #endif
  58. #if ENABLED(PIDTEMP)
  59. #if ENABLED(PID_PARAMS_PER_HOTEND)
  60. float Temperature::Kp[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kp),
  61. Temperature::Ki[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Ki) * (PID_dT)),
  62. Temperature::Kd[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Kd) / (PID_dT));
  63. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  64. float Temperature::Kc[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kc);
  65. #endif
  66. #else
  67. float Temperature::Kp = DEFAULT_Kp,
  68. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  69. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  70. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  71. float Temperature::Kc = DEFAULT_Kc;
  72. #endif
  73. #endif
  74. #endif
  75. #if ENABLED(PIDTEMPBED)
  76. float Temperature::bedKp = DEFAULT_bedKp,
  77. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  78. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  79. #endif
  80. #if ENABLED(BABYSTEPPING)
  81. volatile int Temperature::babystepsTodo[3] = { 0 };
  82. #endif
  83. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  84. int Temperature::watch_target_temp[HOTENDS] = { 0 };
  85. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  86. #endif
  87. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  88. int Temperature::watch_target_bed_temp = 0;
  89. millis_t Temperature::watch_bed_next_ms = 0;
  90. #endif
  91. #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
  92. float Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  93. #endif
  94. // private:
  95. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  96. int Temperature::redundant_temperature_raw = 0;
  97. float Temperature::redundant_temperature = 0.0;
  98. #endif
  99. volatile bool Temperature::temp_meas_ready = false;
  100. #if ENABLED(PIDTEMP)
  101. float Temperature::temp_iState[HOTENDS] = { 0 };
  102. float Temperature::temp_dState[HOTENDS] = { 0 };
  103. float Temperature::pTerm[HOTENDS];
  104. float Temperature::iTerm[HOTENDS];
  105. float Temperature::dTerm[HOTENDS];
  106. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  107. float Temperature::cTerm[HOTENDS];
  108. long Temperature::last_position[HOTENDS];
  109. long Temperature::lpq[LPQ_MAX_LEN];
  110. int Temperature::lpq_ptr = 0;
  111. #endif
  112. float Temperature::pid_error[HOTENDS];
  113. float Temperature::temp_iState_min[HOTENDS];
  114. float Temperature::temp_iState_max[HOTENDS];
  115. bool Temperature::pid_reset[HOTENDS];
  116. #endif
  117. #if ENABLED(PIDTEMPBED)
  118. float Temperature::temp_iState_bed = { 0 };
  119. float Temperature::temp_dState_bed = { 0 };
  120. float Temperature::pTerm_bed;
  121. float Temperature::iTerm_bed;
  122. float Temperature::dTerm_bed;
  123. float Temperature::pid_error_bed;
  124. float Temperature::temp_iState_min_bed;
  125. float Temperature::temp_iState_max_bed;
  126. #else
  127. millis_t Temperature::next_bed_check_ms;
  128. #endif
  129. unsigned long Temperature::raw_temp_value[4] = { 0 };
  130. unsigned long Temperature::raw_temp_bed_value = 0;
  131. // Init min and max temp with extreme values to prevent false errors during startup
  132. int Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  133. int Temperature::maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  134. int Temperature::minttemp[HOTENDS] = { 0 };
  135. int Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  136. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  137. int Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  138. #endif
  139. #ifdef MILLISECONDS_PREHEAT_TIME
  140. unsigned long Temperature::preheat_end_time[HOTENDS] = { 0 };
  141. #endif
  142. #ifdef BED_MINTEMP
  143. int Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  144. #endif
  145. #ifdef BED_MAXTEMP
  146. int Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  147. #endif
  148. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  149. int Temperature::meas_shift_index; // Index of a delayed sample in buffer
  150. #endif
  151. #if HAS_AUTO_FAN
  152. millis_t Temperature::next_auto_fan_check_ms;
  153. #endif
  154. unsigned char Temperature::soft_pwm[HOTENDS];
  155. #if ENABLED(FAN_SOFT_PWM)
  156. unsigned char Temperature::soft_pwm_fan[FAN_COUNT];
  157. #endif
  158. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  159. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  160. #endif
  161. #if HAS_PID_HEATING
  162. void Temperature::PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
  163. float input = 0.0;
  164. int cycles = 0;
  165. bool heating = true;
  166. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  167. long t_high = 0, t_low = 0;
  168. long bias, d;
  169. float Ku, Tu;
  170. float workKp = 0, workKi = 0, workKd = 0;
  171. float max = 0, min = 10000;
  172. #if HAS_AUTO_FAN
  173. next_auto_fan_check_ms = temp_ms + 2500UL;
  174. #endif
  175. if (hotend >=
  176. #if ENABLED(PIDTEMP)
  177. HOTENDS
  178. #else
  179. 0
  180. #endif
  181. || hotend <
  182. #if ENABLED(PIDTEMPBED)
  183. -1
  184. #else
  185. 0
  186. #endif
  187. ) {
  188. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  189. return;
  190. }
  191. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  192. disable_all_heaters(); // switch off all heaters.
  193. #if HAS_PID_FOR_BOTH
  194. if (hotend < 0)
  195. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  196. else
  197. soft_pwm[hotend] = bias = d = (PID_MAX) / 2;
  198. #elif ENABLED(PIDTEMP)
  199. soft_pwm[hotend] = bias = d = (PID_MAX) / 2;
  200. #else
  201. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  202. #endif
  203. wait_for_heatup = true;
  204. // PID Tuning loop
  205. while (wait_for_heatup) {
  206. millis_t ms = millis();
  207. if (temp_meas_ready) { // temp sample ready
  208. updateTemperaturesFromRawValues();
  209. input =
  210. #if HAS_PID_FOR_BOTH
  211. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  212. #elif ENABLED(PIDTEMP)
  213. current_temperature[hotend]
  214. #else
  215. current_temperature_bed
  216. #endif
  217. ;
  218. max = max(max, input);
  219. min = min(min, input);
  220. #if HAS_AUTO_FAN
  221. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  222. checkExtruderAutoFans();
  223. next_auto_fan_check_ms = ms + 2500UL;
  224. }
  225. #endif
  226. if (heating && input > temp) {
  227. if (ELAPSED(ms, t2 + 5000UL)) {
  228. heating = false;
  229. #if HAS_PID_FOR_BOTH
  230. if (hotend < 0)
  231. soft_pwm_bed = (bias - d) >> 1;
  232. else
  233. soft_pwm[hotend] = (bias - d) >> 1;
  234. #elif ENABLED(PIDTEMP)
  235. soft_pwm[hotend] = (bias - d) >> 1;
  236. #elif ENABLED(PIDTEMPBED)
  237. soft_pwm_bed = (bias - d) >> 1;
  238. #endif
  239. t1 = ms;
  240. t_high = t1 - t2;
  241. max = temp;
  242. }
  243. }
  244. if (!heating && input < temp) {
  245. if (ELAPSED(ms, t1 + 5000UL)) {
  246. heating = true;
  247. t2 = ms;
  248. t_low = t2 - t1;
  249. if (cycles > 0) {
  250. long max_pow =
  251. #if HAS_PID_FOR_BOTH
  252. hotend < 0 ? MAX_BED_POWER : PID_MAX
  253. #elif ENABLED(PIDTEMP)
  254. PID_MAX
  255. #else
  256. MAX_BED_POWER
  257. #endif
  258. ;
  259. bias += (d * (t_high - t_low)) / (t_low + t_high);
  260. bias = constrain(bias, 20, max_pow - 20);
  261. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  262. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  263. SERIAL_PROTOCOLPAIR(MSG_D, d);
  264. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  265. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  266. if (cycles > 2) {
  267. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  268. Tu = ((float)(t_low + t_high) / 1000.0);
  269. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  270. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  271. workKp = 0.6 * Ku;
  272. workKi = 2 * workKp / Tu;
  273. workKd = workKp * Tu / 8;
  274. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  275. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  276. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  277. SERIAL_PROTOCOLPAIR(MSG_KD, workKd);
  278. /**
  279. workKp = 0.33*Ku;
  280. workKi = workKp/Tu;
  281. workKd = workKp*Tu/3;
  282. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  283. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  284. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  285. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  286. workKp = 0.2*Ku;
  287. workKi = 2*workKp/Tu;
  288. workKd = workKp*Tu/3;
  289. SERIAL_PROTOCOLLNPGM(" No overshoot");
  290. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  291. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  292. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  293. */
  294. }
  295. }
  296. #if HAS_PID_FOR_BOTH
  297. if (hotend < 0)
  298. soft_pwm_bed = (bias + d) >> 1;
  299. else
  300. soft_pwm[hotend] = (bias + d) >> 1;
  301. #elif ENABLED(PIDTEMP)
  302. soft_pwm[hotend] = (bias + d) >> 1;
  303. #else
  304. soft_pwm_bed = (bias + d) >> 1;
  305. #endif
  306. cycles++;
  307. min = temp;
  308. }
  309. }
  310. }
  311. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  312. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  313. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  314. return;
  315. }
  316. // Every 2 seconds...
  317. if (ELAPSED(ms, temp_ms + 2000UL)) {
  318. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  319. print_heaterstates();
  320. SERIAL_EOL;
  321. #endif
  322. temp_ms = ms;
  323. } // every 2 seconds
  324. // Over 2 minutes?
  325. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  326. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  327. return;
  328. }
  329. if (cycles > ncycles) {
  330. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  331. #if HAS_PID_FOR_BOTH
  332. const char* estring = hotend < 0 ? "bed" : "";
  333. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp);
  334. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi);
  335. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd);
  336. #elif ENABLED(PIDTEMP)
  337. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp);
  338. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi);
  339. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd);
  340. #else
  341. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp);
  342. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi);
  343. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd);
  344. #endif
  345. #define _SET_BED_PID() \
  346. bedKp = workKp; \
  347. bedKi = scalePID_i(workKi); \
  348. bedKd = scalePID_d(workKd); \
  349. updatePID()
  350. #define _SET_EXTRUDER_PID() \
  351. PID_PARAM(Kp, hotend) = workKp; \
  352. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  353. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  354. updatePID()
  355. // Use the result? (As with "M303 U1")
  356. if (set_result) {
  357. #if HAS_PID_FOR_BOTH
  358. if (hotend < 0) {
  359. _SET_BED_PID();
  360. }
  361. else {
  362. _SET_EXTRUDER_PID();
  363. }
  364. #elif ENABLED(PIDTEMP)
  365. _SET_EXTRUDER_PID();
  366. #else
  367. _SET_BED_PID();
  368. #endif
  369. }
  370. return;
  371. }
  372. lcd_update();
  373. }
  374. if (!wait_for_heatup) disable_all_heaters();
  375. }
  376. #endif // HAS_PID_HEATING
  377. /**
  378. * Class and Instance Methods
  379. */
  380. Temperature::Temperature() { }
  381. void Temperature::updatePID() {
  382. #if ENABLED(PIDTEMP)
  383. HOTEND_LOOP() {
  384. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  385. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  386. last_position[e] = 0;
  387. #endif
  388. }
  389. #endif
  390. #if ENABLED(PIDTEMPBED)
  391. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  392. #endif
  393. }
  394. int Temperature::getHeaterPower(int heater) {
  395. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  396. }
  397. #if HAS_AUTO_FAN
  398. void Temperature::checkExtruderAutoFans() {
  399. const int8_t fanPin[] = { EXTRUDER_0_AUTO_FAN_PIN, EXTRUDER_1_AUTO_FAN_PIN, EXTRUDER_2_AUTO_FAN_PIN, EXTRUDER_3_AUTO_FAN_PIN };
  400. const int fanBit[] = { 0,
  401. EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : 1,
  402. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  403. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 : 2,
  404. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  405. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 :
  406. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN ? 2 : 3
  407. };
  408. uint8_t fanState = 0;
  409. HOTEND_LOOP() {
  410. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  411. SBI(fanState, fanBit[e]);
  412. }
  413. uint8_t fanDone = 0;
  414. for (int8_t f = 0; f <= 3; f++) {
  415. int8_t pin = fanPin[f];
  416. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  417. unsigned char newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  418. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  419. digitalWrite(pin, newFanSpeed);
  420. analogWrite(pin, newFanSpeed);
  421. SBI(fanDone, fanBit[f]);
  422. }
  423. }
  424. }
  425. #endif // HAS_AUTO_FAN
  426. //
  427. // Temperature Error Handlers
  428. //
  429. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  430. static bool killed = false;
  431. if (IsRunning()) {
  432. SERIAL_ERROR_START;
  433. serialprintPGM(serial_msg);
  434. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  435. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  436. }
  437. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  438. if (!killed) {
  439. Running = false;
  440. killed = true;
  441. kill(lcd_msg);
  442. }
  443. else
  444. disable_all_heaters(); // paranoia
  445. #endif
  446. }
  447. void Temperature::max_temp_error(uint8_t e) {
  448. #if HOTENDS == 1
  449. UNUSED(e);
  450. #endif
  451. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  452. }
  453. void Temperature::min_temp_error(uint8_t e) {
  454. #if HOTENDS == 1
  455. UNUSED(e);
  456. #endif
  457. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  458. }
  459. float Temperature::get_pid_output(int e) {
  460. #if HOTENDS == 1
  461. UNUSED(e);
  462. #define _HOTEND_TEST true
  463. #define _HOTEND_EXTRUDER active_extruder
  464. #else
  465. #define _HOTEND_TEST e == active_extruder
  466. #define _HOTEND_EXTRUDER e
  467. #endif
  468. float pid_output;
  469. #if ENABLED(PIDTEMP)
  470. #if DISABLED(PID_OPENLOOP)
  471. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  472. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  473. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  474. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  475. pid_output = BANG_MAX;
  476. pid_reset[HOTEND_INDEX] = true;
  477. }
  478. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0) {
  479. pid_output = 0;
  480. pid_reset[HOTEND_INDEX] = true;
  481. }
  482. else {
  483. if (pid_reset[HOTEND_INDEX]) {
  484. temp_iState[HOTEND_INDEX] = 0.0;
  485. pid_reset[HOTEND_INDEX] = false;
  486. }
  487. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  488. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  489. temp_iState[HOTEND_INDEX] = constrain(temp_iState[HOTEND_INDEX], temp_iState_min[HOTEND_INDEX], temp_iState_max[HOTEND_INDEX]);
  490. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  491. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  492. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  493. cTerm[HOTEND_INDEX] = 0;
  494. if (_HOTEND_TEST) {
  495. long e_position = stepper.position(E_AXIS);
  496. if (e_position > last_position[_HOTEND_EXTRUDER]) {
  497. lpq[lpq_ptr++] = e_position - last_position[_HOTEND_EXTRUDER];
  498. last_position[_HOTEND_EXTRUDER] = e_position;
  499. }
  500. else {
  501. lpq[lpq_ptr++] = 0;
  502. }
  503. if (lpq_ptr >= lpq_len) lpq_ptr = 0;
  504. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] / planner.axis_steps_per_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  505. pid_output += cTerm[HOTEND_INDEX];
  506. }
  507. #endif //PID_ADD_EXTRUSION_RATE
  508. if (pid_output > PID_MAX) {
  509. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  510. pid_output = PID_MAX;
  511. }
  512. else if (pid_output < 0) {
  513. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  514. pid_output = 0;
  515. }
  516. }
  517. #else
  518. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  519. #endif //PID_OPENLOOP
  520. #if ENABLED(PID_DEBUG)
  521. SERIAL_ECHO_START;
  522. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  523. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  524. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  525. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  526. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  527. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  528. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  529. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  530. #endif
  531. SERIAL_EOL;
  532. #endif //PID_DEBUG
  533. #else /* PID off */
  534. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  535. #endif
  536. return pid_output;
  537. }
  538. #if ENABLED(PIDTEMPBED)
  539. float Temperature::get_pid_output_bed() {
  540. float pid_output;
  541. #if DISABLED(PID_OPENLOOP)
  542. pid_error_bed = target_temperature_bed - current_temperature_bed;
  543. pTerm_bed = bedKp * pid_error_bed;
  544. temp_iState_bed += pid_error_bed;
  545. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  546. iTerm_bed = bedKi * temp_iState_bed;
  547. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  548. temp_dState_bed = current_temperature_bed;
  549. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  550. if (pid_output > MAX_BED_POWER) {
  551. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  552. pid_output = MAX_BED_POWER;
  553. }
  554. else if (pid_output < 0) {
  555. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  556. pid_output = 0;
  557. }
  558. #else
  559. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  560. #endif // PID_OPENLOOP
  561. #if ENABLED(PID_BED_DEBUG)
  562. SERIAL_ECHO_START;
  563. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  564. SERIAL_ECHOPGM(": Input ");
  565. SERIAL_ECHO(current_temperature_bed);
  566. SERIAL_ECHOPGM(" Output ");
  567. SERIAL_ECHO(pid_output);
  568. SERIAL_ECHOPGM(" pTerm ");
  569. SERIAL_ECHO(pTerm_bed);
  570. SERIAL_ECHOPGM(" iTerm ");
  571. SERIAL_ECHO(iTerm_bed);
  572. SERIAL_ECHOPGM(" dTerm ");
  573. SERIAL_ECHOLN(dTerm_bed);
  574. #endif //PID_BED_DEBUG
  575. return pid_output;
  576. }
  577. #endif //PIDTEMPBED
  578. /**
  579. * Manage heating activities for extruder hot-ends and a heated bed
  580. * - Acquire updated temperature readings
  581. * - Also resets the watchdog timer
  582. * - Invoke thermal runaway protection
  583. * - Manage extruder auto-fan
  584. * - Apply filament width to the extrusion rate (may move)
  585. * - Update the heated bed PID output value
  586. */
  587. void Temperature::manage_heater() {
  588. if (!temp_meas_ready) return;
  589. updateTemperaturesFromRawValues(); // also resets the watchdog
  590. #if ENABLED(HEATER_0_USES_MAX6675)
  591. float ct = current_temperature[0];
  592. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  593. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  594. #endif
  595. #if (ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0) || (ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0) || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  596. millis_t ms = millis();
  597. #endif
  598. // Loop through all hotends
  599. HOTEND_LOOP() {
  600. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  601. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  602. #endif
  603. float pid_output = get_pid_output(e);
  604. // Check if temperature is within the correct range
  605. soft_pwm[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  606. // Check if the temperature is failing to increase
  607. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  608. // Is it time to check this extruder's heater?
  609. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  610. // Has it failed to increase enough?
  611. if (degHotend(e) < watch_target_temp[e]) {
  612. // Stop!
  613. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  614. }
  615. else {
  616. // Start again if the target is still far off
  617. start_watching_heater(e);
  618. }
  619. }
  620. #endif // THERMAL_PROTECTION_HOTENDS
  621. // Check if the temperature is failing to increase
  622. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  623. // Is it time to check the bed?
  624. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) {
  625. // Has it failed to increase enough?
  626. if (degBed() < watch_target_bed_temp) {
  627. // Stop!
  628. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  629. }
  630. else {
  631. // Start again if the target is still far off
  632. start_watching_bed();
  633. }
  634. }
  635. #endif // THERMAL_PROTECTION_HOTENDS
  636. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  637. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  638. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  639. }
  640. #endif
  641. } // Hotends Loop
  642. #if HAS_AUTO_FAN
  643. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  644. checkExtruderAutoFans();
  645. next_auto_fan_check_ms = ms + 2500UL;
  646. }
  647. #endif
  648. // Control the extruder rate based on the width sensor
  649. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  650. if (filament_sensor) {
  651. meas_shift_index = filwidth_delay_index1 - meas_delay_cm;
  652. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  653. // Get the delayed info and add 100 to reconstitute to a percent of
  654. // the nominal filament diameter then square it to get an area
  655. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  656. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  657. NOLESS(vm, 0.01);
  658. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  659. }
  660. #endif //FILAMENT_WIDTH_SENSOR
  661. #if DISABLED(PIDTEMPBED)
  662. if (PENDING(ms, next_bed_check_ms)) return;
  663. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  664. #endif
  665. #if TEMP_SENSOR_BED != 0
  666. #if HAS_THERMALLY_PROTECTED_BED
  667. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  668. #endif
  669. #if ENABLED(PIDTEMPBED)
  670. float pid_output = get_pid_output_bed();
  671. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  672. #elif ENABLED(BED_LIMIT_SWITCHING)
  673. // Check if temperature is within the correct band
  674. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  675. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  676. soft_pwm_bed = 0;
  677. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  678. soft_pwm_bed = MAX_BED_POWER >> 1;
  679. }
  680. else {
  681. soft_pwm_bed = 0;
  682. WRITE_HEATER_BED(LOW);
  683. }
  684. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  685. // Check if temperature is within the correct range
  686. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  687. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  688. }
  689. else {
  690. soft_pwm_bed = 0;
  691. WRITE_HEATER_BED(LOW);
  692. }
  693. #endif
  694. #endif //TEMP_SENSOR_BED != 0
  695. }
  696. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  697. // Derived from RepRap FiveD extruder::getTemperature()
  698. // For hot end temperature measurement.
  699. float Temperature::analog2temp(int raw, uint8_t e) {
  700. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  701. if (e > HOTENDS)
  702. #else
  703. if (e >= HOTENDS)
  704. #endif
  705. {
  706. SERIAL_ERROR_START;
  707. SERIAL_ERROR((int)e);
  708. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  709. kill(PSTR(MSG_KILLED));
  710. return 0.0;
  711. }
  712. #if ENABLED(HEATER_0_USES_MAX6675)
  713. if (e == 0) return 0.25 * raw;
  714. #endif
  715. if (heater_ttbl_map[e] != NULL) {
  716. float celsius = 0;
  717. uint8_t i;
  718. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  719. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  720. if (PGM_RD_W((*tt)[i][0]) > raw) {
  721. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  722. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  723. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  724. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  725. break;
  726. }
  727. }
  728. // Overflow: Set to last value in the table
  729. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  730. return celsius;
  731. }
  732. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  733. }
  734. // Derived from RepRap FiveD extruder::getTemperature()
  735. // For bed temperature measurement.
  736. float Temperature::analog2tempBed(int raw) {
  737. #if ENABLED(BED_USES_THERMISTOR)
  738. float celsius = 0;
  739. byte i;
  740. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  741. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  742. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  743. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  744. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  745. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  746. break;
  747. }
  748. }
  749. // Overflow: Set to last value in the table
  750. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  751. return celsius;
  752. #elif defined(BED_USES_AD595)
  753. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  754. #else
  755. UNUSED(raw);
  756. return 0;
  757. #endif
  758. }
  759. /**
  760. * Get the raw values into the actual temperatures.
  761. * The raw values are created in interrupt context,
  762. * and this function is called from normal context
  763. * as it would block the stepper routine.
  764. */
  765. void Temperature::updateTemperaturesFromRawValues() {
  766. #if ENABLED(HEATER_0_USES_MAX6675)
  767. current_temperature_raw[0] = read_max6675();
  768. #endif
  769. HOTEND_LOOP() {
  770. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  771. }
  772. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  773. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  774. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  775. #endif
  776. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  777. filament_width_meas = analog2widthFil();
  778. #endif
  779. #if ENABLED(USE_WATCHDOG)
  780. // Reset the watchdog after we know we have a temperature measurement.
  781. watchdog_reset();
  782. #endif
  783. CRITICAL_SECTION_START;
  784. temp_meas_ready = false;
  785. CRITICAL_SECTION_END;
  786. }
  787. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  788. // Convert raw Filament Width to millimeters
  789. float Temperature::analog2widthFil() {
  790. return current_raw_filwidth / 16383.0 * 5.0;
  791. //return current_raw_filwidth;
  792. }
  793. // Convert raw Filament Width to a ratio
  794. int Temperature::widthFil_to_size_ratio() {
  795. float temp = filament_width_meas;
  796. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  797. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  798. return filament_width_nominal / temp * 100;
  799. }
  800. #endif
  801. /**
  802. * Initialize the temperature manager
  803. * The manager is implemented by periodic calls to manage_heater()
  804. */
  805. void Temperature::init() {
  806. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  807. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  808. MCUCR = _BV(JTD);
  809. MCUCR = _BV(JTD);
  810. #endif
  811. // Finish init of mult hotend arrays
  812. HOTEND_LOOP() {
  813. // populate with the first value
  814. maxttemp[e] = maxttemp[0];
  815. #if ENABLED(PIDTEMP)
  816. temp_iState_min[e] = 0.0;
  817. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  818. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  819. last_position[e] = 0;
  820. #endif
  821. #endif //PIDTEMP
  822. #if ENABLED(PIDTEMPBED)
  823. temp_iState_min_bed = 0.0;
  824. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  825. #endif //PIDTEMPBED
  826. }
  827. #if HAS_HEATER_0
  828. SET_OUTPUT(HEATER_0_PIN);
  829. #endif
  830. #if HAS_HEATER_1
  831. SET_OUTPUT(HEATER_1_PIN);
  832. #endif
  833. #if HAS_HEATER_2
  834. SET_OUTPUT(HEATER_2_PIN);
  835. #endif
  836. #if HAS_HEATER_3
  837. SET_OUTPUT(HEATER_3_PIN);
  838. #endif
  839. #if HAS_HEATER_BED
  840. SET_OUTPUT(HEATER_BED_PIN);
  841. #endif
  842. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  843. #if HAS_FAN0
  844. SET_OUTPUT(FAN_PIN);
  845. #if ENABLED(FAST_PWM_FAN)
  846. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  847. #endif
  848. #if ENABLED(FAN_SOFT_PWM)
  849. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  850. #endif
  851. #endif
  852. #if HAS_FAN1
  853. SET_OUTPUT(FAN1_PIN);
  854. #if ENABLED(FAST_PWM_FAN)
  855. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  856. #endif
  857. #if ENABLED(FAN_SOFT_PWM)
  858. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  859. #endif
  860. #endif
  861. #if HAS_FAN2
  862. SET_OUTPUT(FAN2_PIN);
  863. #if ENABLED(FAST_PWM_FAN)
  864. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  865. #endif
  866. #if ENABLED(FAN_SOFT_PWM)
  867. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  868. #endif
  869. #endif
  870. #endif // FAST_PWM_FAN || FAN_SOFT_PWM
  871. #if ENABLED(HEATER_0_USES_MAX6675)
  872. #if DISABLED(SDSUPPORT)
  873. OUT_WRITE(SCK_PIN, LOW);
  874. OUT_WRITE(MOSI_PIN, HIGH);
  875. OUT_WRITE(MISO_PIN, HIGH);
  876. #else
  877. pinMode(SS_PIN, OUTPUT);
  878. digitalWrite(SS_PIN, HIGH);
  879. #endif
  880. OUT_WRITE(MAX6675_SS, HIGH);
  881. #endif //HEATER_0_USES_MAX6675
  882. #ifdef DIDR2
  883. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  884. #else
  885. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  886. #endif
  887. // Set analog inputs
  888. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  889. DIDR0 = 0;
  890. #ifdef DIDR2
  891. DIDR2 = 0;
  892. #endif
  893. #if HAS_TEMP_0
  894. ANALOG_SELECT(TEMP_0_PIN);
  895. #endif
  896. #if HAS_TEMP_1
  897. ANALOG_SELECT(TEMP_1_PIN);
  898. #endif
  899. #if HAS_TEMP_2
  900. ANALOG_SELECT(TEMP_2_PIN);
  901. #endif
  902. #if HAS_TEMP_3
  903. ANALOG_SELECT(TEMP_3_PIN);
  904. #endif
  905. #if HAS_TEMP_BED
  906. ANALOG_SELECT(TEMP_BED_PIN);
  907. #endif
  908. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  909. ANALOG_SELECT(FILWIDTH_PIN);
  910. #endif
  911. #if HAS_AUTO_FAN_0
  912. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  913. #endif
  914. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  915. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  916. #endif
  917. #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  918. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  919. #endif
  920. #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  921. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  922. #endif
  923. // Use timer0 for temperature measurement
  924. // Interleave temperature interrupt with millies interrupt
  925. OCR0B = 128;
  926. SBI(TIMSK0, OCIE0B);
  927. // Wait for temperature measurement to settle
  928. delay(250);
  929. #define TEMP_MIN_ROUTINE(NR) \
  930. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  931. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  932. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  933. minttemp_raw[NR] += OVERSAMPLENR; \
  934. else \
  935. minttemp_raw[NR] -= OVERSAMPLENR; \
  936. }
  937. #define TEMP_MAX_ROUTINE(NR) \
  938. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  939. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  940. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  941. maxttemp_raw[NR] -= OVERSAMPLENR; \
  942. else \
  943. maxttemp_raw[NR] += OVERSAMPLENR; \
  944. }
  945. #ifdef HEATER_0_MINTEMP
  946. TEMP_MIN_ROUTINE(0);
  947. #endif
  948. #ifdef HEATER_0_MAXTEMP
  949. TEMP_MAX_ROUTINE(0);
  950. #endif
  951. #if HOTENDS > 1
  952. #ifdef HEATER_1_MINTEMP
  953. TEMP_MIN_ROUTINE(1);
  954. #endif
  955. #ifdef HEATER_1_MAXTEMP
  956. TEMP_MAX_ROUTINE(1);
  957. #endif
  958. #if HOTENDS > 2
  959. #ifdef HEATER_2_MINTEMP
  960. TEMP_MIN_ROUTINE(2);
  961. #endif
  962. #ifdef HEATER_2_MAXTEMP
  963. TEMP_MAX_ROUTINE(2);
  964. #endif
  965. #if HOTENDS > 3
  966. #ifdef HEATER_3_MINTEMP
  967. TEMP_MIN_ROUTINE(3);
  968. #endif
  969. #ifdef HEATER_3_MAXTEMP
  970. TEMP_MAX_ROUTINE(3);
  971. #endif
  972. #endif // HOTENDS > 3
  973. #endif // HOTENDS > 2
  974. #endif // HOTENDS > 1
  975. #ifdef BED_MINTEMP
  976. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  977. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  978. bed_minttemp_raw += OVERSAMPLENR;
  979. #else
  980. bed_minttemp_raw -= OVERSAMPLENR;
  981. #endif
  982. }
  983. #endif //BED_MINTEMP
  984. #ifdef BED_MAXTEMP
  985. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  986. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  987. bed_maxttemp_raw -= OVERSAMPLENR;
  988. #else
  989. bed_maxttemp_raw += OVERSAMPLENR;
  990. #endif
  991. }
  992. #endif //BED_MAXTEMP
  993. }
  994. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  995. /**
  996. * Start Heating Sanity Check for hotends that are below
  997. * their target temperature by a configurable margin.
  998. * This is called when the temperature is set. (M104, M109)
  999. */
  1000. void Temperature::start_watching_heater(uint8_t e) {
  1001. #if HOTENDS == 1
  1002. UNUSED(e);
  1003. #endif
  1004. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1005. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1006. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1007. }
  1008. else
  1009. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1010. }
  1011. #endif
  1012. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  1013. /**
  1014. * Start Heating Sanity Check for hotends that are below
  1015. * their target temperature by a configurable margin.
  1016. * This is called when the temperature is set. (M140, M190)
  1017. */
  1018. void Temperature::start_watching_bed() {
  1019. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1020. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1021. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1022. }
  1023. else
  1024. watch_bed_next_ms = 0;
  1025. }
  1026. #endif
  1027. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1028. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1029. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1030. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1031. #endif
  1032. #if HAS_THERMALLY_PROTECTED_BED
  1033. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1034. millis_t Temperature::thermal_runaway_bed_timer;
  1035. #endif
  1036. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  1037. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1038. /**
  1039. SERIAL_ECHO_START;
  1040. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1041. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1042. SERIAL_ECHOPAIR(" ; State:", *state);
  1043. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1044. SERIAL_ECHOPAIR(" ; Temperature:", temperature);
  1045. SERIAL_ECHOPAIR(" ; Target Temp:", target_temperature);
  1046. SERIAL_EOL;
  1047. */
  1048. int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1049. // If the target temperature changes, restart
  1050. if (tr_target_temperature[heater_index] != target_temperature) {
  1051. tr_target_temperature[heater_index] = target_temperature;
  1052. *state = target_temperature > 0 ? TRFirstHeating : TRInactive;
  1053. }
  1054. switch (*state) {
  1055. // Inactive state waits for a target temperature to be set
  1056. case TRInactive: break;
  1057. // When first heating, wait for the temperature to be reached then go to Stable state
  1058. case TRFirstHeating:
  1059. if (temperature < tr_target_temperature[heater_index]) break;
  1060. *state = TRStable;
  1061. // While the temperature is stable watch for a bad temperature
  1062. case TRStable:
  1063. if (temperature < tr_target_temperature[heater_index] - hysteresis_degc && ELAPSED(millis(), *timer))
  1064. *state = TRRunaway;
  1065. else {
  1066. *timer = millis() + period_seconds * 1000UL;
  1067. break;
  1068. }
  1069. case TRRunaway:
  1070. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1071. }
  1072. }
  1073. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1074. void Temperature::disable_all_heaters() {
  1075. HOTEND_LOOP() setTargetHotend(0, e);
  1076. setTargetBed(0);
  1077. // If all heaters go down then for sure our print job has stopped
  1078. print_job_timer.stop();
  1079. #define DISABLE_HEATER(NR) { \
  1080. setTargetHotend(0, NR); \
  1081. soft_pwm[NR] = 0; \
  1082. WRITE_HEATER_ ## NR (LOW); \
  1083. }
  1084. #if HAS_TEMP_HOTEND
  1085. setTargetHotend(0, 0);
  1086. soft_pwm[0] = 0;
  1087. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  1088. #endif
  1089. #if HOTENDS > 1 && HAS_TEMP_1
  1090. DISABLE_HEATER(1);
  1091. #endif
  1092. #if HOTENDS > 2 && HAS_TEMP_2
  1093. DISABLE_HEATER(2);
  1094. #endif
  1095. #if HOTENDS > 3 && HAS_TEMP_3
  1096. DISABLE_HEATER(3);
  1097. #endif
  1098. #if HAS_TEMP_BED
  1099. target_temperature_bed = 0;
  1100. soft_pwm_bed = 0;
  1101. #if HAS_HEATER_BED
  1102. WRITE_HEATER_BED(LOW);
  1103. #endif
  1104. #endif
  1105. }
  1106. #if ENABLED(HEATER_0_USES_MAX6675)
  1107. #define MAX6675_HEAT_INTERVAL 250u
  1108. #if ENABLED(MAX6675_IS_MAX31855)
  1109. uint32_t max6675_temp = 2000;
  1110. #define MAX6675_ERROR_MASK 7
  1111. #define MAX6675_DISCARD_BITS 18
  1112. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1113. #else
  1114. uint16_t max6675_temp = 2000;
  1115. #define MAX6675_ERROR_MASK 4
  1116. #define MAX6675_DISCARD_BITS 3
  1117. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1118. #endif
  1119. int Temperature::read_max6675() {
  1120. static millis_t next_max6675_ms = 0;
  1121. millis_t ms = millis();
  1122. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1123. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1124. CBI(
  1125. #ifdef PRR
  1126. PRR
  1127. #elif defined(PRR0)
  1128. PRR0
  1129. #endif
  1130. , PRSPI);
  1131. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1132. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1133. // ensure 100ns delay - a bit extra is fine
  1134. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1135. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1136. // Read a big-endian temperature value
  1137. max6675_temp = 0;
  1138. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1139. SPDR = 0;
  1140. for (;!TEST(SPSR, SPIF););
  1141. max6675_temp |= SPDR;
  1142. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1143. }
  1144. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1145. if (max6675_temp & MAX6675_ERROR_MASK)
  1146. max6675_temp = 4000; // thermocouple open
  1147. else
  1148. max6675_temp >>= MAX6675_DISCARD_BITS;
  1149. return (int)max6675_temp;
  1150. }
  1151. #endif //HEATER_0_USES_MAX6675
  1152. /**
  1153. * Stages in the ISR loop
  1154. */
  1155. enum TempState {
  1156. PrepareTemp_0,
  1157. MeasureTemp_0,
  1158. PrepareTemp_BED,
  1159. MeasureTemp_BED,
  1160. PrepareTemp_1,
  1161. MeasureTemp_1,
  1162. PrepareTemp_2,
  1163. MeasureTemp_2,
  1164. PrepareTemp_3,
  1165. MeasureTemp_3,
  1166. Prepare_FILWIDTH,
  1167. Measure_FILWIDTH,
  1168. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1169. };
  1170. /**
  1171. * Get raw temperatures
  1172. */
  1173. void Temperature::set_current_temp_raw() {
  1174. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1175. current_temperature_raw[0] = raw_temp_value[0];
  1176. #endif
  1177. #if HAS_TEMP_1
  1178. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1179. redundant_temperature_raw = raw_temp_value[1];
  1180. #else
  1181. current_temperature_raw[1] = raw_temp_value[1];
  1182. #endif
  1183. #if HAS_TEMP_2
  1184. current_temperature_raw[2] = raw_temp_value[2];
  1185. #if HAS_TEMP_3
  1186. current_temperature_raw[3] = raw_temp_value[3];
  1187. #endif
  1188. #endif
  1189. #endif
  1190. current_temperature_bed_raw = raw_temp_bed_value;
  1191. temp_meas_ready = true;
  1192. }
  1193. /**
  1194. * Timer 0 is shared with millies
  1195. * - Manage PWM to all the heaters and fan
  1196. * - Update the raw temperature values
  1197. * - Check new temperature values for MIN/MAX errors
  1198. * - Step the babysteps value for each axis towards 0
  1199. */
  1200. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1201. void Temperature::isr() {
  1202. static unsigned char temp_count = 0;
  1203. static TempState temp_state = StartupDelay;
  1204. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1205. // Static members for each heater
  1206. #if ENABLED(SLOW_PWM_HEATERS)
  1207. static unsigned char slow_pwm_count = 0;
  1208. #define ISR_STATICS(n) \
  1209. static unsigned char soft_pwm_ ## n; \
  1210. static unsigned char state_heater_ ## n = 0; \
  1211. static unsigned char state_timer_heater_ ## n = 0
  1212. #else
  1213. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1214. #endif
  1215. // Statics per heater
  1216. ISR_STATICS(0);
  1217. #if (HOTENDS > 1) || ENABLED(HEATERS_PARALLEL)
  1218. ISR_STATICS(1);
  1219. #if HOTENDS > 2
  1220. ISR_STATICS(2);
  1221. #if HOTENDS > 3
  1222. ISR_STATICS(3);
  1223. #endif
  1224. #endif
  1225. #endif
  1226. #if HAS_HEATER_BED
  1227. ISR_STATICS(BED);
  1228. #endif
  1229. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1230. static unsigned long raw_filwidth_value = 0;
  1231. #endif
  1232. #if DISABLED(SLOW_PWM_HEATERS)
  1233. /**
  1234. * standard PWM modulation
  1235. */
  1236. if (pwm_count == 0) {
  1237. soft_pwm_0 = soft_pwm[0];
  1238. if (soft_pwm_0 > 0) {
  1239. WRITE_HEATER_0(1);
  1240. }
  1241. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1242. #if HOTENDS > 1
  1243. soft_pwm_1 = soft_pwm[1];
  1244. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1245. #if HOTENDS > 2
  1246. soft_pwm_2 = soft_pwm[2];
  1247. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1248. #if HOTENDS > 3
  1249. soft_pwm_3 = soft_pwm[3];
  1250. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1251. #endif
  1252. #endif
  1253. #endif
  1254. #if HAS_HEATER_BED
  1255. soft_pwm_BED = soft_pwm_bed;
  1256. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1257. #endif
  1258. #if ENABLED(FAN_SOFT_PWM)
  1259. #if HAS_FAN0
  1260. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1261. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1262. #endif
  1263. #if HAS_FAN1
  1264. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1265. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1266. #endif
  1267. #if HAS_FAN2
  1268. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1269. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1270. #endif
  1271. #endif
  1272. }
  1273. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1274. #if HOTENDS > 1
  1275. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1276. #if HOTENDS > 2
  1277. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1278. #if HOTENDS > 3
  1279. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1280. #endif
  1281. #endif
  1282. #endif
  1283. #if HAS_HEATER_BED
  1284. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1285. #endif
  1286. #if ENABLED(FAN_SOFT_PWM)
  1287. #if HAS_FAN0
  1288. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1289. #endif
  1290. #if HAS_FAN1
  1291. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1292. #endif
  1293. #if HAS_FAN2
  1294. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1295. #endif
  1296. #endif
  1297. pwm_count += _BV(SOFT_PWM_SCALE);
  1298. pwm_count &= 0x7f;
  1299. #else // SLOW_PWM_HEATERS
  1300. /**
  1301. * SLOW PWM HEATERS
  1302. *
  1303. * for heaters drived by relay
  1304. */
  1305. #ifndef MIN_STATE_TIME
  1306. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1307. #endif
  1308. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1309. #define _SLOW_PWM_ROUTINE(NR, src) \
  1310. soft_pwm_ ## NR = src; \
  1311. if (soft_pwm_ ## NR > 0) { \
  1312. if (state_timer_heater_ ## NR == 0) { \
  1313. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1314. state_heater_ ## NR = 1; \
  1315. WRITE_HEATER_ ## NR(1); \
  1316. } \
  1317. } \
  1318. else { \
  1319. if (state_timer_heater_ ## NR == 0) { \
  1320. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1321. state_heater_ ## NR = 0; \
  1322. WRITE_HEATER_ ## NR(0); \
  1323. } \
  1324. }
  1325. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1326. #define PWM_OFF_ROUTINE(NR) \
  1327. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1328. if (state_timer_heater_ ## NR == 0) { \
  1329. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1330. state_heater_ ## NR = 0; \
  1331. WRITE_HEATER_ ## NR (0); \
  1332. } \
  1333. }
  1334. if (slow_pwm_count == 0) {
  1335. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1336. #if HOTENDS > 1
  1337. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1338. #if HOTENDS > 2
  1339. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1340. #if HOTENDS > 3
  1341. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1342. #endif
  1343. #endif
  1344. #endif
  1345. #if HAS_HEATER_BED
  1346. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1347. #endif
  1348. } // slow_pwm_count == 0
  1349. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1350. #if HOTENDS > 1
  1351. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1352. #if HOTENDS > 2
  1353. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1354. #if HOTENDS > 3
  1355. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1356. #endif
  1357. #endif
  1358. #endif
  1359. #if HAS_HEATER_BED
  1360. PWM_OFF_ROUTINE(BED); // BED
  1361. #endif
  1362. #if ENABLED(FAN_SOFT_PWM)
  1363. if (pwm_count == 0) {
  1364. #if HAS_FAN0
  1365. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1366. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1367. #endif
  1368. #if HAS_FAN1
  1369. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1370. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1371. #endif
  1372. #if HAS_FAN2
  1373. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1374. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1375. #endif
  1376. }
  1377. #if HAS_FAN0
  1378. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1379. #endif
  1380. #if HAS_FAN1
  1381. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1382. #endif
  1383. #if HAS_FAN2
  1384. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1385. #endif
  1386. #endif //FAN_SOFT_PWM
  1387. pwm_count += _BV(SOFT_PWM_SCALE);
  1388. pwm_count &= 0x7f;
  1389. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1390. if ((pwm_count % 64) == 0) {
  1391. slow_pwm_count++;
  1392. slow_pwm_count &= 0x7f;
  1393. // EXTRUDER 0
  1394. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1395. #if HOTENDS > 1 // EXTRUDER 1
  1396. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1397. #if HOTENDS > 2 // EXTRUDER 2
  1398. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1399. #if HOTENDS > 3 // EXTRUDER 3
  1400. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1401. #endif
  1402. #endif
  1403. #endif
  1404. #if HAS_HEATER_BED
  1405. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1406. #endif
  1407. } // (pwm_count % 64) == 0
  1408. #endif // SLOW_PWM_HEATERS
  1409. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1410. #ifdef MUX5
  1411. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1412. #else
  1413. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1414. #endif
  1415. // Prepare or measure a sensor, each one every 12th frame
  1416. switch (temp_state) {
  1417. case PrepareTemp_0:
  1418. #if HAS_TEMP_0
  1419. START_ADC(TEMP_0_PIN);
  1420. #endif
  1421. lcd_buttons_update();
  1422. temp_state = MeasureTemp_0;
  1423. break;
  1424. case MeasureTemp_0:
  1425. #if HAS_TEMP_0
  1426. raw_temp_value[0] += ADC;
  1427. #endif
  1428. temp_state = PrepareTemp_BED;
  1429. break;
  1430. case PrepareTemp_BED:
  1431. #if HAS_TEMP_BED
  1432. START_ADC(TEMP_BED_PIN);
  1433. #endif
  1434. lcd_buttons_update();
  1435. temp_state = MeasureTemp_BED;
  1436. break;
  1437. case MeasureTemp_BED:
  1438. #if HAS_TEMP_BED
  1439. raw_temp_bed_value += ADC;
  1440. #endif
  1441. temp_state = PrepareTemp_1;
  1442. break;
  1443. case PrepareTemp_1:
  1444. #if HAS_TEMP_1
  1445. START_ADC(TEMP_1_PIN);
  1446. #endif
  1447. lcd_buttons_update();
  1448. temp_state = MeasureTemp_1;
  1449. break;
  1450. case MeasureTemp_1:
  1451. #if HAS_TEMP_1
  1452. raw_temp_value[1] += ADC;
  1453. #endif
  1454. temp_state = PrepareTemp_2;
  1455. break;
  1456. case PrepareTemp_2:
  1457. #if HAS_TEMP_2
  1458. START_ADC(TEMP_2_PIN);
  1459. #endif
  1460. lcd_buttons_update();
  1461. temp_state = MeasureTemp_2;
  1462. break;
  1463. case MeasureTemp_2:
  1464. #if HAS_TEMP_2
  1465. raw_temp_value[2] += ADC;
  1466. #endif
  1467. temp_state = PrepareTemp_3;
  1468. break;
  1469. case PrepareTemp_3:
  1470. #if HAS_TEMP_3
  1471. START_ADC(TEMP_3_PIN);
  1472. #endif
  1473. lcd_buttons_update();
  1474. temp_state = MeasureTemp_3;
  1475. break;
  1476. case MeasureTemp_3:
  1477. #if HAS_TEMP_3
  1478. raw_temp_value[3] += ADC;
  1479. #endif
  1480. temp_state = Prepare_FILWIDTH;
  1481. break;
  1482. case Prepare_FILWIDTH:
  1483. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1484. START_ADC(FILWIDTH_PIN);
  1485. #endif
  1486. lcd_buttons_update();
  1487. temp_state = Measure_FILWIDTH;
  1488. break;
  1489. case Measure_FILWIDTH:
  1490. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1491. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1492. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1493. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1494. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1495. }
  1496. #endif
  1497. temp_state = PrepareTemp_0;
  1498. temp_count++;
  1499. break;
  1500. case StartupDelay:
  1501. temp_state = PrepareTemp_0;
  1502. break;
  1503. // default:
  1504. // SERIAL_ERROR_START;
  1505. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1506. // break;
  1507. } // switch(temp_state)
  1508. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1509. // Update the raw values if they've been read. Else we could be updating them during reading.
  1510. if (!temp_meas_ready) set_current_temp_raw();
  1511. // Filament Sensor - can be read any time since IIR filtering is used
  1512. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1513. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1514. #endif
  1515. temp_count = 0;
  1516. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1517. raw_temp_bed_value = 0;
  1518. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1519. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1520. #define GE0 <=
  1521. #else
  1522. #define GE0 >=
  1523. #endif
  1524. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1525. if (minttemp_raw[0] GE0 current_temperature_raw[0] && !is_preheating(0) && target_temperature[0] > 0.0f) {
  1526. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1527. if (++consecutive_low_temperature_error[0] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1528. #endif
  1529. min_temp_error(0);
  1530. }
  1531. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1532. else
  1533. consecutive_low_temperature_error[0] = 0;
  1534. #endif
  1535. #endif
  1536. #if HAS_TEMP_1 && HOTENDS > 1
  1537. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1538. #define GE1 <=
  1539. #else
  1540. #define GE1 >=
  1541. #endif
  1542. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1543. if (minttemp_raw[1] GE1 current_temperature_raw[1] && !is_preheating(1) && target_temperature[1] > 0.0f) {
  1544. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1545. if (++consecutive_low_temperature_error[1] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1546. #endif
  1547. min_temp_error(1);
  1548. }
  1549. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1550. else
  1551. consecutive_low_temperature_error[1] = 0;
  1552. #endif
  1553. #endif // TEMP_SENSOR_1
  1554. #if HAS_TEMP_2 && HOTENDS > 2
  1555. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1556. #define GE2 <=
  1557. #else
  1558. #define GE2 >=
  1559. #endif
  1560. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1561. if (minttemp_raw[2] GE2 current_temperature_raw[2] && !is_preheating(2) && target_temperature[2] > 0.0f) {
  1562. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1563. if (++consecutive_low_temperature_error[2] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1564. #endif
  1565. min_temp_error(2);
  1566. }
  1567. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1568. else
  1569. consecutive_low_temperature_error[2] = 0;
  1570. #endif
  1571. #endif // TEMP_SENSOR_2
  1572. #if HAS_TEMP_3 && HOTENDS > 3
  1573. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1574. #define GE3 <=
  1575. #else
  1576. #define GE3 >=
  1577. #endif
  1578. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1579. if (minttemp_raw[3] GE3 current_temperature_raw[3] && !is_preheating(3) && target_temperature[3] > 0.0f) {
  1580. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1581. if (++consecutive_low_temperature_error[3] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1582. #endif
  1583. min_temp_error(3);
  1584. }
  1585. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1586. else
  1587. consecutive_low_temperature_error[3] = 0;
  1588. #endif
  1589. #endif // TEMP_SENSOR_3
  1590. #if HAS_TEMP_BED
  1591. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1592. #define GEBED <=
  1593. #else
  1594. #define GEBED >=
  1595. #endif
  1596. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1597. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1598. #endif
  1599. } // temp_count >= OVERSAMPLENR
  1600. #if ENABLED(BABYSTEPPING)
  1601. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1602. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1603. if (curTodo > 0) {
  1604. stepper.babystep(axis,/*fwd*/true);
  1605. babystepsTodo[axis]--; //fewer to do next time
  1606. }
  1607. else if (curTodo < 0) {
  1608. stepper.babystep(axis,/*fwd*/false);
  1609. babystepsTodo[axis]++; //fewer to do next time
  1610. }
  1611. }
  1612. #endif //BABYSTEPPING
  1613. }