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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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 "thermistortables.h"
  29. #include "language.h"
  30. #include "spi.h"
  31. #if ENABLED(BABYSTEPPING)
  32. #include "stepper.h"
  33. #endif
  34. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  35. #include "endstops.h"
  36. #endif
  37. #if ENABLED(USE_WATCHDOG)
  38. #include "watchdog.h"
  39. #endif
  40. #ifdef K1 // Defined in Configuration.h in the PID settings
  41. #define K2 (1.0-K1)
  42. #endif
  43. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  44. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  45. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  46. #else
  47. 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, (void*)HEATER_4_TEMPTABLE);
  48. 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, HEATER_4_TEMPTABLE_LEN);
  49. #endif
  50. Temperature thermalManager;
  51. // public:
  52. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  53. Temperature::current_temperature_bed = 0.0;
  54. int Temperature::current_temperature_raw[HOTENDS] = { 0 },
  55. Temperature::target_temperature[HOTENDS] = { 0 },
  56. Temperature::current_temperature_bed_raw = 0,
  57. Temperature::target_temperature_bed = 0;
  58. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  59. float Temperature::redundant_temperature = 0.0;
  60. #endif
  61. uint8_t Temperature::soft_pwm_bed;
  62. #if ENABLED(FAN_SOFT_PWM)
  63. uint8_t Temperature::fanSpeedSoftPwm[FAN_COUNT];
  64. #endif
  65. #if ENABLED(PIDTEMP)
  66. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  67. float Temperature::Kp[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kp),
  68. Temperature::Ki[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Ki) * (PID_dT)),
  69. Temperature::Kd[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Kd) / (PID_dT));
  70. #if ENABLED(PID_EXTRUSION_SCALING)
  71. float Temperature::Kc[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kc);
  72. #endif
  73. #else
  74. float Temperature::Kp = DEFAULT_Kp,
  75. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  76. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  77. #if ENABLED(PID_EXTRUSION_SCALING)
  78. float Temperature::Kc = DEFAULT_Kc;
  79. #endif
  80. #endif
  81. #endif
  82. #if ENABLED(PIDTEMPBED)
  83. float Temperature::bedKp = DEFAULT_bedKp,
  84. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  85. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  86. #endif
  87. #if ENABLED(BABYSTEPPING)
  88. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  89. #endif
  90. #if WATCH_HOTENDS
  91. int Temperature::watch_target_temp[HOTENDS] = { 0 };
  92. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  93. #endif
  94. #if WATCH_THE_BED
  95. int Temperature::watch_target_bed_temp = 0;
  96. millis_t Temperature::watch_bed_next_ms = 0;
  97. #endif
  98. #if ENABLED(PREVENT_COLD_EXTRUSION)
  99. bool Temperature::allow_cold_extrude = false;
  100. float Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  101. #endif
  102. // private:
  103. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  104. int Temperature::redundant_temperature_raw = 0;
  105. float Temperature::redundant_temperature = 0.0;
  106. #endif
  107. volatile bool Temperature::temp_meas_ready = false;
  108. #if ENABLED(PIDTEMP)
  109. float Temperature::temp_iState[HOTENDS] = { 0 },
  110. Temperature::temp_dState[HOTENDS] = { 0 },
  111. Temperature::pTerm[HOTENDS],
  112. Temperature::iTerm[HOTENDS],
  113. Temperature::dTerm[HOTENDS];
  114. #if ENABLED(PID_EXTRUSION_SCALING)
  115. float Temperature::cTerm[HOTENDS];
  116. long Temperature::last_e_position;
  117. long Temperature::lpq[LPQ_MAX_LEN];
  118. int Temperature::lpq_ptr = 0;
  119. #endif
  120. float Temperature::pid_error[HOTENDS];
  121. bool Temperature::pid_reset[HOTENDS];
  122. #endif
  123. #if ENABLED(PIDTEMPBED)
  124. float Temperature::temp_iState_bed = { 0 },
  125. Temperature::temp_dState_bed = { 0 },
  126. Temperature::pTerm_bed,
  127. Temperature::iTerm_bed,
  128. Temperature::dTerm_bed,
  129. Temperature::pid_error_bed;
  130. #else
  131. millis_t Temperature::next_bed_check_ms;
  132. #endif
  133. unsigned long Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 };
  134. unsigned long Temperature::raw_temp_bed_value = 0;
  135. // Init min and max temp with extreme values to prevent false errors during startup
  136. 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, HEATER_4_RAW_LO_TEMP),
  137. 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, HEATER_4_RAW_HI_TEMP),
  138. Temperature::minttemp[HOTENDS] = { 0 },
  139. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  140. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  141. int Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  142. #endif
  143. #ifdef MILLISECONDS_PREHEAT_TIME
  144. unsigned long Temperature::preheat_end_time[HOTENDS] = { 0 };
  145. #endif
  146. #ifdef BED_MINTEMP
  147. int Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  148. #endif
  149. #ifdef BED_MAXTEMP
  150. int Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  151. #endif
  152. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  153. int Temperature::meas_shift_index; // Index of a delayed sample in buffer
  154. #endif
  155. #if HAS_AUTO_FAN
  156. millis_t Temperature::next_auto_fan_check_ms = 0;
  157. #endif
  158. uint8_t Temperature::soft_pwm[HOTENDS];
  159. #if ENABLED(FAN_SOFT_PWM)
  160. uint8_t Temperature::soft_pwm_fan[FAN_COUNT];
  161. #endif
  162. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  163. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  164. #endif
  165. #if HAS_PID_HEATING
  166. void Temperature::PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
  167. float input = 0.0;
  168. int cycles = 0;
  169. bool heating = true;
  170. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  171. long t_high = 0, t_low = 0;
  172. long bias, d;
  173. float Ku, Tu;
  174. float workKp = 0, workKi = 0, workKd = 0;
  175. float max = 0, min = 10000;
  176. #if HAS_AUTO_FAN
  177. next_auto_fan_check_ms = temp_ms + 2500UL;
  178. #endif
  179. if (hotend >=
  180. #if ENABLED(PIDTEMP)
  181. HOTENDS
  182. #else
  183. 0
  184. #endif
  185. || hotend <
  186. #if ENABLED(PIDTEMPBED)
  187. -1
  188. #else
  189. 0
  190. #endif
  191. ) {
  192. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  193. return;
  194. }
  195. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  196. disable_all_heaters(); // switch off all heaters.
  197. #if HAS_PID_FOR_BOTH
  198. if (hotend < 0)
  199. soft_pwm_bed = bias = d = (MAX_BED_POWER) >> 1;
  200. else
  201. soft_pwm[hotend] = bias = d = (PID_MAX) >> 1;
  202. #elif ENABLED(PIDTEMP)
  203. soft_pwm[hotend] = bias = d = (PID_MAX) >> 1;
  204. #else
  205. soft_pwm_bed = bias = d = (MAX_BED_POWER) >> 1;
  206. #endif
  207. wait_for_heatup = true;
  208. // PID Tuning loop
  209. while (wait_for_heatup) {
  210. millis_t ms = millis();
  211. if (temp_meas_ready) { // temp sample ready
  212. updateTemperaturesFromRawValues();
  213. input =
  214. #if HAS_PID_FOR_BOTH
  215. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  216. #elif ENABLED(PIDTEMP)
  217. current_temperature[hotend]
  218. #else
  219. current_temperature_bed
  220. #endif
  221. ;
  222. NOLESS(max, input);
  223. NOMORE(min, input);
  224. #if HAS_AUTO_FAN
  225. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  226. checkExtruderAutoFans();
  227. next_auto_fan_check_ms = ms + 2500UL;
  228. }
  229. #endif
  230. if (heating && input > temp) {
  231. if (ELAPSED(ms, t2 + 5000UL)) {
  232. heating = false;
  233. #if HAS_PID_FOR_BOTH
  234. if (hotend < 0)
  235. soft_pwm_bed = (bias - d) >> 1;
  236. else
  237. soft_pwm[hotend] = (bias - d) >> 1;
  238. #elif ENABLED(PIDTEMP)
  239. soft_pwm[hotend] = (bias - d) >> 1;
  240. #elif ENABLED(PIDTEMPBED)
  241. soft_pwm_bed = (bias - d) >> 1;
  242. #endif
  243. t1 = ms;
  244. t_high = t1 - t2;
  245. max = temp;
  246. }
  247. }
  248. if (!heating && input < temp) {
  249. if (ELAPSED(ms, t1 + 5000UL)) {
  250. heating = true;
  251. t2 = ms;
  252. t_low = t2 - t1;
  253. if (cycles > 0) {
  254. long max_pow =
  255. #if HAS_PID_FOR_BOTH
  256. hotend < 0 ? MAX_BED_POWER : PID_MAX
  257. #elif ENABLED(PIDTEMP)
  258. PID_MAX
  259. #else
  260. MAX_BED_POWER
  261. #endif
  262. ;
  263. bias += (d * (t_high - t_low)) / (t_low + t_high);
  264. bias = constrain(bias, 20, max_pow - 20);
  265. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  266. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  267. SERIAL_PROTOCOLPAIR(MSG_D, d);
  268. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  269. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  270. if (cycles > 2) {
  271. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  272. Tu = ((float)(t_low + t_high) * 0.001);
  273. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  274. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  275. workKp = 0.6 * Ku;
  276. workKi = 2 * workKp / Tu;
  277. workKd = workKp * Tu * 0.125;
  278. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  279. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  280. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  281. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  282. /**
  283. workKp = 0.33*Ku;
  284. workKi = workKp/Tu;
  285. workKd = workKp*Tu/3;
  286. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  287. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  288. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  289. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  290. workKp = 0.2*Ku;
  291. workKi = 2*workKp/Tu;
  292. workKd = workKp*Tu/3;
  293. SERIAL_PROTOCOLLNPGM(" No overshoot");
  294. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  295. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  296. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  297. */
  298. }
  299. }
  300. #if HAS_PID_FOR_BOTH
  301. if (hotend < 0)
  302. soft_pwm_bed = (bias + d) >> 1;
  303. else
  304. soft_pwm[hotend] = (bias + d) >> 1;
  305. #elif ENABLED(PIDTEMP)
  306. soft_pwm[hotend] = (bias + d) >> 1;
  307. #else
  308. soft_pwm_bed = (bias + d) >> 1;
  309. #endif
  310. cycles++;
  311. min = temp;
  312. }
  313. }
  314. }
  315. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  316. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  317. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  318. return;
  319. }
  320. // Every 2 seconds...
  321. if (ELAPSED(ms, temp_ms + 2000UL)) {
  322. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  323. print_heaterstates();
  324. SERIAL_EOL;
  325. #endif
  326. temp_ms = ms;
  327. } // every 2 seconds
  328. // Over 2 minutes?
  329. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  330. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  331. return;
  332. }
  333. if (cycles > ncycles) {
  334. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  335. #if HAS_PID_FOR_BOTH
  336. const char* estring = hotend < 0 ? "bed" : "";
  337. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL;
  338. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL;
  339. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL;
  340. #elif ENABLED(PIDTEMP)
  341. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL;
  342. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL;
  343. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL;
  344. #else
  345. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL;
  346. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL;
  347. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL;
  348. #endif
  349. #define _SET_BED_PID() do { \
  350. bedKp = workKp; \
  351. bedKi = scalePID_i(workKi); \
  352. bedKd = scalePID_d(workKd); \
  353. updatePID(); } while(0)
  354. #define _SET_EXTRUDER_PID() do { \
  355. PID_PARAM(Kp, hotend) = workKp; \
  356. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  357. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  358. updatePID(); } while(0)
  359. // Use the result? (As with "M303 U1")
  360. if (set_result) {
  361. #if HAS_PID_FOR_BOTH
  362. if (hotend < 0)
  363. _SET_BED_PID();
  364. else
  365. _SET_EXTRUDER_PID();
  366. #elif ENABLED(PIDTEMP)
  367. _SET_EXTRUDER_PID();
  368. #else
  369. _SET_BED_PID();
  370. #endif
  371. }
  372. return;
  373. }
  374. lcd_update();
  375. }
  376. if (!wait_for_heatup) disable_all_heaters();
  377. }
  378. #endif // HAS_PID_HEATING
  379. /**
  380. * Class and Instance Methods
  381. */
  382. Temperature::Temperature() { }
  383. void Temperature::updatePID() {
  384. #if ENABLED(PIDTEMP)
  385. #if ENABLED(PID_EXTRUSION_SCALING)
  386. last_e_position = 0;
  387. #endif
  388. #endif
  389. }
  390. int Temperature::getHeaterPower(int heater) {
  391. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  392. }
  393. #if HAS_AUTO_FAN
  394. void Temperature::checkExtruderAutoFans() {
  395. constexpr int8_t fanPin[] = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
  396. constexpr int fanBit[] = {
  397. 0,
  398. AUTO_1_IS_0 ? 0 : 1,
  399. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  400. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  401. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4
  402. };
  403. uint8_t fanState = 0;
  404. HOTEND_LOOP() {
  405. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  406. SBI(fanState, fanBit[e]);
  407. }
  408. uint8_t fanDone = 0;
  409. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  410. int8_t pin = fanPin[f];
  411. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  412. uint8_t newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  413. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  414. digitalWrite(pin, newFanSpeed);
  415. analogWrite(pin, newFanSpeed);
  416. SBI(fanDone, fanBit[f]);
  417. }
  418. }
  419. }
  420. #endif // HAS_AUTO_FAN
  421. //
  422. // Temperature Error Handlers
  423. //
  424. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  425. static bool killed = false;
  426. if (IsRunning()) {
  427. SERIAL_ERROR_START;
  428. serialprintPGM(serial_msg);
  429. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  430. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  431. }
  432. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  433. if (!killed) {
  434. Running = false;
  435. killed = true;
  436. kill(lcd_msg);
  437. }
  438. else
  439. disable_all_heaters(); // paranoia
  440. #endif
  441. }
  442. void Temperature::max_temp_error(int8_t e) {
  443. #if HAS_TEMP_BED
  444. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  445. #else
  446. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  447. #if HOTENDS == 1
  448. UNUSED(e);
  449. #endif
  450. #endif
  451. }
  452. void Temperature::min_temp_error(int8_t e) {
  453. #if HAS_TEMP_BED
  454. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  455. #else
  456. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  457. #if HOTENDS == 1
  458. UNUSED(e);
  459. #endif
  460. #endif
  461. }
  462. float Temperature::get_pid_output(int e) {
  463. #if HOTENDS == 1
  464. UNUSED(e);
  465. #define _HOTEND_TEST true
  466. #else
  467. #define _HOTEND_TEST e == active_extruder
  468. #endif
  469. float pid_output;
  470. #if ENABLED(PIDTEMP)
  471. #if DISABLED(PID_OPENLOOP)
  472. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  473. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  474. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  475. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  476. pid_output = BANG_MAX;
  477. pid_reset[HOTEND_INDEX] = true;
  478. }
  479. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0) {
  480. pid_output = 0;
  481. pid_reset[HOTEND_INDEX] = true;
  482. }
  483. else {
  484. if (pid_reset[HOTEND_INDEX]) {
  485. temp_iState[HOTEND_INDEX] = 0.0;
  486. pid_reset[HOTEND_INDEX] = false;
  487. }
  488. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  489. temp_iState[HOTEND_INDEX] += pid_error[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_EXTRUSION_SCALING)
  493. cTerm[HOTEND_INDEX] = 0;
  494. if (_HOTEND_TEST) {
  495. long e_position = stepper.position(E_AXIS);
  496. if (e_position > last_e_position) {
  497. lpq[lpq_ptr] = e_position - last_e_position;
  498. last_e_position = 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.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  505. pid_output += cTerm[HOTEND_INDEX];
  506. }
  507. #endif // PID_EXTRUSION_SCALING
  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_EXTRUSION_SCALING)
  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. iTerm_bed = bedKi * temp_iState_bed;
  546. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  547. temp_dState_bed = current_temperature_bed;
  548. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  549. if (pid_output > MAX_BED_POWER) {
  550. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  551. pid_output = MAX_BED_POWER;
  552. }
  553. else if (pid_output < 0) {
  554. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  555. pid_output = 0;
  556. }
  557. #else
  558. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  559. #endif // PID_OPENLOOP
  560. #if ENABLED(PID_BED_DEBUG)
  561. SERIAL_ECHO_START;
  562. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  563. SERIAL_ECHOPGM(": Input ");
  564. SERIAL_ECHO(current_temperature_bed);
  565. SERIAL_ECHOPGM(" Output ");
  566. SERIAL_ECHO(pid_output);
  567. SERIAL_ECHOPGM(" pTerm ");
  568. SERIAL_ECHO(pTerm_bed);
  569. SERIAL_ECHOPGM(" iTerm ");
  570. SERIAL_ECHO(iTerm_bed);
  571. SERIAL_ECHOPGM(" dTerm ");
  572. SERIAL_ECHOLN(dTerm_bed);
  573. #endif //PID_BED_DEBUG
  574. return pid_output;
  575. }
  576. #endif //PIDTEMPBED
  577. /**
  578. * Manage heating activities for extruder hot-ends and a heated bed
  579. * - Acquire updated temperature readings
  580. * - Also resets the watchdog timer
  581. * - Invoke thermal runaway protection
  582. * - Manage extruder auto-fan
  583. * - Apply filament width to the extrusion rate (may move)
  584. * - Update the heated bed PID output value
  585. */
  586. /**
  587. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  588. * compile error.
  589. * 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);
  590. *
  591. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  592. *
  593. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  594. */
  595. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  596. void Temperature::manage_heater() {
  597. if (!temp_meas_ready) return;
  598. updateTemperaturesFromRawValues(); // also resets the watchdog
  599. #if ENABLED(HEATER_0_USES_MAX6675)
  600. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1)) max_temp_error(0);
  601. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + 0.01)) min_temp_error(0);
  602. #endif
  603. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  604. millis_t ms = millis();
  605. #endif
  606. // Loop through all hotends
  607. HOTEND_LOOP() {
  608. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  609. 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);
  610. #endif
  611. float pid_output = get_pid_output(e);
  612. // Check if temperature is within the correct range
  613. soft_pwm[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  614. // Check if the temperature is failing to increase
  615. #if WATCH_HOTENDS
  616. // Is it time to check this extruder's heater?
  617. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  618. // Has it failed to increase enough?
  619. if (degHotend(e) < watch_target_temp[e]) {
  620. // Stop!
  621. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  622. }
  623. else {
  624. // Start again if the target is still far off
  625. start_watching_heater(e);
  626. }
  627. }
  628. #endif // THERMAL_PROTECTION_HOTENDS
  629. // Check if the temperature is failing to increase
  630. #if WATCH_THE_BED
  631. // Is it time to check the bed?
  632. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) {
  633. // Has it failed to increase enough?
  634. if (degBed() < watch_target_bed_temp) {
  635. // Stop!
  636. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  637. }
  638. else {
  639. // Start again if the target is still far off
  640. start_watching_bed();
  641. }
  642. }
  643. #endif // THERMAL_PROTECTION_HOTENDS
  644. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  645. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  646. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  647. }
  648. #endif
  649. } // HOTEND_LOOP
  650. #if HAS_AUTO_FAN
  651. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  652. checkExtruderAutoFans();
  653. next_auto_fan_check_ms = ms + 2500UL;
  654. }
  655. #endif
  656. // Control the extruder rate based on the width sensor
  657. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  658. if (filament_sensor) {
  659. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  660. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  661. // Get the delayed info and add 100 to reconstitute to a percent of
  662. // the nominal filament diameter then square it to get an area
  663. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  664. float vm = pow((measurement_delay[meas_shift_index] + 100.0) * 0.01, 2);
  665. NOLESS(vm, 0.01);
  666. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  667. }
  668. #endif //FILAMENT_WIDTH_SENSOR
  669. #if DISABLED(PIDTEMPBED)
  670. if (PENDING(ms, next_bed_check_ms)) return;
  671. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  672. #endif
  673. #if TEMP_SENSOR_BED != 0
  674. #if HAS_THERMALLY_PROTECTED_BED
  675. 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);
  676. #endif
  677. #if ENABLED(PIDTEMPBED)
  678. float pid_output = get_pid_output_bed();
  679. soft_pwm_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)pid_output >> 1 : 0;
  680. #elif ENABLED(BED_LIMIT_SWITCHING)
  681. // Check if temperature is within the correct band
  682. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  683. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  684. soft_pwm_bed = 0;
  685. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  686. soft_pwm_bed = MAX_BED_POWER >> 1;
  687. }
  688. else {
  689. soft_pwm_bed = 0;
  690. WRITE_HEATER_BED(LOW);
  691. }
  692. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  693. // Check if temperature is within the correct range
  694. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  695. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  696. }
  697. else {
  698. soft_pwm_bed = 0;
  699. WRITE_HEATER_BED(LOW);
  700. }
  701. #endif
  702. #endif //TEMP_SENSOR_BED != 0
  703. }
  704. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  705. // Derived from RepRap FiveD extruder::getTemperature()
  706. // For hot end temperature measurement.
  707. float Temperature::analog2temp(int raw, uint8_t e) {
  708. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  709. if (e > HOTENDS)
  710. #else
  711. if (e >= HOTENDS)
  712. #endif
  713. {
  714. SERIAL_ERROR_START;
  715. SERIAL_ERROR((int)e);
  716. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  717. kill(PSTR(MSG_KILLED));
  718. return 0.0;
  719. }
  720. #if ENABLED(HEATER_0_USES_MAX6675)
  721. if (e == 0) return 0.25 * raw;
  722. #endif
  723. if (heater_ttbl_map[e] != NULL) {
  724. float celsius = 0;
  725. uint8_t i;
  726. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  727. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  728. if (PGM_RD_W((*tt)[i][0]) > raw) {
  729. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  730. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  731. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  732. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  733. break;
  734. }
  735. }
  736. // Overflow: Set to last value in the table
  737. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  738. return celsius;
  739. }
  740. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  741. }
  742. // Derived from RepRap FiveD extruder::getTemperature()
  743. // For bed temperature measurement.
  744. float Temperature::analog2tempBed(int raw) {
  745. #if ENABLED(BED_USES_THERMISTOR)
  746. float celsius = 0;
  747. byte i;
  748. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  749. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  750. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  751. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  752. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  753. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  754. break;
  755. }
  756. }
  757. // Overflow: Set to last value in the table
  758. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  759. return celsius;
  760. #elif defined(BED_USES_AD595)
  761. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  762. #else
  763. UNUSED(raw);
  764. return 0;
  765. #endif
  766. }
  767. /**
  768. * Get the raw values into the actual temperatures.
  769. * The raw values are created in interrupt context,
  770. * and this function is called from normal context
  771. * as it would block the stepper routine.
  772. */
  773. void Temperature::updateTemperaturesFromRawValues() {
  774. #if ENABLED(HEATER_0_USES_MAX6675)
  775. current_temperature_raw[0] = read_max6675();
  776. #endif
  777. HOTEND_LOOP()
  778. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  779. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  780. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  781. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  782. #endif
  783. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  784. filament_width_meas = analog2widthFil();
  785. #endif
  786. #if ENABLED(USE_WATCHDOG)
  787. // Reset the watchdog after we know we have a temperature measurement.
  788. watchdog_reset();
  789. #endif
  790. CRITICAL_SECTION_START;
  791. temp_meas_ready = false;
  792. CRITICAL_SECTION_END;
  793. }
  794. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  795. // Convert raw Filament Width to millimeters
  796. float Temperature::analog2widthFil() {
  797. return current_raw_filwidth / 16383.0 * 5.0;
  798. //return current_raw_filwidth;
  799. }
  800. // Convert raw Filament Width to a ratio
  801. int Temperature::widthFil_to_size_ratio() {
  802. float temp = filament_width_meas;
  803. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  804. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  805. return filament_width_nominal / temp * 100;
  806. }
  807. #endif
  808. #if ENABLED(HEATER_0_USES_MAX6675)
  809. #ifndef MAX6675_SCK_PIN
  810. #define MAX6675_SCK_PIN SCK_PIN
  811. #endif
  812. #ifndef MAX6675_DO_PIN
  813. #define MAX6675_DO_PIN MISO_PIN
  814. #endif
  815. Spi<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  816. #endif
  817. /**
  818. * Initialize the temperature manager
  819. * The manager is implemented by periodic calls to manage_heater()
  820. */
  821. void Temperature::init() {
  822. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  823. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  824. MCUCR = _BV(JTD);
  825. MCUCR = _BV(JTD);
  826. #endif
  827. // Finish init of mult hotend arrays
  828. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  829. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  830. last_e_position = 0;
  831. #endif
  832. #if HAS_HEATER_0
  833. SET_OUTPUT(HEATER_0_PIN);
  834. #endif
  835. #if HAS_HEATER_1
  836. SET_OUTPUT(HEATER_1_PIN);
  837. #endif
  838. #if HAS_HEATER_2
  839. SET_OUTPUT(HEATER_2_PIN);
  840. #endif
  841. #if HAS_HEATER_3
  842. SET_OUTPUT(HEATER_3_PIN);
  843. #endif
  844. #if HAS_HEATER_4
  845. SET_OUTPUT(HEATER_3_PIN);
  846. #endif
  847. #if HAS_HEATER_BED
  848. SET_OUTPUT(HEATER_BED_PIN);
  849. #endif
  850. #if HAS_FAN0
  851. SET_OUTPUT(FAN_PIN);
  852. #if ENABLED(FAST_PWM_FAN)
  853. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  854. #endif
  855. #endif
  856. #if HAS_FAN1
  857. SET_OUTPUT(FAN1_PIN);
  858. #if ENABLED(FAST_PWM_FAN)
  859. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  860. #endif
  861. #endif
  862. #if HAS_FAN2
  863. SET_OUTPUT(FAN2_PIN);
  864. #if ENABLED(FAST_PWM_FAN)
  865. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  866. #endif
  867. #endif
  868. #if ENABLED(HEATER_0_USES_MAX6675)
  869. OUT_WRITE(SCK_PIN, LOW);
  870. OUT_WRITE(MOSI_PIN, HIGH);
  871. SET_INPUT_PULLUP(MISO_PIN);
  872. max6675_spi.init();
  873. OUT_WRITE(SS_PIN, HIGH);
  874. OUT_WRITE(MAX6675_SS, HIGH);
  875. #endif // HEATER_0_USES_MAX6675
  876. #ifdef DIDR2
  877. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  878. #else
  879. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  880. #endif
  881. // Set analog inputs
  882. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  883. DIDR0 = 0;
  884. #ifdef DIDR2
  885. DIDR2 = 0;
  886. #endif
  887. #if HAS_TEMP_0
  888. ANALOG_SELECT(TEMP_0_PIN);
  889. #endif
  890. #if HAS_TEMP_1
  891. ANALOG_SELECT(TEMP_1_PIN);
  892. #endif
  893. #if HAS_TEMP_2
  894. ANALOG_SELECT(TEMP_2_PIN);
  895. #endif
  896. #if HAS_TEMP_3
  897. ANALOG_SELECT(TEMP_3_PIN);
  898. #endif
  899. #if HAS_TEMP_4
  900. ANALOG_SELECT(TEMP_4_PIN);
  901. #endif
  902. #if HAS_TEMP_BED
  903. ANALOG_SELECT(TEMP_BED_PIN);
  904. #endif
  905. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  906. ANALOG_SELECT(FILWIDTH_PIN);
  907. #endif
  908. #if HAS_AUTO_FAN_0
  909. #if E0_AUTO_FAN_PIN == FAN1_PIN
  910. SET_OUTPUT(E0_AUTO_FAN_PIN);
  911. #if ENABLED(FAST_PWM_FAN)
  912. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  913. #endif
  914. #else
  915. SET_OUTPUT(E0_AUTO_FAN_PIN);
  916. #endif
  917. #endif
  918. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  919. #if E1_AUTO_FAN_PIN == FAN1_PIN
  920. SET_OUTPUT(E1_AUTO_FAN_PIN);
  921. #if ENABLED(FAST_PWM_FAN)
  922. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  923. #endif
  924. #else
  925. SET_OUTPUT(E1_AUTO_FAN_PIN);
  926. #endif
  927. #endif
  928. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  929. #if E2_AUTO_FAN_PIN == FAN1_PIN
  930. SET_OUTPUT(E2_AUTO_FAN_PIN);
  931. #if ENABLED(FAST_PWM_FAN)
  932. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  933. #endif
  934. #else
  935. SET_OUTPUT(E2_AUTO_FAN_PIN);
  936. #endif
  937. #endif
  938. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  939. #if E3_AUTO_FAN_PIN == FAN1_PIN
  940. SET_OUTPUT(E3_AUTO_FAN_PIN);
  941. #if ENABLED(FAST_PWM_FAN)
  942. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  943. #endif
  944. #else
  945. SET_OUTPUT(E3_AUTO_FAN_PIN);
  946. #endif
  947. #endif
  948. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  949. #if E4_AUTO_FAN_PIN == FAN1_PIN
  950. SET_OUTPUT(E4_AUTO_FAN_PIN);
  951. #if ENABLED(FAST_PWM_FAN)
  952. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  953. #endif
  954. #else
  955. SET_OUTPUT(E4_AUTO_FAN_PIN);
  956. #endif
  957. #endif
  958. // Use timer0 for temperature measurement
  959. // Interleave temperature interrupt with millies interrupt
  960. OCR0B = 128;
  961. SBI(TIMSK0, OCIE0B);
  962. // Wait for temperature measurement to settle
  963. delay(250);
  964. #define TEMP_MIN_ROUTINE(NR) \
  965. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  966. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  967. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  968. minttemp_raw[NR] += OVERSAMPLENR; \
  969. else \
  970. minttemp_raw[NR] -= OVERSAMPLENR; \
  971. }
  972. #define TEMP_MAX_ROUTINE(NR) \
  973. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  974. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  975. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  976. maxttemp_raw[NR] -= OVERSAMPLENR; \
  977. else \
  978. maxttemp_raw[NR] += OVERSAMPLENR; \
  979. }
  980. #ifdef HEATER_0_MINTEMP
  981. TEMP_MIN_ROUTINE(0);
  982. #endif
  983. #ifdef HEATER_0_MAXTEMP
  984. TEMP_MAX_ROUTINE(0);
  985. #endif
  986. #if HOTENDS > 1
  987. #ifdef HEATER_1_MINTEMP
  988. TEMP_MIN_ROUTINE(1);
  989. #endif
  990. #ifdef HEATER_1_MAXTEMP
  991. TEMP_MAX_ROUTINE(1);
  992. #endif
  993. #if HOTENDS > 2
  994. #ifdef HEATER_2_MINTEMP
  995. TEMP_MIN_ROUTINE(2);
  996. #endif
  997. #ifdef HEATER_2_MAXTEMP
  998. TEMP_MAX_ROUTINE(2);
  999. #endif
  1000. #if HOTENDS > 3
  1001. #ifdef HEATER_3_MINTEMP
  1002. TEMP_MIN_ROUTINE(3);
  1003. #endif
  1004. #ifdef HEATER_3_MAXTEMP
  1005. TEMP_MAX_ROUTINE(3);
  1006. #endif
  1007. #if HOTENDS > 4
  1008. #ifdef HEATER_4_MINTEMP
  1009. TEMP_MIN_ROUTINE(4);
  1010. #endif
  1011. #ifdef HEATER_4_MAXTEMP
  1012. TEMP_MAX_ROUTINE(4);
  1013. #endif
  1014. #endif // HOTENDS > 4
  1015. #endif // HOTENDS > 3
  1016. #endif // HOTENDS > 2
  1017. #endif // HOTENDS > 1
  1018. #ifdef BED_MINTEMP
  1019. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1020. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1021. bed_minttemp_raw += OVERSAMPLENR;
  1022. #else
  1023. bed_minttemp_raw -= OVERSAMPLENR;
  1024. #endif
  1025. }
  1026. #endif //BED_MINTEMP
  1027. #ifdef BED_MAXTEMP
  1028. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1029. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1030. bed_maxttemp_raw -= OVERSAMPLENR;
  1031. #else
  1032. bed_maxttemp_raw += OVERSAMPLENR;
  1033. #endif
  1034. }
  1035. #endif //BED_MAXTEMP
  1036. }
  1037. #if WATCH_HOTENDS
  1038. /**
  1039. * Start Heating Sanity Check for hotends that are below
  1040. * their target temperature by a configurable margin.
  1041. * This is called when the temperature is set. (M104, M109)
  1042. */
  1043. void Temperature::start_watching_heater(uint8_t e) {
  1044. #if HOTENDS == 1
  1045. UNUSED(e);
  1046. #endif
  1047. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1048. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1049. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1050. }
  1051. else
  1052. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1053. }
  1054. #endif
  1055. #if WATCH_THE_BED
  1056. /**
  1057. * Start Heating Sanity Check for hotends that are below
  1058. * their target temperature by a configurable margin.
  1059. * This is called when the temperature is set. (M140, M190)
  1060. */
  1061. void Temperature::start_watching_bed() {
  1062. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1063. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1064. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1065. }
  1066. else
  1067. watch_bed_next_ms = 0;
  1068. }
  1069. #endif
  1070. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1071. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1072. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1073. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1074. #endif
  1075. #if HAS_THERMALLY_PROTECTED_BED
  1076. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1077. millis_t Temperature::thermal_runaway_bed_timer;
  1078. #endif
  1079. 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) {
  1080. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1081. /**
  1082. SERIAL_ECHO_START;
  1083. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1084. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1085. SERIAL_ECHOPAIR(" ; State:", *state);
  1086. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1087. SERIAL_ECHOPAIR(" ; Temperature:", temperature);
  1088. SERIAL_ECHOPAIR(" ; Target Temp:", target_temperature);
  1089. SERIAL_EOL;
  1090. */
  1091. int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1092. // If the target temperature changes, restart
  1093. if (tr_target_temperature[heater_index] != target_temperature) {
  1094. tr_target_temperature[heater_index] = target_temperature;
  1095. *state = target_temperature > 0 ? TRFirstHeating : TRInactive;
  1096. }
  1097. switch (*state) {
  1098. // Inactive state waits for a target temperature to be set
  1099. case TRInactive: break;
  1100. // When first heating, wait for the temperature to be reached then go to Stable state
  1101. case TRFirstHeating:
  1102. if (temperature < tr_target_temperature[heater_index]) break;
  1103. *state = TRStable;
  1104. // While the temperature is stable watch for a bad temperature
  1105. case TRStable:
  1106. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1107. *timer = millis() + period_seconds * 1000UL;
  1108. break;
  1109. }
  1110. else if (PENDING(millis(), *timer)) break;
  1111. *state = TRRunaway;
  1112. case TRRunaway:
  1113. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1114. }
  1115. }
  1116. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1117. void Temperature::disable_all_heaters() {
  1118. HOTEND_LOOP() setTargetHotend(0, e);
  1119. setTargetBed(0);
  1120. // If all heaters go down then for sure our print job has stopped
  1121. print_job_timer.stop();
  1122. #define DISABLE_HEATER(NR) { \
  1123. setTargetHotend(0, NR); \
  1124. soft_pwm[NR] = 0; \
  1125. WRITE_HEATER_ ##NR (LOW); \
  1126. }
  1127. #if HAS_TEMP_HOTEND
  1128. DISABLE_HEATER(0);
  1129. #if HOTENDS > 1
  1130. DISABLE_HEATER(1);
  1131. #if HOTENDS > 2
  1132. DISABLE_HEATER(2);
  1133. #if HOTENDS > 3
  1134. DISABLE_HEATER(3);
  1135. #if HOTENDS > 4
  1136. DISABLE_HEATER(4);
  1137. #endif // HOTENDS > 4
  1138. #endif // HOTENDS > 3
  1139. #endif // HOTENDS > 2
  1140. #endif // HOTENDS > 1
  1141. #endif
  1142. #if HAS_TEMP_BED
  1143. target_temperature_bed = 0;
  1144. soft_pwm_bed = 0;
  1145. #if HAS_HEATER_BED
  1146. WRITE_HEATER_BED(LOW);
  1147. #endif
  1148. #endif
  1149. }
  1150. #if ENABLED(HEATER_0_USES_MAX6675)
  1151. #define MAX6675_HEAT_INTERVAL 250u
  1152. #if ENABLED(MAX6675_IS_MAX31855)
  1153. uint32_t max6675_temp = 2000;
  1154. #define MAX6675_ERROR_MASK 7
  1155. #define MAX6675_DISCARD_BITS 18
  1156. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1157. #else
  1158. uint16_t max6675_temp = 2000;
  1159. #define MAX6675_ERROR_MASK 4
  1160. #define MAX6675_DISCARD_BITS 3
  1161. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1162. #endif
  1163. int Temperature::read_max6675() {
  1164. static millis_t next_max6675_ms = 0;
  1165. millis_t ms = millis();
  1166. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1167. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1168. CBI(
  1169. #ifdef PRR
  1170. PRR
  1171. #elif defined(PRR0)
  1172. PRR0
  1173. #endif
  1174. , PRSPI);
  1175. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1176. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1177. // ensure 100ns delay - a bit extra is fine
  1178. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1179. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1180. // Read a big-endian temperature value
  1181. max6675_temp = 0;
  1182. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1183. max6675_temp |= max6675_spi.receive();
  1184. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1185. }
  1186. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1187. if (max6675_temp & MAX6675_ERROR_MASK) {
  1188. SERIAL_ERROR_START;
  1189. SERIAL_ERRORPGM("Temp measurement error! ");
  1190. #if MAX6675_ERROR_MASK == 7
  1191. SERIAL_ERRORPGM("MAX31855 ");
  1192. if (max6675_temp & 1)
  1193. SERIAL_ERRORLNPGM("Open Circuit");
  1194. else if (max6675_temp & 2)
  1195. SERIAL_ERRORLNPGM("Short to GND");
  1196. else if (max6675_temp & 4)
  1197. SERIAL_ERRORLNPGM("Short to VCC");
  1198. #else
  1199. SERIAL_ERRORLNPGM("MAX6675");
  1200. #endif
  1201. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1202. }
  1203. else
  1204. max6675_temp >>= MAX6675_DISCARD_BITS;
  1205. #if ENABLED(MAX6675_IS_MAX31855)
  1206. // Support negative temperature
  1207. if (max6675_temp & 0x00002000) max6675_temp |= 0xffffc000;
  1208. #endif
  1209. return (int)max6675_temp;
  1210. }
  1211. #endif //HEATER_0_USES_MAX6675
  1212. /**
  1213. * Get raw temperatures
  1214. */
  1215. void Temperature::set_current_temp_raw() {
  1216. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1217. current_temperature_raw[0] = raw_temp_value[0];
  1218. #endif
  1219. #if HAS_TEMP_1
  1220. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1221. redundant_temperature_raw = raw_temp_value[1];
  1222. #else
  1223. current_temperature_raw[1] = raw_temp_value[1];
  1224. #endif
  1225. #if HAS_TEMP_2
  1226. current_temperature_raw[2] = raw_temp_value[2];
  1227. #if HAS_TEMP_3
  1228. current_temperature_raw[3] = raw_temp_value[3];
  1229. #if HAS_TEMP_4
  1230. current_temperature_raw[4] = raw_temp_value[4];
  1231. #endif
  1232. #endif
  1233. #endif
  1234. #endif
  1235. current_temperature_bed_raw = raw_temp_bed_value;
  1236. temp_meas_ready = true;
  1237. }
  1238. #if ENABLED(PINS_DEBUGGING)
  1239. /**
  1240. * monitors endstops & Z probe for changes
  1241. *
  1242. * If a change is detected then the LED is toggled and
  1243. * a message is sent out the serial port
  1244. *
  1245. * Yes, we could miss a rapid back & forth change but
  1246. * that won't matter because this is all manual.
  1247. *
  1248. */
  1249. void endstop_monitor() {
  1250. static uint16_t old_endstop_bits_local = 0;
  1251. static uint8_t local_LED_status = 0;
  1252. uint16_t current_endstop_bits_local = 0;
  1253. #if HAS_X_MIN
  1254. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  1255. #endif
  1256. #if HAS_X_MAX
  1257. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  1258. #endif
  1259. #if HAS_Y_MIN
  1260. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  1261. #endif
  1262. #if HAS_Y_MAX
  1263. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  1264. #endif
  1265. #if HAS_Z_MIN
  1266. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  1267. #endif
  1268. #if HAS_Z_MAX
  1269. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  1270. #endif
  1271. #if HAS_Z_MIN_PROBE_PIN
  1272. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  1273. #endif
  1274. #if HAS_Z2_MIN
  1275. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  1276. #endif
  1277. #if HAS_Z2_MAX
  1278. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  1279. #endif
  1280. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  1281. if (endstop_change) {
  1282. #if HAS_X_MIN
  1283. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
  1284. #endif
  1285. #if HAS_X_MAX
  1286. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
  1287. #endif
  1288. #if HAS_Y_MIN
  1289. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
  1290. #endif
  1291. #if HAS_Y_MAX
  1292. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
  1293. #endif
  1294. #if HAS_Z_MIN
  1295. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
  1296. #endif
  1297. #if HAS_Z_MAX
  1298. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
  1299. #endif
  1300. #if HAS_Z_MIN_PROBE_PIN
  1301. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
  1302. #endif
  1303. #if HAS_Z2_MIN
  1304. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
  1305. #endif
  1306. #if HAS_Z2_MAX
  1307. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
  1308. #endif
  1309. SERIAL_PROTOCOLPGM("\n\n");
  1310. analogWrite(LED_PIN, local_LED_status);
  1311. local_LED_status ^= 255;
  1312. old_endstop_bits_local = current_endstop_bits_local;
  1313. }
  1314. }
  1315. #endif // PINS_DEBUGGING
  1316. /**
  1317. * Timer 0 is shared with millies so don't change the prescaler.
  1318. *
  1319. * This ISR uses the compare method so it runs at the base
  1320. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1321. * in OCR0B above (128 or halfway between OVFs).
  1322. *
  1323. * - Manage PWM to all the heaters and fan
  1324. * - Prepare or Measure one of the raw ADC sensor values
  1325. * - Check new temperature values for MIN/MAX errors (kill on error)
  1326. * - Step the babysteps value for each axis towards 0
  1327. * - For PINS_DEBUGGING, monitor and report endstop pins
  1328. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1329. */
  1330. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1331. volatile bool Temperature::in_temp_isr = false;
  1332. void Temperature::isr() {
  1333. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1334. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1335. if (in_temp_isr) return;
  1336. in_temp_isr = true;
  1337. // Allow UART and stepper ISRs
  1338. CBI(TIMSK0, OCIE0B); //Disable Temperature ISR
  1339. sei();
  1340. static int8_t temp_count = -1;
  1341. static ADCSensorState adc_sensor_state = StartupDelay;
  1342. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1343. // avoid multiple loads of pwm_count
  1344. uint8_t pwm_count_tmp = pwm_count;
  1345. // Static members for each heater
  1346. #if ENABLED(SLOW_PWM_HEATERS)
  1347. static uint8_t slow_pwm_count = 0;
  1348. #define ISR_STATICS(n) \
  1349. static uint8_t soft_pwm_ ## n; \
  1350. static uint8_t state_heater_ ## n = 0; \
  1351. static uint8_t state_timer_heater_ ## n = 0
  1352. #else
  1353. #define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0
  1354. #endif
  1355. // Statics per heater
  1356. ISR_STATICS(0);
  1357. #if HOTENDS > 1
  1358. ISR_STATICS(1);
  1359. #if HOTENDS > 2
  1360. ISR_STATICS(2);
  1361. #if HOTENDS > 3
  1362. ISR_STATICS(3);
  1363. #if HOTENDS > 4
  1364. ISR_STATICS(4);
  1365. #endif // HOTENDS > 4
  1366. #endif // HOTENDS > 3
  1367. #endif // HOTENDS > 2
  1368. #endif // HOTENDS > 1
  1369. #if HAS_HEATER_BED
  1370. ISR_STATICS(BED);
  1371. #endif
  1372. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1373. static unsigned long raw_filwidth_value = 0;
  1374. #endif
  1375. #if DISABLED(SLOW_PWM_HEATERS)
  1376. constexpr uint8_t pwm_mask =
  1377. #if ENABLED(SOFT_PWM_DITHER)
  1378. _BV(SOFT_PWM_SCALE) - 1
  1379. #else
  1380. 0
  1381. #endif
  1382. ;
  1383. /**
  1384. * Standard PWM modulation
  1385. */
  1386. if (pwm_count_tmp >= 127) {
  1387. pwm_count_tmp -= 127;
  1388. soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
  1389. WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
  1390. #if HOTENDS > 1
  1391. soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1];
  1392. WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW);
  1393. #if HOTENDS > 2
  1394. soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2];
  1395. WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW);
  1396. #if HOTENDS > 3
  1397. soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3];
  1398. WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW);
  1399. #if HOTENDS > 4
  1400. soft_pwm_4 = (soft_pwm_4 & pwm_mask) + soft_pwm[4];
  1401. WRITE_HEATER_4(soft_pwm_4 > pwm_mask ? HIGH : LOW);
  1402. #endif // HOTENDS > 4
  1403. #endif // HOTENDS > 3
  1404. #endif // HOTENDS > 2
  1405. #endif // HOTENDS > 1
  1406. #if HAS_HEATER_BED
  1407. soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed;
  1408. WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW);
  1409. #endif
  1410. #if ENABLED(FAN_SOFT_PWM)
  1411. #if HAS_FAN0
  1412. soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1;
  1413. WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW);
  1414. #endif
  1415. #if HAS_FAN1
  1416. soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1;
  1417. WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW);
  1418. #endif
  1419. #if HAS_FAN2
  1420. soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1;
  1421. WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW);
  1422. #endif
  1423. #endif
  1424. }
  1425. else {
  1426. if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
  1427. #if HOTENDS > 1
  1428. if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
  1429. #if HOTENDS > 2
  1430. if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
  1431. #if HOTENDS > 3
  1432. if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
  1433. #if HOTENDS > 4
  1434. if (soft_pwm_4 <= pwm_count_tmp) WRITE_HEATER_4(0);
  1435. #endif // HOTENDS > 4
  1436. #endif // HOTENDS > 3
  1437. #endif // HOTENDS > 2
  1438. #endif // HOTENDS > 1
  1439. #if HAS_HEATER_BED
  1440. if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
  1441. #endif
  1442. #if ENABLED(FAN_SOFT_PWM)
  1443. #if HAS_FAN0
  1444. if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1445. #endif
  1446. #if HAS_FAN1
  1447. if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1448. #endif
  1449. #if HAS_FAN2
  1450. if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1451. #endif
  1452. #endif
  1453. }
  1454. // SOFT_PWM_SCALE to frequency:
  1455. //
  1456. // 0: 16000000/64/256/128 = 7.6294 Hz
  1457. // 1: / 64 = 15.2588 Hz
  1458. // 2: / 32 = 30.5176 Hz
  1459. // 3: / 16 = 61.0352 Hz
  1460. // 4: / 8 = 122.0703 Hz
  1461. // 5: / 4 = 244.1406 Hz
  1462. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1463. #else // SLOW_PWM_HEATERS
  1464. /**
  1465. * SLOW PWM HEATERS
  1466. *
  1467. * For relay-driven heaters
  1468. */
  1469. #ifndef MIN_STATE_TIME
  1470. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1471. #endif
  1472. // Macros for Slow PWM timer logic
  1473. #define _SLOW_PWM_ROUTINE(NR, src) \
  1474. soft_pwm_ ##NR = src; \
  1475. if (soft_pwm_ ##NR > 0) { \
  1476. if (state_timer_heater_ ##NR == 0) { \
  1477. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1478. state_heater_ ##NR = 1; \
  1479. WRITE_HEATER_ ##NR(1); \
  1480. } \
  1481. } \
  1482. else { \
  1483. if (state_timer_heater_ ##NR == 0) { \
  1484. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1485. state_heater_ ##NR = 0; \
  1486. WRITE_HEATER_ ##NR(0); \
  1487. } \
  1488. }
  1489. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1490. #define PWM_OFF_ROUTINE(NR) \
  1491. if (soft_pwm_ ##NR < slow_pwm_count) { \
  1492. if (state_timer_heater_ ##NR == 0) { \
  1493. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1494. state_heater_ ##NR = 0; \
  1495. WRITE_HEATER_ ##NR (0); \
  1496. } \
  1497. }
  1498. if (slow_pwm_count == 0) {
  1499. SLOW_PWM_ROUTINE(0);
  1500. #if HOTENDS > 1
  1501. SLOW_PWM_ROUTINE(1);
  1502. #if HOTENDS > 2
  1503. SLOW_PWM_ROUTINE(2);
  1504. #if HOTENDS > 3
  1505. SLOW_PWM_ROUTINE(3);
  1506. #if HOTENDS > 4
  1507. SLOW_PWM_ROUTINE(4);
  1508. #endif // HOTENDS > 4
  1509. #endif // HOTENDS > 3
  1510. #endif // HOTENDS > 2
  1511. #endif // HOTENDS > 1
  1512. #if HAS_HEATER_BED
  1513. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1514. #endif
  1515. } // slow_pwm_count == 0
  1516. PWM_OFF_ROUTINE(0);
  1517. #if HOTENDS > 1
  1518. PWM_OFF_ROUTINE(1);
  1519. #if HOTENDS > 2
  1520. PWM_OFF_ROUTINE(2);
  1521. #if HOTENDS > 3
  1522. PWM_OFF_ROUTINE(3);
  1523. #if HOTENDS > 4
  1524. PWM_OFF_ROUTINE(4);
  1525. #endif // HOTENDS > 4
  1526. #endif // HOTENDS > 3
  1527. #endif // HOTENDS > 2
  1528. #endif // HOTENDS > 1
  1529. #if HAS_HEATER_BED
  1530. PWM_OFF_ROUTINE(BED); // BED
  1531. #endif
  1532. #if ENABLED(FAN_SOFT_PWM)
  1533. if (pwm_count_tmp >= 127) {
  1534. pwm_count_tmp = 0;
  1535. #if HAS_FAN0
  1536. soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
  1537. WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
  1538. #endif
  1539. #if HAS_FAN1
  1540. soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1;
  1541. WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW);
  1542. #endif
  1543. #if HAS_FAN2
  1544. soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1;
  1545. WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW);
  1546. #endif
  1547. }
  1548. #if HAS_FAN0
  1549. if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1550. #endif
  1551. #if HAS_FAN1
  1552. if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1553. #endif
  1554. #if HAS_FAN2
  1555. if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1556. #endif
  1557. #endif // FAN_SOFT_PWM
  1558. // SOFT_PWM_SCALE to frequency:
  1559. //
  1560. // 0: 16000000/64/256/128 = 7.6294 Hz
  1561. // 1: / 64 = 15.2588 Hz
  1562. // 2: / 32 = 30.5176 Hz
  1563. // 3: / 16 = 61.0352 Hz
  1564. // 4: / 8 = 122.0703 Hz
  1565. // 5: / 4 = 244.1406 Hz
  1566. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1567. // increment slow_pwm_count only every 64th pwm_count,
  1568. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1569. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1570. slow_pwm_count++;
  1571. slow_pwm_count &= 0x7F;
  1572. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1573. #if HOTENDS > 1
  1574. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1575. #if HOTENDS > 2
  1576. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1577. #if HOTENDS > 3
  1578. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1579. #if HOTENDS > 4
  1580. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1581. #endif // HOTENDS > 4
  1582. #endif // HOTENDS > 3
  1583. #endif // HOTENDS > 2
  1584. #endif // HOTENDS > 1
  1585. #if HAS_HEATER_BED
  1586. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1587. #endif
  1588. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1589. #endif // SLOW_PWM_HEATERS
  1590. //
  1591. // Update lcd buttons 488 times per second
  1592. //
  1593. static bool do_buttons;
  1594. if ((do_buttons ^= true)) lcd_buttons_update();
  1595. /**
  1596. * One sensor is sampled on every other call of the ISR.
  1597. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1598. *
  1599. * On each Prepare pass, ADC is started for a sensor pin.
  1600. * On the next pass, the ADC value is read and accumulated.
  1601. *
  1602. * This gives each ADC 0.9765ms to charge up.
  1603. */
  1604. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1605. #ifdef MUX5
  1606. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1607. #else
  1608. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1609. #endif
  1610. switch (adc_sensor_state) {
  1611. case SensorsReady: {
  1612. // All sensors have been read. Stay in this state for a few
  1613. // ISRs to save on calls to temp update/checking code below.
  1614. constexpr int extra_loops = MIN_ADC_ISR_LOOPS - (int)SensorsReady;
  1615. static uint8_t delay_count = 0;
  1616. if (extra_loops > 0) {
  1617. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1618. if (--delay_count) // While delaying...
  1619. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1620. break;
  1621. }
  1622. else
  1623. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1624. }
  1625. #if HAS_TEMP_0
  1626. case PrepareTemp_0:
  1627. START_ADC(TEMP_0_PIN);
  1628. break;
  1629. case MeasureTemp_0:
  1630. raw_temp_value[0] += ADC;
  1631. break;
  1632. #endif
  1633. #if HAS_TEMP_BED
  1634. case PrepareTemp_BED:
  1635. START_ADC(TEMP_BED_PIN);
  1636. break;
  1637. case MeasureTemp_BED:
  1638. raw_temp_bed_value += ADC;
  1639. break;
  1640. #endif
  1641. #if HAS_TEMP_1
  1642. case PrepareTemp_1:
  1643. START_ADC(TEMP_1_PIN);
  1644. break;
  1645. case MeasureTemp_1:
  1646. raw_temp_value[1] += ADC;
  1647. break;
  1648. #endif
  1649. #if HAS_TEMP_2
  1650. case PrepareTemp_2:
  1651. START_ADC(TEMP_2_PIN);
  1652. break;
  1653. case MeasureTemp_2:
  1654. raw_temp_value[2] += ADC;
  1655. break;
  1656. #endif
  1657. #if HAS_TEMP_3
  1658. case PrepareTemp_3:
  1659. START_ADC(TEMP_3_PIN);
  1660. break;
  1661. case MeasureTemp_3:
  1662. raw_temp_value[3] += ADC;
  1663. break;
  1664. #endif
  1665. #if HAS_TEMP_4
  1666. case PrepareTemp_4:
  1667. START_ADC(TEMP_4_PIN);
  1668. break;
  1669. case MeasureTemp_4:
  1670. raw_temp_value[4] += ADC;
  1671. break;
  1672. #endif
  1673. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1674. case Prepare_FILWIDTH:
  1675. START_ADC(FILWIDTH_PIN);
  1676. break;
  1677. case Measure_FILWIDTH:
  1678. if (ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1679. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1680. raw_filwidth_value += ((unsigned long)ADC << 7); // Add new ADC reading, scaled by 128
  1681. }
  1682. break;
  1683. #endif
  1684. case StartupDelay: break;
  1685. } // switch(adc_sensor_state)
  1686. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1687. temp_count = 0;
  1688. // Update the raw values if they've been read. Else we could be updating them during reading.
  1689. if (!temp_meas_ready) set_current_temp_raw();
  1690. // Filament Sensor - can be read any time since IIR filtering is used
  1691. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1692. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1693. #endif
  1694. ZERO(raw_temp_value);
  1695. raw_temp_bed_value = 0;
  1696. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1697. int constexpr temp_dir[] = {
  1698. #if ENABLED(HEATER_0_USES_MAX6675)
  1699. 0
  1700. #else
  1701. TEMPDIR(0)
  1702. #endif
  1703. #if HOTENDS > 1
  1704. , TEMPDIR(1)
  1705. #if HOTENDS > 2
  1706. , TEMPDIR(2)
  1707. #if HOTENDS > 3
  1708. , TEMPDIR(3)
  1709. #if HOTENDS > 4
  1710. , TEMPDIR(4)
  1711. #endif // HOTENDS > 4
  1712. #endif // HOTENDS > 3
  1713. #endif // HOTENDS > 2
  1714. #endif // HOTENDS > 1
  1715. };
  1716. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1717. const int tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1718. if (rawtemp > maxttemp_raw[e] * tdir && target_temperature[e] > 0.0f) max_temp_error(e);
  1719. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && target_temperature[e] > 0.0f) {
  1720. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1721. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1722. #endif
  1723. min_temp_error(e);
  1724. }
  1725. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1726. else
  1727. consecutive_low_temperature_error[e] = 0;
  1728. #endif
  1729. }
  1730. #if HAS_TEMP_BED
  1731. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1732. #define GEBED <=
  1733. #else
  1734. #define GEBED >=
  1735. #endif
  1736. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && target_temperature_bed > 0.0f) max_temp_error(-1);
  1737. if (bed_minttemp_raw GEBED current_temperature_bed_raw && target_temperature_bed > 0.0f) min_temp_error(-1);
  1738. #endif
  1739. } // temp_count >= OVERSAMPLENR
  1740. // Go to the next state, up to SensorsReady
  1741. adc_sensor_state = (ADCSensorState)((int(adc_sensor_state) + 1) % int(StartupDelay));
  1742. #if ENABLED(BABYSTEPPING)
  1743. LOOP_XYZ(axis) {
  1744. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1745. if (curTodo > 0) {
  1746. stepper.babystep((AxisEnum)axis,/*fwd*/true);
  1747. babystepsTodo[axis]--; //fewer to do next time
  1748. }
  1749. else if (curTodo < 0) {
  1750. stepper.babystep((AxisEnum)axis,/*fwd*/false);
  1751. babystepsTodo[axis]++; //fewer to do next time
  1752. }
  1753. }
  1754. #endif //BABYSTEPPING
  1755. #if ENABLED(PINS_DEBUGGING)
  1756. extern bool endstop_monitor_flag;
  1757. // run the endstop monitor at 15Hz
  1758. static uint8_t endstop_monitor_count = 16; // offset this check from the others
  1759. if (endstop_monitor_flag) {
  1760. endstop_monitor_count += _BV(1); // 15 Hz
  1761. endstop_monitor_count &= 0x7F;
  1762. if (!endstop_monitor_count) endstop_monitor(); // report changes in endstop status
  1763. }
  1764. #endif
  1765. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1766. extern volatile uint8_t e_hit;
  1767. if (e_hit && ENDSTOPS_ENABLED) {
  1768. endstops.update(); // call endstop update routine
  1769. e_hit--;
  1770. }
  1771. #endif
  1772. cli();
  1773. in_temp_isr = false;
  1774. SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR
  1775. }