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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "temperature.h"
  26. #include "endstops.h"
  27. #include "../MarlinCore.h"
  28. #include "planner.h"
  29. #include "../HAL/shared/Delay.h"
  30. #include "../lcd/ultralcd.h"
  31. #if ENABLED(DWIN_CREALITY_LCD)
  32. #include "../lcd/dwin/dwin.h"
  33. #endif
  34. #if ENABLED(EXTENSIBLE_UI)
  35. #include "../lcd/extui/ui_api.h"
  36. #endif
  37. #if ENABLED(MAX6675_IS_MAX31865)
  38. #include <Adafruit_MAX31865.h>
  39. #ifndef MAX31865_CS_PIN
  40. #define MAX31865_CS_PIN MAX6675_SS_PIN // HW:49 SW:65 for example
  41. #endif
  42. #ifndef MAX31865_MOSI_PIN
  43. #define MAX31865_MOSI_PIN MOSI_PIN // 63
  44. #endif
  45. #ifndef MAX31865_MISO_PIN
  46. #define MAX31865_MISO_PIN MAX6675_DO_PIN // 42
  47. #endif
  48. #ifndef MAX31865_SCK_PIN
  49. #define MAX31865_SCK_PIN MAX6675_SCK_PIN // 40
  50. #endif
  51. Adafruit_MAX31865 max31865 = Adafruit_MAX31865(MAX31865_CS_PIN
  52. #if MAX31865_CS_PIN != MAX6675_SS_PIN
  53. , MAX31865_MOSI_PIN // For software SPI also set MOSI/MISO/SCK
  54. , MAX31865_MISO_PIN
  55. , MAX31865_SCK_PIN
  56. #endif
  57. );
  58. #endif
  59. #define MAX6675_SEPARATE_SPI (EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675) && PINS_EXIST(MAX6675_SCK, MAX6675_DO))
  60. #if MAX6675_SEPARATE_SPI
  61. #include "../libs/private_spi.h"
  62. #endif
  63. #if ENABLED(PID_EXTRUSION_SCALING)
  64. #include "stepper.h"
  65. #endif
  66. #if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
  67. #include "../feature/babystep.h"
  68. #endif
  69. #include "printcounter.h"
  70. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  71. #include "../feature/filwidth.h"
  72. #endif
  73. #if HAS_POWER_MONITOR
  74. #include "../feature/power_monitor.h"
  75. #endif
  76. #if ENABLED(EMERGENCY_PARSER)
  77. #include "../feature/e_parser.h"
  78. #endif
  79. #if ENABLED(PRINTER_EVENT_LEDS)
  80. #include "../feature/leds/printer_event_leds.h"
  81. #endif
  82. #if ENABLED(JOYSTICK)
  83. #include "../feature/joystick.h"
  84. #endif
  85. #if ENABLED(SINGLENOZZLE)
  86. #include "tool_change.h"
  87. #endif
  88. #if USE_BEEPER
  89. #include "../libs/buzzer.h"
  90. #endif
  91. #if HOTEND_USES_THERMISTOR
  92. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  93. static const temp_entry_t* heater_ttbl_map[2] = { HEATER_0_TEMPTABLE, HEATER_1_TEMPTABLE };
  94. static constexpr uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  95. #else
  96. #define NEXT_TEMPTABLE(N) ,HEATER_##N##_TEMPTABLE
  97. #define NEXT_TEMPTABLE_LEN(N) ,HEATER_##N##_TEMPTABLE_LEN
  98. static const temp_entry_t* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE));
  99. static constexpr uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE_LEN));
  100. #endif
  101. #endif
  102. Temperature thermalManager;
  103. const char str_t_thermal_runaway[] PROGMEM = STR_T_THERMAL_RUNAWAY,
  104. str_t_heating_failed[] PROGMEM = STR_T_HEATING_FAILED;
  105. /**
  106. * Macros to include the heater id in temp errors. The compiler's dead-code
  107. * elimination should (hopefully) optimize out the unused strings.
  108. */
  109. #if HAS_HEATED_BED
  110. #define _BED_PSTR(h) (h) == H_BED ? GET_TEXT(MSG_BED) :
  111. #else
  112. #define _BED_PSTR(h)
  113. #endif
  114. #if HAS_HEATED_CHAMBER
  115. #define _CHAMBER_PSTR(h) (h) == H_CHAMBER ? GET_TEXT(MSG_CHAMBER) :
  116. #else
  117. #define _CHAMBER_PSTR(h)
  118. #endif
  119. #define _E_PSTR(h,N) ((HOTENDS) > N && (h) == N) ? PSTR(LCD_STR_E##N) :
  120. #define HEATER_PSTR(h) _BED_PSTR(h) _CHAMBER_PSTR(h) _E_PSTR(h,1) _E_PSTR(h,2) _E_PSTR(h,3) _E_PSTR(h,4) _E_PSTR(h,5) PSTR(LCD_STR_E0)
  121. // public:
  122. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  123. bool Temperature::adaptive_fan_slowing = true;
  124. #endif
  125. #if HAS_HOTEND
  126. hotend_info_t Temperature::temp_hotend[HOTEND_TEMPS]; // = { 0 }
  127. const uint16_t Temperature::heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP, HEATER_6_MAXTEMP, HEATER_7_MAXTEMP);
  128. #endif
  129. #if ENABLED(AUTO_POWER_E_FANS)
  130. uint8_t Temperature::autofan_speed[HOTENDS]; // = { 0 }
  131. #endif
  132. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  133. uint8_t Temperature::chamberfan_speed; // = 0
  134. #endif
  135. #if HAS_FAN
  136. uint8_t Temperature::fan_speed[FAN_COUNT]; // = { 0 }
  137. #if ENABLED(EXTRA_FAN_SPEED)
  138. uint8_t Temperature::old_fan_speed[FAN_COUNT], Temperature::new_fan_speed[FAN_COUNT];
  139. void Temperature::set_temp_fan_speed(const uint8_t fan, const uint16_t tmp_temp) {
  140. switch (tmp_temp) {
  141. case 1:
  142. set_fan_speed(fan, old_fan_speed[fan]);
  143. break;
  144. case 2:
  145. old_fan_speed[fan] = fan_speed[fan];
  146. set_fan_speed(fan, new_fan_speed[fan]);
  147. break;
  148. default:
  149. new_fan_speed[fan] = _MIN(tmp_temp, 255U);
  150. break;
  151. }
  152. }
  153. #endif
  154. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  155. bool Temperature::fans_paused; // = false;
  156. uint8_t Temperature::saved_fan_speed[FAN_COUNT]; // = { 0 }
  157. #endif
  158. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  159. uint8_t Temperature::fan_speed_scaler[FAN_COUNT] = ARRAY_N(FAN_COUNT, 128, 128, 128, 128, 128, 128);
  160. #endif
  161. /**
  162. * Set the print fan speed for a target extruder
  163. */
  164. void Temperature::set_fan_speed(uint8_t target, uint16_t speed) {
  165. NOMORE(speed, 255U);
  166. #if ENABLED(SINGLENOZZLE_STANDBY_FAN)
  167. if (target != active_extruder) {
  168. if (target < EXTRUDERS) singlenozzle_fan_speed[target] = speed;
  169. return;
  170. }
  171. target = 0; // Always use fan index 0 with SINGLENOZZLE
  172. #endif
  173. if (target >= FAN_COUNT) return;
  174. fan_speed[target] = speed;
  175. report_fan_speed(target);
  176. }
  177. /**
  178. * Report print fan speed for a target extruder
  179. */
  180. void Temperature::report_fan_speed(const uint8_t target) {
  181. if (target >= FAN_COUNT) return;
  182. PORT_REDIRECT(SERIAL_BOTH);
  183. SERIAL_ECHOLNPAIR("M106 P", target, " S", fan_speed[target]);
  184. }
  185. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  186. void Temperature::set_fans_paused(const bool p) {
  187. if (p != fans_paused) {
  188. fans_paused = p;
  189. if (p)
  190. FANS_LOOP(i) { saved_fan_speed[i] = fan_speed[i]; fan_speed[i] = 0; }
  191. else
  192. FANS_LOOP(i) fan_speed[i] = saved_fan_speed[i];
  193. }
  194. }
  195. #endif
  196. #endif // HAS_FAN
  197. #if WATCH_HOTENDS
  198. hotend_watch_t Temperature::watch_hotend[HOTENDS]; // = { { 0 } }
  199. #endif
  200. #if HEATER_IDLE_HANDLER
  201. hotend_idle_t Temperature::hotend_idle[HOTENDS]; // = { { 0 } }
  202. #endif
  203. #if HAS_HEATED_BED
  204. bed_info_t Temperature::temp_bed; // = { 0 }
  205. // Init min and max temp with extreme values to prevent false errors during startup
  206. #ifdef BED_MINTEMP
  207. int16_t Temperature::mintemp_raw_BED = HEATER_BED_RAW_LO_TEMP;
  208. #endif
  209. #ifdef BED_MAXTEMP
  210. int16_t Temperature::maxtemp_raw_BED = HEATER_BED_RAW_HI_TEMP;
  211. #endif
  212. TERN_(WATCH_BED, bed_watch_t Temperature::watch_bed); // = { 0 }
  213. TERN(PIDTEMPBED,, millis_t Temperature::next_bed_check_ms);
  214. TERN_(HEATER_IDLE_HANDLER, hotend_idle_t Temperature::bed_idle); // = { 0 }
  215. #endif // HAS_HEATED_BED
  216. #if HAS_TEMP_CHAMBER
  217. chamber_info_t Temperature::temp_chamber; // = { 0 }
  218. #if HAS_HEATED_CHAMBER
  219. #ifdef CHAMBER_MINTEMP
  220. int16_t Temperature::mintemp_raw_CHAMBER = HEATER_CHAMBER_RAW_LO_TEMP;
  221. #endif
  222. #ifdef CHAMBER_MAXTEMP
  223. int16_t Temperature::maxtemp_raw_CHAMBER = HEATER_CHAMBER_RAW_HI_TEMP;
  224. #endif
  225. #if WATCH_CHAMBER
  226. chamber_watch_t Temperature::watch_chamber{0};
  227. #endif
  228. millis_t Temperature::next_chamber_check_ms;
  229. #endif // HAS_HEATED_CHAMBER
  230. #endif // HAS_TEMP_CHAMBER
  231. #if HAS_TEMP_PROBE
  232. probe_info_t Temperature::temp_probe; // = { 0 }
  233. #endif
  234. // Initialized by settings.load()
  235. #if ENABLED(PIDTEMP)
  236. //hotend_pid_t Temperature::pid[HOTENDS];
  237. #endif
  238. #if ENABLED(PREVENT_COLD_EXTRUSION)
  239. bool Temperature::allow_cold_extrude = false;
  240. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  241. #endif
  242. // private:
  243. #if EARLY_WATCHDOG
  244. bool Temperature::inited = false;
  245. #endif
  246. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  247. uint16_t Temperature::redundant_temperature_raw = 0;
  248. float Temperature::redundant_temperature = 0.0;
  249. #endif
  250. volatile bool Temperature::raw_temps_ready = false;
  251. #if ENABLED(PID_EXTRUSION_SCALING)
  252. int32_t Temperature::last_e_position, Temperature::lpq[LPQ_MAX_LEN];
  253. lpq_ptr_t Temperature::lpq_ptr = 0;
  254. #endif
  255. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) < (HEATER_##N##_RAW_HI_TEMP) ? 1 : -1)
  256. #if HAS_HOTEND
  257. // Init mintemp and maxtemp with extreme values to prevent false errors during startup
  258. constexpr temp_range_t sensor_heater_0 { HEATER_0_RAW_LO_TEMP, HEATER_0_RAW_HI_TEMP, 0, 16383 },
  259. sensor_heater_1 { HEATER_1_RAW_LO_TEMP, HEATER_1_RAW_HI_TEMP, 0, 16383 },
  260. sensor_heater_2 { HEATER_2_RAW_LO_TEMP, HEATER_2_RAW_HI_TEMP, 0, 16383 },
  261. sensor_heater_3 { HEATER_3_RAW_LO_TEMP, HEATER_3_RAW_HI_TEMP, 0, 16383 },
  262. sensor_heater_4 { HEATER_4_RAW_LO_TEMP, HEATER_4_RAW_HI_TEMP, 0, 16383 },
  263. sensor_heater_5 { HEATER_5_RAW_LO_TEMP, HEATER_5_RAW_HI_TEMP, 0, 16383 },
  264. sensor_heater_6 { HEATER_6_RAW_LO_TEMP, HEATER_6_RAW_HI_TEMP, 0, 16383 },
  265. sensor_heater_7 { HEATER_7_RAW_LO_TEMP, HEATER_7_RAW_HI_TEMP, 0, 16383 };
  266. temp_range_t Temperature::temp_range[HOTENDS] = ARRAY_BY_HOTENDS(sensor_heater_0, sensor_heater_1, sensor_heater_2, sensor_heater_3, sensor_heater_4, sensor_heater_5, sensor_heater_6, sensor_heater_7);
  267. #endif
  268. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  269. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  270. #endif
  271. #ifdef MILLISECONDS_PREHEAT_TIME
  272. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  273. #endif
  274. #if HAS_AUTO_FAN
  275. millis_t Temperature::next_auto_fan_check_ms = 0;
  276. #endif
  277. #if ENABLED(FAN_SOFT_PWM)
  278. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  279. Temperature::soft_pwm_count_fan[FAN_COUNT];
  280. #endif
  281. #if ENABLED(PROBING_HEATERS_OFF)
  282. bool Temperature::paused;
  283. #endif
  284. // public:
  285. #if HAS_ADC_BUTTONS
  286. uint32_t Temperature::current_ADCKey_raw = HAL_ADC_RANGE;
  287. uint16_t Temperature::ADCKey_count = 0;
  288. #endif
  289. #if ENABLED(PID_EXTRUSION_SCALING)
  290. int16_t Temperature::lpq_len; // Initialized in configuration_store
  291. #endif
  292. #if HAS_PID_HEATING
  293. inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); }
  294. /**
  295. * PID Autotuning (M303)
  296. *
  297. * Alternately heat and cool the nozzle, observing its behavior to
  298. * determine the best PID values to achieve a stable temperature.
  299. * Needs sufficient heater power to make some overshoot at target
  300. * temperature to succeed.
  301. */
  302. void Temperature::PID_autotune(const float &target, const heater_ind_t heater, const int8_t ncycles, const bool set_result/*=false*/) {
  303. float current_temp = 0.0;
  304. int cycles = 0;
  305. bool heating = true;
  306. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  307. long t_high = 0, t_low = 0;
  308. long bias, d;
  309. PID_t tune_pid = { 0, 0, 0 };
  310. float maxT = 0, minT = 10000;
  311. const bool isbed = (heater == H_BED);
  312. #if HAS_PID_FOR_BOTH
  313. #define GHV(B,H) (isbed ? (B) : (H))
  314. #define SHV(B,H) do{ if (isbed) temp_bed.soft_pwm_amount = B; else temp_hotend[heater].soft_pwm_amount = H; }while(0)
  315. #define ONHEATINGSTART() (isbed ? printerEventLEDs.onBedHeatingStart() : printerEventLEDs.onHotendHeatingStart())
  316. #define ONHEATING(S,C,T) (isbed ? printerEventLEDs.onBedHeating(S,C,T) : printerEventLEDs.onHotendHeating(S,C,T))
  317. #elif ENABLED(PIDTEMPBED)
  318. #define GHV(B,H) B
  319. #define SHV(B,H) (temp_bed.soft_pwm_amount = B)
  320. #define ONHEATINGSTART() printerEventLEDs.onBedHeatingStart()
  321. #define ONHEATING(S,C,T) printerEventLEDs.onBedHeating(S,C,T)
  322. #else
  323. #define GHV(B,H) H
  324. #define SHV(B,H) (temp_hotend[heater].soft_pwm_amount = H)
  325. #define ONHEATINGSTART() printerEventLEDs.onHotendHeatingStart()
  326. #define ONHEATING(S,C,T) printerEventLEDs.onHotendHeating(S,C,T)
  327. #endif
  328. #define WATCH_PID BOTH(WATCH_BED, PIDTEMPBED) || BOTH(WATCH_HOTENDS, PIDTEMP)
  329. #if WATCH_PID
  330. #if ALL(THERMAL_PROTECTION_HOTENDS, PIDTEMP, THERMAL_PROTECTION_BED, PIDTEMPBED)
  331. #define GTV(B,H) (isbed ? (B) : (H))
  332. #elif BOTH(THERMAL_PROTECTION_HOTENDS, PIDTEMP)
  333. #define GTV(B,H) (H)
  334. #else
  335. #define GTV(B,H) (B)
  336. #endif
  337. const uint16_t watch_temp_period = GTV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
  338. const uint8_t watch_temp_increase = GTV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
  339. const float watch_temp_target = target - float(watch_temp_increase + GTV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1);
  340. millis_t temp_change_ms = next_temp_ms + SEC_TO_MS(watch_temp_period);
  341. float next_watch_temp = 0.0;
  342. bool heated = false;
  343. #endif
  344. TERN_(HAS_AUTO_FAN, next_auto_fan_check_ms = next_temp_ms + 2500UL);
  345. if (target > GHV(BED_MAX_TARGET, temp_range[heater].maxtemp - HOTEND_OVERSHOOT)) {
  346. SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
  347. TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH));
  348. return;
  349. }
  350. SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_START);
  351. disable_all_heaters();
  352. SHV(bias = d = (MAX_BED_POWER) >> 1, bias = d = (PID_MAX) >> 1);
  353. wait_for_heatup = true; // Can be interrupted with M108
  354. #if ENABLED(PRINTER_EVENT_LEDS)
  355. const float start_temp = GHV(temp_bed.celsius, temp_hotend[heater].celsius);
  356. LEDColor color = ONHEATINGSTART();
  357. #endif
  358. TERN_(NO_FAN_SLOWING_IN_PID_TUNING, adaptive_fan_slowing = false);
  359. // PID Tuning loop
  360. while (wait_for_heatup) {
  361. const millis_t ms = millis();
  362. if (raw_temps_ready) { // temp sample ready
  363. updateTemperaturesFromRawValues();
  364. // Get the current temperature and constrain it
  365. current_temp = GHV(temp_bed.celsius, temp_hotend[heater].celsius);
  366. NOLESS(maxT, current_temp);
  367. NOMORE(minT, current_temp);
  368. #if ENABLED(PRINTER_EVENT_LEDS)
  369. ONHEATING(start_temp, current_temp, target);
  370. #endif
  371. #if HAS_AUTO_FAN
  372. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  373. checkExtruderAutoFans();
  374. next_auto_fan_check_ms = ms + 2500UL;
  375. }
  376. #endif
  377. if (heating && current_temp > target) {
  378. if (ELAPSED(ms, t2 + 5000UL)) {
  379. heating = false;
  380. SHV((bias - d) >> 1, (bias - d) >> 1);
  381. t1 = ms;
  382. t_high = t1 - t2;
  383. maxT = target;
  384. }
  385. }
  386. if (!heating && current_temp < target) {
  387. if (ELAPSED(ms, t1 + 5000UL)) {
  388. heating = true;
  389. t2 = ms;
  390. t_low = t2 - t1;
  391. if (cycles > 0) {
  392. const long max_pow = GHV(MAX_BED_POWER, PID_MAX);
  393. bias += (d * (t_high - t_low)) / (t_low + t_high);
  394. LIMIT(bias, 20, max_pow - 20);
  395. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  396. SERIAL_ECHOPAIR(STR_BIAS, bias, STR_D_COLON, d, STR_T_MIN, minT, STR_T_MAX, maxT);
  397. if (cycles > 2) {
  398. const float Ku = (4.0f * d) / (float(M_PI) * (maxT - minT) * 0.5f),
  399. Tu = float(t_low + t_high) * 0.001f,
  400. pf = isbed ? 0.2f : 0.6f,
  401. df = isbed ? 1.0f / 3.0f : 1.0f / 8.0f;
  402. SERIAL_ECHOPAIR(STR_KU, Ku, STR_TU, Tu);
  403. if (isbed) { // Do not remove this otherwise PID autotune won't work right for the bed!
  404. tune_pid.Kp = Ku * 0.2f;
  405. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  406. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  407. SERIAL_ECHOLNPGM("\n" " No overshoot"); // Works far better for the bed. Classic and some have bad ringing.
  408. SERIAL_ECHOLNPAIR(STR_KP, tune_pid.Kp, STR_KI, tune_pid.Ki, STR_KD, tune_pid.Kd);
  409. }
  410. else {
  411. tune_pid.Kp = Ku * pf;
  412. tune_pid.Kd = tune_pid.Kp * Tu * df;
  413. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  414. SERIAL_ECHOLNPGM("\n" STR_CLASSIC_PID);
  415. SERIAL_ECHOLNPAIR(STR_KP, tune_pid.Kp, STR_KI, tune_pid.Ki, STR_KD, tune_pid.Kd);
  416. }
  417. /**
  418. tune_pid.Kp = 0.33 * Ku;
  419. tune_pid.Ki = tune_pid.Kp / Tu;
  420. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  421. SERIAL_ECHOLNPGM(" Some overshoot");
  422. SERIAL_ECHOLNPAIR(" Kp: ", tune_pid.Kp, " Ki: ", tune_pid.Ki, " Kd: ", tune_pid.Kd, " No overshoot");
  423. tune_pid.Kp = 0.2 * Ku;
  424. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  425. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  426. SERIAL_ECHOPAIR(" Kp: ", tune_pid.Kp, " Ki: ", tune_pid.Ki, " Kd: ", tune_pid.Kd);
  427. */
  428. }
  429. }
  430. SHV((bias + d) >> 1, (bias + d) >> 1);
  431. cycles++;
  432. minT = target;
  433. }
  434. }
  435. }
  436. // Did the temperature overshoot very far?
  437. #ifndef MAX_OVERSHOOT_PID_AUTOTUNE
  438. #define MAX_OVERSHOOT_PID_AUTOTUNE 30
  439. #endif
  440. if (current_temp > target + MAX_OVERSHOOT_PID_AUTOTUNE) {
  441. SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
  442. TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH));
  443. break;
  444. }
  445. // Report heater states every 2 seconds
  446. if (ELAPSED(ms, next_temp_ms)) {
  447. #if HAS_TEMP_SENSOR
  448. print_heater_states(isbed ? active_extruder : heater);
  449. SERIAL_EOL();
  450. #endif
  451. next_temp_ms = ms + 2000UL;
  452. // Make sure heating is actually working
  453. #if WATCH_PID
  454. if (BOTH(WATCH_BED, WATCH_HOTENDS) || isbed == DISABLED(WATCH_HOTENDS)) {
  455. if (!heated) { // If not yet reached target...
  456. if (current_temp > next_watch_temp) { // Over the watch temp?
  457. next_watch_temp = current_temp + watch_temp_increase; // - set the next temp to watch for
  458. temp_change_ms = ms + SEC_TO_MS(watch_temp_period); // - move the expiration timer up
  459. if (current_temp > watch_temp_target) heated = true; // - Flag if target temperature reached
  460. }
  461. else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
  462. _temp_error(heater, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  463. }
  464. else if (current_temp < target - (MAX_OVERSHOOT_PID_AUTOTUNE)) // Heated, then temperature fell too far?
  465. _temp_error(heater, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  466. }
  467. #endif
  468. } // every 2 seconds
  469. // Timeout after MAX_CYCLE_TIME_PID_AUTOTUNE minutes since the last undershoot/overshoot cycle
  470. #ifndef MAX_CYCLE_TIME_PID_AUTOTUNE
  471. #define MAX_CYCLE_TIME_PID_AUTOTUNE 20L
  472. #endif
  473. if ((ms - _MIN(t1, t2)) > (MAX_CYCLE_TIME_PID_AUTOTUNE * 60L * 1000L)) {
  474. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  475. TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TUNING_TIMEOUT));
  476. SERIAL_ECHOLNPGM(STR_PID_TIMEOUT);
  477. break;
  478. }
  479. if (cycles > ncycles && cycles > 2) {
  480. SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_FINISHED);
  481. #if HAS_PID_FOR_BOTH
  482. const char * const estring = GHV(PSTR("bed"), NUL_STR);
  483. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Kp ", tune_pid.Kp);
  484. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Ki ", tune_pid.Ki);
  485. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Kd ", tune_pid.Kd);
  486. #elif ENABLED(PIDTEMP)
  487. say_default_(); SERIAL_ECHOLNPAIR("Kp ", tune_pid.Kp);
  488. say_default_(); SERIAL_ECHOLNPAIR("Ki ", tune_pid.Ki);
  489. say_default_(); SERIAL_ECHOLNPAIR("Kd ", tune_pid.Kd);
  490. #else
  491. say_default_(); SERIAL_ECHOLNPAIR("bedKp ", tune_pid.Kp);
  492. say_default_(); SERIAL_ECHOLNPAIR("bedKi ", tune_pid.Ki);
  493. say_default_(); SERIAL_ECHOLNPAIR("bedKd ", tune_pid.Kd);
  494. #endif
  495. #define _SET_BED_PID() do { \
  496. temp_bed.pid.Kp = tune_pid.Kp; \
  497. temp_bed.pid.Ki = scalePID_i(tune_pid.Ki); \
  498. temp_bed.pid.Kd = scalePID_d(tune_pid.Kd); \
  499. }while(0)
  500. #define _SET_EXTRUDER_PID() do { \
  501. PID_PARAM(Kp, heater) = tune_pid.Kp; \
  502. PID_PARAM(Ki, heater) = scalePID_i(tune_pid.Ki); \
  503. PID_PARAM(Kd, heater) = scalePID_d(tune_pid.Kd); \
  504. updatePID(); }while(0)
  505. // Use the result? (As with "M303 U1")
  506. if (set_result) {
  507. #if HAS_PID_FOR_BOTH
  508. if (isbed) _SET_BED_PID(); else _SET_EXTRUDER_PID();
  509. #elif ENABLED(PIDTEMP)
  510. _SET_EXTRUDER_PID();
  511. #else
  512. _SET_BED_PID();
  513. #endif
  514. }
  515. TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
  516. TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
  517. goto EXIT_M303;
  518. }
  519. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  520. }
  521. disable_all_heaters();
  522. TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
  523. TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
  524. EXIT_M303:
  525. TERN_(NO_FAN_SLOWING_IN_PID_TUNING, adaptive_fan_slowing = true);
  526. return;
  527. }
  528. #endif // HAS_PID_HEATING
  529. /**
  530. * Class and Instance Methods
  531. */
  532. int16_t Temperature::getHeaterPower(const heater_ind_t heater_id) {
  533. switch (heater_id) {
  534. #if HAS_HEATED_BED
  535. case H_BED: return temp_bed.soft_pwm_amount;
  536. #endif
  537. #if HAS_HEATED_CHAMBER
  538. case H_CHAMBER: return temp_chamber.soft_pwm_amount;
  539. #endif
  540. default:
  541. return TERN0(HAS_HOTEND, temp_hotend[heater_id].soft_pwm_amount);
  542. }
  543. }
  544. #define _EFANOVERLAP(A,B) _FANOVERLAP(E##A,B)
  545. #if HAS_AUTO_FAN
  546. #define CHAMBER_FAN_INDEX HOTENDS
  547. void Temperature::checkExtruderAutoFans() {
  548. #define _EFAN(B,A) _EFANOVERLAP(A,B) ? B :
  549. static const uint8_t fanBit[] PROGMEM = {
  550. 0
  551. #if HAS_MULTI_HOTEND
  552. #define _NEXT_FAN(N) , REPEAT2(N,_EFAN,N) N
  553. RREPEAT_S(1, HOTENDS, _NEXT_FAN)
  554. #endif
  555. #if HAS_AUTO_CHAMBER_FAN
  556. #define _CFAN(B) _FANOVERLAP(CHAMBER,B) ? B :
  557. , REPEAT(HOTENDS,_CFAN) (HOTENDS)
  558. #endif
  559. };
  560. uint8_t fanState = 0;
  561. HOTEND_LOOP()
  562. if (temp_hotend[e].celsius >= EXTRUDER_AUTO_FAN_TEMPERATURE)
  563. SBI(fanState, pgm_read_byte(&fanBit[e]));
  564. #if HAS_AUTO_CHAMBER_FAN
  565. if (temp_chamber.celsius >= CHAMBER_AUTO_FAN_TEMPERATURE)
  566. SBI(fanState, pgm_read_byte(&fanBit[CHAMBER_FAN_INDEX]));
  567. #endif
  568. #define _UPDATE_AUTO_FAN(P,D,A) do{ \
  569. if (PWM_PIN(P##_AUTO_FAN_PIN) && A < 255) \
  570. analogWrite(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
  571. else \
  572. WRITE(P##_AUTO_FAN_PIN, D); \
  573. }while(0)
  574. uint8_t fanDone = 0;
  575. LOOP_L_N(f, COUNT(fanBit)) {
  576. const uint8_t realFan = pgm_read_byte(&fanBit[f]);
  577. if (TEST(fanDone, realFan)) continue;
  578. const bool fan_on = TEST(fanState, realFan);
  579. switch (f) {
  580. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  581. case CHAMBER_FAN_INDEX:
  582. chamberfan_speed = fan_on ? CHAMBER_AUTO_FAN_SPEED : 0;
  583. break;
  584. #endif
  585. default:
  586. #if ENABLED(AUTO_POWER_E_FANS)
  587. autofan_speed[realFan] = fan_on ? EXTRUDER_AUTO_FAN_SPEED : 0;
  588. #endif
  589. break;
  590. }
  591. switch (f) {
  592. #if HAS_AUTO_FAN_0
  593. case 0: _UPDATE_AUTO_FAN(E0, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  594. #endif
  595. #if HAS_AUTO_FAN_1
  596. case 1: _UPDATE_AUTO_FAN(E1, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  597. #endif
  598. #if HAS_AUTO_FAN_2
  599. case 2: _UPDATE_AUTO_FAN(E2, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  600. #endif
  601. #if HAS_AUTO_FAN_3
  602. case 3: _UPDATE_AUTO_FAN(E3, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  603. #endif
  604. #if HAS_AUTO_FAN_4
  605. case 4: _UPDATE_AUTO_FAN(E4, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  606. #endif
  607. #if HAS_AUTO_FAN_5
  608. case 5: _UPDATE_AUTO_FAN(E5, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  609. #endif
  610. #if HAS_AUTO_FAN_6
  611. case 6: _UPDATE_AUTO_FAN(E6, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  612. #endif
  613. #if HAS_AUTO_FAN_7
  614. case 7: _UPDATE_AUTO_FAN(E7, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  615. #endif
  616. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  617. case CHAMBER_FAN_INDEX: _UPDATE_AUTO_FAN(CHAMBER, fan_on, CHAMBER_AUTO_FAN_SPEED); break;
  618. #endif
  619. }
  620. SBI(fanDone, realFan);
  621. }
  622. }
  623. #endif // HAS_AUTO_FAN
  624. //
  625. // Temperature Error Handlers
  626. //
  627. inline void loud_kill(PGM_P const lcd_msg, const heater_ind_t heater) {
  628. marlin_state = MF_KILLED;
  629. #if USE_BEEPER
  630. for (uint8_t i = 20; i--;) {
  631. WRITE(BEEPER_PIN, HIGH); delay(25);
  632. WRITE(BEEPER_PIN, LOW); delay(80);
  633. }
  634. WRITE(BEEPER_PIN, HIGH);
  635. #endif
  636. kill(lcd_msg, HEATER_PSTR(heater));
  637. }
  638. void Temperature::_temp_error(const heater_ind_t heater, PGM_P const serial_msg, PGM_P const lcd_msg) {
  639. static uint8_t killed = 0;
  640. if (IsRunning() && TERN1(BOGUS_TEMPERATURE_GRACE_PERIOD, killed == 2)) {
  641. SERIAL_ERROR_START();
  642. serialprintPGM(serial_msg);
  643. SERIAL_ECHOPGM(STR_STOPPED_HEATER);
  644. if (heater >= 0)
  645. SERIAL_ECHO((int)heater);
  646. else if (TERN0(HAS_HEATED_CHAMBER, heater == H_CHAMBER))
  647. SERIAL_ECHOPGM(STR_HEATER_CHAMBER);
  648. else
  649. SERIAL_ECHOPGM(STR_HEATER_BED);
  650. SERIAL_EOL();
  651. }
  652. disable_all_heaters(); // always disable (even for bogus temp)
  653. #if BOGUS_TEMPERATURE_GRACE_PERIOD
  654. const millis_t ms = millis();
  655. static millis_t expire_ms;
  656. switch (killed) {
  657. case 0:
  658. expire_ms = ms + BOGUS_TEMPERATURE_GRACE_PERIOD;
  659. ++killed;
  660. break;
  661. case 1:
  662. if (ELAPSED(ms, expire_ms)) ++killed;
  663. break;
  664. case 2:
  665. loud_kill(lcd_msg, heater);
  666. ++killed;
  667. break;
  668. }
  669. #elif defined(BOGUS_TEMPERATURE_GRACE_PERIOD)
  670. UNUSED(killed);
  671. #else
  672. if (!killed) { killed = 1; loud_kill(lcd_msg, heater); }
  673. #endif
  674. }
  675. void Temperature::max_temp_error(const heater_ind_t heater) {
  676. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(1));
  677. _temp_error(heater, PSTR(STR_T_MAXTEMP), GET_TEXT(MSG_ERR_MAXTEMP));
  678. }
  679. void Temperature::min_temp_error(const heater_ind_t heater) {
  680. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  681. _temp_error(heater, PSTR(STR_T_MINTEMP), GET_TEXT(MSG_ERR_MINTEMP));
  682. }
  683. #if HAS_HOTEND
  684. #if ENABLED(PID_DEBUG)
  685. extern bool pid_debug_flag;
  686. #endif
  687. float Temperature::get_pid_output_hotend(const uint8_t E_NAME) {
  688. const uint8_t ee = HOTEND_INDEX;
  689. #if ENABLED(PIDTEMP)
  690. #if DISABLED(PID_OPENLOOP)
  691. static hotend_pid_t work_pid[HOTENDS];
  692. static float temp_iState[HOTENDS] = { 0 },
  693. temp_dState[HOTENDS] = { 0 };
  694. static bool pid_reset[HOTENDS] = { false };
  695. const float pid_error = temp_hotend[ee].target - temp_hotend[ee].celsius;
  696. float pid_output;
  697. if (temp_hotend[ee].target == 0
  698. || pid_error < -(PID_FUNCTIONAL_RANGE)
  699. || TERN0(HEATER_IDLE_HANDLER, hotend_idle[ee].timed_out)
  700. ) {
  701. pid_output = 0;
  702. pid_reset[ee] = true;
  703. }
  704. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  705. pid_output = BANG_MAX;
  706. pid_reset[ee] = true;
  707. }
  708. else {
  709. if (pid_reset[ee]) {
  710. temp_iState[ee] = 0.0;
  711. work_pid[ee].Kd = 0.0;
  712. pid_reset[ee] = false;
  713. }
  714. work_pid[ee].Kd = work_pid[ee].Kd + PID_K2 * (PID_PARAM(Kd, ee) * (temp_dState[ee] - temp_hotend[ee].celsius) - work_pid[ee].Kd);
  715. const float max_power_over_i_gain = float(PID_MAX) / PID_PARAM(Ki, ee) - float(MIN_POWER);
  716. temp_iState[ee] = constrain(temp_iState[ee] + pid_error, 0, max_power_over_i_gain);
  717. work_pid[ee].Kp = PID_PARAM(Kp, ee) * pid_error;
  718. work_pid[ee].Ki = PID_PARAM(Ki, ee) * temp_iState[ee];
  719. pid_output = work_pid[ee].Kp + work_pid[ee].Ki + work_pid[ee].Kd + float(MIN_POWER);
  720. #if ENABLED(PID_EXTRUSION_SCALING)
  721. #if HOTENDS == 1
  722. constexpr bool this_hotend = true;
  723. #else
  724. const bool this_hotend = (ee == active_extruder);
  725. #endif
  726. work_pid[ee].Kc = 0;
  727. if (this_hotend) {
  728. const long e_position = stepper.position(E_AXIS);
  729. if (e_position > last_e_position) {
  730. lpq[lpq_ptr] = e_position - last_e_position;
  731. last_e_position = e_position;
  732. }
  733. else
  734. lpq[lpq_ptr] = 0;
  735. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  736. work_pid[ee].Kc = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, ee);
  737. pid_output += work_pid[ee].Kc;
  738. }
  739. #endif // PID_EXTRUSION_SCALING
  740. #if ENABLED(PID_FAN_SCALING)
  741. if (thermalManager.fan_speed[active_extruder] > PID_FAN_SCALING_MIN_SPEED) {
  742. work_pid[ee].Kf = PID_PARAM(Kf, ee) + (PID_FAN_SCALING_LIN_FACTOR) * thermalManager.fan_speed[active_extruder];
  743. pid_output += work_pid[ee].Kf;
  744. }
  745. //pid_output -= work_pid[ee].Ki;
  746. //pid_output += work_pid[ee].Ki * work_pid[ee].Kf
  747. #endif // PID_FAN_SCALING
  748. LIMIT(pid_output, 0, PID_MAX);
  749. }
  750. temp_dState[ee] = temp_hotend[ee].celsius;
  751. #else // PID_OPENLOOP
  752. const float pid_output = constrain(temp_hotend[ee].target, 0, PID_MAX);
  753. #endif // PID_OPENLOOP
  754. #if ENABLED(PID_DEBUG)
  755. if (ee == active_extruder && pid_debug_flag) {
  756. SERIAL_ECHO_START();
  757. SERIAL_ECHOPAIR(STR_PID_DEBUG, ee, STR_PID_DEBUG_INPUT, temp_hotend[ee].celsius, STR_PID_DEBUG_OUTPUT, pid_output);
  758. #if DISABLED(PID_OPENLOOP)
  759. {
  760. SERIAL_ECHOPAIR( STR_PID_DEBUG_PTERM, work_pid[ee].Kp, STR_PID_DEBUG_ITERM, work_pid[ee].Ki, STR_PID_DEBUG_DTERM, work_pid[ee].Kd
  761. #if ENABLED(PID_EXTRUSION_SCALING)
  762. , STR_PID_DEBUG_CTERM, work_pid[ee].Kc
  763. #endif
  764. );
  765. }
  766. #endif
  767. SERIAL_EOL();
  768. }
  769. #endif // PID_DEBUG
  770. #else // No PID enabled
  771. const bool is_idling = TERN0(HEATER_IDLE_HANDLER, hotend_idle[ee].timed_out);
  772. const float pid_output = (!is_idling && temp_hotend[ee].celsius < temp_hotend[ee].target) ? BANG_MAX : 0;
  773. #endif
  774. return pid_output;
  775. }
  776. #endif // HAS_HOTEND
  777. #if ENABLED(PIDTEMPBED)
  778. float Temperature::get_pid_output_bed() {
  779. #if DISABLED(PID_OPENLOOP)
  780. static PID_t work_pid{0};
  781. static float temp_iState = 0, temp_dState = 0;
  782. static bool pid_reset = true;
  783. float pid_output = 0;
  784. const float max_power_over_i_gain = float(MAX_BED_POWER) / temp_bed.pid.Ki - float(MIN_BED_POWER),
  785. pid_error = temp_bed.target - temp_bed.celsius;
  786. if (!temp_bed.target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
  787. pid_output = 0;
  788. pid_reset = true;
  789. }
  790. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  791. pid_output = MAX_BED_POWER;
  792. pid_reset = true;
  793. }
  794. else {
  795. if (pid_reset) {
  796. temp_iState = 0.0;
  797. work_pid.Kd = 0.0;
  798. pid_reset = false;
  799. }
  800. temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
  801. work_pid.Kp = temp_bed.pid.Kp * pid_error;
  802. work_pid.Ki = temp_bed.pid.Ki * temp_iState;
  803. work_pid.Kd = work_pid.Kd + PID_K2 * (temp_bed.pid.Kd * (temp_dState - temp_bed.celsius) - work_pid.Kd);
  804. temp_dState = temp_bed.celsius;
  805. pid_output = constrain(work_pid.Kp + work_pid.Ki + work_pid.Kd + float(MIN_BED_POWER), 0, MAX_BED_POWER);
  806. }
  807. #else // PID_OPENLOOP
  808. const float pid_output = constrain(temp_bed.target, 0, MAX_BED_POWER);
  809. #endif // PID_OPENLOOP
  810. #if ENABLED(PID_BED_DEBUG)
  811. {
  812. SERIAL_ECHO_START();
  813. SERIAL_ECHOLNPAIR(
  814. " PID_BED_DEBUG : Input ", temp_bed.celsius, " Output ", pid_output,
  815. #if DISABLED(PID_OPENLOOP)
  816. STR_PID_DEBUG_PTERM, work_pid.Kp,
  817. STR_PID_DEBUG_ITERM, work_pid.Ki,
  818. STR_PID_DEBUG_DTERM, work_pid.Kd,
  819. #endif
  820. );
  821. }
  822. #endif
  823. return pid_output;
  824. }
  825. #endif // PIDTEMPBED
  826. /**
  827. * Manage heating activities for extruder hot-ends and a heated bed
  828. * - Acquire updated temperature readings
  829. * - Also resets the watchdog timer
  830. * - Invoke thermal runaway protection
  831. * - Manage extruder auto-fan
  832. * - Apply filament width to the extrusion rate (may move)
  833. * - Update the heated bed PID output value
  834. */
  835. void Temperature::manage_heater() {
  836. #if EARLY_WATCHDOG
  837. // If thermal manager is still not running, make sure to at least reset the watchdog!
  838. if (!inited) return watchdog_refresh();
  839. #endif
  840. if (TERN0(EMERGENCY_PARSER, emergency_parser.killed_by_M112))
  841. kill(M112_KILL_STR, nullptr, true);
  842. if (!raw_temps_ready) return;
  843. updateTemperaturesFromRawValues(); // also resets the watchdog
  844. #if ENABLED(HEATER_0_USES_MAX6675)
  845. if (temp_hotend[0].celsius > _MIN(HEATER_0_MAXTEMP, HEATER_0_MAX6675_TMAX - 1.0)) max_temp_error(H_E0);
  846. if (temp_hotend[0].celsius < _MAX(HEATER_0_MINTEMP, HEATER_0_MAX6675_TMIN + .01)) min_temp_error(H_E0);
  847. #endif
  848. #if ENABLED(HEATER_1_USES_MAX6675)
  849. if (temp_hotend[1].celsius > _MIN(HEATER_1_MAXTEMP, HEATER_1_MAX6675_TMAX - 1.0)) max_temp_error(H_E1);
  850. if (temp_hotend[1].celsius < _MAX(HEATER_1_MINTEMP, HEATER_1_MAX6675_TMIN + .01)) min_temp_error(H_E1);
  851. #endif
  852. millis_t ms = millis();
  853. #if HAS_HOTEND
  854. HOTEND_LOOP() {
  855. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  856. if (degHotend(e) > temp_range[e].maxtemp)
  857. _temp_error((heater_ind_t)e, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  858. #endif
  859. TERN_(HEATER_IDLE_HANDLER, hotend_idle[e].update(ms));
  860. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  861. // Check for thermal runaway
  862. thermal_runaway_protection(tr_state_machine[e], temp_hotend[e].celsius, temp_hotend[e].target, (heater_ind_t)e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  863. #endif
  864. temp_hotend[e].soft_pwm_amount = (temp_hotend[e].celsius > temp_range[e].mintemp || is_preheating(e)) && temp_hotend[e].celsius < temp_range[e].maxtemp ? (int)get_pid_output_hotend(e) >> 1 : 0;
  865. #if WATCH_HOTENDS
  866. // Make sure temperature is increasing
  867. if (watch_hotend[e].next_ms && ELAPSED(ms, watch_hotend[e].next_ms)) { // Time to check this extruder?
  868. if (degHotend(e) < watch_hotend[e].target) { // Failed to increase enough?
  869. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  870. _temp_error((heater_ind_t)e, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  871. }
  872. else // Start again if the target is still far off
  873. start_watching_hotend(e);
  874. }
  875. #endif
  876. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  877. // Make sure measured temperatures are close together
  878. if (ABS(temp_hotend[0].celsius - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  879. _temp_error(H_E0, PSTR(STR_REDUNDANCY), GET_TEXT(MSG_ERR_REDUNDANT_TEMP));
  880. #endif
  881. } // HOTEND_LOOP
  882. #endif // HAS_HOTEND
  883. #if HAS_AUTO_FAN
  884. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  885. checkExtruderAutoFans();
  886. next_auto_fan_check_ms = ms + 2500UL;
  887. }
  888. #endif
  889. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  890. /**
  891. * Dynamically set the volumetric multiplier based
  892. * on the delayed Filament Width measurement.
  893. */
  894. filwidth.update_volumetric();
  895. #endif
  896. #if HAS_HEATED_BED
  897. #if ENABLED(THERMAL_PROTECTION_BED)
  898. if (degBed() > BED_MAXTEMP)
  899. _temp_error(H_BED, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  900. #endif
  901. #if WATCH_BED
  902. // Make sure temperature is increasing
  903. if (watch_bed.elapsed(ms)) { // Time to check the bed?
  904. if (degBed() < watch_bed.target) { // Failed to increase enough?
  905. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  906. _temp_error(H_BED, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  907. }
  908. else // Start again if the target is still far off
  909. start_watching_bed();
  910. }
  911. #endif // WATCH_BED
  912. #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  913. #define PAUSE_CHANGE_REQD 1
  914. #endif
  915. #if PAUSE_CHANGE_REQD
  916. static bool last_pause_state;
  917. #endif
  918. do {
  919. #if DISABLED(PIDTEMPBED)
  920. if (PENDING(ms, next_bed_check_ms)
  921. && TERN1(PAUSE_CHANGE_REQD, paused == last_pause_state)
  922. ) break;
  923. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  924. TERN_(PAUSE_CHANGE_REQD, last_pause_state = paused);
  925. #endif
  926. TERN_(HEATER_IDLE_HANDLER, bed_idle.update(ms));
  927. TERN_(HAS_THERMALLY_PROTECTED_BED, thermal_runaway_protection(tr_state_machine_bed, temp_bed.celsius, temp_bed.target, H_BED, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS));
  928. #if HEATER_IDLE_HANDLER
  929. if (bed_idle.timed_out) {
  930. temp_bed.soft_pwm_amount = 0;
  931. #if DISABLED(PIDTEMPBED)
  932. WRITE_HEATER_BED(LOW);
  933. #endif
  934. }
  935. else
  936. #endif
  937. {
  938. #if ENABLED(PIDTEMPBED)
  939. temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  940. #else
  941. // Check if temperature is within the correct band
  942. if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
  943. #if ENABLED(BED_LIMIT_SWITCHING)
  944. if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
  945. temp_bed.soft_pwm_amount = 0;
  946. else if (temp_bed.celsius <= temp_bed.target - (BED_HYSTERESIS))
  947. temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
  948. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  949. temp_bed.soft_pwm_amount = temp_bed.celsius < temp_bed.target ? MAX_BED_POWER >> 1 : 0;
  950. #endif
  951. }
  952. else {
  953. temp_bed.soft_pwm_amount = 0;
  954. WRITE_HEATER_BED(LOW);
  955. }
  956. #endif
  957. }
  958. } while (false);
  959. #endif // HAS_HEATED_BED
  960. #if HAS_HEATED_CHAMBER
  961. #ifndef CHAMBER_CHECK_INTERVAL
  962. #define CHAMBER_CHECK_INTERVAL 1000UL
  963. #endif
  964. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  965. if (degChamber() > CHAMBER_MAXTEMP)
  966. _temp_error(H_CHAMBER, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  967. #endif
  968. #if WATCH_CHAMBER
  969. // Make sure temperature is increasing
  970. if (watch_chamber.elapsed(ms)) { // Time to check the chamber?
  971. if (degChamber() < watch_chamber.target) // Failed to increase enough?
  972. _temp_error(H_CHAMBER, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  973. else
  974. start_watching_chamber(); // Start again if the target is still far off
  975. }
  976. #endif
  977. if (ELAPSED(ms, next_chamber_check_ms)) {
  978. next_chamber_check_ms = ms + CHAMBER_CHECK_INTERVAL;
  979. if (WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP)) {
  980. #if ENABLED(CHAMBER_LIMIT_SWITCHING)
  981. if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
  982. temp_chamber.soft_pwm_amount = 0;
  983. else if (temp_chamber.celsius <= temp_chamber.target - (TEMP_CHAMBER_HYSTERESIS))
  984. temp_chamber.soft_pwm_amount = MAX_CHAMBER_POWER >> 1;
  985. #else
  986. temp_chamber.soft_pwm_amount = temp_chamber.celsius < temp_chamber.target ? MAX_CHAMBER_POWER >> 1 : 0;
  987. #endif
  988. }
  989. else {
  990. temp_chamber.soft_pwm_amount = 0;
  991. WRITE_HEATER_CHAMBER(LOW);
  992. }
  993. TERN_(THERMAL_PROTECTION_CHAMBER, thermal_runaway_protection(tr_state_machine_chamber, temp_chamber.celsius, temp_chamber.target, H_CHAMBER, THERMAL_PROTECTION_CHAMBER_PERIOD, THERMAL_PROTECTION_CHAMBER_HYSTERESIS));
  994. }
  995. // TODO: Implement true PID pwm
  996. //temp_bed.soft_pwm_amount = WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP) ? (int)get_pid_output_chamber() >> 1 : 0;
  997. #endif // HAS_HEATED_CHAMBER
  998. UNUSED(ms);
  999. }
  1000. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  1001. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  1002. /**
  1003. * Bisect search for the range of the 'raw' value, then interpolate
  1004. * proportionally between the under and over values.
  1005. */
  1006. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  1007. uint8_t l = 0, r = LEN, m; \
  1008. for (;;) { \
  1009. m = (l + r) >> 1; \
  1010. if (!m) return int16_t(pgm_read_word(&TBL[0].celsius)); \
  1011. if (m == l || m == r) return int16_t(pgm_read_word(&TBL[LEN-1].celsius)); \
  1012. int16_t v00 = pgm_read_word(&TBL[m-1].value), \
  1013. v10 = pgm_read_word(&TBL[m-0].value); \
  1014. if (raw < v00) r = m; \
  1015. else if (raw > v10) l = m; \
  1016. else { \
  1017. const int16_t v01 = int16_t(pgm_read_word(&TBL[m-1].celsius)), \
  1018. v11 = int16_t(pgm_read_word(&TBL[m-0].celsius)); \
  1019. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  1020. } \
  1021. } \
  1022. }while(0)
  1023. #if HAS_USER_THERMISTORS
  1024. user_thermistor_t Temperature::user_thermistor[USER_THERMISTORS]; // Initialized by settings.load()
  1025. void Temperature::reset_user_thermistors() {
  1026. user_thermistor_t user_thermistor[USER_THERMISTORS] = {
  1027. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1028. { true, 0, 0, HOTEND0_PULLUP_RESISTOR_OHMS, HOTEND0_RESISTANCE_25C_OHMS, 0, 0, HOTEND0_BETA, 0 },
  1029. #endif
  1030. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1031. { true, 0, 0, HOTEND1_PULLUP_RESISTOR_OHMS, HOTEND1_RESISTANCE_25C_OHMS, 0, 0, HOTEND1_BETA, 0 },
  1032. #endif
  1033. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1034. { true, 0, 0, HOTEND2_PULLUP_RESISTOR_OHMS, HOTEND2_RESISTANCE_25C_OHMS, 0, 0, HOTEND2_BETA, 0 },
  1035. #endif
  1036. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1037. { true, 0, 0, HOTEND3_PULLUP_RESISTOR_OHMS, HOTEND3_RESISTANCE_25C_OHMS, 0, 0, HOTEND3_BETA, 0 },
  1038. #endif
  1039. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1040. { true, 0, 0, HOTEND4_PULLUP_RESISTOR_OHMS, HOTEND4_RESISTANCE_25C_OHMS, 0, 0, HOTEND4_BETA, 0 },
  1041. #endif
  1042. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1043. { true, 0, 0, HOTEND5_PULLUP_RESISTOR_OHMS, HOTEND5_RESISTANCE_25C_OHMS, 0, 0, HOTEND5_BETA, 0 },
  1044. #endif
  1045. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1046. { true, 0, 0, HOTEND6_PULLUP_RESISTOR_OHMS, HOTEND6_RESISTANCE_25C_OHMS, 0, 0, HOTEND6_BETA, 0 },
  1047. #endif
  1048. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1049. { true, 0, 0, HOTEND7_PULLUP_RESISTOR_OHMS, HOTEND7_RESISTANCE_25C_OHMS, 0, 0, HOTEND7_BETA, 0 },
  1050. #endif
  1051. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1052. { true, 0, 0, BED_PULLUP_RESISTOR_OHMS, BED_RESISTANCE_25C_OHMS, 0, 0, BED_BETA, 0 },
  1053. #endif
  1054. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1055. { true, 0, 0, CHAMBER_PULLUP_RESISTOR_OHMS, CHAMBER_RESISTANCE_25C_OHMS, 0, 0, CHAMBER_BETA, 0 }
  1056. #endif
  1057. };
  1058. COPY(thermalManager.user_thermistor, user_thermistor);
  1059. }
  1060. void Temperature::log_user_thermistor(const uint8_t t_index, const bool eprom/*=false*/) {
  1061. if (eprom)
  1062. SERIAL_ECHOPGM(" M305 ");
  1063. else
  1064. SERIAL_ECHO_START();
  1065. SERIAL_CHAR('P');
  1066. SERIAL_CHAR('0' + t_index);
  1067. const user_thermistor_t &t = user_thermistor[t_index];
  1068. SERIAL_ECHOPAIR_F(" R", t.series_res, 1);
  1069. SERIAL_ECHOPAIR_F_P(SP_T_STR, t.res_25, 1);
  1070. SERIAL_ECHOPAIR_F_P(SP_B_STR, t.beta, 1);
  1071. SERIAL_ECHOPAIR_F_P(SP_C_STR, t.sh_c_coeff, 9);
  1072. SERIAL_ECHOPGM(" ; ");
  1073. serialprintPGM(
  1074. TERN_(HEATER_0_USER_THERMISTOR, t_index == CTI_HOTEND_0 ? PSTR("HOTEND 0") :)
  1075. TERN_(HEATER_1_USER_THERMISTOR, t_index == CTI_HOTEND_1 ? PSTR("HOTEND 1") :)
  1076. TERN_(HEATER_2_USER_THERMISTOR, t_index == CTI_HOTEND_2 ? PSTR("HOTEND 2") :)
  1077. TERN_(HEATER_3_USER_THERMISTOR, t_index == CTI_HOTEND_3 ? PSTR("HOTEND 3") :)
  1078. TERN_(HEATER_4_USER_THERMISTOR, t_index == CTI_HOTEND_4 ? PSTR("HOTEND 4") :)
  1079. TERN_(HEATER_5_USER_THERMISTOR, t_index == CTI_HOTEND_5 ? PSTR("HOTEND 5") :)
  1080. TERN_(HEATER_6_USER_THERMISTOR, t_index == CTI_HOTEND_6 ? PSTR("HOTEND 6") :)
  1081. TERN_(HEATER_7_USER_THERMISTOR, t_index == CTI_HOTEND_7 ? PSTR("HOTEND 7") :)
  1082. TERN_(HEATER_BED_USER_THERMISTOR, t_index == CTI_BED ? PSTR("BED") :)
  1083. TERN_(HEATER_CHAMBER_USER_THERMISTOR, t_index == CTI_CHAMBER ? PSTR("CHAMBER") :)
  1084. nullptr
  1085. );
  1086. SERIAL_EOL();
  1087. }
  1088. float Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int raw) {
  1089. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1090. // static uint32_t clocks_total = 0;
  1091. // static uint32_t calls = 0;
  1092. // uint32_t tcnt5 = TCNT5;
  1093. //#endif
  1094. if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
  1095. user_thermistor_t &t = user_thermistor[t_index];
  1096. if (t.pre_calc) { // pre-calculate some variables
  1097. t.pre_calc = false;
  1098. t.res_25_recip = 1.0f / t.res_25;
  1099. t.res_25_log = logf(t.res_25);
  1100. t.beta_recip = 1.0f / t.beta;
  1101. t.sh_alpha = RECIPROCAL(THERMISTOR_RESISTANCE_NOMINAL_C - (THERMISTOR_ABS_ZERO_C))
  1102. - (t.beta_recip * t.res_25_log) - (t.sh_c_coeff * cu(t.res_25_log));
  1103. }
  1104. // maximum adc value .. take into account the over sampling
  1105. const int adc_max = MAX_RAW_THERMISTOR_VALUE,
  1106. adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
  1107. const float adc_inverse = (adc_max - adc_raw) - 0.5f,
  1108. resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
  1109. log_resistance = logf(resistance);
  1110. float value = t.sh_alpha;
  1111. value += log_resistance * t.beta_recip;
  1112. if (t.sh_c_coeff != 0)
  1113. value += t.sh_c_coeff * cu(log_resistance);
  1114. value = 1.0f / value;
  1115. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1116. // int32_t clocks = TCNT5 - tcnt5;
  1117. // if (clocks >= 0) {
  1118. // clocks_total += clocks;
  1119. // calls++;
  1120. // }
  1121. //#endif
  1122. // Return degrees C (up to 999, as the LCD only displays 3 digits)
  1123. return _MIN(value + THERMISTOR_ABS_ZERO_C, 999);
  1124. }
  1125. #endif
  1126. #if HAS_HOTEND
  1127. // Derived from RepRap FiveD extruder::getTemperature()
  1128. // For hot end temperature measurement.
  1129. float Temperature::analog_to_celsius_hotend(const int raw, const uint8_t e) {
  1130. if (e > HOTENDS - DISABLED(TEMP_SENSOR_1_AS_REDUNDANT)) {
  1131. SERIAL_ERROR_START();
  1132. SERIAL_ECHO((int)e);
  1133. SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER_NUM);
  1134. kill();
  1135. return 0;
  1136. }
  1137. switch (e) {
  1138. case 0:
  1139. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1140. return user_thermistor_to_deg_c(CTI_HOTEND_0, raw);
  1141. #elif ENABLED(HEATER_0_USES_MAX6675)
  1142. return (
  1143. #if ENABLED(MAX6675_IS_MAX31865)
  1144. max31865.temperature(100, 400) // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1145. #else
  1146. raw * 0.25
  1147. #endif
  1148. );
  1149. #elif ENABLED(HEATER_0_USES_AD595)
  1150. return TEMP_AD595(raw);
  1151. #elif ENABLED(HEATER_0_USES_AD8495)
  1152. return TEMP_AD8495(raw);
  1153. #else
  1154. break;
  1155. #endif
  1156. case 1:
  1157. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1158. return user_thermistor_to_deg_c(CTI_HOTEND_1, raw);
  1159. #elif ENABLED(HEATER_1_USES_MAX6675)
  1160. return raw * 0.25;
  1161. #elif ENABLED(HEATER_1_USES_AD595)
  1162. return TEMP_AD595(raw);
  1163. #elif ENABLED(HEATER_1_USES_AD8495)
  1164. return TEMP_AD8495(raw);
  1165. #else
  1166. break;
  1167. #endif
  1168. case 2:
  1169. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1170. return user_thermistor_to_deg_c(CTI_HOTEND_2, raw);
  1171. #elif ENABLED(HEATER_2_USES_AD595)
  1172. return TEMP_AD595(raw);
  1173. #elif ENABLED(HEATER_2_USES_AD8495)
  1174. return TEMP_AD8495(raw);
  1175. #else
  1176. break;
  1177. #endif
  1178. case 3:
  1179. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1180. return user_thermistor_to_deg_c(CTI_HOTEND_3, raw);
  1181. #elif ENABLED(HEATER_3_USES_AD595)
  1182. return TEMP_AD595(raw);
  1183. #elif ENABLED(HEATER_3_USES_AD8495)
  1184. return TEMP_AD8495(raw);
  1185. #else
  1186. break;
  1187. #endif
  1188. case 4:
  1189. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1190. return user_thermistor_to_deg_c(CTI_HOTEND_4, raw);
  1191. #elif ENABLED(HEATER_4_USES_AD595)
  1192. return TEMP_AD595(raw);
  1193. #elif ENABLED(HEATER_4_USES_AD8495)
  1194. return TEMP_AD8495(raw);
  1195. #else
  1196. break;
  1197. #endif
  1198. case 5:
  1199. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1200. return user_thermistor_to_deg_c(CTI_HOTEND_5, raw);
  1201. #elif ENABLED(HEATER_5_USES_AD595)
  1202. return TEMP_AD595(raw);
  1203. #elif ENABLED(HEATER_5_USES_AD8495)
  1204. return TEMP_AD8495(raw);
  1205. #else
  1206. break;
  1207. #endif
  1208. case 6:
  1209. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1210. return user_thermistor_to_deg_c(CTI_HOTEND_6, raw);
  1211. #elif ENABLED(HEATER_6_USES_AD595)
  1212. return TEMP_AD595(raw);
  1213. #elif ENABLED(HEATER_6_USES_AD8495)
  1214. return TEMP_AD8495(raw);
  1215. #else
  1216. break;
  1217. #endif
  1218. case 7:
  1219. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1220. return user_thermistor_to_deg_c(CTI_HOTEND_7, raw);
  1221. #elif ENABLED(HEATER_7_USES_AD595)
  1222. return TEMP_AD595(raw);
  1223. #elif ENABLED(HEATER_7_USES_AD8495)
  1224. return TEMP_AD8495(raw);
  1225. #else
  1226. break;
  1227. #endif
  1228. default: break;
  1229. }
  1230. #if HOTEND_USES_THERMISTOR
  1231. // Thermistor with conversion table?
  1232. const temp_entry_t(*tt)[] = (temp_entry_t(*)[])(heater_ttbl_map[e]);
  1233. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  1234. #endif
  1235. return 0;
  1236. }
  1237. #endif // HAS_HOTEND
  1238. #if HAS_HEATED_BED
  1239. // Derived from RepRap FiveD extruder::getTemperature()
  1240. // For bed temperature measurement.
  1241. float Temperature::analog_to_celsius_bed(const int raw) {
  1242. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1243. return user_thermistor_to_deg_c(CTI_BED, raw);
  1244. #elif ENABLED(HEATER_BED_USES_THERMISTOR)
  1245. SCAN_THERMISTOR_TABLE(BED_TEMPTABLE, BED_TEMPTABLE_LEN);
  1246. #elif ENABLED(HEATER_BED_USES_AD595)
  1247. return TEMP_AD595(raw);
  1248. #elif ENABLED(HEATER_BED_USES_AD8495)
  1249. return TEMP_AD8495(raw);
  1250. #else
  1251. UNUSED(raw);
  1252. return 0;
  1253. #endif
  1254. }
  1255. #endif // HAS_HEATED_BED
  1256. #if HAS_TEMP_CHAMBER
  1257. // Derived from RepRap FiveD extruder::getTemperature()
  1258. // For chamber temperature measurement.
  1259. float Temperature::analog_to_celsius_chamber(const int raw) {
  1260. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1261. return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
  1262. #elif ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  1263. SCAN_THERMISTOR_TABLE(CHAMBER_TEMPTABLE, CHAMBER_TEMPTABLE_LEN);
  1264. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  1265. return TEMP_AD595(raw);
  1266. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  1267. return TEMP_AD8495(raw);
  1268. #else
  1269. UNUSED(raw);
  1270. return 0;
  1271. #endif
  1272. }
  1273. #endif // HAS_TEMP_CHAMBER
  1274. #if HAS_TEMP_PROBE
  1275. // Derived from RepRap FiveD extruder::getTemperature()
  1276. // For probe temperature measurement.
  1277. float Temperature::analog_to_celsius_probe(const int raw) {
  1278. #if ENABLED(PROBE_USER_THERMISTOR)
  1279. return user_thermistor_to_deg_c(CTI_PROBE, raw);
  1280. #elif ENABLED(PROBE_USES_THERMISTOR)
  1281. SCAN_THERMISTOR_TABLE(PROBE_TEMPTABLE, PROBE_TEMPTABLE_LEN);
  1282. #elif ENABLED(PROBE_USES_AD595)
  1283. return TEMP_AD595(raw);
  1284. #elif ENABLED(PROBE_USES_AD8495)
  1285. return TEMP_AD8495(raw);
  1286. #else
  1287. UNUSED(raw);
  1288. return 0;
  1289. #endif
  1290. }
  1291. #endif // HAS_TEMP_PROBE
  1292. /**
  1293. * Get the raw values into the actual temperatures.
  1294. * The raw values are created in interrupt context,
  1295. * and this function is called from normal context
  1296. * as it would block the stepper routine.
  1297. */
  1298. void Temperature::updateTemperaturesFromRawValues() {
  1299. #if ENABLED(HEATER_0_USES_MAX6675)
  1300. temp_hotend[0].raw = READ_MAX6675(0);
  1301. #endif
  1302. #if ENABLED(HEATER_1_USES_MAX6675)
  1303. temp_hotend[1].raw = READ_MAX6675(1);
  1304. #endif
  1305. #if HAS_HOTEND
  1306. HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
  1307. #endif
  1308. TERN_(HAS_HEATED_BED, temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw));
  1309. TERN_(HAS_TEMP_CHAMBER, temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw));
  1310. TERN_(HAS_TEMP_PROBE, temp_probe.celsius = analog_to_celsius_probe(temp_probe.raw));
  1311. TERN_(TEMP_SENSOR_1_AS_REDUNDANT, redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1));
  1312. TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
  1313. TERN_(HAS_POWER_MONITOR, power_monitor.capture_values());
  1314. // Reset the watchdog on good temperature measurement
  1315. watchdog_refresh();
  1316. raw_temps_ready = false;
  1317. }
  1318. #if MAX6675_SEPARATE_SPI
  1319. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  1320. #endif
  1321. // Init fans according to whether they're native PWM or Software PWM
  1322. #ifdef ALFAWISE_UX0
  1323. #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH)
  1324. #else
  1325. #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH)
  1326. #endif
  1327. #if ENABLED(FAN_SOFT_PWM)
  1328. #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P)
  1329. #else
  1330. #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
  1331. #endif
  1332. #if ENABLED(FAST_PWM_FAN)
  1333. #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY)
  1334. #else
  1335. #define SET_FAST_PWM_FREQ(P) NOOP
  1336. #endif
  1337. #define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0)
  1338. #if EXTRUDER_AUTO_FAN_SPEED != 255
  1339. #define INIT_E_AUTO_FAN_PIN(P) do{ if (P == FAN1_PIN || P == FAN2_PIN) { SET_PWM(P); SET_FAST_PWM_FREQ(FAST_PWM_FAN_FREQUENCY); } else SET_OUTPUT(P); }while(0)
  1340. #else
  1341. #define INIT_E_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1342. #endif
  1343. #if CHAMBER_AUTO_FAN_SPEED != 255
  1344. #define INIT_CHAMBER_AUTO_FAN_PIN(P) do{ if (P == FAN1_PIN || P == FAN2_PIN) { SET_PWM(P); SET_FAST_PWM_FREQ(FAST_PWM_FAN_FREQUENCY); } else SET_OUTPUT(P); }while(0)
  1345. #else
  1346. #define INIT_CHAMBER_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1347. #endif
  1348. /**
  1349. * Initialize the temperature manager
  1350. * The manager is implemented by periodic calls to manage_heater()
  1351. */
  1352. void Temperature::init() {
  1353. TERN_(MAX6675_IS_MAX31865, max31865.begin(MAX31865_2WIRE)); // MAX31865_2WIRE, MAX31865_3WIRE, MAX31865_4WIRE
  1354. #if EARLY_WATCHDOG
  1355. // Flag that the thermalManager should be running
  1356. if (inited) return;
  1357. inited = true;
  1358. #endif
  1359. #if MB(RUMBA)
  1360. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  1361. #define _AD(N) ANY(HEATER_##N##_USES_AD595, HEATER_##N##_USES_AD8495)
  1362. #if _AD(0) || _AD(1) || _AD(2) || _AD(BED) || _AD(CHAMBER)
  1363. MCUCR = _BV(JTD);
  1364. MCUCR = _BV(JTD);
  1365. #endif
  1366. #endif
  1367. #if BOTH(PIDTEMP, PID_EXTRUSION_SCALING)
  1368. last_e_position = 0;
  1369. #endif
  1370. #if HAS_HEATER_0
  1371. #ifdef ALFAWISE_UX0
  1372. OUT_WRITE_OD(HEATER_0_PIN, HEATER_0_INVERTING);
  1373. #else
  1374. OUT_WRITE(HEATER_0_PIN, HEATER_0_INVERTING);
  1375. #endif
  1376. #endif
  1377. #if HAS_HEATER_1
  1378. OUT_WRITE(HEATER_1_PIN, HEATER_1_INVERTING);
  1379. #endif
  1380. #if HAS_HEATER_2
  1381. OUT_WRITE(HEATER_2_PIN, HEATER_2_INVERTING);
  1382. #endif
  1383. #if HAS_HEATER_3
  1384. OUT_WRITE(HEATER_3_PIN, HEATER_3_INVERTING);
  1385. #endif
  1386. #if HAS_HEATER_4
  1387. OUT_WRITE(HEATER_4_PIN, HEATER_4_INVERTING);
  1388. #endif
  1389. #if HAS_HEATER_5
  1390. OUT_WRITE(HEATER_5_PIN, HEATER_5_INVERTING);
  1391. #endif
  1392. #if HAS_HEATER_6
  1393. OUT_WRITE(HEATER_6_PIN, HEATER_6_INVERTING);
  1394. #endif
  1395. #if HAS_HEATER_7
  1396. OUT_WRITE(HEATER_7_PIN, HEATER_7_INVERTING);
  1397. #endif
  1398. #if HAS_HEATED_BED
  1399. #ifdef ALFAWISE_UX0
  1400. OUT_WRITE_OD(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1401. #else
  1402. OUT_WRITE(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1403. #endif
  1404. #endif
  1405. #if HAS_HEATED_CHAMBER
  1406. OUT_WRITE(HEATER_CHAMBER_PIN, HEATER_CHAMBER_INVERTING);
  1407. #endif
  1408. #if HAS_FAN0
  1409. INIT_FAN_PIN(FAN_PIN);
  1410. #endif
  1411. #if HAS_FAN1
  1412. INIT_FAN_PIN(FAN1_PIN);
  1413. #endif
  1414. #if HAS_FAN2
  1415. INIT_FAN_PIN(FAN2_PIN);
  1416. #endif
  1417. #if HAS_FAN3
  1418. INIT_FAN_PIN(FAN3_PIN);
  1419. #endif
  1420. #if HAS_FAN4
  1421. INIT_FAN_PIN(FAN4_PIN);
  1422. #endif
  1423. #if HAS_FAN5
  1424. INIT_FAN_PIN(FAN5_PIN);
  1425. #endif
  1426. #if HAS_FAN6
  1427. INIT_FAN_PIN(FAN6_PIN);
  1428. #endif
  1429. #if HAS_FAN7
  1430. INIT_FAN_PIN(FAN7_PIN);
  1431. #endif
  1432. #if ENABLED(USE_CONTROLLER_FAN)
  1433. INIT_FAN_PIN(CONTROLLER_FAN_PIN);
  1434. #endif
  1435. #if MAX6675_SEPARATE_SPI
  1436. OUT_WRITE(SCK_PIN, LOW);
  1437. OUT_WRITE(MOSI_PIN, HIGH);
  1438. SET_INPUT_PULLUP(MISO_PIN);
  1439. max6675_spi.init();
  1440. OUT_WRITE(SS_PIN, HIGH);
  1441. OUT_WRITE(MAX6675_SS_PIN, HIGH);
  1442. #endif
  1443. #if ENABLED(HEATER_1_USES_MAX6675)
  1444. OUT_WRITE(MAX6675_SS2_PIN, HIGH);
  1445. #endif
  1446. HAL_adc_init();
  1447. #if HAS_TEMP_ADC_0
  1448. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1449. #endif
  1450. #if HAS_TEMP_ADC_1
  1451. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1452. #endif
  1453. #if HAS_TEMP_ADC_2
  1454. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1455. #endif
  1456. #if HAS_TEMP_ADC_3
  1457. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1458. #endif
  1459. #if HAS_TEMP_ADC_4
  1460. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1461. #endif
  1462. #if HAS_TEMP_ADC_5
  1463. HAL_ANALOG_SELECT(TEMP_5_PIN);
  1464. #endif
  1465. #if HAS_TEMP_ADC_6
  1466. HAL_ANALOG_SELECT(TEMP_6_PIN);
  1467. #endif
  1468. #if HAS_TEMP_ADC_7
  1469. HAL_ANALOG_SELECT(TEMP_7_PIN);
  1470. #endif
  1471. #if HAS_JOY_ADC_X
  1472. HAL_ANALOG_SELECT(JOY_X_PIN);
  1473. #endif
  1474. #if HAS_JOY_ADC_Y
  1475. HAL_ANALOG_SELECT(JOY_Y_PIN);
  1476. #endif
  1477. #if HAS_JOY_ADC_Z
  1478. HAL_ANALOG_SELECT(JOY_Z_PIN);
  1479. #endif
  1480. #if HAS_JOY_ADC_EN
  1481. SET_INPUT_PULLUP(JOY_EN_PIN);
  1482. #endif
  1483. #if HAS_HEATED_BED
  1484. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1485. #endif
  1486. #if HAS_TEMP_CHAMBER
  1487. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1488. #endif
  1489. #if HAS_TEMP_PROBE
  1490. HAL_ANALOG_SELECT(TEMP_PROBE_PIN);
  1491. #endif
  1492. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1493. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1494. #endif
  1495. #if HAS_ADC_BUTTONS
  1496. HAL_ANALOG_SELECT(ADC_KEYPAD_PIN);
  1497. #endif
  1498. #if ENABLED(POWER_MONITOR_CURRENT)
  1499. HAL_ANALOG_SELECT(POWER_MONITOR_CURRENT_PIN);
  1500. #endif
  1501. #if ENABLED(POWER_MONITOR_VOLTAGE)
  1502. HAL_ANALOG_SELECT(POWER_MONITOR_VOLTAGE_PIN);
  1503. #endif
  1504. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1505. ENABLE_TEMPERATURE_INTERRUPT();
  1506. #if HAS_AUTO_FAN_0
  1507. INIT_E_AUTO_FAN_PIN(E0_AUTO_FAN_PIN);
  1508. #endif
  1509. #if HAS_AUTO_FAN_1 && !_EFANOVERLAP(1,0)
  1510. INIT_E_AUTO_FAN_PIN(E1_AUTO_FAN_PIN);
  1511. #endif
  1512. #if HAS_AUTO_FAN_2 && !(_EFANOVERLAP(2,0) || _EFANOVERLAP(2,1))
  1513. INIT_E_AUTO_FAN_PIN(E2_AUTO_FAN_PIN);
  1514. #endif
  1515. #if HAS_AUTO_FAN_3 && !(_EFANOVERLAP(3,0) || _EFANOVERLAP(3,1) || _EFANOVERLAP(3,2))
  1516. INIT_E_AUTO_FAN_PIN(E3_AUTO_FAN_PIN);
  1517. #endif
  1518. #if HAS_AUTO_FAN_4 && !(_EFANOVERLAP(4,0) || _EFANOVERLAP(4,1) || _EFANOVERLAP(4,2) || _EFANOVERLAP(4,3))
  1519. INIT_E_AUTO_FAN_PIN(E4_AUTO_FAN_PIN);
  1520. #endif
  1521. #if HAS_AUTO_FAN_5 && !(_EFANOVERLAP(5,0) || _EFANOVERLAP(5,1) || _EFANOVERLAP(5,2) || _EFANOVERLAP(5,3) || _EFANOVERLAP(5,4))
  1522. INIT_E_AUTO_FAN_PIN(E5_AUTO_FAN_PIN);
  1523. #endif
  1524. #if HAS_AUTO_FAN_6 && !(_EFANOVERLAP(6,0) || _EFANOVERLAP(6,1) || _EFANOVERLAP(6,2) || _EFANOVERLAP(6,3) || _EFANOVERLAP(6,4) || _EFANOVERLAP(6,5))
  1525. INIT_E_AUTO_FAN_PIN(E6_AUTO_FAN_PIN);
  1526. #endif
  1527. #if HAS_AUTO_FAN_7 && !(_EFANOVERLAP(7,0) || _EFANOVERLAP(7,1) || _EFANOVERLAP(7,2) || _EFANOVERLAP(7,3) || _EFANOVERLAP(7,4) || _EFANOVERLAP(7,5) || _EFANOVERLAP(7,6))
  1528. INIT_E_AUTO_FAN_PIN(E7_AUTO_FAN_PIN);
  1529. #endif
  1530. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  1531. INIT_CHAMBER_AUTO_FAN_PIN(CHAMBER_AUTO_FAN_PIN);
  1532. #endif
  1533. // Wait for temperature measurement to settle
  1534. delay(250);
  1535. #if HAS_HOTEND
  1536. #define _TEMP_MIN_E(NR) do{ \
  1537. const int16_t tmin = _MAX(HEATER_ ##NR## _MINTEMP, (int16_t)pgm_read_word(&HEATER_ ##NR## _TEMPTABLE[HEATER_ ##NR## _SENSOR_MINTEMP_IND].celsius)); \
  1538. temp_range[NR].mintemp = tmin; \
  1539. while (analog_to_celsius_hotend(temp_range[NR].raw_min, NR) < tmin) \
  1540. temp_range[NR].raw_min += TEMPDIR(NR) * (OVERSAMPLENR); \
  1541. }while(0)
  1542. #define _TEMP_MAX_E(NR) do{ \
  1543. const int16_t tmax = _MIN(HEATER_ ##NR## _MAXTEMP, (int16_t)pgm_read_word(&HEATER_ ##NR## _TEMPTABLE[HEATER_ ##NR## _SENSOR_MAXTEMP_IND].celsius) - 1); \
  1544. temp_range[NR].maxtemp = tmax; \
  1545. while (analog_to_celsius_hotend(temp_range[NR].raw_max, NR) > tmax) \
  1546. temp_range[NR].raw_max -= TEMPDIR(NR) * (OVERSAMPLENR); \
  1547. }while(0)
  1548. #define _MINMAX_TEST(N,M) (HOTENDS > N && THERMISTOR_HEATER_##N && THERMISTOR_HEATER_##N != 998 && THERMISTOR_HEATER_##N != 999 && defined(HEATER_##N##_##M##TEMP))
  1549. #if _MINMAX_TEST(0, MIN)
  1550. _TEMP_MIN_E(0);
  1551. #endif
  1552. #if _MINMAX_TEST(0, MAX)
  1553. _TEMP_MAX_E(0);
  1554. #endif
  1555. #if _MINMAX_TEST(1, MIN)
  1556. _TEMP_MIN_E(1);
  1557. #endif
  1558. #if _MINMAX_TEST(1, MAX)
  1559. _TEMP_MAX_E(1);
  1560. #endif
  1561. #if _MINMAX_TEST(2, MIN)
  1562. _TEMP_MIN_E(2);
  1563. #endif
  1564. #if _MINMAX_TEST(2, MAX)
  1565. _TEMP_MAX_E(2);
  1566. #endif
  1567. #if _MINMAX_TEST(3, MIN)
  1568. _TEMP_MIN_E(3);
  1569. #endif
  1570. #if _MINMAX_TEST(3, MAX)
  1571. _TEMP_MAX_E(3);
  1572. #endif
  1573. #if _MINMAX_TEST(4, MIN)
  1574. _TEMP_MIN_E(4);
  1575. #endif
  1576. #if _MINMAX_TEST(4, MAX)
  1577. _TEMP_MAX_E(4);
  1578. #endif
  1579. #if _MINMAX_TEST(5, MIN)
  1580. _TEMP_MIN_E(5);
  1581. #endif
  1582. #if _MINMAX_TEST(5, MAX)
  1583. _TEMP_MAX_E(5);
  1584. #endif
  1585. #if _MINMAX_TEST(6, MIN)
  1586. _TEMP_MIN_E(6);
  1587. #endif
  1588. #if _MINMAX_TEST(6, MAX)
  1589. _TEMP_MAX_E(6);
  1590. #endif
  1591. #if _MINMAX_TEST(7, MIN)
  1592. _TEMP_MIN_E(7);
  1593. #endif
  1594. #if _MINMAX_TEST(7, MAX)
  1595. _TEMP_MAX_E(7);
  1596. #endif
  1597. #endif // HAS_HOTEND
  1598. #if HAS_HEATED_BED
  1599. #ifdef BED_MINTEMP
  1600. while (analog_to_celsius_bed(mintemp_raw_BED) < BED_MINTEMP) mintemp_raw_BED += TEMPDIR(BED) * (OVERSAMPLENR);
  1601. #endif
  1602. #ifdef BED_MAXTEMP
  1603. while (analog_to_celsius_bed(maxtemp_raw_BED) > BED_MAXTEMP) maxtemp_raw_BED -= TEMPDIR(BED) * (OVERSAMPLENR);
  1604. #endif
  1605. #endif // HAS_HEATED_BED
  1606. #if HAS_HEATED_CHAMBER
  1607. #ifdef CHAMBER_MINTEMP
  1608. while (analog_to_celsius_chamber(mintemp_raw_CHAMBER) < CHAMBER_MINTEMP) mintemp_raw_CHAMBER += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1609. #endif
  1610. #ifdef CHAMBER_MAXTEMP
  1611. while (analog_to_celsius_chamber(maxtemp_raw_CHAMBER) > CHAMBER_MAXTEMP) maxtemp_raw_CHAMBER -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1612. #endif
  1613. #endif
  1614. TERN_(PROBING_HEATERS_OFF, paused = false);
  1615. }
  1616. #if WATCH_HOTENDS
  1617. /**
  1618. * Start Heating Sanity Check for hotends that are below
  1619. * their target temperature by a configurable margin.
  1620. * This is called when the temperature is set. (M104, M109)
  1621. */
  1622. void Temperature::start_watching_hotend(const uint8_t E_NAME) {
  1623. const uint8_t ee = HOTEND_INDEX;
  1624. watch_hotend[ee].restart(degHotend(ee), degTargetHotend(ee));
  1625. }
  1626. #endif
  1627. #if WATCH_BED
  1628. /**
  1629. * Start Heating Sanity Check for hotends that are below
  1630. * their target temperature by a configurable margin.
  1631. * This is called when the temperature is set. (M140, M190)
  1632. */
  1633. void Temperature::start_watching_bed() {
  1634. watch_bed.restart(degBed(), degTargetBed());
  1635. }
  1636. #endif
  1637. #if WATCH_CHAMBER
  1638. /**
  1639. * Start Heating Sanity Check for chamber that is below
  1640. * its target temperature by a configurable margin.
  1641. * This is called when the temperature is set. (M141, M191)
  1642. */
  1643. void Temperature::start_watching_chamber() {
  1644. watch_chamber.restart(degChamber(), degTargetChamber());
  1645. }
  1646. #endif
  1647. #if HAS_THERMAL_PROTECTION
  1648. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1649. Temperature::tr_state_machine_t Temperature::tr_state_machine[HOTENDS]; // = { { TRInactive, 0 } };
  1650. #endif
  1651. #if HAS_THERMALLY_PROTECTED_BED
  1652. Temperature::tr_state_machine_t Temperature::tr_state_machine_bed; // = { TRInactive, 0 };
  1653. #endif
  1654. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1655. Temperature::tr_state_machine_t Temperature::tr_state_machine_chamber; // = { TRInactive, 0 };
  1656. #endif
  1657. void Temperature::thermal_runaway_protection(Temperature::tr_state_machine_t &sm, const float &current, const float &target, const heater_ind_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc) {
  1658. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1659. /**
  1660. SERIAL_ECHO_START();
  1661. SERIAL_ECHOPGM("Thermal Runaway Running. Heater ID: ");
  1662. if (heater_id == H_CHAMBER) SERIAL_ECHOPGM("chamber");
  1663. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1664. SERIAL_ECHOPAIR(" ; State:", sm.state, " ; Timer:", sm.timer, " ; Temperature:", current, " ; Target Temp:", target);
  1665. if (heater_id >= 0)
  1666. SERIAL_ECHOPAIR(" ; Idle Timeout:", hotend_idle[heater_id].timed_out);
  1667. else
  1668. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle.timed_out);
  1669. SERIAL_EOL();
  1670. //*/
  1671. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1672. #if HEATER_IDLE_HANDLER
  1673. // If the heater idle timeout expires, restart
  1674. if ((heater_id >= 0 && hotend_idle[heater_id].timed_out)
  1675. || TERN0(HAS_HEATED_BED, (heater_id < 0 && bed_idle.timed_out))
  1676. ) {
  1677. sm.state = TRInactive;
  1678. tr_target_temperature[heater_index] = 0;
  1679. }
  1680. else
  1681. #endif
  1682. {
  1683. // If the target temperature changes, restart
  1684. if (tr_target_temperature[heater_index] != target) {
  1685. tr_target_temperature[heater_index] = target;
  1686. sm.state = target > 0 ? TRFirstHeating : TRInactive;
  1687. }
  1688. }
  1689. switch (sm.state) {
  1690. // Inactive state waits for a target temperature to be set
  1691. case TRInactive: break;
  1692. // When first heating, wait for the temperature to be reached then go to Stable state
  1693. case TRFirstHeating:
  1694. if (current < tr_target_temperature[heater_index]) break;
  1695. sm.state = TRStable;
  1696. // While the temperature is stable watch for a bad temperature
  1697. case TRStable:
  1698. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  1699. if (adaptive_fan_slowing && heater_id >= 0) {
  1700. const int fan_index = _MIN(heater_id, FAN_COUNT - 1);
  1701. if (fan_speed[fan_index] == 0 || current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.25f))
  1702. fan_speed_scaler[fan_index] = 128;
  1703. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.3335f))
  1704. fan_speed_scaler[fan_index] = 96;
  1705. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.5f))
  1706. fan_speed_scaler[fan_index] = 64;
  1707. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.8f))
  1708. fan_speed_scaler[fan_index] = 32;
  1709. else
  1710. fan_speed_scaler[fan_index] = 0;
  1711. }
  1712. #endif
  1713. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1714. sm.timer = millis() + SEC_TO_MS(period_seconds);
  1715. break;
  1716. }
  1717. else if (PENDING(millis(), sm.timer)) break;
  1718. sm.state = TRRunaway;
  1719. case TRRunaway:
  1720. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  1721. _temp_error(heater_id, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  1722. }
  1723. }
  1724. #endif // HAS_THERMAL_PROTECTION
  1725. void Temperature::disable_all_heaters() {
  1726. TERN_(AUTOTEMP, planner.autotemp_enabled = false);
  1727. #if HAS_HOTEND
  1728. HOTEND_LOOP() setTargetHotend(0, e);
  1729. #endif
  1730. TERN_(HAS_HEATED_BED, setTargetBed(0));
  1731. TERN_(HAS_HEATED_CHAMBER, setTargetChamber(0));
  1732. // Unpause and reset everything
  1733. TERN_(PROBING_HEATERS_OFF, pause(false));
  1734. #define DISABLE_HEATER(N) { \
  1735. setTargetHotend(0, N); \
  1736. temp_hotend[N].soft_pwm_amount = 0; \
  1737. WRITE_HEATER_##N(LOW); \
  1738. }
  1739. #if HAS_TEMP_HOTEND
  1740. REPEAT(HOTENDS, DISABLE_HEATER);
  1741. #endif
  1742. #if HAS_HEATED_BED
  1743. temp_bed.target = 0;
  1744. temp_bed.soft_pwm_amount = 0;
  1745. WRITE_HEATER_BED(LOW);
  1746. #endif
  1747. #if HAS_HEATED_CHAMBER
  1748. temp_chamber.target = 0;
  1749. temp_chamber.soft_pwm_amount = 0;
  1750. WRITE_HEATER_CHAMBER(LOW);
  1751. #endif
  1752. }
  1753. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  1754. bool Temperature::over_autostart_threshold() {
  1755. #if HAS_HOTEND
  1756. HOTEND_LOOP() if (degTargetHotend(e) > (EXTRUDE_MINTEMP) / 2) return true;
  1757. #endif
  1758. return TERN0(HAS_HEATED_BED, degTargetBed() > BED_MINTEMP)
  1759. || TERN0(HAS_HEATED_CHAMBER, degTargetChamber() > CHAMBER_MINTEMP);
  1760. }
  1761. void Temperature::check_timer_autostart(const bool can_start, const bool can_stop) {
  1762. if (over_autostart_threshold()) {
  1763. if (can_start) startOrResumeJob();
  1764. }
  1765. else if (can_stop) {
  1766. print_job_timer.stop();
  1767. ui.reset_status();
  1768. }
  1769. }
  1770. #endif
  1771. #if ENABLED(PROBING_HEATERS_OFF)
  1772. void Temperature::pause(const bool p) {
  1773. if (p != paused) {
  1774. paused = p;
  1775. if (p) {
  1776. HOTEND_LOOP() hotend_idle[e].expire(); // Timeout immediately
  1777. TERN_(HAS_HEATED_BED, bed_idle.expire()); // Timeout immediately
  1778. }
  1779. else {
  1780. HOTEND_LOOP() reset_hotend_idle_timer(e);
  1781. TERN_(HAS_HEATED_BED, reset_bed_idle_timer());
  1782. }
  1783. }
  1784. }
  1785. #endif // PROBING_HEATERS_OFF
  1786. #if HAS_MAX6675
  1787. #ifndef THERMOCOUPLE_MAX_ERRORS
  1788. #define THERMOCOUPLE_MAX_ERRORS 15
  1789. #endif
  1790. int Temperature::read_max6675(
  1791. #if COUNT_6675 > 1
  1792. const uint8_t hindex
  1793. #endif
  1794. ) {
  1795. #if COUNT_6675 == 1
  1796. constexpr uint8_t hindex = 0;
  1797. #else
  1798. // Needed to return the correct temp when this is called too soon
  1799. static uint16_t max6675_temp_previous[COUNT_6675] = { 0 };
  1800. #endif
  1801. static uint8_t max6675_errors[COUNT_6675] = { 0 };
  1802. #define MAX6675_HEAT_INTERVAL 250UL
  1803. #if ENABLED(MAX6675_IS_MAX31855)
  1804. static uint32_t max6675_temp = 2000;
  1805. #define MAX6675_ERROR_MASK 7
  1806. #define MAX6675_DISCARD_BITS 18
  1807. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1808. #else
  1809. static uint16_t max6675_temp = 2000;
  1810. #define MAX6675_ERROR_MASK 4
  1811. #define MAX6675_DISCARD_BITS 3
  1812. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1813. #endif
  1814. // Return last-read value between readings
  1815. static millis_t next_max6675_ms[COUNT_6675] = { 0 };
  1816. millis_t ms = millis();
  1817. if (PENDING(ms, next_max6675_ms[hindex]))
  1818. return int(
  1819. #if COUNT_6675 == 1
  1820. max6675_temp
  1821. #else
  1822. max6675_temp_previous[hindex] // Need to return the correct previous value
  1823. #endif
  1824. );
  1825. next_max6675_ms[hindex] = ms + MAX6675_HEAT_INTERVAL;
  1826. #if ENABLED(MAX6675_IS_MAX31865)
  1827. max6675_temp = int(max31865.temperature(100, 400)); // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1828. #endif
  1829. //
  1830. // TODO: spiBegin, spiRec and spiInit doesn't work when soft spi is used.
  1831. //
  1832. #if !MAX6675_SEPARATE_SPI
  1833. spiBegin();
  1834. spiInit(MAX6675_SPEED_BITS);
  1835. #endif
  1836. #if COUNT_6675 > 1
  1837. #define WRITE_MAX6675(V) do{ switch (hindex) { case 1: WRITE(MAX6675_SS2_PIN, V); break; default: WRITE(MAX6675_SS_PIN, V); } }while(0)
  1838. #define SET_OUTPUT_MAX6675() do{ switch (hindex) { case 1: SET_OUTPUT(MAX6675_SS2_PIN); break; default: SET_OUTPUT(MAX6675_SS_PIN); } }while(0)
  1839. #elif ENABLED(HEATER_1_USES_MAX6675)
  1840. #define WRITE_MAX6675(V) WRITE(MAX6675_SS2_PIN, V)
  1841. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS2_PIN)
  1842. #else
  1843. #define WRITE_MAX6675(V) WRITE(MAX6675_SS_PIN, V)
  1844. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS_PIN)
  1845. #endif
  1846. SET_OUTPUT_MAX6675();
  1847. WRITE_MAX6675(LOW); // enable TT_MAX6675
  1848. DELAY_NS(100); // Ensure 100ns delay
  1849. // Read a big-endian temperature value
  1850. max6675_temp = 0;
  1851. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1852. max6675_temp |= (
  1853. #if MAX6675_SEPARATE_SPI
  1854. max6675_spi.receive()
  1855. #else
  1856. spiRec()
  1857. #endif
  1858. );
  1859. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1860. }
  1861. WRITE_MAX6675(HIGH); // disable TT_MAX6675
  1862. if (max6675_temp & MAX6675_ERROR_MASK) {
  1863. max6675_errors[hindex] += 1;
  1864. if (max6675_errors[hindex] > THERMOCOUPLE_MAX_ERRORS) {
  1865. SERIAL_ERROR_START();
  1866. SERIAL_ECHOPGM("Temp measurement error! ");
  1867. #if MAX6675_ERROR_MASK == 7
  1868. SERIAL_ECHOPGM("MAX31855 ");
  1869. if (max6675_temp & 1)
  1870. SERIAL_ECHOLNPGM("Open Circuit");
  1871. else if (max6675_temp & 2)
  1872. SERIAL_ECHOLNPGM("Short to GND");
  1873. else if (max6675_temp & 4)
  1874. SERIAL_ECHOLNPGM("Short to VCC");
  1875. #else
  1876. SERIAL_ECHOLNPGM("MAX6675");
  1877. #endif
  1878. // Thermocouple open
  1879. max6675_temp = 4 * (
  1880. #if COUNT_6675 > 1
  1881. hindex ? HEATER_1_MAX6675_TMAX : HEATER_0_MAX6675_TMAX
  1882. #elif ENABLED(HEATER_1_USES_MAX6675)
  1883. HEATER_1_MAX6675_TMAX
  1884. #else
  1885. HEATER_0_MAX6675_TMAX
  1886. #endif
  1887. );
  1888. }
  1889. else {
  1890. max6675_temp >>= MAX6675_DISCARD_BITS;
  1891. }
  1892. }
  1893. else {
  1894. max6675_temp >>= MAX6675_DISCARD_BITS;
  1895. max6675_errors[hindex] = 0;
  1896. }
  1897. #if ENABLED(MAX6675_IS_MAX31855)
  1898. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000; // Support negative temperature
  1899. #endif
  1900. #if COUNT_6675 > 1
  1901. max6675_temp_previous[hindex] = max6675_temp;
  1902. #endif
  1903. return int(max6675_temp);
  1904. }
  1905. #endif // HAS_MAX6675
  1906. /**
  1907. * Update raw temperatures
  1908. */
  1909. void Temperature::update_raw_temperatures() {
  1910. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  1911. temp_hotend[0].update();
  1912. #endif
  1913. #if HAS_TEMP_ADC_1
  1914. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1915. redundant_temperature_raw = temp_hotend[1].acc;
  1916. #elif DISABLED(HEATER_1_USES_MAX6675)
  1917. temp_hotend[1].update();
  1918. #endif
  1919. #endif
  1920. TERN_(HAS_TEMP_ADC_2, temp_hotend[2].update());
  1921. TERN_(HAS_TEMP_ADC_3, temp_hotend[3].update());
  1922. TERN_(HAS_TEMP_ADC_4, temp_hotend[4].update());
  1923. TERN_(HAS_TEMP_ADC_5, temp_hotend[5].update());
  1924. TERN_(HAS_TEMP_ADC_6, temp_hotend[6].update());
  1925. TERN_(HAS_TEMP_ADC_7, temp_hotend[7].update());
  1926. TERN_(HAS_HEATED_BED, temp_bed.update());
  1927. TERN_(HAS_TEMP_CHAMBER, temp_chamber.update());
  1928. TERN_(HAS_TEMP_PROBE, temp_probe.update());
  1929. TERN_(HAS_JOY_ADC_X, joystick.x.update());
  1930. TERN_(HAS_JOY_ADC_Y, joystick.y.update());
  1931. TERN_(HAS_JOY_ADC_Z, joystick.z.update());
  1932. raw_temps_ready = true;
  1933. }
  1934. void Temperature::readings_ready() {
  1935. // Update the raw values if they've been read. Else we could be updating them during reading.
  1936. if (!raw_temps_ready) update_raw_temperatures();
  1937. // Filament Sensor - can be read any time since IIR filtering is used
  1938. TERN_(FILAMENT_WIDTH_SENSOR, filwidth.reading_ready());
  1939. #if HAS_HOTEND
  1940. HOTEND_LOOP() temp_hotend[e].reset();
  1941. TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_hotend[1].reset());
  1942. #endif
  1943. TERN_(HAS_HEATED_BED, temp_bed.reset());
  1944. TERN_(HAS_TEMP_CHAMBER, temp_chamber.reset());
  1945. TERN_(HAS_TEMP_PROBE, temp_probe.reset());
  1946. TERN_(HAS_JOY_ADC_X, joystick.x.reset());
  1947. TERN_(HAS_JOY_ADC_Y, joystick.y.reset());
  1948. TERN_(HAS_JOY_ADC_Z, joystick.z.reset());
  1949. #if HAS_HOTEND
  1950. static constexpr int8_t temp_dir[] = {
  1951. TERN(HEATER_0_USES_MAX6675, 0, TEMPDIR(0))
  1952. #if HAS_MULTI_HOTEND
  1953. , TERN(HEATER_1_USES_MAX6675, 0, TEMPDIR(1))
  1954. #if HOTENDS > 2
  1955. #define _TEMPDIR(N) , TEMPDIR(N)
  1956. REPEAT_S(2, HOTENDS, _TEMPDIR)
  1957. #endif
  1958. #endif
  1959. };
  1960. LOOP_L_N(e, COUNT(temp_dir)) {
  1961. const int8_t tdir = temp_dir[e];
  1962. if (tdir) {
  1963. const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
  1964. const bool heater_on = (temp_hotend[e].target > 0
  1965. || TERN0(PIDTEMP, temp_hotend[e].soft_pwm_amount) > 0
  1966. );
  1967. if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_ind_t)e);
  1968. if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
  1969. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1970. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1971. #endif
  1972. min_temp_error((heater_ind_t)e);
  1973. }
  1974. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1975. else
  1976. consecutive_low_temperature_error[e] = 0;
  1977. #endif
  1978. }
  1979. }
  1980. #endif // HAS_HOTEND
  1981. #if HAS_HEATED_BED
  1982. #if TEMPDIR(BED) < 0
  1983. #define BEDCMP(A,B) ((A)<(B))
  1984. #else
  1985. #define BEDCMP(A,B) ((A)>(B))
  1986. #endif
  1987. const bool bed_on = temp_bed.target > 0
  1988. || TERN0(PIDTEMPBED, temp_bed.soft_pwm_amount) > 0
  1989. ;
  1990. if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
  1991. if (bed_on && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
  1992. #endif
  1993. #if HAS_HEATED_CHAMBER
  1994. #if TEMPDIR(CHAMBER) < 0
  1995. #define CHAMBERCMP(A,B) ((A)<(B))
  1996. #else
  1997. #define CHAMBERCMP(A,B) ((A)>(B))
  1998. #endif
  1999. const bool chamber_on = (temp_chamber.target > 0);
  2000. if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
  2001. if (chamber_on && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
  2002. #endif
  2003. }
  2004. /**
  2005. * Timer 0 is shared with millies so don't change the prescaler.
  2006. *
  2007. * On AVR this ISR uses the compare method so it runs at the base
  2008. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  2009. * in OCR0B above (128 or halfway between OVFs).
  2010. *
  2011. * - Manage PWM to all the heaters and fan
  2012. * - Prepare or Measure one of the raw ADC sensor values
  2013. * - Check new temperature values for MIN/MAX errors (kill on error)
  2014. * - Step the babysteps value for each axis towards 0
  2015. * - For PINS_DEBUGGING, monitor and report endstop pins
  2016. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  2017. * - Call planner.tick to count down its "ignore" time
  2018. */
  2019. HAL_TEMP_TIMER_ISR() {
  2020. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  2021. Temperature::tick();
  2022. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  2023. }
  2024. #if ENABLED(SLOW_PWM_HEATERS) && !defined(MIN_STATE_TIME)
  2025. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  2026. #endif
  2027. class SoftPWM {
  2028. public:
  2029. uint8_t count;
  2030. inline bool add(const uint8_t mask, const uint8_t amount) {
  2031. count = (count & mask) + amount; return (count > mask);
  2032. }
  2033. #if ENABLED(SLOW_PWM_HEATERS)
  2034. bool state_heater;
  2035. uint8_t state_timer_heater;
  2036. inline void dec() { if (state_timer_heater > 0) state_timer_heater--; }
  2037. inline bool ready(const bool v) {
  2038. const bool rdy = !state_timer_heater;
  2039. if (rdy && state_heater != v) {
  2040. state_heater = v;
  2041. state_timer_heater = MIN_STATE_TIME;
  2042. }
  2043. return rdy;
  2044. }
  2045. #endif
  2046. };
  2047. /**
  2048. * Handle various ~1KHz tasks associated with temperature
  2049. * - Heater PWM (~1KHz with scaler)
  2050. * - LCD Button polling (~500Hz)
  2051. * - Start / Read one ADC sensor
  2052. * - Advance Babysteps
  2053. * - Endstop polling
  2054. * - Planner clean buffer
  2055. */
  2056. void Temperature::tick() {
  2057. static int8_t temp_count = -1;
  2058. static ADCSensorState adc_sensor_state = StartupDelay;
  2059. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  2060. // avoid multiple loads of pwm_count
  2061. uint8_t pwm_count_tmp = pwm_count;
  2062. #if HAS_ADC_BUTTONS
  2063. static unsigned int raw_ADCKey_value = 0;
  2064. static bool ADCKey_pressed = false;
  2065. #endif
  2066. #if HAS_HOTEND
  2067. static SoftPWM soft_pwm_hotend[HOTENDS];
  2068. #endif
  2069. #if HAS_HEATED_BED
  2070. static SoftPWM soft_pwm_bed;
  2071. #endif
  2072. #if HAS_HEATED_CHAMBER
  2073. static SoftPWM soft_pwm_chamber;
  2074. #endif
  2075. #if DISABLED(SLOW_PWM_HEATERS)
  2076. #if HAS_HOTEND || HAS_HEATED_BED || HAS_HEATED_CHAMBER
  2077. constexpr uint8_t pwm_mask =
  2078. #if ENABLED(SOFT_PWM_DITHER)
  2079. _BV(SOFT_PWM_SCALE) - 1
  2080. #else
  2081. 0
  2082. #endif
  2083. ;
  2084. #define _PWM_MOD(N,S,T) do{ \
  2085. const bool on = S.add(pwm_mask, T.soft_pwm_amount); \
  2086. WRITE_HEATER_##N(on); \
  2087. }while(0)
  2088. #endif
  2089. /**
  2090. * Standard heater PWM modulation
  2091. */
  2092. if (pwm_count_tmp >= 127) {
  2093. pwm_count_tmp -= 127;
  2094. #if HAS_HOTEND
  2095. #define _PWM_MOD_E(N) _PWM_MOD(N,soft_pwm_hotend[N],temp_hotend[N]);
  2096. REPEAT(HOTENDS, _PWM_MOD_E);
  2097. #endif
  2098. #if HAS_HEATED_BED
  2099. _PWM_MOD(BED,soft_pwm_bed,temp_bed);
  2100. #endif
  2101. #if HAS_HEATED_CHAMBER
  2102. _PWM_MOD(CHAMBER,soft_pwm_chamber,temp_chamber);
  2103. #endif
  2104. #if ENABLED(FAN_SOFT_PWM)
  2105. #define _FAN_PWM(N) do{ \
  2106. uint8_t &spcf = soft_pwm_count_fan[N]; \
  2107. spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \
  2108. WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \
  2109. }while(0)
  2110. #if HAS_FAN0
  2111. _FAN_PWM(0);
  2112. #endif
  2113. #if HAS_FAN1
  2114. _FAN_PWM(1);
  2115. #endif
  2116. #if HAS_FAN2
  2117. _FAN_PWM(2);
  2118. #endif
  2119. #if HAS_FAN3
  2120. _FAN_PWM(3);
  2121. #endif
  2122. #if HAS_FAN4
  2123. _FAN_PWM(4);
  2124. #endif
  2125. #if HAS_FAN5
  2126. _FAN_PWM(5);
  2127. #endif
  2128. #if HAS_FAN6
  2129. _FAN_PWM(6);
  2130. #endif
  2131. #if HAS_FAN7
  2132. _FAN_PWM(7);
  2133. #endif
  2134. #endif
  2135. }
  2136. else {
  2137. #define _PWM_LOW(N,S) do{ if (S.count <= pwm_count_tmp) WRITE_HEATER_##N(LOW); }while(0)
  2138. #if HAS_HOTEND
  2139. #define _PWM_LOW_E(N) _PWM_LOW(N, soft_pwm_hotend[N]);
  2140. REPEAT(HOTENDS, _PWM_LOW_E);
  2141. #endif
  2142. #if HAS_HEATED_BED
  2143. _PWM_LOW(BED, soft_pwm_bed);
  2144. #endif
  2145. #if HAS_HEATED_CHAMBER
  2146. _PWM_LOW(CHAMBER, soft_pwm_chamber);
  2147. #endif
  2148. #if ENABLED(FAN_SOFT_PWM)
  2149. #if HAS_FAN0
  2150. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2151. #endif
  2152. #if HAS_FAN1
  2153. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2154. #endif
  2155. #if HAS_FAN2
  2156. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2157. #endif
  2158. #if HAS_FAN3
  2159. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2160. #endif
  2161. #if HAS_FAN4
  2162. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2163. #endif
  2164. #if HAS_FAN5
  2165. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2166. #endif
  2167. #if HAS_FAN6
  2168. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2169. #endif
  2170. #if HAS_FAN7
  2171. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2172. #endif
  2173. #endif
  2174. }
  2175. // SOFT_PWM_SCALE to frequency:
  2176. //
  2177. // 0: 16000000/64/256/128 = 7.6294 Hz
  2178. // 1: / 64 = 15.2588 Hz
  2179. // 2: / 32 = 30.5176 Hz
  2180. // 3: / 16 = 61.0352 Hz
  2181. // 4: / 8 = 122.0703 Hz
  2182. // 5: / 4 = 244.1406 Hz
  2183. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2184. #else // SLOW_PWM_HEATERS
  2185. /**
  2186. * SLOW PWM HEATERS
  2187. *
  2188. * For relay-driven heaters
  2189. */
  2190. #define _SLOW_SET(NR,PWM,V) do{ if (PWM.ready(V)) WRITE_HEATER_##NR(V); }while(0)
  2191. #define _SLOW_PWM(NR,PWM,SRC) do{ PWM.count = SRC.soft_pwm_amount; _SLOW_SET(NR,PWM,(PWM.count > 0)); }while(0)
  2192. #define _PWM_OFF(NR,PWM) do{ if (PWM.count < slow_pwm_count) _SLOW_SET(NR,PWM,0); }while(0)
  2193. static uint8_t slow_pwm_count = 0;
  2194. if (slow_pwm_count == 0) {
  2195. #if HAS_HOTEND
  2196. #define _SLOW_PWM_E(N) _SLOW_PWM(N, soft_pwm_hotend[N], temp_hotend[N]);
  2197. REPEAT(HOTENDS, _SLOW_PWM_E);
  2198. #endif
  2199. #if HAS_HEATED_BED
  2200. _SLOW_PWM(BED, soft_pwm_bed, temp_bed);
  2201. #endif
  2202. #if HAS_HEATED_CHAMBER
  2203. _SLOW_PWM(CHAMBER, soft_pwm_chamber, temp_chamber);
  2204. #endif
  2205. } // slow_pwm_count == 0
  2206. #if HAS_HOTEND
  2207. #define _PWM_OFF_E(N) _PWM_OFF(N, soft_pwm_hotend[N]);
  2208. REPEAT(HOTENDS, _PWM_OFF_E);
  2209. #endif
  2210. #if HAS_HEATED_BED
  2211. _PWM_OFF(BED, soft_pwm_bed);
  2212. #endif
  2213. #if HAS_HEATED_CHAMBER
  2214. _PWM_OFF(CHAMBER, soft_pwm_chamber);
  2215. #endif
  2216. #if ENABLED(FAN_SOFT_PWM)
  2217. if (pwm_count_tmp >= 127) {
  2218. pwm_count_tmp = 0;
  2219. #define _PWM_FAN(N) do{ \
  2220. soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \
  2221. WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \
  2222. }while(0)
  2223. #if HAS_FAN0
  2224. _PWM_FAN(0);
  2225. #endif
  2226. #if HAS_FAN1
  2227. _PWM_FAN(1);
  2228. #endif
  2229. #if HAS_FAN2
  2230. _PWM_FAN(2);
  2231. #endif
  2232. #if HAS_FAN3
  2233. _FAN_PWM(3);
  2234. #endif
  2235. #if HAS_FAN4
  2236. _FAN_PWM(4);
  2237. #endif
  2238. #if HAS_FAN5
  2239. _FAN_PWM(5);
  2240. #endif
  2241. #if HAS_FAN6
  2242. _FAN_PWM(6);
  2243. #endif
  2244. #if HAS_FAN7
  2245. _FAN_PWM(7);
  2246. #endif
  2247. }
  2248. #if HAS_FAN0
  2249. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2250. #endif
  2251. #if HAS_FAN1
  2252. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2253. #endif
  2254. #if HAS_FAN2
  2255. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2256. #endif
  2257. #if HAS_FAN3
  2258. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2259. #endif
  2260. #if HAS_FAN4
  2261. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2262. #endif
  2263. #if HAS_FAN5
  2264. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2265. #endif
  2266. #if HAS_FAN6
  2267. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2268. #endif
  2269. #if HAS_FAN7
  2270. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2271. #endif
  2272. #endif // FAN_SOFT_PWM
  2273. // SOFT_PWM_SCALE to frequency:
  2274. //
  2275. // 0: 16000000/64/256/128 = 7.6294 Hz
  2276. // 1: / 64 = 15.2588 Hz
  2277. // 2: / 32 = 30.5176 Hz
  2278. // 3: / 16 = 61.0352 Hz
  2279. // 4: / 8 = 122.0703 Hz
  2280. // 5: / 4 = 244.1406 Hz
  2281. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2282. // increment slow_pwm_count only every 64th pwm_count,
  2283. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  2284. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  2285. slow_pwm_count++;
  2286. slow_pwm_count &= 0x7F;
  2287. #if HAS_HOTEND
  2288. HOTEND_LOOP() soft_pwm_hotend[e].dec();
  2289. #endif
  2290. TERN_(HAS_HEATED_BED, soft_pwm_bed.dec());
  2291. TERN_(HAS_HEATED_CHAMBER, soft_pwm_chamber.dec());
  2292. }
  2293. #endif // SLOW_PWM_HEATERS
  2294. //
  2295. // Update lcd buttons 488 times per second
  2296. //
  2297. static bool do_buttons;
  2298. if ((do_buttons ^= true)) ui.update_buttons();
  2299. /**
  2300. * One sensor is sampled on every other call of the ISR.
  2301. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  2302. *
  2303. * On each Prepare pass, ADC is started for a sensor pin.
  2304. * On the next pass, the ADC value is read and accumulated.
  2305. *
  2306. * This gives each ADC 0.9765ms to charge up.
  2307. */
  2308. #define ACCUMULATE_ADC(obj) do{ \
  2309. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
  2310. else obj.sample(HAL_READ_ADC()); \
  2311. }while(0)
  2312. ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
  2313. switch (adc_sensor_state) {
  2314. case SensorsReady: {
  2315. // All sensors have been read. Stay in this state for a few
  2316. // ISRs to save on calls to temp update/checking code below.
  2317. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  2318. static uint8_t delay_count = 0;
  2319. if (extra_loops > 0) {
  2320. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  2321. if (--delay_count) // While delaying...
  2322. next_sensor_state = SensorsReady; // retain this state (else, next state will be 0)
  2323. break;
  2324. }
  2325. else {
  2326. adc_sensor_state = StartSampling; // Fall-through to start sampling
  2327. next_sensor_state = (ADCSensorState)(int(StartSampling) + 1);
  2328. }
  2329. }
  2330. case StartSampling: // Start of sampling loops. Do updates/checks.
  2331. if (++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  2332. temp_count = 0;
  2333. readings_ready();
  2334. }
  2335. break;
  2336. #if HAS_TEMP_ADC_0
  2337. case PrepareTemp_0: HAL_START_ADC(TEMP_0_PIN); break;
  2338. case MeasureTemp_0: ACCUMULATE_ADC(temp_hotend[0]); break;
  2339. #endif
  2340. #if HAS_HEATED_BED
  2341. case PrepareTemp_BED: HAL_START_ADC(TEMP_BED_PIN); break;
  2342. case MeasureTemp_BED: ACCUMULATE_ADC(temp_bed); break;
  2343. #endif
  2344. #if HAS_TEMP_CHAMBER
  2345. case PrepareTemp_CHAMBER: HAL_START_ADC(TEMP_CHAMBER_PIN); break;
  2346. case MeasureTemp_CHAMBER: ACCUMULATE_ADC(temp_chamber); break;
  2347. #endif
  2348. #if HAS_TEMP_PROBE
  2349. case PrepareTemp_PROBE: HAL_START_ADC(TEMP_PROBE_PIN); break;
  2350. case MeasureTemp_PROBE: ACCUMULATE_ADC(temp_probe); break;
  2351. #endif
  2352. #if HAS_TEMP_ADC_1
  2353. case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break;
  2354. case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break;
  2355. #endif
  2356. #if HAS_TEMP_ADC_2
  2357. case PrepareTemp_2: HAL_START_ADC(TEMP_2_PIN); break;
  2358. case MeasureTemp_2: ACCUMULATE_ADC(temp_hotend[2]); break;
  2359. #endif
  2360. #if HAS_TEMP_ADC_3
  2361. case PrepareTemp_3: HAL_START_ADC(TEMP_3_PIN); break;
  2362. case MeasureTemp_3: ACCUMULATE_ADC(temp_hotend[3]); break;
  2363. #endif
  2364. #if HAS_TEMP_ADC_4
  2365. case PrepareTemp_4: HAL_START_ADC(TEMP_4_PIN); break;
  2366. case MeasureTemp_4: ACCUMULATE_ADC(temp_hotend[4]); break;
  2367. #endif
  2368. #if HAS_TEMP_ADC_5
  2369. case PrepareTemp_5: HAL_START_ADC(TEMP_5_PIN); break;
  2370. case MeasureTemp_5: ACCUMULATE_ADC(temp_hotend[5]); break;
  2371. #endif
  2372. #if HAS_TEMP_ADC_6
  2373. case PrepareTemp_6: HAL_START_ADC(TEMP_6_PIN); break;
  2374. case MeasureTemp_6: ACCUMULATE_ADC(temp_hotend[6]); break;
  2375. #endif
  2376. #if HAS_TEMP_ADC_7
  2377. case PrepareTemp_7: HAL_START_ADC(TEMP_7_PIN); break;
  2378. case MeasureTemp_7: ACCUMULATE_ADC(temp_hotend[7]); break;
  2379. #endif
  2380. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  2381. case Prepare_FILWIDTH: HAL_START_ADC(FILWIDTH_PIN); break;
  2382. case Measure_FILWIDTH:
  2383. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2384. else filwidth.accumulate(HAL_READ_ADC());
  2385. break;
  2386. #endif
  2387. #if ENABLED(POWER_MONITOR_CURRENT)
  2388. case Prepare_POWER_MONITOR_CURRENT:
  2389. HAL_START_ADC(POWER_MONITOR_CURRENT_PIN);
  2390. break;
  2391. case Measure_POWER_MONITOR_CURRENT:
  2392. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2393. else power_monitor.add_current_sample(HAL_READ_ADC());
  2394. break;
  2395. #endif
  2396. #if ENABLED(POWER_MONITOR_VOLTAGE)
  2397. case Prepare_POWER_MONITOR_VOLTAGE:
  2398. HAL_START_ADC(POWER_MONITOR_VOLTAGE_PIN);
  2399. break;
  2400. case Measure_POWER_MONITOR_VOLTAGE:
  2401. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2402. else power_monitor.add_voltage_sample(HAL_READ_ADC());
  2403. break;
  2404. #endif
  2405. #if HAS_JOY_ADC_X
  2406. case PrepareJoy_X: HAL_START_ADC(JOY_X_PIN); break;
  2407. case MeasureJoy_X: ACCUMULATE_ADC(joystick.x); break;
  2408. #endif
  2409. #if HAS_JOY_ADC_Y
  2410. case PrepareJoy_Y: HAL_START_ADC(JOY_Y_PIN); break;
  2411. case MeasureJoy_Y: ACCUMULATE_ADC(joystick.y); break;
  2412. #endif
  2413. #if HAS_JOY_ADC_Z
  2414. case PrepareJoy_Z: HAL_START_ADC(JOY_Z_PIN); break;
  2415. case MeasureJoy_Z: ACCUMULATE_ADC(joystick.z); break;
  2416. #endif
  2417. #if HAS_ADC_BUTTONS
  2418. #ifndef ADC_BUTTON_DEBOUNCE_DELAY
  2419. #define ADC_BUTTON_DEBOUNCE_DELAY 16
  2420. #endif
  2421. case Prepare_ADC_KEY: HAL_START_ADC(ADC_KEYPAD_PIN); break;
  2422. case Measure_ADC_KEY:
  2423. if (!HAL_ADC_READY())
  2424. next_sensor_state = adc_sensor_state; // redo this state
  2425. else if (ADCKey_count < ADC_BUTTON_DEBOUNCE_DELAY) {
  2426. raw_ADCKey_value = HAL_READ_ADC();
  2427. if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
  2428. NOMORE(current_ADCKey_raw, raw_ADCKey_value);
  2429. ADCKey_count++;
  2430. }
  2431. else { //ADC Key release
  2432. if (ADCKey_count > 0) ADCKey_count++; else ADCKey_pressed = false;
  2433. if (ADCKey_pressed) {
  2434. ADCKey_count = 0;
  2435. current_ADCKey_raw = HAL_ADC_RANGE;
  2436. }
  2437. }
  2438. }
  2439. if (ADCKey_count == ADC_BUTTON_DEBOUNCE_DELAY) ADCKey_pressed = true;
  2440. break;
  2441. #endif // HAS_ADC_BUTTONS
  2442. case StartupDelay: break;
  2443. } // switch(adc_sensor_state)
  2444. // Go to the next state
  2445. adc_sensor_state = next_sensor_state;
  2446. //
  2447. // Additional ~1KHz Tasks
  2448. //
  2449. #if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
  2450. babystep.task();
  2451. #endif
  2452. // Poll endstops state, if required
  2453. endstops.poll();
  2454. // Periodically call the planner timer
  2455. planner.tick();
  2456. }
  2457. #if HAS_TEMP_SENSOR
  2458. #include "../gcode/gcode.h"
  2459. static void print_heater_state(const float &c, const float &t
  2460. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2461. , const float r
  2462. #endif
  2463. , const heater_ind_t e=INDEX_NONE
  2464. ) {
  2465. char k;
  2466. switch (e) {
  2467. #if HAS_TEMP_CHAMBER
  2468. case H_CHAMBER: k = 'C'; break;
  2469. #endif
  2470. #if HAS_TEMP_PROBE
  2471. case H_PROBE: k = 'P'; break;
  2472. #endif
  2473. #if HAS_TEMP_HOTEND
  2474. default: k = 'T'; break;
  2475. #if HAS_HEATED_BED
  2476. case H_BED: k = 'B'; break;
  2477. #endif
  2478. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2479. case H_REDUNDANT: k = 'R'; break;
  2480. #endif
  2481. #elif HAS_HEATED_BED
  2482. default: k = 'B'; break;
  2483. #endif
  2484. }
  2485. SERIAL_CHAR(' ');
  2486. SERIAL_CHAR(k);
  2487. #if HAS_MULTI_HOTEND
  2488. if (e >= 0) SERIAL_CHAR('0' + e);
  2489. #endif
  2490. SERIAL_CHAR(':');
  2491. SERIAL_ECHO(c);
  2492. SERIAL_ECHOPAIR(" /" , t);
  2493. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2494. SERIAL_ECHOPAIR(" (", r * RECIPROCAL(OVERSAMPLENR));
  2495. SERIAL_CHAR(')');
  2496. #endif
  2497. delay(2);
  2498. }
  2499. void Temperature::print_heater_states(const uint8_t target_extruder
  2500. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2501. , const bool include_r/*=false*/
  2502. #endif
  2503. ) {
  2504. #if HAS_TEMP_HOTEND
  2505. print_heater_state(degHotend(target_extruder), degTargetHotend(target_extruder)
  2506. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2507. , rawHotendTemp(target_extruder)
  2508. #endif
  2509. );
  2510. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2511. if (include_r) print_heater_state(redundant_temperature, degTargetHotend(target_extruder)
  2512. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2513. , redundant_temperature_raw
  2514. #endif
  2515. , H_REDUNDANT
  2516. );
  2517. #endif
  2518. #endif
  2519. #if HAS_HEATED_BED
  2520. print_heater_state(degBed(), degTargetBed()
  2521. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2522. , rawBedTemp()
  2523. #endif
  2524. , H_BED
  2525. );
  2526. #endif
  2527. #if HAS_TEMP_CHAMBER
  2528. print_heater_state(degChamber()
  2529. #if HAS_HEATED_CHAMBER
  2530. , degTargetChamber()
  2531. #else
  2532. , 0
  2533. #endif
  2534. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2535. , rawChamberTemp()
  2536. #endif
  2537. , H_CHAMBER
  2538. );
  2539. #endif // HAS_TEMP_CHAMBER
  2540. #if HAS_TEMP_PROBE
  2541. print_heater_state(degProbe(), 0
  2542. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2543. , rawProbeTemp()
  2544. #endif
  2545. , H_PROBE
  2546. );
  2547. #endif // HAS_TEMP_PROBE
  2548. #if HAS_MULTI_HOTEND
  2549. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2550. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2551. , rawHotendTemp(e)
  2552. #endif
  2553. , (heater_ind_t)e
  2554. );
  2555. #endif
  2556. SERIAL_ECHOPAIR(" @:", getHeaterPower((heater_ind_t)target_extruder));
  2557. #if HAS_HEATED_BED
  2558. SERIAL_ECHOPAIR(" B@:", getHeaterPower(H_BED));
  2559. #endif
  2560. #if HAS_HEATED_CHAMBER
  2561. SERIAL_ECHOPAIR(" C@:", getHeaterPower(H_CHAMBER));
  2562. #endif
  2563. #if HAS_MULTI_HOTEND
  2564. HOTEND_LOOP() {
  2565. SERIAL_ECHOPAIR(" @", e);
  2566. SERIAL_CHAR(':');
  2567. SERIAL_ECHO(getHeaterPower((heater_ind_t)e));
  2568. }
  2569. #endif
  2570. }
  2571. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2572. uint8_t Temperature::auto_report_temp_interval;
  2573. millis_t Temperature::next_temp_report_ms;
  2574. void Temperature::auto_report_temperatures() {
  2575. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2576. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2577. PORT_REDIRECT(SERIAL_BOTH);
  2578. print_heater_states(active_extruder);
  2579. SERIAL_EOL();
  2580. }
  2581. }
  2582. #endif // AUTO_REPORT_TEMPERATURES
  2583. #if HAS_HOTEND && HAS_DISPLAY
  2584. void Temperature::set_heating_message(const uint8_t e) {
  2585. const bool heating = isHeatingHotend(e);
  2586. ui.status_printf_P(0,
  2587. #if HAS_MULTI_HOTEND
  2588. PSTR("E%c " S_FMT), '1' + e
  2589. #else
  2590. PSTR("E " S_FMT)
  2591. #endif
  2592. , heating ? GET_TEXT(MSG_HEATING) : GET_TEXT(MSG_COOLING)
  2593. );
  2594. }
  2595. #endif
  2596. #if HAS_TEMP_HOTEND
  2597. #ifndef MIN_COOLING_SLOPE_DEG
  2598. #define MIN_COOLING_SLOPE_DEG 1.50
  2599. #endif
  2600. #ifndef MIN_COOLING_SLOPE_TIME
  2601. #define MIN_COOLING_SLOPE_TIME 60
  2602. #endif
  2603. bool Temperature::wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling/*=true*/
  2604. #if G26_CLICK_CAN_CANCEL
  2605. , const bool click_to_cancel/*=false*/
  2606. #endif
  2607. ) {
  2608. #if ENABLED(AUTOTEMP)
  2609. REMEMBER(1, planner.autotemp_enabled, false);
  2610. #endif
  2611. #if TEMP_RESIDENCY_TIME > 0
  2612. millis_t residency_start_ms = 0;
  2613. bool first_loop = true;
  2614. // Loop until the temperature has stabilized
  2615. #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_RESIDENCY_TIME)))
  2616. #else
  2617. // Loop until the temperature is very close target
  2618. #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
  2619. #endif
  2620. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2621. KEEPALIVE_STATE(NOT_BUSY);
  2622. #endif
  2623. #if ENABLED(PRINTER_EVENT_LEDS)
  2624. const float start_temp = degHotend(target_extruder);
  2625. printerEventLEDs.onHotendHeatingStart();
  2626. #endif
  2627. float target_temp = -1.0, old_temp = 9999.0;
  2628. bool wants_to_cool = false;
  2629. wait_for_heatup = true;
  2630. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2631. do {
  2632. // Target temperature might be changed during the loop
  2633. if (target_temp != degTargetHotend(target_extruder)) {
  2634. wants_to_cool = isCoolingHotend(target_extruder);
  2635. target_temp = degTargetHotend(target_extruder);
  2636. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2637. if (no_wait_for_cooling && wants_to_cool) break;
  2638. }
  2639. now = millis();
  2640. if (ELAPSED(now, next_temp_ms)) { // Print temp & remaining time every 1s while waiting
  2641. next_temp_ms = now + 1000UL;
  2642. print_heater_states(target_extruder);
  2643. #if TEMP_RESIDENCY_TIME > 0
  2644. SERIAL_ECHOPGM(" W:");
  2645. if (residency_start_ms)
  2646. SERIAL_ECHO(long((SEC_TO_MS(TEMP_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2647. else
  2648. SERIAL_CHAR('?');
  2649. #endif
  2650. SERIAL_EOL();
  2651. }
  2652. idle();
  2653. gcode.reset_stepper_timeout(); // Keep steppers powered
  2654. const float temp = degHotend(target_extruder);
  2655. #if ENABLED(PRINTER_EVENT_LEDS)
  2656. // Gradually change LED strip from violet to red as nozzle heats up
  2657. if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp);
  2658. #endif
  2659. #if TEMP_RESIDENCY_TIME > 0
  2660. const float temp_diff = ABS(target_temp - temp);
  2661. if (!residency_start_ms) {
  2662. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
  2663. if (temp_diff < TEMP_WINDOW) {
  2664. residency_start_ms = now;
  2665. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_RESIDENCY_TIME);
  2666. }
  2667. }
  2668. else if (temp_diff > TEMP_HYSTERESIS) {
  2669. // Restart the timer whenever the temperature falls outside the hysteresis.
  2670. residency_start_ms = now;
  2671. }
  2672. first_loop = false;
  2673. #endif
  2674. // Prevent a wait-forever situation if R is misused i.e. M109 R0
  2675. if (wants_to_cool) {
  2676. // break after MIN_COOLING_SLOPE_TIME seconds
  2677. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG
  2678. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2679. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG)) break;
  2680. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME;
  2681. old_temp = temp;
  2682. }
  2683. }
  2684. #if G26_CLICK_CAN_CANCEL
  2685. if (click_to_cancel && ui.use_click()) {
  2686. wait_for_heatup = false;
  2687. ui.quick_feedback();
  2688. }
  2689. #endif
  2690. } while (wait_for_heatup && TEMP_CONDITIONS);
  2691. if (wait_for_heatup) {
  2692. #if ENABLED(DWIN_CREALITY_LCD)
  2693. HMI_flag.heat_flag = 0;
  2694. duration_t elapsed = print_job_timer.duration(); // print timer
  2695. dwin_heat_time = elapsed.value;
  2696. #else
  2697. ui.reset_status();
  2698. #endif
  2699. TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onHeatingDone());
  2700. }
  2701. return wait_for_heatup;
  2702. }
  2703. #endif // HAS_TEMP_HOTEND
  2704. #if HAS_HEATED_BED
  2705. #ifndef MIN_COOLING_SLOPE_DEG_BED
  2706. #define MIN_COOLING_SLOPE_DEG_BED 1.00
  2707. #endif
  2708. #ifndef MIN_COOLING_SLOPE_TIME_BED
  2709. #define MIN_COOLING_SLOPE_TIME_BED 60
  2710. #endif
  2711. bool Temperature::wait_for_bed(const bool no_wait_for_cooling/*=true*/
  2712. #if G26_CLICK_CAN_CANCEL
  2713. , const bool click_to_cancel/*=false*/
  2714. #endif
  2715. ) {
  2716. #if TEMP_BED_RESIDENCY_TIME > 0
  2717. millis_t residency_start_ms = 0;
  2718. bool first_loop = true;
  2719. // Loop until the temperature has stabilized
  2720. #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_BED_RESIDENCY_TIME)))
  2721. #else
  2722. // Loop until the temperature is very close target
  2723. #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
  2724. #endif
  2725. float target_temp = -1, old_temp = 9999;
  2726. bool wants_to_cool = false;
  2727. wait_for_heatup = true;
  2728. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2729. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2730. KEEPALIVE_STATE(NOT_BUSY);
  2731. #endif
  2732. #if ENABLED(PRINTER_EVENT_LEDS)
  2733. const float start_temp = degBed();
  2734. printerEventLEDs.onBedHeatingStart();
  2735. #endif
  2736. do {
  2737. // Target temperature might be changed during the loop
  2738. if (target_temp != degTargetBed()) {
  2739. wants_to_cool = isCoolingBed();
  2740. target_temp = degTargetBed();
  2741. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2742. if (no_wait_for_cooling && wants_to_cool) break;
  2743. }
  2744. now = millis();
  2745. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2746. next_temp_ms = now + 1000UL;
  2747. print_heater_states(active_extruder);
  2748. #if TEMP_BED_RESIDENCY_TIME > 0
  2749. SERIAL_ECHOPGM(" W:");
  2750. if (residency_start_ms)
  2751. SERIAL_ECHO(long((SEC_TO_MS(TEMP_BED_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2752. else
  2753. SERIAL_CHAR('?');
  2754. #endif
  2755. SERIAL_EOL();
  2756. }
  2757. idle();
  2758. gcode.reset_stepper_timeout(); // Keep steppers powered
  2759. const float temp = degBed();
  2760. #if ENABLED(PRINTER_EVENT_LEDS)
  2761. // Gradually change LED strip from blue to violet as bed heats up
  2762. if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp);
  2763. #endif
  2764. #if TEMP_BED_RESIDENCY_TIME > 0
  2765. const float temp_diff = ABS(target_temp - temp);
  2766. if (!residency_start_ms) {
  2767. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
  2768. if (temp_diff < TEMP_BED_WINDOW) {
  2769. residency_start_ms = now;
  2770. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_BED_RESIDENCY_TIME);
  2771. }
  2772. }
  2773. else if (temp_diff > TEMP_BED_HYSTERESIS) {
  2774. // Restart the timer whenever the temperature falls outside the hysteresis.
  2775. residency_start_ms = now;
  2776. }
  2777. #endif // TEMP_BED_RESIDENCY_TIME > 0
  2778. // Prevent a wait-forever situation if R is misused i.e. M190 R0
  2779. if (wants_to_cool) {
  2780. // Break after MIN_COOLING_SLOPE_TIME_BED seconds
  2781. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED
  2782. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2783. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_BED)) break;
  2784. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED;
  2785. old_temp = temp;
  2786. }
  2787. }
  2788. #if G26_CLICK_CAN_CANCEL
  2789. if (click_to_cancel && ui.use_click()) {
  2790. wait_for_heatup = false;
  2791. ui.quick_feedback();
  2792. }
  2793. #endif
  2794. #if TEMP_BED_RESIDENCY_TIME > 0
  2795. first_loop = false;
  2796. #endif
  2797. } while (wait_for_heatup && TEMP_BED_CONDITIONS);
  2798. if (wait_for_heatup) ui.reset_status();
  2799. return wait_for_heatup;
  2800. }
  2801. void Temperature::wait_for_bed_heating() {
  2802. if (isHeatingBed()) {
  2803. SERIAL_ECHOLNPGM("Wait for bed heating...");
  2804. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2805. wait_for_bed();
  2806. ui.reset_status();
  2807. }
  2808. }
  2809. #endif // HAS_HEATED_BED
  2810. #if HAS_HEATED_CHAMBER
  2811. #ifndef MIN_COOLING_SLOPE_DEG_CHAMBER
  2812. #define MIN_COOLING_SLOPE_DEG_CHAMBER 1.50
  2813. #endif
  2814. #ifndef MIN_COOLING_SLOPE_TIME_CHAMBER
  2815. #define MIN_COOLING_SLOPE_TIME_CHAMBER 60
  2816. #endif
  2817. bool Temperature::wait_for_chamber(const bool no_wait_for_cooling/*=true*/) {
  2818. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2819. millis_t residency_start_ms = 0;
  2820. bool first_loop = true;
  2821. // Loop until the temperature has stabilized
  2822. #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME)))
  2823. #else
  2824. // Loop until the temperature is very close target
  2825. #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
  2826. #endif
  2827. float target_temp = -1, old_temp = 9999;
  2828. bool wants_to_cool = false;
  2829. wait_for_heatup = true;
  2830. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2831. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2832. KEEPALIVE_STATE(NOT_BUSY);
  2833. #endif
  2834. do {
  2835. // Target temperature might be changed during the loop
  2836. if (target_temp != degTargetChamber()) {
  2837. wants_to_cool = isCoolingChamber();
  2838. target_temp = degTargetChamber();
  2839. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2840. if (no_wait_for_cooling && wants_to_cool) break;
  2841. }
  2842. now = millis();
  2843. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2844. next_temp_ms = now + 1000UL;
  2845. print_heater_states(active_extruder);
  2846. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2847. SERIAL_ECHOPGM(" W:");
  2848. if (residency_start_ms)
  2849. SERIAL_ECHO(long((SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2850. else
  2851. SERIAL_CHAR('?');
  2852. #endif
  2853. SERIAL_EOL();
  2854. }
  2855. idle();
  2856. gcode.reset_stepper_timeout(); // Keep steppers powered
  2857. const float temp = degChamber();
  2858. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2859. const float temp_diff = ABS(target_temp - temp);
  2860. if (!residency_start_ms) {
  2861. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
  2862. if (temp_diff < TEMP_CHAMBER_WINDOW) {
  2863. residency_start_ms = now;
  2864. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME);
  2865. }
  2866. }
  2867. else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {
  2868. // Restart the timer whenever the temperature falls outside the hysteresis.
  2869. residency_start_ms = now;
  2870. }
  2871. first_loop = false;
  2872. #endif // TEMP_CHAMBER_RESIDENCY_TIME > 0
  2873. // Prevent a wait-forever situation if R is misused i.e. M191 R0
  2874. if (wants_to_cool) {
  2875. // Break after MIN_COOLING_SLOPE_TIME_CHAMBER seconds
  2876. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_CHAMBER
  2877. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2878. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_CHAMBER)) break;
  2879. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_CHAMBER;
  2880. old_temp = temp;
  2881. }
  2882. }
  2883. } while (wait_for_heatup && TEMP_CHAMBER_CONDITIONS);
  2884. if (wait_for_heatup) ui.reset_status();
  2885. return wait_for_heatup;
  2886. }
  2887. #endif // HAS_HEATED_CHAMBER
  2888. #endif // HAS_TEMP_SENSOR