temperature.cpp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. #include "printcounter.h"
  32. #include "delay.h"
  33. #include "endstops.h"
  34. #if ENABLED(HEATER_0_USES_MAX6675)
  35. #include "MarlinSPI.h"
  36. #endif
  37. #if ENABLED(BABYSTEPPING)
  38. #include "stepper.h"
  39. #endif
  40. #if ENABLED(USE_WATCHDOG)
  41. #include "watchdog.h"
  42. #endif
  43. #if ENABLED(EMERGENCY_PARSER)
  44. #include "emergency_parser.h"
  45. #endif
  46. #if HOTEND_USES_THERMISTOR
  47. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  48. static void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  49. static constexpr uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  50. #else
  51. 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);
  52. static constexpr 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);
  53. #endif
  54. #endif
  55. Temperature thermalManager;
  56. /**
  57. * Macros to include the heater id in temp errors. The compiler's dead-code
  58. * elimination should (hopefully) optimize out the unused strings.
  59. */
  60. #if HAS_HEATED_BED
  61. #define TEMP_ERR_PSTR(MSG, E) \
  62. (E) == -1 ? PSTR(MSG ## _BED) : \
  63. (HOTENDS > 1 && (E) == 1) ? PSTR(MSG_E2 " " MSG) : \
  64. (HOTENDS > 2 && (E) == 2) ? PSTR(MSG_E3 " " MSG) : \
  65. (HOTENDS > 3 && (E) == 3) ? PSTR(MSG_E4 " " MSG) : \
  66. (HOTENDS > 4 && (E) == 4) ? PSTR(MSG_E5 " " MSG) : \
  67. PSTR(MSG_E1 " " MSG)
  68. #else
  69. #define TEMP_ERR_PSTR(MSG, E) \
  70. (HOTENDS > 1 && (E) == 1) ? PSTR(MSG_E2 " " MSG) : \
  71. (HOTENDS > 2 && (E) == 2) ? PSTR(MSG_E3 " " MSG) : \
  72. (HOTENDS > 3 && (E) == 3) ? PSTR(MSG_E4 " " MSG) : \
  73. (HOTENDS > 4 && (E) == 4) ? PSTR(MSG_E5 " " MSG) : \
  74. PSTR(MSG_E1 " " MSG)
  75. #endif
  76. // public:
  77. float Temperature::current_temperature[HOTENDS] = { 0.0 };
  78. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  79. Temperature::target_temperature[HOTENDS] = { 0 };
  80. #if ENABLED(AUTO_POWER_E_FANS)
  81. int16_t Temperature::autofan_speed[HOTENDS] = { 0 };
  82. #endif
  83. #if HAS_HEATED_BED
  84. float Temperature::current_temperature_bed = 0.0;
  85. int16_t Temperature::current_temperature_bed_raw = 0,
  86. Temperature::target_temperature_bed = 0;
  87. uint8_t Temperature::soft_pwm_amount_bed;
  88. #ifdef BED_MINTEMP
  89. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  90. #endif
  91. #ifdef BED_MAXTEMP
  92. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  93. #endif
  94. #if WATCH_THE_BED
  95. uint16_t Temperature::watch_target_bed_temp = 0;
  96. millis_t Temperature::watch_bed_next_ms = 0;
  97. #endif
  98. #if ENABLED(PIDTEMPBED)
  99. float Temperature::bedKp, Temperature::bedKi, Temperature::bedKd, // Initialized by settings.load()
  100. Temperature::temp_iState_bed = { 0 },
  101. Temperature::temp_dState_bed = { 0 },
  102. Temperature::pTerm_bed,
  103. Temperature::iTerm_bed,
  104. Temperature::dTerm_bed,
  105. Temperature::pid_error_bed;
  106. #else
  107. millis_t Temperature::next_bed_check_ms;
  108. #endif
  109. uint16_t Temperature::raw_temp_bed_value = 0;
  110. #if HEATER_IDLE_HANDLER
  111. millis_t Temperature::bed_idle_timeout_ms = 0;
  112. bool Temperature::bed_idle_timeout_exceeded = false;
  113. #endif
  114. #endif // HAS_HEATED_BED
  115. #if HAS_TEMP_CHAMBER
  116. float Temperature::current_temperature_chamber = 0.0;
  117. int16_t Temperature::current_temperature_chamber_raw = 0;
  118. uint16_t Temperature::raw_temp_chamber_value = 0;
  119. #endif
  120. // Initialized by settings.load()
  121. #if ENABLED(PIDTEMP)
  122. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  123. float Temperature::Kp[HOTENDS], Temperature::Ki[HOTENDS], Temperature::Kd[HOTENDS];
  124. #if ENABLED(PID_EXTRUSION_SCALING)
  125. float Temperature::Kc[HOTENDS];
  126. #endif
  127. #else
  128. float Temperature::Kp, Temperature::Ki, Temperature::Kd;
  129. #if ENABLED(PID_EXTRUSION_SCALING)
  130. float Temperature::Kc;
  131. #endif
  132. #endif
  133. #endif
  134. #if ENABLED(BABYSTEPPING)
  135. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  136. #endif
  137. #if WATCH_HOTENDS
  138. uint16_t Temperature::watch_target_temp[HOTENDS] = { 0 };
  139. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  140. #endif
  141. #if ENABLED(PREVENT_COLD_EXTRUSION)
  142. bool Temperature::allow_cold_extrude = false;
  143. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  144. #endif
  145. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  146. uint16_t Temperature::redundant_temperature_raw = 0;
  147. float Temperature::redundant_temperature = 0.0;
  148. #endif
  149. volatile bool Temperature::temp_meas_ready = false;
  150. #if ENABLED(PIDTEMP)
  151. float Temperature::temp_iState[HOTENDS] = { 0 },
  152. Temperature::temp_dState[HOTENDS] = { 0 },
  153. Temperature::pTerm[HOTENDS],
  154. Temperature::iTerm[HOTENDS],
  155. Temperature::dTerm[HOTENDS];
  156. #if ENABLED(PID_EXTRUSION_SCALING)
  157. float Temperature::cTerm[HOTENDS];
  158. long Temperature::last_e_position;
  159. long Temperature::lpq[LPQ_MAX_LEN];
  160. int Temperature::lpq_ptr = 0;
  161. #endif
  162. float Temperature::pid_error[HOTENDS];
  163. bool Temperature::pid_reset[HOTENDS];
  164. #endif
  165. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 };
  166. // Init min and max temp with extreme values to prevent false errors during startup
  167. 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),
  168. 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),
  169. Temperature::minttemp[HOTENDS] = { 0 },
  170. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  171. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  172. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  173. #endif
  174. #ifdef MILLISECONDS_PREHEAT_TIME
  175. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  176. #endif
  177. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  178. int8_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  179. #endif
  180. #if HAS_AUTO_FAN
  181. millis_t Temperature::next_auto_fan_check_ms = 0;
  182. #endif
  183. uint8_t Temperature::soft_pwm_amount[HOTENDS];
  184. #if ENABLED(FAN_SOFT_PWM)
  185. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  186. Temperature::soft_pwm_count_fan[FAN_COUNT];
  187. #endif
  188. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  189. uint16_t Temperature::current_raw_filwidth = 0; // Measured filament diameter - one extruder only
  190. #endif
  191. #if ENABLED(PROBING_HEATERS_OFF)
  192. bool Temperature::paused;
  193. #endif
  194. #if HEATER_IDLE_HANDLER
  195. millis_t Temperature::heater_idle_timeout_ms[HOTENDS] = { 0 };
  196. bool Temperature::heater_idle_timeout_exceeded[HOTENDS] = { false };
  197. #endif
  198. #if ENABLED(ADC_KEYPAD)
  199. uint32_t Temperature::current_ADCKey_raw = 0;
  200. uint8_t Temperature::ADCKey_count = 0;
  201. #endif
  202. #if ENABLED(PID_EXTRUSION_SCALING)
  203. int16_t Temperature::lpq_len; // Initialized in configuration_store
  204. #endif
  205. #if HAS_PID_HEATING
  206. /**
  207. * PID Autotuning (M303)
  208. *
  209. * Alternately heat and cool the nozzle, observing its behavior to
  210. * determine the best PID values to achieve a stable temperature.
  211. */
  212. void Temperature::PID_autotune(const float &target, const int8_t hotend, const int8_t ncycles, const bool set_result/*=false*/) {
  213. float current = 0.0;
  214. int cycles = 0;
  215. bool heating = true;
  216. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  217. long t_high = 0, t_low = 0;
  218. long bias, d;
  219. float Ku, Tu,
  220. workKp = 0, workKi = 0, workKd = 0,
  221. max = 0, min = 10000;
  222. #if HAS_PID_FOR_BOTH
  223. #define GHV(B,H) (hotend < 0 ? (B) : (H))
  224. #define SHV(S,B,H) if (hotend < 0) S##_bed = B; else S [hotend] = H;
  225. #elif ENABLED(PIDTEMPBED)
  226. #define GHV(B,H) B
  227. #define SHV(S,B,H) (S##_bed = B)
  228. #else
  229. #define GHV(B,H) H
  230. #define SHV(S,B,H) (S [hotend] = H)
  231. #endif
  232. #if WATCH_THE_BED || WATCH_HOTENDS
  233. #define HAS_TP_BED (ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED))
  234. #if HAS_TP_BED && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  235. #define GTV(B,H) (hotend < 0 ? (B) : (H))
  236. #elif HAS_TP_BED
  237. #define GTV(B,H) (B)
  238. #else
  239. #define GTV(B,H) (H)
  240. #endif
  241. const uint16_t watch_temp_period = GTV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
  242. const uint8_t watch_temp_increase = GTV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
  243. const float watch_temp_target = target - float(watch_temp_increase + GTV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1);
  244. millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL;
  245. float next_watch_temp = 0.0;
  246. bool heated = false;
  247. #endif
  248. #if HAS_AUTO_FAN
  249. next_auto_fan_check_ms = next_temp_ms + 2500UL;
  250. #endif
  251. #if ENABLED(PIDTEMP)
  252. #define _TOP_HOTEND HOTENDS - 1
  253. #else
  254. #define _TOP_HOTEND -1
  255. #endif
  256. #if ENABLED(PIDTEMPBED)
  257. #define _BOT_HOTEND -1
  258. #else
  259. #define _BOT_HOTEND 0
  260. #endif
  261. if (!WITHIN(hotend, _BOT_HOTEND, _TOP_HOTEND)) {
  262. SERIAL_ECHOLNPGM(MSG_PID_BAD_EXTRUDER_NUM);
  263. return;
  264. }
  265. if (target > GHV(BED_MAXTEMP, maxttemp[hotend]) - 15) {
  266. SERIAL_ECHOLNPGM(MSG_PID_TEMP_TOO_HIGH);
  267. return;
  268. }
  269. SERIAL_ECHOLNPGM(MSG_PID_AUTOTUNE_START);
  270. disable_all_heaters(); // switch off all heaters.
  271. SHV(soft_pwm_amount, bias = d = (MAX_BED_POWER) >> 1, bias = d = (PID_MAX) >> 1);
  272. wait_for_heatup = true; // Can be interrupted with M108
  273. // PID Tuning loop
  274. while (wait_for_heatup) {
  275. const millis_t ms = millis();
  276. if (temp_meas_ready) { // temp sample ready
  277. updateTemperaturesFromRawValues();
  278. // Get the current temperature and constrain it
  279. current = GHV(current_temperature_bed, current_temperature[hotend]);
  280. NOLESS(max, current);
  281. NOMORE(min, current);
  282. #if HAS_AUTO_FAN
  283. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  284. checkExtruderAutoFans();
  285. next_auto_fan_check_ms = ms + 2500UL;
  286. }
  287. #endif
  288. if (heating && current > target) {
  289. if (ELAPSED(ms, t2 + 5000UL)) {
  290. heating = false;
  291. SHV(soft_pwm_amount, (bias - d) >> 1, (bias - d) >> 1);
  292. t1 = ms;
  293. t_high = t1 - t2;
  294. max = target;
  295. }
  296. }
  297. if (!heating && current < target) {
  298. if (ELAPSED(ms, t1 + 5000UL)) {
  299. heating = true;
  300. t2 = ms;
  301. t_low = t2 - t1;
  302. if (cycles > 0) {
  303. const long max_pow = GHV(MAX_BED_POWER, PID_MAX);
  304. bias += (d * (t_high - t_low)) / (t_low + t_high);
  305. bias = constrain(bias, 20, max_pow - 20);
  306. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  307. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  308. SERIAL_PROTOCOLPAIR(MSG_D, d);
  309. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  310. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  311. if (cycles > 2) {
  312. Ku = (4.0f * d) / (M_PI * (max - min) * 0.5f);
  313. Tu = ((float)(t_low + t_high) * 0.001f);
  314. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  315. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  316. workKp = 0.6f * Ku;
  317. workKi = 2 * workKp / Tu;
  318. workKd = workKp * Tu * 0.125f;
  319. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  320. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  321. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  322. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  323. /**
  324. workKp = 0.33*Ku;
  325. workKi = workKp/Tu;
  326. workKd = workKp*Tu/3;
  327. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  328. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  329. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  330. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  331. workKp = 0.2*Ku;
  332. workKi = 2*workKp/Tu;
  333. workKd = workKp*Tu/3;
  334. SERIAL_PROTOCOLLNPGM(" No overshoot");
  335. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  336. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  337. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  338. */
  339. }
  340. }
  341. SHV(soft_pwm_amount, (bias + d) >> 1, (bias + d) >> 1);
  342. cycles++;
  343. min = target;
  344. }
  345. }
  346. }
  347. // Did the temperature overshoot very far?
  348. #ifndef MAX_OVERSHOOT_PID_AUTOTUNE
  349. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  350. #endif
  351. if (current > target + MAX_OVERSHOOT_PID_AUTOTUNE) {
  352. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  353. break;
  354. }
  355. // Report heater states every 2 seconds
  356. if (ELAPSED(ms, next_temp_ms)) {
  357. #if HAS_TEMP_SENSOR
  358. print_heaterstates();
  359. SERIAL_EOL();
  360. #endif
  361. next_temp_ms = ms + 2000UL;
  362. // Make sure heating is actually working
  363. #if WATCH_THE_BED || WATCH_HOTENDS
  364. if (
  365. #if WATCH_THE_BED && WATCH_HOTENDS
  366. true
  367. #elif WATCH_HOTENDS
  368. hotend >= 0
  369. #else
  370. hotend < 0
  371. #endif
  372. ) {
  373. if (!heated) { // If not yet reached target...
  374. if (current > next_watch_temp) { // Over the watch temp?
  375. next_watch_temp = current + watch_temp_increase; // - set the next temp to watch for
  376. temp_change_ms = ms + watch_temp_period * 1000UL; // - move the expiration timer up
  377. if (current > watch_temp_target) heated = true; // - Flag if target temperature reached
  378. }
  379. else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
  380. _temp_error(hotend, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, hotend));
  381. }
  382. else if (current < target - (MAX_OVERSHOOT_PID_AUTOTUNE)) // Heated, then temperature fell too far?
  383. _temp_error(hotend, PSTR(MSG_T_THERMAL_RUNAWAY), TEMP_ERR_PSTR(MSG_THERMAL_RUNAWAY, hotend));
  384. }
  385. #endif
  386. } // every 2 seconds
  387. // Timeout after MAX_CYCLE_TIME_PID_AUTOTUNE minutes since the last undershoot/overshoot cycle
  388. #ifndef MAX_CYCLE_TIME_PID_AUTOTUNE
  389. #define MAX_CYCLE_TIME_PID_AUTOTUNE 20L
  390. #endif
  391. if (((ms - t1) + (ms - t2)) > (MAX_CYCLE_TIME_PID_AUTOTUNE * 60L * 1000L)) {
  392. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  393. break;
  394. }
  395. if (cycles > ncycles) {
  396. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  397. #if HAS_PID_FOR_BOTH
  398. const char* estring = GHV("bed", "");
  399. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL();
  400. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL();
  401. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL();
  402. #elif ENABLED(PIDTEMP)
  403. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL();
  404. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL();
  405. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL();
  406. #else
  407. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL();
  408. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL();
  409. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL();
  410. #endif
  411. #define _SET_BED_PID() do { \
  412. bedKp = workKp; \
  413. bedKi = scalePID_i(workKi); \
  414. bedKd = scalePID_d(workKd); \
  415. }while(0)
  416. #define _SET_EXTRUDER_PID() do { \
  417. PID_PARAM(Kp, hotend) = workKp; \
  418. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  419. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  420. updatePID(); }while(0)
  421. // Use the result? (As with "M303 U1")
  422. if (set_result) {
  423. #if HAS_PID_FOR_BOTH
  424. if (hotend < 0)
  425. _SET_BED_PID();
  426. else
  427. _SET_EXTRUDER_PID();
  428. #elif ENABLED(PIDTEMP)
  429. _SET_EXTRUDER_PID();
  430. #else
  431. _SET_BED_PID();
  432. #endif
  433. }
  434. return;
  435. }
  436. lcd_update();
  437. }
  438. disable_all_heaters();
  439. }
  440. #endif // HAS_PID_HEATING
  441. /**
  442. * Class and Instance Methods
  443. */
  444. Temperature::Temperature() { }
  445. int Temperature::getHeaterPower(const int heater) {
  446. return (
  447. #if HAS_HEATED_BED
  448. heater < 0 ? soft_pwm_amount_bed :
  449. #endif
  450. soft_pwm_amount[heater]
  451. );
  452. }
  453. #if HAS_AUTO_FAN
  454. void Temperature::checkExtruderAutoFans() {
  455. static const pin_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN, CHAMBER_AUTO_FAN_PIN };
  456. static const uint8_t fanBit[] PROGMEM = {
  457. 0,
  458. AUTO_1_IS_0 ? 0 : 1,
  459. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  460. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  461. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4,
  462. AUTO_CHAMBER_IS_0 ? 0 : AUTO_CHAMBER_IS_1 ? 1 : AUTO_CHAMBER_IS_2 ? 2 : AUTO_CHAMBER_IS_3 ? 3 : AUTO_CHAMBER_IS_4 ? 4 : 5
  463. };
  464. uint8_t fanState = 0;
  465. HOTEND_LOOP()
  466. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  467. SBI(fanState, pgm_read_byte(&fanBit[e]));
  468. #if HAS_TEMP_CHAMBER
  469. if (current_temperature_chamber > EXTRUDER_AUTO_FAN_TEMPERATURE)
  470. SBI(fanState, pgm_read_byte(&fanBit[5]));
  471. #endif
  472. uint8_t fanDone = 0;
  473. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  474. const pin_t pin =
  475. #ifdef ARDUINO
  476. pgm_read_byte(&fanPin[f])
  477. #else
  478. fanPin[f]
  479. #endif
  480. ;
  481. const uint8_t bit = pgm_read_byte(&fanBit[f]);
  482. if (pin >= 0 && !TEST(fanDone, bit)) {
  483. uint8_t newFanSpeed = TEST(fanState, bit) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  484. #if ENABLED(AUTO_POWER_E_FANS)
  485. autofan_speed[f] = newFanSpeed;
  486. #endif
  487. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  488. digitalWrite(pin, newFanSpeed);
  489. analogWrite(pin, newFanSpeed);
  490. SBI(fanDone, bit);
  491. }
  492. }
  493. }
  494. #endif // HAS_AUTO_FAN
  495. //
  496. // Temperature Error Handlers
  497. //
  498. void Temperature::_temp_error(const int8_t e, const char * const serial_msg, const char * const lcd_msg) {
  499. if (IsRunning()) {
  500. SERIAL_ERROR_START();
  501. serialprintPGM(serial_msg);
  502. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  503. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  504. }
  505. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  506. static bool killed = false;
  507. if (!killed) {
  508. Running = false;
  509. killed = true;
  510. kill(lcd_msg);
  511. }
  512. else
  513. disable_all_heaters(); // paranoia
  514. #endif
  515. }
  516. void Temperature::max_temp_error(const int8_t e) {
  517. _temp_error(e, PSTR(MSG_T_MAXTEMP), TEMP_ERR_PSTR(MSG_ERR_MAXTEMP, e));
  518. }
  519. void Temperature::min_temp_error(const int8_t e) {
  520. _temp_error(e, PSTR(MSG_T_MINTEMP), TEMP_ERR_PSTR(MSG_ERR_MINTEMP, e));
  521. }
  522. float Temperature::get_pid_output(const int8_t e) {
  523. #if HOTENDS == 1
  524. UNUSED(e);
  525. #define _HOTEND_TEST true
  526. #else
  527. #define _HOTEND_TEST e == active_extruder
  528. #endif
  529. float pid_output;
  530. #if ENABLED(PIDTEMP)
  531. #if DISABLED(PID_OPENLOOP)
  532. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  533. dTerm[HOTEND_INDEX] = PID_K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + float(PID_K1) * dTerm[HOTEND_INDEX];
  534. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  535. #if HEATER_IDLE_HANDLER
  536. if (heater_idle_timeout_exceeded[HOTEND_INDEX]) {
  537. pid_output = 0;
  538. pid_reset[HOTEND_INDEX] = true;
  539. }
  540. else
  541. #endif
  542. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  543. pid_output = BANG_MAX;
  544. pid_reset[HOTEND_INDEX] = true;
  545. }
  546. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0
  547. #if HEATER_IDLE_HANDLER
  548. || heater_idle_timeout_exceeded[HOTEND_INDEX]
  549. #endif
  550. ) {
  551. pid_output = 0;
  552. pid_reset[HOTEND_INDEX] = true;
  553. }
  554. else {
  555. if (pid_reset[HOTEND_INDEX]) {
  556. temp_iState[HOTEND_INDEX] = 0.0;
  557. pid_reset[HOTEND_INDEX] = false;
  558. }
  559. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  560. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  561. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  562. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  563. #if ENABLED(PID_EXTRUSION_SCALING)
  564. cTerm[HOTEND_INDEX] = 0;
  565. if (_HOTEND_TEST) {
  566. const long e_position = stepper.position(E_AXIS);
  567. if (e_position > last_e_position) {
  568. lpq[lpq_ptr] = e_position - last_e_position;
  569. last_e_position = e_position;
  570. }
  571. else
  572. lpq[lpq_ptr] = 0;
  573. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  574. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  575. pid_output += cTerm[HOTEND_INDEX];
  576. }
  577. #endif // PID_EXTRUSION_SCALING
  578. if (pid_output > PID_MAX) {
  579. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  580. pid_output = PID_MAX;
  581. }
  582. else if (pid_output < 0) {
  583. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  584. pid_output = 0;
  585. }
  586. }
  587. #else
  588. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  589. #endif // PID_OPENLOOP
  590. #if ENABLED(PID_DEBUG)
  591. SERIAL_ECHO_START();
  592. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  593. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  594. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  595. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  596. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  597. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  598. #if ENABLED(PID_EXTRUSION_SCALING)
  599. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  600. #endif
  601. SERIAL_EOL();
  602. #endif // PID_DEBUG
  603. #else /* PID off */
  604. #if HEATER_IDLE_HANDLER
  605. if (heater_idle_timeout_exceeded[HOTEND_INDEX])
  606. pid_output = 0;
  607. else
  608. #endif
  609. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  610. #endif
  611. return pid_output;
  612. }
  613. #if ENABLED(PIDTEMPBED)
  614. float Temperature::get_pid_output_bed() {
  615. float pid_output;
  616. #if DISABLED(PID_OPENLOOP)
  617. pid_error_bed = target_temperature_bed - current_temperature_bed;
  618. pTerm_bed = bedKp * pid_error_bed;
  619. temp_iState_bed += pid_error_bed;
  620. iTerm_bed = bedKi * temp_iState_bed;
  621. dTerm_bed = PID_K2 * bedKd * (current_temperature_bed - temp_dState_bed) + PID_K1 * dTerm_bed;
  622. temp_dState_bed = current_temperature_bed;
  623. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  624. if (pid_output > MAX_BED_POWER) {
  625. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  626. pid_output = MAX_BED_POWER;
  627. }
  628. else if (pid_output < 0) {
  629. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  630. pid_output = 0;
  631. }
  632. #else
  633. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  634. #endif // PID_OPENLOOP
  635. #if ENABLED(PID_BED_DEBUG)
  636. SERIAL_ECHO_START();
  637. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  638. SERIAL_ECHOPGM(": Input ");
  639. SERIAL_ECHO(current_temperature_bed);
  640. SERIAL_ECHOPGM(" Output ");
  641. SERIAL_ECHO(pid_output);
  642. SERIAL_ECHOPGM(" pTerm ");
  643. SERIAL_ECHO(pTerm_bed);
  644. SERIAL_ECHOPGM(" iTerm ");
  645. SERIAL_ECHO(iTerm_bed);
  646. SERIAL_ECHOPGM(" dTerm ");
  647. SERIAL_ECHOLN(dTerm_bed);
  648. #endif // PID_BED_DEBUG
  649. return pid_output;
  650. }
  651. #endif // PIDTEMPBED
  652. /**
  653. * Manage heating activities for extruder hot-ends and a heated bed
  654. * - Acquire updated temperature readings
  655. * - Also resets the watchdog timer
  656. * - Invoke thermal runaway protection
  657. * - Manage extruder auto-fan
  658. * - Apply filament width to the extrusion rate (may move)
  659. * - Update the heated bed PID output value
  660. */
  661. void Temperature::manage_heater() {
  662. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  663. static bool last_pause_state;
  664. #endif
  665. #if ENABLED(EMERGENCY_PARSER)
  666. if (emergency_parser.killed_by_M112) kill(PSTR(MSG_KILLED));
  667. #endif
  668. if (!temp_meas_ready) return;
  669. updateTemperaturesFromRawValues(); // also resets the watchdog
  670. #if ENABLED(HEATER_0_USES_MAX6675)
  671. if (current_temperature[0] > MIN(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0);
  672. if (current_temperature[0] < MAX(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0);
  673. #endif
  674. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || HEATER_IDLE_HANDLER
  675. millis_t ms = millis();
  676. #endif
  677. HOTEND_LOOP() {
  678. #if HEATER_IDLE_HANDLER
  679. if (!heater_idle_timeout_exceeded[e] && heater_idle_timeout_ms[e] && ELAPSED(ms, heater_idle_timeout_ms[e]))
  680. heater_idle_timeout_exceeded[e] = true;
  681. #endif
  682. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  683. // Check for thermal runaway
  684. 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);
  685. #endif
  686. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)get_pid_output(e) >> 1 : 0;
  687. #if WATCH_HOTENDS
  688. // Make sure temperature is increasing
  689. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) { // Time to check this extruder?
  690. if (degHotend(e) < watch_target_temp[e]) // Failed to increase enough?
  691. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, e));
  692. else // Start again if the target is still far off
  693. start_watching_heater(e);
  694. }
  695. #endif
  696. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  697. // Make sure measured temperatures are close together
  698. if (ABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  699. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  700. #endif
  701. } // HOTEND_LOOP
  702. #if HAS_AUTO_FAN
  703. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  704. checkExtruderAutoFans();
  705. next_auto_fan_check_ms = ms + 2500UL;
  706. }
  707. #endif
  708. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  709. /**
  710. * Filament Width Sensor dynamically sets the volumetric multiplier
  711. * based on a delayed measurement of the filament diameter.
  712. */
  713. if (filament_sensor) {
  714. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  715. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  716. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  717. planner.calculate_volumetric_for_width_sensor(measurement_delay[meas_shift_index]);
  718. }
  719. #endif // FILAMENT_WIDTH_SENSOR
  720. #if HAS_HEATED_BED
  721. #if WATCH_THE_BED
  722. // Make sure temperature is increasing
  723. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  724. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  725. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, -1));
  726. else // Start again if the target is still far off
  727. start_watching_bed();
  728. }
  729. #endif // WATCH_THE_BED
  730. #if DISABLED(PIDTEMPBED)
  731. if (PENDING(ms, next_bed_check_ms)
  732. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  733. && paused == last_pause_state
  734. #endif
  735. ) return;
  736. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  737. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  738. last_pause_state = paused;
  739. #endif
  740. #endif
  741. #if HEATER_IDLE_HANDLER
  742. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  743. bed_idle_timeout_exceeded = true;
  744. #endif
  745. #if HAS_THERMALLY_PROTECTED_BED
  746. 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);
  747. #endif
  748. #if HEATER_IDLE_HANDLER
  749. if (bed_idle_timeout_exceeded) {
  750. soft_pwm_amount_bed = 0;
  751. #if DISABLED(PIDTEMPBED)
  752. WRITE_HEATER_BED(LOW);
  753. #endif
  754. }
  755. else
  756. #endif
  757. {
  758. #if ENABLED(PIDTEMPBED)
  759. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  760. #else
  761. // Check if temperature is within the correct band
  762. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  763. #if ENABLED(BED_LIMIT_SWITCHING)
  764. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  765. soft_pwm_amount_bed = 0;
  766. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  767. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  768. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  769. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  770. #endif
  771. }
  772. else {
  773. soft_pwm_amount_bed = 0;
  774. WRITE_HEATER_BED(LOW);
  775. }
  776. #endif
  777. }
  778. #endif // HAS_HEATED_BED
  779. }
  780. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  781. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  782. /**
  783. * Bisect search for the range of the 'raw' value, then interpolate
  784. * proportionally between the under and over values.
  785. */
  786. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  787. uint8_t l = 0, r = LEN, m; \
  788. for (;;) { \
  789. m = (l + r) >> 1; \
  790. if (m == l || m == r) return (short)pgm_read_word(&TBL[LEN-1][1]); \
  791. short v00 = pgm_read_word(&TBL[m-1][0]), \
  792. v10 = pgm_read_word(&TBL[m-0][0]); \
  793. if (raw < v00) r = m; \
  794. else if (raw > v10) l = m; \
  795. else { \
  796. const short v01 = (short)pgm_read_word(&TBL[m-1][1]), \
  797. v11 = (short)pgm_read_word(&TBL[m-0][1]); \
  798. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  799. } \
  800. } \
  801. }while(0)
  802. // Derived from RepRap FiveD extruder::getTemperature()
  803. // For hot end temperature measurement.
  804. float Temperature::analog2temp(const int raw, const uint8_t e) {
  805. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  806. if (e > HOTENDS)
  807. #else
  808. if (e >= HOTENDS)
  809. #endif
  810. {
  811. SERIAL_ERROR_START();
  812. SERIAL_ERROR((int)e);
  813. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  814. kill(PSTR(MSG_KILLED));
  815. return 0.0;
  816. }
  817. switch (e) {
  818. case 0:
  819. #if ENABLED(HEATER_0_USES_MAX6675)
  820. return raw * 0.25;
  821. #elif ENABLED(HEATER_0_USES_AD595)
  822. return TEMP_AD595(raw);
  823. #elif ENABLED(HEATER_0_USES_AD8495)
  824. return TEMP_AD8495(raw);
  825. #else
  826. break;
  827. #endif
  828. case 1:
  829. #if ENABLED(HEATER_1_USES_AD595)
  830. return TEMP_AD595(raw);
  831. #elif ENABLED(HEATER_1_USES_AD8495)
  832. return TEMP_AD8495(raw);
  833. #else
  834. break;
  835. #endif
  836. case 2:
  837. #if ENABLED(HEATER_2_USES_AD595)
  838. return TEMP_AD595(raw);
  839. #elif ENABLED(HEATER_2_USES_AD8495)
  840. return TEMP_AD8495(raw);
  841. #else
  842. break;
  843. #endif
  844. case 3:
  845. #if ENABLED(HEATER_3_USES_AD595)
  846. return TEMP_AD595(raw);
  847. #elif ENABLED(HEATER_3_USES_AD8495)
  848. return TEMP_AD8495(raw);
  849. #else
  850. break;
  851. #endif
  852. case 4:
  853. #if ENABLED(HEATER_4_USES_AD595)
  854. return TEMP_AD595(raw);
  855. #elif ENABLED(HEATER_4_USES_AD8495)
  856. return TEMP_AD8495(raw);
  857. #else
  858. break;
  859. #endif
  860. default: break;
  861. }
  862. #if HOTEND_USES_THERMISTOR
  863. // Thermistor with conversion table?
  864. const short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  865. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  866. #endif
  867. return 0;
  868. }
  869. #if HAS_HEATED_BED
  870. // Derived from RepRap FiveD extruder::getTemperature()
  871. // For bed temperature measurement.
  872. float Temperature::analog2tempBed(const int raw) {
  873. #if ENABLED(HEATER_BED_USES_THERMISTOR)
  874. SCAN_THERMISTOR_TABLE(BEDTEMPTABLE, BEDTEMPTABLE_LEN);
  875. #elif ENABLED(HEATER_BED_USES_AD595)
  876. return TEMP_AD595(raw);
  877. #elif ENABLED(HEATER_BED_USES_AD8495)
  878. return TEMP_AD8495(raw);
  879. #else
  880. return 0;
  881. #endif
  882. }
  883. #endif // HAS_HEATED_BED
  884. #if HAS_TEMP_CHAMBER
  885. // Derived from RepRap FiveD extruder::getTemperature()
  886. // For chamber temperature measurement.
  887. float Temperature::analog2tempChamber(const int raw) {
  888. #if ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  889. SCAN_THERMISTOR_TABLE(CHAMBERTEMPTABLE, CHAMBERTEMPTABLE_LEN);
  890. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  891. return TEMP_AD595(raw);
  892. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  893. return TEMP_AD8495(raw);
  894. #else
  895. return 0;
  896. #endif
  897. }
  898. #endif // HAS_TEMP_CHAMBER
  899. /**
  900. * Get the raw values into the actual temperatures.
  901. * The raw values are created in interrupt context,
  902. * and this function is called from normal context
  903. * as it would block the stepper routine.
  904. */
  905. void Temperature::updateTemperaturesFromRawValues() {
  906. #if ENABLED(HEATER_0_USES_MAX6675)
  907. current_temperature_raw[0] = read_max6675();
  908. #endif
  909. HOTEND_LOOP() current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  910. #if HAS_HEATED_BED
  911. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  912. #endif
  913. #if HAS_TEMP_CHAMBER
  914. current_temperature_chamber = Temperature::analog2tempChamber(current_temperature_chamber_raw);
  915. #endif
  916. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  917. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  918. #endif
  919. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  920. filament_width_meas = analog2widthFil();
  921. #endif
  922. #if ENABLED(USE_WATCHDOG)
  923. // Reset the watchdog after we know we have a temperature measurement.
  924. watchdog_reset();
  925. #endif
  926. temp_meas_ready = false;
  927. }
  928. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  929. // Convert raw Filament Width to millimeters
  930. float Temperature::analog2widthFil() {
  931. return current_raw_filwidth * 5.0f * (1.0f / 16383.0);
  932. }
  933. /**
  934. * Convert Filament Width (mm) to a simple ratio
  935. * and reduce to an 8 bit value.
  936. *
  937. * A nominal width of 1.75 and measured width of 1.73
  938. * gives (100 * 1.75 / 1.73) for a ratio of 101 and
  939. * a return value of 1.
  940. */
  941. int8_t Temperature::widthFil_to_size_ratio() {
  942. if (ABS(filament_width_nominal - filament_width_meas) <= FILWIDTH_ERROR_MARGIN)
  943. return int(100.0f * filament_width_nominal / filament_width_meas) - 100;
  944. return 0;
  945. }
  946. #endif
  947. #if ENABLED(HEATER_0_USES_MAX6675)
  948. #ifndef MAX6675_SCK_PIN
  949. #define MAX6675_SCK_PIN SCK_PIN
  950. #endif
  951. #ifndef MAX6675_DO_PIN
  952. #define MAX6675_DO_PIN MISO_PIN
  953. #endif
  954. SPI<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  955. #endif
  956. /**
  957. * Initialize the temperature manager
  958. * The manager is implemented by periodic calls to manage_heater()
  959. */
  960. void Temperature::init() {
  961. #if MB(RUMBA) && ( \
  962. ENABLED(HEATER_0_USES_AD595) || ENABLED(HEATER_1_USES_AD595) || ENABLED(HEATER_2_USES_AD595) || ENABLED(HEATER_3_USES_AD595) || ENABLED(HEATER_4_USES_AD595) || ENABLED(HEATER_BED_USES_AD595) || ENABLED(HEATER_CHAMBER_USES_AD595) \
  963. || ENABLED(HEATER_0_USES_AD8495) || ENABLED(HEATER_1_USES_AD8495) || ENABLED(HEATER_2_USES_AD8495) || ENABLED(HEATER_3_USES_AD8495) || ENABLED(HEATER_4_USES_AD8495) || ENABLED(HEATER_BED_USES_AD8495) || ENABLED(HEATER_CHAMBER_USES_AD8495))
  964. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  965. MCUCR = _BV(JTD);
  966. MCUCR = _BV(JTD);
  967. #endif
  968. // Finish init of mult hotend arrays
  969. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  970. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  971. last_e_position = 0;
  972. #endif
  973. #if HAS_HEATER_0
  974. SET_OUTPUT(HEATER_0_PIN);
  975. #endif
  976. #if HAS_HEATER_1
  977. SET_OUTPUT(HEATER_1_PIN);
  978. #endif
  979. #if HAS_HEATER_2
  980. SET_OUTPUT(HEATER_2_PIN);
  981. #endif
  982. #if HAS_HEATER_3
  983. SET_OUTPUT(HEATER_3_PIN);
  984. #endif
  985. #if HAS_HEATER_4
  986. SET_OUTPUT(HEATER_3_PIN);
  987. #endif
  988. #if HAS_HEATED_BED
  989. SET_OUTPUT(HEATER_BED_PIN);
  990. #endif
  991. #if HAS_FAN0
  992. SET_OUTPUT(FAN_PIN);
  993. #if ENABLED(FAST_PWM_FAN)
  994. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  995. #endif
  996. #endif
  997. #if HAS_FAN1
  998. SET_OUTPUT(FAN1_PIN);
  999. #if ENABLED(FAST_PWM_FAN)
  1000. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1001. #endif
  1002. #endif
  1003. #if HAS_FAN2
  1004. SET_OUTPUT(FAN2_PIN);
  1005. #if ENABLED(FAST_PWM_FAN)
  1006. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1007. #endif
  1008. #endif
  1009. #if ENABLED(HEATER_0_USES_MAX6675)
  1010. OUT_WRITE(SCK_PIN, LOW);
  1011. OUT_WRITE(MOSI_PIN, HIGH);
  1012. SET_INPUT_PULLUP(MISO_PIN);
  1013. max6675_spi.init();
  1014. OUT_WRITE(SS_PIN, HIGH);
  1015. OUT_WRITE(MAX6675_SS, HIGH);
  1016. #endif // HEATER_0_USES_MAX6675
  1017. HAL_adc_init();
  1018. #if HAS_TEMP_ADC_0
  1019. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1020. #endif
  1021. #if HAS_TEMP_ADC_1
  1022. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1023. #endif
  1024. #if HAS_TEMP_ADC_2
  1025. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1026. #endif
  1027. #if HAS_TEMP_ADC_3
  1028. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1029. #endif
  1030. #if HAS_TEMP_ADC_4
  1031. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1032. #endif
  1033. #if HAS_HEATED_BED
  1034. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1035. #endif
  1036. #if HAS_TEMP_CHAMBER
  1037. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1038. #endif
  1039. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1040. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1041. #endif
  1042. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1043. ENABLE_TEMPERATURE_INTERRUPT();
  1044. #if HAS_AUTO_FAN_0
  1045. #if E0_AUTO_FAN_PIN == FAN1_PIN
  1046. SET_OUTPUT(E0_AUTO_FAN_PIN);
  1047. #if ENABLED(FAST_PWM_FAN)
  1048. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1049. #endif
  1050. #else
  1051. SET_OUTPUT(E0_AUTO_FAN_PIN);
  1052. #endif
  1053. #endif
  1054. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  1055. #if E1_AUTO_FAN_PIN == FAN1_PIN
  1056. SET_OUTPUT(E1_AUTO_FAN_PIN);
  1057. #if ENABLED(FAST_PWM_FAN)
  1058. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1059. #endif
  1060. #else
  1061. SET_OUTPUT(E1_AUTO_FAN_PIN);
  1062. #endif
  1063. #endif
  1064. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  1065. #if E2_AUTO_FAN_PIN == FAN1_PIN
  1066. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1067. #if ENABLED(FAST_PWM_FAN)
  1068. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1069. #endif
  1070. #else
  1071. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1072. #endif
  1073. #endif
  1074. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  1075. #if E3_AUTO_FAN_PIN == FAN1_PIN
  1076. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1077. #if ENABLED(FAST_PWM_FAN)
  1078. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1079. #endif
  1080. #else
  1081. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1082. #endif
  1083. #endif
  1084. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  1085. #if E4_AUTO_FAN_PIN == FAN1_PIN
  1086. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1087. #if ENABLED(FAST_PWM_FAN)
  1088. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1089. #endif
  1090. #else
  1091. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1092. #endif
  1093. #endif
  1094. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_0 && !AUTO_CHAMBER_IS_1 && !AUTO_CHAMBER_IS_2 && !AUTO_CHAMBER_IS_3 && ! AUTO_CHAMBER_IS_4
  1095. #if CHAMBER_AUTO_FAN_PIN == FAN1_PIN
  1096. SET_OUTPUT(CHAMBER_AUTO_FAN_PIN);
  1097. #if ENABLED(FAST_PWM_FAN)
  1098. setPwmFrequency(CHAMBER_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1099. #endif
  1100. #else
  1101. SET_OUTPUT(CHAMBER_AUTO_FAN_PIN);
  1102. #endif
  1103. #endif
  1104. // Wait for temperature measurement to settle
  1105. delay(250);
  1106. #define TEMP_MIN_ROUTINE(NR) \
  1107. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1108. while (analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1109. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1110. minttemp_raw[NR] += OVERSAMPLENR; \
  1111. else \
  1112. minttemp_raw[NR] -= OVERSAMPLENR; \
  1113. }
  1114. #define TEMP_MAX_ROUTINE(NR) \
  1115. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1116. while (analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1117. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1118. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1119. else \
  1120. maxttemp_raw[NR] += OVERSAMPLENR; \
  1121. }
  1122. #ifdef HEATER_0_MINTEMP
  1123. TEMP_MIN_ROUTINE(0);
  1124. #endif
  1125. #ifdef HEATER_0_MAXTEMP
  1126. TEMP_MAX_ROUTINE(0);
  1127. #endif
  1128. #if HOTENDS > 1
  1129. #ifdef HEATER_1_MINTEMP
  1130. TEMP_MIN_ROUTINE(1);
  1131. #endif
  1132. #ifdef HEATER_1_MAXTEMP
  1133. TEMP_MAX_ROUTINE(1);
  1134. #endif
  1135. #if HOTENDS > 2
  1136. #ifdef HEATER_2_MINTEMP
  1137. TEMP_MIN_ROUTINE(2);
  1138. #endif
  1139. #ifdef HEATER_2_MAXTEMP
  1140. TEMP_MAX_ROUTINE(2);
  1141. #endif
  1142. #if HOTENDS > 3
  1143. #ifdef HEATER_3_MINTEMP
  1144. TEMP_MIN_ROUTINE(3);
  1145. #endif
  1146. #ifdef HEATER_3_MAXTEMP
  1147. TEMP_MAX_ROUTINE(3);
  1148. #endif
  1149. #if HOTENDS > 4
  1150. #ifdef HEATER_4_MINTEMP
  1151. TEMP_MIN_ROUTINE(4);
  1152. #endif
  1153. #ifdef HEATER_4_MAXTEMP
  1154. TEMP_MAX_ROUTINE(4);
  1155. #endif
  1156. #endif // HOTENDS > 4
  1157. #endif // HOTENDS > 3
  1158. #endif // HOTENDS > 2
  1159. #endif // HOTENDS > 1
  1160. #if HAS_HEATED_BED
  1161. #ifdef BED_MINTEMP
  1162. while (analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1163. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1164. bed_minttemp_raw += OVERSAMPLENR;
  1165. #else
  1166. bed_minttemp_raw -= OVERSAMPLENR;
  1167. #endif
  1168. }
  1169. #endif // BED_MINTEMP
  1170. #ifdef BED_MAXTEMP
  1171. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1172. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1173. bed_maxttemp_raw -= OVERSAMPLENR;
  1174. #else
  1175. bed_maxttemp_raw += OVERSAMPLENR;
  1176. #endif
  1177. }
  1178. #endif // BED_MAXTEMP
  1179. #endif // HAS_HEATED_BED
  1180. #if ENABLED(PROBING_HEATERS_OFF)
  1181. paused = false;
  1182. #endif
  1183. }
  1184. #if ENABLED(FAST_PWM_FAN)
  1185. void Temperature::setPwmFrequency(const pin_t pin, int val) {
  1186. val &= 0x07;
  1187. switch (digitalPinToTimer(pin)) {
  1188. #ifdef TCCR0A
  1189. #if !AVR_AT90USB1286_FAMILY
  1190. case TIMER0A:
  1191. #endif
  1192. case TIMER0B: //_SET_CS(0, val);
  1193. break;
  1194. #endif
  1195. #ifdef TCCR1A
  1196. case TIMER1A: case TIMER1B: //_SET_CS(1, val);
  1197. break;
  1198. #endif
  1199. #if defined(TCCR2) || defined(TCCR2A)
  1200. #ifdef TCCR2
  1201. case TIMER2:
  1202. #endif
  1203. #ifdef TCCR2A
  1204. case TIMER2A: case TIMER2B:
  1205. #endif
  1206. _SET_CS(2, val); break;
  1207. #endif
  1208. #ifdef TCCR3A
  1209. case TIMER3A: case TIMER3B: case TIMER3C: _SET_CS(3, val); break;
  1210. #endif
  1211. #ifdef TCCR4A
  1212. case TIMER4A: case TIMER4B: case TIMER4C: _SET_CS(4, val); break;
  1213. #endif
  1214. #ifdef TCCR5A
  1215. case TIMER5A: case TIMER5B: case TIMER5C: _SET_CS(5, val); break;
  1216. #endif
  1217. }
  1218. }
  1219. #endif // FAST_PWM_FAN
  1220. #if WATCH_HOTENDS
  1221. /**
  1222. * Start Heating Sanity Check for hotends that are below
  1223. * their target temperature by a configurable margin.
  1224. * This is called when the temperature is set. (M104, M109)
  1225. */
  1226. void Temperature::start_watching_heater(const uint8_t e) {
  1227. #if HOTENDS == 1
  1228. UNUSED(e);
  1229. #endif
  1230. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1231. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1232. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1233. }
  1234. else
  1235. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1236. }
  1237. #endif
  1238. #if WATCH_THE_BED
  1239. /**
  1240. * Start Heating Sanity Check for hotends that are below
  1241. * their target temperature by a configurable margin.
  1242. * This is called when the temperature is set. (M140, M190)
  1243. */
  1244. void Temperature::start_watching_bed() {
  1245. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1246. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1247. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1248. }
  1249. else
  1250. watch_bed_next_ms = 0;
  1251. }
  1252. #endif
  1253. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1254. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1255. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1256. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1257. #endif
  1258. #if HAS_THERMALLY_PROTECTED_BED
  1259. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1260. millis_t Temperature::thermal_runaway_bed_timer;
  1261. #endif
  1262. void Temperature::thermal_runaway_protection(Temperature::TRState * const state, millis_t * const timer, const float &current, const float &target, const int8_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc) {
  1263. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1264. /**
  1265. SERIAL_ECHO_START();
  1266. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1267. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1268. SERIAL_ECHOPAIR(" ; State:", *state);
  1269. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1270. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1271. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1272. if (heater_id >= 0)
  1273. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1274. else
  1275. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1276. SERIAL_EOL();
  1277. */
  1278. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1279. #if HEATER_IDLE_HANDLER
  1280. // If the heater idle timeout expires, restart
  1281. if ((heater_id >= 0 && heater_idle_timeout_exceeded[heater_id])
  1282. #if HAS_HEATED_BED
  1283. || (heater_id < 0 && bed_idle_timeout_exceeded)
  1284. #endif
  1285. ) {
  1286. *state = TRInactive;
  1287. tr_target_temperature[heater_index] = 0;
  1288. }
  1289. else
  1290. #endif
  1291. {
  1292. // If the target temperature changes, restart
  1293. if (tr_target_temperature[heater_index] != target) {
  1294. tr_target_temperature[heater_index] = target;
  1295. *state = target > 0 ? TRFirstHeating : TRInactive;
  1296. }
  1297. }
  1298. switch (*state) {
  1299. // Inactive state waits for a target temperature to be set
  1300. case TRInactive: break;
  1301. // When first heating, wait for the temperature to be reached then go to Stable state
  1302. case TRFirstHeating:
  1303. if (current < tr_target_temperature[heater_index]) break;
  1304. *state = TRStable;
  1305. // While the temperature is stable watch for a bad temperature
  1306. case TRStable:
  1307. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1308. *timer = millis() + period_seconds * 1000UL;
  1309. break;
  1310. }
  1311. else if (PENDING(millis(), *timer)) break;
  1312. *state = TRRunaway;
  1313. case TRRunaway:
  1314. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), TEMP_ERR_PSTR(MSG_THERMAL_RUNAWAY, heater_id));
  1315. }
  1316. }
  1317. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1318. void Temperature::disable_all_heaters() {
  1319. #if ENABLED(AUTOTEMP)
  1320. planner.autotemp_enabled = false;
  1321. #endif
  1322. HOTEND_LOOP() setTargetHotend(0, e);
  1323. #if HAS_HEATED_BED
  1324. setTargetBed(0);
  1325. #endif
  1326. // Unpause and reset everything
  1327. #if ENABLED(PROBING_HEATERS_OFF)
  1328. pause(false);
  1329. #endif
  1330. // If all heaters go down then for sure our print job has stopped
  1331. print_job_timer.stop();
  1332. #define DISABLE_HEATER(NR) { \
  1333. setTargetHotend(0, NR); \
  1334. soft_pwm_amount[NR] = 0; \
  1335. WRITE_HEATER_ ##NR (LOW); \
  1336. }
  1337. #if HAS_TEMP_HOTEND
  1338. DISABLE_HEATER(0);
  1339. #if HOTENDS > 1
  1340. DISABLE_HEATER(1);
  1341. #if HOTENDS > 2
  1342. DISABLE_HEATER(2);
  1343. #if HOTENDS > 3
  1344. DISABLE_HEATER(3);
  1345. #if HOTENDS > 4
  1346. DISABLE_HEATER(4);
  1347. #endif // HOTENDS > 4
  1348. #endif // HOTENDS > 3
  1349. #endif // HOTENDS > 2
  1350. #endif // HOTENDS > 1
  1351. #endif
  1352. #if HAS_HEATED_BED
  1353. target_temperature_bed = 0;
  1354. soft_pwm_amount_bed = 0;
  1355. #if HAS_HEATED_BED
  1356. WRITE_HEATER_BED(LOW);
  1357. #endif
  1358. #endif
  1359. }
  1360. #if ENABLED(PROBING_HEATERS_OFF)
  1361. void Temperature::pause(const bool p) {
  1362. if (p != paused) {
  1363. paused = p;
  1364. if (p) {
  1365. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1366. #if HAS_HEATED_BED
  1367. start_bed_idle_timer(0); // timeout immediately
  1368. #endif
  1369. }
  1370. else {
  1371. HOTEND_LOOP() reset_heater_idle_timer(e);
  1372. #if HAS_HEATED_BED
  1373. reset_bed_idle_timer();
  1374. #endif
  1375. }
  1376. }
  1377. }
  1378. #endif // PROBING_HEATERS_OFF
  1379. #if ENABLED(HEATER_0_USES_MAX6675)
  1380. #define MAX6675_HEAT_INTERVAL 250u
  1381. #if ENABLED(MAX6675_IS_MAX31855)
  1382. uint32_t max6675_temp = 2000;
  1383. #define MAX6675_ERROR_MASK 7
  1384. #define MAX6675_DISCARD_BITS 18
  1385. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1386. #else
  1387. uint16_t max6675_temp = 2000;
  1388. #define MAX6675_ERROR_MASK 4
  1389. #define MAX6675_DISCARD_BITS 3
  1390. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1391. #endif
  1392. int Temperature::read_max6675() {
  1393. static millis_t next_max6675_ms = 0;
  1394. millis_t ms = millis();
  1395. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1396. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1397. CBI(
  1398. #ifdef PRR
  1399. PRR
  1400. #elif defined(PRR0)
  1401. PRR0
  1402. #endif
  1403. , PRSPI);
  1404. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1405. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1406. DELAY_NS(100); // Ensure 100ns delay
  1407. // Read a big-endian temperature value
  1408. max6675_temp = 0;
  1409. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1410. max6675_temp |= max6675_spi.receive();
  1411. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1412. }
  1413. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1414. if (max6675_temp & MAX6675_ERROR_MASK) {
  1415. SERIAL_ERROR_START();
  1416. SERIAL_ERRORPGM("Temp measurement error! ");
  1417. #if MAX6675_ERROR_MASK == 7
  1418. SERIAL_ERRORPGM("MAX31855 ");
  1419. if (max6675_temp & 1)
  1420. SERIAL_ERRORLNPGM("Open Circuit");
  1421. else if (max6675_temp & 2)
  1422. SERIAL_ERRORLNPGM("Short to GND");
  1423. else if (max6675_temp & 4)
  1424. SERIAL_ERRORLNPGM("Short to VCC");
  1425. #else
  1426. SERIAL_ERRORLNPGM("MAX6675");
  1427. #endif
  1428. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1429. }
  1430. else
  1431. max6675_temp >>= MAX6675_DISCARD_BITS;
  1432. #if ENABLED(MAX6675_IS_MAX31855)
  1433. // Support negative temperature
  1434. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1435. #endif
  1436. return (int)max6675_temp;
  1437. }
  1438. #endif // HEATER_0_USES_MAX6675
  1439. /**
  1440. * Get raw temperatures
  1441. */
  1442. void Temperature::set_current_temp_raw() {
  1443. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  1444. current_temperature_raw[0] = raw_temp_value[0];
  1445. #endif
  1446. #if HAS_TEMP_ADC_1
  1447. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1448. redundant_temperature_raw = raw_temp_value[1];
  1449. #else
  1450. current_temperature_raw[1] = raw_temp_value[1];
  1451. #endif
  1452. #if HAS_TEMP_ADC_2
  1453. current_temperature_raw[2] = raw_temp_value[2];
  1454. #if HAS_TEMP_ADC_3
  1455. current_temperature_raw[3] = raw_temp_value[3];
  1456. #if HAS_TEMP_ADC_4
  1457. current_temperature_raw[4] = raw_temp_value[4];
  1458. #endif
  1459. #endif
  1460. #endif
  1461. #endif
  1462. #if HAS_HEATED_BED
  1463. current_temperature_bed_raw = raw_temp_bed_value;
  1464. #endif
  1465. #if HAS_TEMP_CHAMBER
  1466. current_temperature_chamber_raw = raw_temp_chamber_value;
  1467. #endif
  1468. temp_meas_ready = true;
  1469. }
  1470. #if ENABLED(PINS_DEBUGGING)
  1471. /**
  1472. * monitors endstops & Z probe for changes
  1473. *
  1474. * If a change is detected then the LED is toggled and
  1475. * a message is sent out the serial port
  1476. *
  1477. * Yes, we could miss a rapid back & forth change but
  1478. * that won't matter because this is all manual.
  1479. *
  1480. */
  1481. void endstop_monitor() {
  1482. static uint16_t old_live_state_local = 0;
  1483. static uint8_t local_LED_status = 0;
  1484. uint16_t live_state_local = 0;
  1485. #if HAS_X_MIN
  1486. if (READ(X_MIN_PIN)) SBI(live_state_local, X_MIN);
  1487. #endif
  1488. #if HAS_X_MAX
  1489. if (READ(X_MAX_PIN)) SBI(live_state_local, X_MAX);
  1490. #endif
  1491. #if HAS_Y_MIN
  1492. if (READ(Y_MIN_PIN)) SBI(live_state_local, Y_MIN);
  1493. #endif
  1494. #if HAS_Y_MAX
  1495. if (READ(Y_MAX_PIN)) SBI(live_state_local, Y_MAX);
  1496. #endif
  1497. #if HAS_Z_MIN
  1498. if (READ(Z_MIN_PIN)) SBI(live_state_local, Z_MIN);
  1499. #endif
  1500. #if HAS_Z_MAX
  1501. if (READ(Z_MAX_PIN)) SBI(live_state_local, Z_MAX);
  1502. #endif
  1503. #if HAS_Z_MIN_PROBE_PIN
  1504. if (READ(Z_MIN_PROBE_PIN)) SBI(live_state_local, Z_MIN_PROBE);
  1505. #endif
  1506. #if HAS_Z2_MIN
  1507. if (READ(Z2_MIN_PIN)) SBI(live_state_local, Z2_MIN);
  1508. #endif
  1509. #if HAS_Z2_MAX
  1510. if (READ(Z2_MAX_PIN)) SBI(live_state_local, Z2_MAX);
  1511. #endif
  1512. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  1513. if (endstop_change) {
  1514. #if HAS_X_MIN
  1515. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", !!TEST(live_state_local, X_MIN));
  1516. #endif
  1517. #if HAS_X_MAX
  1518. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(live_state_local, X_MAX));
  1519. #endif
  1520. #if HAS_Y_MIN
  1521. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(live_state_local, Y_MIN));
  1522. #endif
  1523. #if HAS_Y_MAX
  1524. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(live_state_local, Y_MAX));
  1525. #endif
  1526. #if HAS_Z_MIN
  1527. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(live_state_local, Z_MIN));
  1528. #endif
  1529. #if HAS_Z_MAX
  1530. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(live_state_local, Z_MAX));
  1531. #endif
  1532. #if HAS_Z_MIN_PROBE_PIN
  1533. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(live_state_local, Z_MIN_PROBE));
  1534. #endif
  1535. #if HAS_Z2_MIN
  1536. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(live_state_local, Z2_MIN));
  1537. #endif
  1538. #if HAS_Z2_MAX
  1539. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(live_state_local, Z2_MAX));
  1540. #endif
  1541. SERIAL_PROTOCOLPGM("\n\n");
  1542. analogWrite(LED_PIN, local_LED_status);
  1543. local_LED_status ^= 255;
  1544. old_live_state_local = live_state_local;
  1545. }
  1546. }
  1547. #endif // PINS_DEBUGGING
  1548. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1549. uint32_t raw_filwidth_value; // = 0
  1550. #endif
  1551. void Temperature::readings_ready() {
  1552. // Update the raw values if they've been read. Else we could be updating them during reading.
  1553. if (!temp_meas_ready) set_current_temp_raw();
  1554. // Filament Sensor - can be read any time since IIR filtering is used
  1555. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1556. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1557. #endif
  1558. ZERO(raw_temp_value);
  1559. #if HAS_HEATED_BED
  1560. raw_temp_bed_value = 0;
  1561. #endif
  1562. #if HAS_TEMP_CHAMBER
  1563. raw_temp_chamber_value = 0;
  1564. #endif
  1565. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1566. int constexpr temp_dir[] = {
  1567. #if ENABLED(HEATER_0_USES_MAX6675)
  1568. 0
  1569. #else
  1570. TEMPDIR(0)
  1571. #endif
  1572. #if HOTENDS > 1
  1573. , TEMPDIR(1)
  1574. #if HOTENDS > 2
  1575. , TEMPDIR(2)
  1576. #if HOTENDS > 3
  1577. , TEMPDIR(3)
  1578. #if HOTENDS > 4
  1579. , TEMPDIR(4)
  1580. #endif // HOTENDS > 4
  1581. #endif // HOTENDS > 3
  1582. #endif // HOTENDS > 2
  1583. #endif // HOTENDS > 1
  1584. };
  1585. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1586. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1587. const bool heater_on = (target_temperature[e] > 0)
  1588. #if ENABLED(PIDTEMP)
  1589. || (soft_pwm_amount[e] > 0)
  1590. #endif
  1591. ;
  1592. if (rawtemp > maxttemp_raw[e] * tdir) max_temp_error(e);
  1593. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && heater_on) {
  1594. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1595. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1596. #endif
  1597. min_temp_error(e);
  1598. }
  1599. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1600. else
  1601. consecutive_low_temperature_error[e] = 0;
  1602. #endif
  1603. }
  1604. #if HAS_HEATED_BED
  1605. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1606. #define GEBED <=
  1607. #else
  1608. #define GEBED >=
  1609. #endif
  1610. const bool bed_on = (target_temperature_bed > 0)
  1611. #if ENABLED(PIDTEMPBED)
  1612. || (soft_pwm_amount_bed > 0)
  1613. #endif
  1614. ;
  1615. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) max_temp_error(-1);
  1616. if (bed_minttemp_raw GEBED current_temperature_bed_raw && bed_on) min_temp_error(-1);
  1617. #endif
  1618. }
  1619. /**
  1620. * Timer 0 is shared with millis so don't change the prescaler.
  1621. *
  1622. * This ISR uses the compare method so it runs at the base
  1623. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1624. * in OCR0B above (128 or halfway between OVFs).
  1625. *
  1626. * - Manage PWM to all the heaters and fan
  1627. * - Prepare or Measure one of the raw ADC sensor values
  1628. * - Check new temperature values for MIN/MAX errors (kill on error)
  1629. * - Step the babysteps value for each axis towards 0
  1630. * - For PINS_DEBUGGING, monitor and report endstop pins
  1631. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1632. * - Call planner.tick to count down its "ignore" time
  1633. */
  1634. HAL_TEMP_TIMER_ISR {
  1635. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  1636. Temperature::isr();
  1637. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  1638. }
  1639. void Temperature::isr() {
  1640. static int8_t temp_count = -1;
  1641. static ADCSensorState adc_sensor_state = StartupDelay;
  1642. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1643. // avoid multiple loads of pwm_count
  1644. uint8_t pwm_count_tmp = pwm_count;
  1645. #if ENABLED(ADC_KEYPAD)
  1646. static unsigned int raw_ADCKey_value = 0;
  1647. #endif
  1648. // Static members for each heater
  1649. #if ENABLED(SLOW_PWM_HEATERS)
  1650. static uint8_t slow_pwm_count = 0;
  1651. #define ISR_STATICS(n) \
  1652. static uint8_t soft_pwm_count_ ## n, \
  1653. state_heater_ ## n = 0, \
  1654. state_timer_heater_ ## n = 0
  1655. #else
  1656. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1657. #endif
  1658. // Statics per heater
  1659. ISR_STATICS(0);
  1660. #if HOTENDS > 1
  1661. ISR_STATICS(1);
  1662. #if HOTENDS > 2
  1663. ISR_STATICS(2);
  1664. #if HOTENDS > 3
  1665. ISR_STATICS(3);
  1666. #if HOTENDS > 4
  1667. ISR_STATICS(4);
  1668. #endif // HOTENDS > 4
  1669. #endif // HOTENDS > 3
  1670. #endif // HOTENDS > 2
  1671. #endif // HOTENDS > 1
  1672. #if HAS_HEATED_BED
  1673. ISR_STATICS(BED);
  1674. #endif
  1675. #if DISABLED(SLOW_PWM_HEATERS)
  1676. constexpr uint8_t pwm_mask =
  1677. #if ENABLED(SOFT_PWM_DITHER)
  1678. _BV(SOFT_PWM_SCALE) - 1
  1679. #else
  1680. 0
  1681. #endif
  1682. ;
  1683. /**
  1684. * Standard PWM modulation
  1685. */
  1686. if (pwm_count_tmp >= 127) {
  1687. pwm_count_tmp -= 127;
  1688. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1689. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1690. #if HOTENDS > 1
  1691. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1692. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1693. #if HOTENDS > 2
  1694. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1695. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1696. #if HOTENDS > 3
  1697. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1698. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1699. #if HOTENDS > 4
  1700. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1701. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1702. #endif // HOTENDS > 4
  1703. #endif // HOTENDS > 3
  1704. #endif // HOTENDS > 2
  1705. #endif // HOTENDS > 1
  1706. #if HAS_HEATED_BED
  1707. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1708. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1709. #endif
  1710. #if ENABLED(FAN_SOFT_PWM)
  1711. #if HAS_FAN0
  1712. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + (soft_pwm_amount_fan[0] >> 1);
  1713. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1714. #endif
  1715. #if HAS_FAN1
  1716. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + (soft_pwm_amount_fan[1] >> 1);
  1717. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1718. #endif
  1719. #if HAS_FAN2
  1720. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + (soft_pwm_amount_fan[2] >> 1);
  1721. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1722. #endif
  1723. #endif
  1724. }
  1725. else {
  1726. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(LOW);
  1727. #if HOTENDS > 1
  1728. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(LOW);
  1729. #if HOTENDS > 2
  1730. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(LOW);
  1731. #if HOTENDS > 3
  1732. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(LOW);
  1733. #if HOTENDS > 4
  1734. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(LOW);
  1735. #endif // HOTENDS > 4
  1736. #endif // HOTENDS > 3
  1737. #endif // HOTENDS > 2
  1738. #endif // HOTENDS > 1
  1739. #if HAS_HEATED_BED
  1740. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(LOW);
  1741. #endif
  1742. #if ENABLED(FAN_SOFT_PWM)
  1743. #if HAS_FAN0
  1744. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1745. #endif
  1746. #if HAS_FAN1
  1747. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1748. #endif
  1749. #if HAS_FAN2
  1750. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1751. #endif
  1752. #endif
  1753. }
  1754. // SOFT_PWM_SCALE to frequency:
  1755. //
  1756. // 0: 16000000/64/256/128 = 7.6294 Hz
  1757. // 1: / 64 = 15.2588 Hz
  1758. // 2: / 32 = 30.5176 Hz
  1759. // 3: / 16 = 61.0352 Hz
  1760. // 4: / 8 = 122.0703 Hz
  1761. // 5: / 4 = 244.1406 Hz
  1762. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1763. #else // SLOW_PWM_HEATERS
  1764. /**
  1765. * SLOW PWM HEATERS
  1766. *
  1767. * For relay-driven heaters
  1768. */
  1769. #ifndef MIN_STATE_TIME
  1770. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1771. #endif
  1772. // Macros for Slow PWM timer logic
  1773. #define _SLOW_PWM_ROUTINE(NR, src) \
  1774. soft_pwm_count_ ##NR = src; \
  1775. if (soft_pwm_count_ ##NR > 0) { \
  1776. if (state_timer_heater_ ##NR == 0) { \
  1777. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1778. state_heater_ ##NR = 1; \
  1779. WRITE_HEATER_ ##NR(1); \
  1780. } \
  1781. } \
  1782. else { \
  1783. if (state_timer_heater_ ##NR == 0) { \
  1784. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1785. state_heater_ ##NR = 0; \
  1786. WRITE_HEATER_ ##NR(0); \
  1787. } \
  1788. }
  1789. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1790. #define PWM_OFF_ROUTINE(NR) \
  1791. if (soft_pwm_count_ ##NR < slow_pwm_count) { \
  1792. if (state_timer_heater_ ##NR == 0) { \
  1793. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1794. state_heater_ ##NR = 0; \
  1795. WRITE_HEATER_ ##NR (0); \
  1796. } \
  1797. }
  1798. if (slow_pwm_count == 0) {
  1799. SLOW_PWM_ROUTINE(0);
  1800. #if HOTENDS > 1
  1801. SLOW_PWM_ROUTINE(1);
  1802. #if HOTENDS > 2
  1803. SLOW_PWM_ROUTINE(2);
  1804. #if HOTENDS > 3
  1805. SLOW_PWM_ROUTINE(3);
  1806. #if HOTENDS > 4
  1807. SLOW_PWM_ROUTINE(4);
  1808. #endif // HOTENDS > 4
  1809. #endif // HOTENDS > 3
  1810. #endif // HOTENDS > 2
  1811. #endif // HOTENDS > 1
  1812. #if HAS_HEATED_BED
  1813. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1814. #endif
  1815. } // slow_pwm_count == 0
  1816. PWM_OFF_ROUTINE(0);
  1817. #if HOTENDS > 1
  1818. PWM_OFF_ROUTINE(1);
  1819. #if HOTENDS > 2
  1820. PWM_OFF_ROUTINE(2);
  1821. #if HOTENDS > 3
  1822. PWM_OFF_ROUTINE(3);
  1823. #if HOTENDS > 4
  1824. PWM_OFF_ROUTINE(4);
  1825. #endif // HOTENDS > 4
  1826. #endif // HOTENDS > 3
  1827. #endif // HOTENDS > 2
  1828. #endif // HOTENDS > 1
  1829. #if HAS_HEATED_BED
  1830. PWM_OFF_ROUTINE(BED); // BED
  1831. #endif
  1832. #if ENABLED(FAN_SOFT_PWM)
  1833. if (pwm_count_tmp >= 127) {
  1834. pwm_count_tmp = 0;
  1835. #if HAS_FAN0
  1836. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1837. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1838. #endif
  1839. #if HAS_FAN1
  1840. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1841. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1842. #endif
  1843. #if HAS_FAN2
  1844. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1845. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1846. #endif
  1847. }
  1848. #if HAS_FAN0
  1849. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1850. #endif
  1851. #if HAS_FAN1
  1852. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1853. #endif
  1854. #if HAS_FAN2
  1855. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1856. #endif
  1857. #endif // FAN_SOFT_PWM
  1858. // SOFT_PWM_SCALE to frequency:
  1859. //
  1860. // 0: 16000000/64/256/128 = 7.6294 Hz
  1861. // 1: / 64 = 15.2588 Hz
  1862. // 2: / 32 = 30.5176 Hz
  1863. // 3: / 16 = 61.0352 Hz
  1864. // 4: / 8 = 122.0703 Hz
  1865. // 5: / 4 = 244.1406 Hz
  1866. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1867. // increment slow_pwm_count only every 64th pwm_count,
  1868. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1869. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1870. slow_pwm_count++;
  1871. slow_pwm_count &= 0x7F;
  1872. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1873. #if HOTENDS > 1
  1874. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1875. #if HOTENDS > 2
  1876. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1877. #if HOTENDS > 3
  1878. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1879. #if HOTENDS > 4
  1880. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1881. #endif // HOTENDS > 4
  1882. #endif // HOTENDS > 3
  1883. #endif // HOTENDS > 2
  1884. #endif // HOTENDS > 1
  1885. #if HAS_HEATED_BED
  1886. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1887. #endif
  1888. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1889. #endif // SLOW_PWM_HEATERS
  1890. //
  1891. // Update lcd buttons 488 times per second
  1892. //
  1893. static bool do_buttons;
  1894. if ((do_buttons ^= true)) lcd_buttons_update();
  1895. /**
  1896. * One sensor is sampled on every other call of the ISR.
  1897. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1898. *
  1899. * On each Prepare pass, ADC is started for a sensor pin.
  1900. * On the next pass, the ADC value is read and accumulated.
  1901. *
  1902. * This gives each ADC 0.9765ms to charge up.
  1903. */
  1904. #define ACCUMULATE_ADC(var) do{ \
  1905. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
  1906. else var += HAL_READ_ADC(); \
  1907. }while(0)
  1908. ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
  1909. switch (adc_sensor_state) {
  1910. case SensorsReady: {
  1911. // All sensors have been read. Stay in this state for a few
  1912. // ISRs to save on calls to temp update/checking code below.
  1913. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  1914. static uint8_t delay_count = 0;
  1915. if (extra_loops > 0) {
  1916. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1917. if (--delay_count) // While delaying...
  1918. next_sensor_state = SensorsReady; // retain this state (else, next state will be 0)
  1919. break;
  1920. }
  1921. else {
  1922. adc_sensor_state = StartSampling; // Fall-through to start sampling
  1923. next_sensor_state = (ADCSensorState)(int(StartSampling) + 1);
  1924. }
  1925. }
  1926. case StartSampling: // Start of sampling loops. Do updates/checks.
  1927. if (++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1928. temp_count = 0;
  1929. readings_ready();
  1930. }
  1931. break;
  1932. #if HAS_TEMP_ADC_0
  1933. case PrepareTemp_0:
  1934. HAL_START_ADC(TEMP_0_PIN);
  1935. break;
  1936. case MeasureTemp_0:
  1937. ACCUMULATE_ADC(raw_temp_value[0]);
  1938. break;
  1939. #endif
  1940. #if HAS_HEATED_BED
  1941. case PrepareTemp_BED:
  1942. HAL_START_ADC(TEMP_BED_PIN);
  1943. break;
  1944. case MeasureTemp_BED:
  1945. ACCUMULATE_ADC(raw_temp_bed_value);
  1946. break;
  1947. #endif
  1948. #if HAS_TEMP_CHAMBER
  1949. case PrepareTemp_CHAMBER:
  1950. HAL_START_ADC(TEMP_CHAMBER_PIN);
  1951. break;
  1952. case MeasureTemp_CHAMBER:
  1953. ACCUMULATE_ADC(raw_temp_chamber_value);
  1954. break;
  1955. #endif
  1956. #if HAS_TEMP_ADC_1
  1957. case PrepareTemp_1:
  1958. HAL_START_ADC(TEMP_1_PIN);
  1959. break;
  1960. case MeasureTemp_1:
  1961. ACCUMULATE_ADC(raw_temp_value[1]);
  1962. break;
  1963. #endif
  1964. #if HAS_TEMP_ADC_2
  1965. case PrepareTemp_2:
  1966. HAL_START_ADC(TEMP_2_PIN);
  1967. break;
  1968. case MeasureTemp_2:
  1969. ACCUMULATE_ADC(raw_temp_value[2]);
  1970. break;
  1971. #endif
  1972. #if HAS_TEMP_ADC_3
  1973. case PrepareTemp_3:
  1974. HAL_START_ADC(TEMP_3_PIN);
  1975. break;
  1976. case MeasureTemp_3:
  1977. ACCUMULATE_ADC(raw_temp_value[3]);
  1978. break;
  1979. #endif
  1980. #if HAS_TEMP_ADC_4
  1981. case PrepareTemp_4:
  1982. HAL_START_ADC(TEMP_4_PIN);
  1983. break;
  1984. case MeasureTemp_4:
  1985. ACCUMULATE_ADC(raw_temp_value[4]);
  1986. break;
  1987. #endif
  1988. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1989. case Prepare_FILWIDTH:
  1990. HAL_START_ADC(FILWIDTH_PIN);
  1991. break;
  1992. case Measure_FILWIDTH:
  1993. if (!HAL_ADC_READY())
  1994. next_sensor_state = adc_sensor_state; // redo this state
  1995. else if (HAL_READ_ADC() > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1996. raw_filwidth_value -= raw_filwidth_value >> 7; // Subtract 1/128th of the raw_filwidth_value
  1997. raw_filwidth_value += uint32_t(HAL_READ_ADC()) << 7; // Add new ADC reading, scaled by 128
  1998. }
  1999. break;
  2000. #endif
  2001. #if ENABLED(ADC_KEYPAD)
  2002. case Prepare_ADC_KEY:
  2003. HAL_START_ADC(ADC_KEYPAD_PIN);
  2004. break;
  2005. case Measure_ADC_KEY:
  2006. if (!HAL_ADC_READY())
  2007. next_sensor_state = adc_sensor_state; // redo this state
  2008. else if (ADCKey_count < 16) {
  2009. raw_ADCKey_value = HAL_READ_ADC();
  2010. if (raw_ADCKey_value > 900) {
  2011. //ADC Key release
  2012. ADCKey_count = 0;
  2013. current_ADCKey_raw = 0;
  2014. }
  2015. else {
  2016. current_ADCKey_raw += raw_ADCKey_value;
  2017. ADCKey_count++;
  2018. }
  2019. }
  2020. break;
  2021. #endif // ADC_KEYPAD
  2022. case StartupDelay: break;
  2023. } // switch(adc_sensor_state)
  2024. // Go to the next state
  2025. adc_sensor_state = next_sensor_state;
  2026. //
  2027. // Additional ~1KHz Tasks
  2028. //
  2029. #if ENABLED(BABYSTEPPING)
  2030. LOOP_XYZ(axis) {
  2031. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  2032. if (curTodo) {
  2033. stepper.babystep((AxisEnum)axis, curTodo > 0);
  2034. if (curTodo > 0) babystepsTodo[axis]--;
  2035. else babystepsTodo[axis]++;
  2036. }
  2037. }
  2038. #endif // BABYSTEPPING
  2039. // Poll endstops state, if required
  2040. endstops.poll();
  2041. // Periodically call the planner timer
  2042. planner.tick();
  2043. }
  2044. #if HAS_TEMP_SENSOR
  2045. void print_heater_state(const float &c, const float &t,
  2046. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2047. const float r,
  2048. #endif
  2049. const int8_t e=-3
  2050. ) {
  2051. #if !(HAS_HEATED_BED && HAS_TEMP_HOTEND && HAS_TEMP_CHAMBER) && HOTENDS <= 1
  2052. UNUSED(e);
  2053. #endif
  2054. SERIAL_PROTOCOLCHAR(' ');
  2055. SERIAL_PROTOCOLCHAR(
  2056. #if HAS_TEMP_CHAMBER && HAS_HEATED_BED && HAS_TEMP_HOTEND
  2057. e == -2 ? 'C' : e == -1 ? 'B' : 'T'
  2058. #elif HAS_HEATED_BED && HAS_TEMP_HOTEND
  2059. e == -1 ? 'B' : 'T'
  2060. #elif HAS_TEMP_HOTEND
  2061. 'T'
  2062. #else
  2063. 'B'
  2064. #endif
  2065. );
  2066. #if HOTENDS > 1
  2067. if (e >= 0) SERIAL_PROTOCOLCHAR('0' + e);
  2068. #endif
  2069. SERIAL_PROTOCOLCHAR(':');
  2070. SERIAL_PROTOCOL(c);
  2071. SERIAL_PROTOCOLPAIR(" /" , t);
  2072. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2073. SERIAL_PROTOCOLPAIR(" (", r / OVERSAMPLENR);
  2074. SERIAL_PROTOCOLCHAR(')');
  2075. #endif
  2076. delay(2);
  2077. }
  2078. extern uint8_t target_extruder;
  2079. void Temperature::print_heaterstates() {
  2080. #if HAS_TEMP_HOTEND
  2081. print_heater_state(degHotend(target_extruder), degTargetHotend(target_extruder)
  2082. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2083. , rawHotendTemp(target_extruder)
  2084. #endif
  2085. );
  2086. #endif
  2087. #if HAS_HEATED_BED
  2088. print_heater_state(degBed(), degTargetBed()
  2089. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2090. , rawBedTemp()
  2091. #endif
  2092. , -1 // BED
  2093. );
  2094. #endif
  2095. #if HAS_TEMP_CHAMBER
  2096. print_heater_state(degChamber(), 0
  2097. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2098. , rawChamberTemp()
  2099. #endif
  2100. , -2 // CHAMBER
  2101. );
  2102. #endif
  2103. #if HOTENDS > 1
  2104. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2105. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2106. , rawHotendTemp(e)
  2107. #endif
  2108. , e
  2109. );
  2110. #endif
  2111. SERIAL_PROTOCOLPGM(" @:");
  2112. SERIAL_PROTOCOL(getHeaterPower(target_extruder));
  2113. #if HAS_HEATED_BED
  2114. SERIAL_PROTOCOLPGM(" B@:");
  2115. SERIAL_PROTOCOL(getHeaterPower(-1));
  2116. #endif
  2117. #if HOTENDS > 1
  2118. HOTEND_LOOP() {
  2119. SERIAL_PROTOCOLPAIR(" @", e);
  2120. SERIAL_PROTOCOLCHAR(':');
  2121. SERIAL_PROTOCOL(getHeaterPower(e));
  2122. }
  2123. #endif
  2124. }
  2125. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2126. uint8_t Temperature::auto_report_temp_interval;
  2127. millis_t Temperature::next_temp_report_ms;
  2128. void Temperature::auto_report_temperatures() {
  2129. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2130. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2131. print_heaterstates();
  2132. SERIAL_EOL();
  2133. }
  2134. }
  2135. #endif // AUTO_REPORT_TEMPERATURES
  2136. #endif // HAS_TEMP_SENSOR