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

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