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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369
  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 void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)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 void* 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 int16_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. uint8_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. 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
  760. #if ENABLED(PID_EXTRUSION_SCALING)
  761. , STR_PID_DEBUG_CTERM, work_pid[ee].Kc
  762. #endif
  763. );
  764. #endif
  765. SERIAL_EOL();
  766. }
  767. #endif // PID_DEBUG
  768. #else // No PID enabled
  769. const bool is_idling = TERN0(HEATER_IDLE_HANDLER, hotend_idle[ee].timed_out);
  770. const float pid_output = (!is_idling && temp_hotend[ee].celsius < temp_hotend[ee].target) ? BANG_MAX : 0;
  771. #endif
  772. return pid_output;
  773. }
  774. #endif // HOTENDS
  775. #if ENABLED(PIDTEMPBED)
  776. float Temperature::get_pid_output_bed() {
  777. #if DISABLED(PID_OPENLOOP)
  778. static PID_t work_pid{0};
  779. static float temp_iState = 0, temp_dState = 0;
  780. static bool pid_reset = true;
  781. float pid_output = 0;
  782. const float max_power_over_i_gain = float(MAX_BED_POWER) / temp_bed.pid.Ki - float(MIN_BED_POWER),
  783. pid_error = temp_bed.target - temp_bed.celsius;
  784. if (!temp_bed.target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
  785. pid_output = 0;
  786. pid_reset = true;
  787. }
  788. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  789. pid_output = MAX_BED_POWER;
  790. pid_reset = true;
  791. }
  792. else {
  793. if (pid_reset) {
  794. temp_iState = 0.0;
  795. work_pid.Kd = 0.0;
  796. pid_reset = false;
  797. }
  798. temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
  799. work_pid.Kp = temp_bed.pid.Kp * pid_error;
  800. work_pid.Ki = temp_bed.pid.Ki * temp_iState;
  801. work_pid.Kd = work_pid.Kd + PID_K2 * (temp_bed.pid.Kd * (temp_dState - temp_bed.celsius) - work_pid.Kd);
  802. temp_dState = temp_bed.celsius;
  803. pid_output = constrain(work_pid.Kp + work_pid.Ki + work_pid.Kd + float(MIN_BED_POWER), 0, MAX_BED_POWER);
  804. }
  805. #else // PID_OPENLOOP
  806. const float pid_output = constrain(temp_bed.target, 0, MAX_BED_POWER);
  807. #endif // PID_OPENLOOP
  808. #if ENABLED(PID_BED_DEBUG)
  809. {
  810. SERIAL_ECHO_START();
  811. SERIAL_ECHOLNPAIR(
  812. " PID_BED_DEBUG : Input ", temp_bed.celsius, " Output ", pid_output,
  813. #if DISABLED(PID_OPENLOOP)
  814. STR_PID_DEBUG_PTERM, work_pid.Kp,
  815. STR_PID_DEBUG_ITERM, work_pid.Ki,
  816. STR_PID_DEBUG_DTERM, work_pid.Kd,
  817. #endif
  818. );
  819. }
  820. #endif
  821. return pid_output;
  822. }
  823. #endif // PIDTEMPBED
  824. /**
  825. * Manage heating activities for extruder hot-ends and a heated bed
  826. * - Acquire updated temperature readings
  827. * - Also resets the watchdog timer
  828. * - Invoke thermal runaway protection
  829. * - Manage extruder auto-fan
  830. * - Apply filament width to the extrusion rate (may move)
  831. * - Update the heated bed PID output value
  832. */
  833. void Temperature::manage_heater() {
  834. #if EARLY_WATCHDOG
  835. // If thermal manager is still not running, make sure to at least reset the watchdog!
  836. if (!inited) return watchdog_refresh();
  837. #endif
  838. if (TERN0(EMERGENCY_PARSER, emergency_parser.killed_by_M112))
  839. kill(M112_KILL_STR, nullptr, true);
  840. if (!raw_temps_ready) return;
  841. updateTemperaturesFromRawValues(); // also resets the watchdog
  842. #if ENABLED(HEATER_0_USES_MAX6675)
  843. if (temp_hotend[0].celsius > _MIN(HEATER_0_MAXTEMP, HEATER_0_MAX6675_TMAX - 1.0)) max_temp_error(H_E0);
  844. if (temp_hotend[0].celsius < _MAX(HEATER_0_MINTEMP, HEATER_0_MAX6675_TMIN + .01)) min_temp_error(H_E0);
  845. #endif
  846. #if ENABLED(HEATER_1_USES_MAX6675)
  847. if (temp_hotend[1].celsius > _MIN(HEATER_1_MAXTEMP, HEATER_1_MAX6675_TMAX - 1.0)) max_temp_error(H_E1);
  848. if (temp_hotend[1].celsius < _MAX(HEATER_1_MINTEMP, HEATER_1_MAX6675_TMIN + .01)) min_temp_error(H_E1);
  849. #endif
  850. millis_t ms = millis();
  851. #if HAS_HOTEND
  852. HOTEND_LOOP() {
  853. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  854. if (degHotend(e) > temp_range[e].maxtemp)
  855. _temp_error((heater_ind_t)e, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  856. #endif
  857. TERN_(HEATER_IDLE_HANDLER, hotend_idle[e].update(ms));
  858. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  859. // Check for thermal runaway
  860. 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);
  861. #endif
  862. 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;
  863. #if WATCH_HOTENDS
  864. // Make sure temperature is increasing
  865. if (watch_hotend[e].next_ms && ELAPSED(ms, watch_hotend[e].next_ms)) { // Time to check this extruder?
  866. if (degHotend(e) < watch_hotend[e].target) { // Failed to increase enough?
  867. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  868. _temp_error((heater_ind_t)e, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  869. }
  870. else // Start again if the target is still far off
  871. start_watching_hotend(e);
  872. }
  873. #endif
  874. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  875. // Make sure measured temperatures are close together
  876. if (ABS(temp_hotend[0].celsius - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  877. _temp_error(H_E0, PSTR(STR_REDUNDANCY), GET_TEXT(MSG_ERR_REDUNDANT_TEMP));
  878. #endif
  879. } // HOTEND_LOOP
  880. #endif // HOTENDS
  881. #if HAS_AUTO_FAN
  882. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  883. checkExtruderAutoFans();
  884. next_auto_fan_check_ms = ms + 2500UL;
  885. }
  886. #endif
  887. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  888. /**
  889. * Dynamically set the volumetric multiplier based
  890. * on the delayed Filament Width measurement.
  891. */
  892. filwidth.update_volumetric();
  893. #endif
  894. #if HAS_HEATED_BED
  895. #if ENABLED(THERMAL_PROTECTION_BED)
  896. if (degBed() > BED_MAXTEMP)
  897. _temp_error(H_BED, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  898. #endif
  899. #if WATCH_BED
  900. // Make sure temperature is increasing
  901. if (watch_bed.elapsed(ms)) { // Time to check the bed?
  902. if (degBed() < watch_bed.target) { // Failed to increase enough?
  903. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  904. _temp_error(H_BED, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  905. }
  906. else // Start again if the target is still far off
  907. start_watching_bed();
  908. }
  909. #endif // WATCH_BED
  910. #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  911. #define PAUSE_CHANGE_REQD 1
  912. #endif
  913. #if PAUSE_CHANGE_REQD
  914. static bool last_pause_state;
  915. #endif
  916. do {
  917. #if DISABLED(PIDTEMPBED)
  918. if (PENDING(ms, next_bed_check_ms)
  919. && TERN1(PAUSE_CHANGE_REQD, paused == last_pause_state)
  920. ) break;
  921. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  922. TERN_(PAUSE_CHANGE_REQD, last_pause_state = paused);
  923. #endif
  924. TERN_(HEATER_IDLE_HANDLER, bed_idle.update(ms));
  925. 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));
  926. #if HEATER_IDLE_HANDLER
  927. if (bed_idle.timed_out) {
  928. temp_bed.soft_pwm_amount = 0;
  929. #if DISABLED(PIDTEMPBED)
  930. WRITE_HEATER_BED(LOW);
  931. #endif
  932. }
  933. else
  934. #endif
  935. {
  936. #if ENABLED(PIDTEMPBED)
  937. temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  938. #else
  939. // Check if temperature is within the correct band
  940. if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
  941. #if ENABLED(BED_LIMIT_SWITCHING)
  942. if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
  943. temp_bed.soft_pwm_amount = 0;
  944. else if (temp_bed.celsius <= temp_bed.target - (BED_HYSTERESIS))
  945. temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
  946. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  947. temp_bed.soft_pwm_amount = temp_bed.celsius < temp_bed.target ? MAX_BED_POWER >> 1 : 0;
  948. #endif
  949. }
  950. else {
  951. temp_bed.soft_pwm_amount = 0;
  952. WRITE_HEATER_BED(LOW);
  953. }
  954. #endif
  955. }
  956. } while (false);
  957. #endif // HAS_HEATED_BED
  958. #if HAS_HEATED_CHAMBER
  959. #ifndef CHAMBER_CHECK_INTERVAL
  960. #define CHAMBER_CHECK_INTERVAL 1000UL
  961. #endif
  962. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  963. if (degChamber() > CHAMBER_MAXTEMP)
  964. _temp_error(H_CHAMBER, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  965. #endif
  966. #if WATCH_CHAMBER
  967. // Make sure temperature is increasing
  968. if (watch_chamber.elapsed(ms)) { // Time to check the chamber?
  969. if (degChamber() < watch_chamber.target) // Failed to increase enough?
  970. _temp_error(H_CHAMBER, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  971. else
  972. start_watching_chamber(); // Start again if the target is still far off
  973. }
  974. #endif
  975. if (ELAPSED(ms, next_chamber_check_ms)) {
  976. next_chamber_check_ms = ms + CHAMBER_CHECK_INTERVAL;
  977. if (WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP)) {
  978. #if ENABLED(CHAMBER_LIMIT_SWITCHING)
  979. if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
  980. temp_chamber.soft_pwm_amount = 0;
  981. else if (temp_chamber.celsius <= temp_chamber.target - (TEMP_CHAMBER_HYSTERESIS))
  982. temp_chamber.soft_pwm_amount = MAX_CHAMBER_POWER >> 1;
  983. #else
  984. temp_chamber.soft_pwm_amount = temp_chamber.celsius < temp_chamber.target ? MAX_CHAMBER_POWER >> 1 : 0;
  985. #endif
  986. }
  987. else {
  988. temp_chamber.soft_pwm_amount = 0;
  989. WRITE_HEATER_CHAMBER(LOW);
  990. }
  991. 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));
  992. }
  993. // TODO: Implement true PID pwm
  994. //temp_bed.soft_pwm_amount = WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP) ? (int)get_pid_output_chamber() >> 1 : 0;
  995. #endif // HAS_HEATED_CHAMBER
  996. UNUSED(ms);
  997. }
  998. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  999. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  1000. /**
  1001. * Bisect search for the range of the 'raw' value, then interpolate
  1002. * proportionally between the under and over values.
  1003. */
  1004. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  1005. uint8_t l = 0, r = LEN, m; \
  1006. for (;;) { \
  1007. m = (l + r) >> 1; \
  1008. if (!m) return short(pgm_read_word(&TBL[0][1])); \
  1009. if (m == l || m == r) return short(pgm_read_word(&TBL[LEN-1][1])); \
  1010. short v00 = pgm_read_word(&TBL[m-1][0]), \
  1011. v10 = pgm_read_word(&TBL[m-0][0]); \
  1012. if (raw < v00) r = m; \
  1013. else if (raw > v10) l = m; \
  1014. else { \
  1015. const short v01 = short(pgm_read_word(&TBL[m-1][1])), \
  1016. v11 = short(pgm_read_word(&TBL[m-0][1])); \
  1017. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  1018. } \
  1019. } \
  1020. }while(0)
  1021. #if HAS_USER_THERMISTORS
  1022. user_thermistor_t Temperature::user_thermistor[USER_THERMISTORS]; // Initialized by settings.load()
  1023. void Temperature::reset_user_thermistors() {
  1024. user_thermistor_t user_thermistor[USER_THERMISTORS] = {
  1025. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1026. { true, 0, 0, HOTEND0_PULLUP_RESISTOR_OHMS, HOTEND0_RESISTANCE_25C_OHMS, 0, 0, HOTEND0_BETA, 0 },
  1027. #endif
  1028. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1029. { true, 0, 0, HOTEND1_PULLUP_RESISTOR_OHMS, HOTEND1_RESISTANCE_25C_OHMS, 0, 0, HOTEND1_BETA, 0 },
  1030. #endif
  1031. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1032. { true, 0, 0, HOTEND2_PULLUP_RESISTOR_OHMS, HOTEND2_RESISTANCE_25C_OHMS, 0, 0, HOTEND2_BETA, 0 },
  1033. #endif
  1034. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1035. { true, 0, 0, HOTEND3_PULLUP_RESISTOR_OHMS, HOTEND3_RESISTANCE_25C_OHMS, 0, 0, HOTEND3_BETA, 0 },
  1036. #endif
  1037. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1038. { true, 0, 0, HOTEND4_PULLUP_RESISTOR_OHMS, HOTEND4_RESISTANCE_25C_OHMS, 0, 0, HOTEND4_BETA, 0 },
  1039. #endif
  1040. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1041. { true, 0, 0, HOTEND5_PULLUP_RESISTOR_OHMS, HOTEND5_RESISTANCE_25C_OHMS, 0, 0, HOTEND5_BETA, 0 },
  1042. #endif
  1043. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1044. { true, 0, 0, HOTEND6_PULLUP_RESISTOR_OHMS, HOTEND6_RESISTANCE_25C_OHMS, 0, 0, HOTEND6_BETA, 0 },
  1045. #endif
  1046. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1047. { true, 0, 0, HOTEND7_PULLUP_RESISTOR_OHMS, HOTEND7_RESISTANCE_25C_OHMS, 0, 0, HOTEND7_BETA, 0 },
  1048. #endif
  1049. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1050. { true, 0, 0, BED_PULLUP_RESISTOR_OHMS, BED_RESISTANCE_25C_OHMS, 0, 0, BED_BETA, 0 },
  1051. #endif
  1052. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1053. { true, 0, 0, CHAMBER_PULLUP_RESISTOR_OHMS, CHAMBER_RESISTANCE_25C_OHMS, 0, 0, CHAMBER_BETA, 0 }
  1054. #endif
  1055. };
  1056. COPY(thermalManager.user_thermistor, user_thermistor);
  1057. }
  1058. void Temperature::log_user_thermistor(const uint8_t t_index, const bool eprom/*=false*/) {
  1059. if (eprom)
  1060. SERIAL_ECHOPGM(" M305 ");
  1061. else
  1062. SERIAL_ECHO_START();
  1063. SERIAL_CHAR('P');
  1064. SERIAL_CHAR('0' + t_index);
  1065. const user_thermistor_t &t = user_thermistor[t_index];
  1066. SERIAL_ECHOPAIR_F(" R", t.series_res, 1);
  1067. SERIAL_ECHOPAIR_F_P(SP_T_STR, t.res_25, 1);
  1068. SERIAL_ECHOPAIR_F(" B", t.beta, 1);
  1069. SERIAL_ECHOPAIR_F(" C", t.sh_c_coeff, 9);
  1070. SERIAL_ECHOPGM(" ; ");
  1071. serialprintPGM(
  1072. TERN_(HEATER_0_USER_THERMISTOR, t_index == CTI_HOTEND_0 ? PSTR("HOTEND 0") :)
  1073. TERN_(HEATER_1_USER_THERMISTOR, t_index == CTI_HOTEND_1 ? PSTR("HOTEND 1") :)
  1074. TERN_(HEATER_2_USER_THERMISTOR, t_index == CTI_HOTEND_2 ? PSTR("HOTEND 2") :)
  1075. TERN_(HEATER_3_USER_THERMISTOR, t_index == CTI_HOTEND_3 ? PSTR("HOTEND 3") :)
  1076. TERN_(HEATER_4_USER_THERMISTOR, t_index == CTI_HOTEND_4 ? PSTR("HOTEND 4") :)
  1077. TERN_(HEATER_5_USER_THERMISTOR, t_index == CTI_HOTEND_5 ? PSTR("HOTEND 5") :)
  1078. TERN_(HEATER_6_USER_THERMISTOR, t_index == CTI_HOTEND_6 ? PSTR("HOTEND 6") :)
  1079. TERN_(HEATER_7_USER_THERMISTOR, t_index == CTI_HOTEND_7 ? PSTR("HOTEND 7") :)
  1080. TERN_(HEATER_BED_USER_THERMISTOR, t_index == CTI_BED ? PSTR("BED") :)
  1081. TERN_(HEATER_CHAMBER_USER_THERMISTOR, t_index == CTI_CHAMBER ? PSTR("CHAMBER") :)
  1082. nullptr
  1083. );
  1084. SERIAL_EOL();
  1085. }
  1086. float Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int raw) {
  1087. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1088. // static uint32_t clocks_total = 0;
  1089. // static uint32_t calls = 0;
  1090. // uint32_t tcnt5 = TCNT5;
  1091. //#endif
  1092. if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
  1093. user_thermistor_t &t = user_thermistor[t_index];
  1094. if (t.pre_calc) { // pre-calculate some variables
  1095. t.pre_calc = false;
  1096. t.res_25_recip = 1.0f / t.res_25;
  1097. t.res_25_log = logf(t.res_25);
  1098. t.beta_recip = 1.0f / t.beta;
  1099. t.sh_alpha = RECIPROCAL(THERMISTOR_RESISTANCE_NOMINAL_C - (THERMISTOR_ABS_ZERO_C))
  1100. - (t.beta_recip * t.res_25_log) - (t.sh_c_coeff * cu(t.res_25_log));
  1101. }
  1102. // maximum adc value .. take into account the over sampling
  1103. const int adc_max = MAX_RAW_THERMISTOR_VALUE,
  1104. adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
  1105. const float adc_inverse = (adc_max - adc_raw) - 0.5f,
  1106. resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
  1107. log_resistance = logf(resistance);
  1108. float value = t.sh_alpha;
  1109. value += log_resistance * t.beta_recip;
  1110. if (t.sh_c_coeff != 0)
  1111. value += t.sh_c_coeff * cu(log_resistance);
  1112. value = 1.0f / value;
  1113. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1114. // int32_t clocks = TCNT5 - tcnt5;
  1115. // if (clocks >= 0) {
  1116. // clocks_total += clocks;
  1117. // calls++;
  1118. // }
  1119. //#endif
  1120. // Return degrees C (up to 999, as the LCD only displays 3 digits)
  1121. return _MIN(value + THERMISTOR_ABS_ZERO_C, 999);
  1122. }
  1123. #endif
  1124. #if HAS_HOTEND
  1125. // Derived from RepRap FiveD extruder::getTemperature()
  1126. // For hot end temperature measurement.
  1127. float Temperature::analog_to_celsius_hotend(const int raw, const uint8_t e) {
  1128. if (e > HOTENDS - DISABLED(TEMP_SENSOR_1_AS_REDUNDANT)) {
  1129. SERIAL_ERROR_START();
  1130. SERIAL_ECHO((int)e);
  1131. SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER_NUM);
  1132. kill();
  1133. return 0;
  1134. }
  1135. switch (e) {
  1136. case 0:
  1137. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1138. return user_thermistor_to_deg_c(CTI_HOTEND_0, raw);
  1139. #elif ENABLED(HEATER_0_USES_MAX6675)
  1140. return (
  1141. #if ENABLED(MAX6675_IS_MAX31865)
  1142. max31865.temperature(100, 400) // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1143. #else
  1144. raw * 0.25
  1145. #endif
  1146. );
  1147. #elif ENABLED(HEATER_0_USES_AD595)
  1148. return TEMP_AD595(raw);
  1149. #elif ENABLED(HEATER_0_USES_AD8495)
  1150. return TEMP_AD8495(raw);
  1151. #else
  1152. break;
  1153. #endif
  1154. case 1:
  1155. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1156. return user_thermistor_to_deg_c(CTI_HOTEND_1, raw);
  1157. #elif ENABLED(HEATER_1_USES_MAX6675)
  1158. return raw * 0.25;
  1159. #elif ENABLED(HEATER_1_USES_AD595)
  1160. return TEMP_AD595(raw);
  1161. #elif ENABLED(HEATER_1_USES_AD8495)
  1162. return TEMP_AD8495(raw);
  1163. #else
  1164. break;
  1165. #endif
  1166. case 2:
  1167. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1168. return user_thermistor_to_deg_c(CTI_HOTEND_2, raw);
  1169. #elif ENABLED(HEATER_2_USES_AD595)
  1170. return TEMP_AD595(raw);
  1171. #elif ENABLED(HEATER_2_USES_AD8495)
  1172. return TEMP_AD8495(raw);
  1173. #else
  1174. break;
  1175. #endif
  1176. case 3:
  1177. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1178. return user_thermistor_to_deg_c(CTI_HOTEND_3, raw);
  1179. #elif ENABLED(HEATER_3_USES_AD595)
  1180. return TEMP_AD595(raw);
  1181. #elif ENABLED(HEATER_3_USES_AD8495)
  1182. return TEMP_AD8495(raw);
  1183. #else
  1184. break;
  1185. #endif
  1186. case 4:
  1187. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1188. return user_thermistor_to_deg_c(CTI_HOTEND_4, raw);
  1189. #elif ENABLED(HEATER_4_USES_AD595)
  1190. return TEMP_AD595(raw);
  1191. #elif ENABLED(HEATER_4_USES_AD8495)
  1192. return TEMP_AD8495(raw);
  1193. #else
  1194. break;
  1195. #endif
  1196. case 5:
  1197. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1198. return user_thermistor_to_deg_c(CTI_HOTEND_5, raw);
  1199. #elif ENABLED(HEATER_5_USES_AD595)
  1200. return TEMP_AD595(raw);
  1201. #elif ENABLED(HEATER_5_USES_AD8495)
  1202. return TEMP_AD8495(raw);
  1203. #else
  1204. break;
  1205. #endif
  1206. case 6:
  1207. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1208. return user_thermistor_to_deg_c(CTI_HOTEND_6, raw);
  1209. #elif ENABLED(HEATER_6_USES_AD595)
  1210. return TEMP_AD595(raw);
  1211. #elif ENABLED(HEATER_6_USES_AD8495)
  1212. return TEMP_AD8495(raw);
  1213. #else
  1214. break;
  1215. #endif
  1216. case 7:
  1217. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1218. return user_thermistor_to_deg_c(CTI_HOTEND_7, raw);
  1219. #elif ENABLED(HEATER_7_USES_AD595)
  1220. return TEMP_AD595(raw);
  1221. #elif ENABLED(HEATER_7_USES_AD8495)
  1222. return TEMP_AD8495(raw);
  1223. #else
  1224. break;
  1225. #endif
  1226. default: break;
  1227. }
  1228. #if HOTEND_USES_THERMISTOR
  1229. // Thermistor with conversion table?
  1230. const short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  1231. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  1232. #endif
  1233. return 0;
  1234. }
  1235. #endif // HOTENDS
  1236. #if HAS_HEATED_BED
  1237. // Derived from RepRap FiveD extruder::getTemperature()
  1238. // For bed temperature measurement.
  1239. float Temperature::analog_to_celsius_bed(const int raw) {
  1240. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1241. return user_thermistor_to_deg_c(CTI_BED, raw);
  1242. #elif ENABLED(HEATER_BED_USES_THERMISTOR)
  1243. SCAN_THERMISTOR_TABLE(BED_TEMPTABLE, BED_TEMPTABLE_LEN);
  1244. #elif ENABLED(HEATER_BED_USES_AD595)
  1245. return TEMP_AD595(raw);
  1246. #elif ENABLED(HEATER_BED_USES_AD8495)
  1247. return TEMP_AD8495(raw);
  1248. #else
  1249. UNUSED(raw);
  1250. return 0;
  1251. #endif
  1252. }
  1253. #endif // HAS_HEATED_BED
  1254. #if HAS_TEMP_CHAMBER
  1255. // Derived from RepRap FiveD extruder::getTemperature()
  1256. // For chamber temperature measurement.
  1257. float Temperature::analog_to_celsius_chamber(const int raw) {
  1258. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1259. return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
  1260. #elif ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  1261. SCAN_THERMISTOR_TABLE(CHAMBER_TEMPTABLE, CHAMBER_TEMPTABLE_LEN);
  1262. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  1263. return TEMP_AD595(raw);
  1264. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  1265. return TEMP_AD8495(raw);
  1266. #else
  1267. UNUSED(raw);
  1268. return 0;
  1269. #endif
  1270. }
  1271. #endif // HAS_TEMP_CHAMBER
  1272. #if HAS_TEMP_PROBE
  1273. // Derived from RepRap FiveD extruder::getTemperature()
  1274. // For probe temperature measurement.
  1275. float Temperature::analog_to_celsius_probe(const int raw) {
  1276. #if ENABLED(PROBE_USER_THERMISTOR)
  1277. return user_thermistor_to_deg_c(CTI_PROBE, raw);
  1278. #elif ENABLED(PROBE_USES_THERMISTOR)
  1279. SCAN_THERMISTOR_TABLE(PROBE_TEMPTABLE, PROBE_TEMPTABLE_LEN);
  1280. #elif ENABLED(PROBE_USES_AD595)
  1281. return TEMP_AD595(raw);
  1282. #elif ENABLED(PROBE_USES_AD8495)
  1283. return TEMP_AD8495(raw);
  1284. #else
  1285. UNUSED(raw);
  1286. return 0;
  1287. #endif
  1288. }
  1289. #endif // HAS_TEMP_PROBE
  1290. /**
  1291. * Get the raw values into the actual temperatures.
  1292. * The raw values are created in interrupt context,
  1293. * and this function is called from normal context
  1294. * as it would block the stepper routine.
  1295. */
  1296. void Temperature::updateTemperaturesFromRawValues() {
  1297. #if ENABLED(HEATER_0_USES_MAX6675)
  1298. temp_hotend[0].raw = READ_MAX6675(0);
  1299. #endif
  1300. #if ENABLED(HEATER_1_USES_MAX6675)
  1301. temp_hotend[1].raw = READ_MAX6675(1);
  1302. #endif
  1303. #if HAS_HOTEND
  1304. HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
  1305. #endif
  1306. TERN_(HAS_HEATED_BED, temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw));
  1307. TERN_(HAS_TEMP_CHAMBER, temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw));
  1308. TERN_(HAS_TEMP_PROBE, temp_probe.celsius = analog_to_celsius_probe(temp_probe.raw));
  1309. TERN_(TEMP_SENSOR_1_AS_REDUNDANT, redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1));
  1310. TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
  1311. TERN_(HAS_POWER_MONITOR, power_monitor.capture_values());
  1312. // Reset the watchdog on good temperature measurement
  1313. watchdog_refresh();
  1314. raw_temps_ready = false;
  1315. }
  1316. #if MAX6675_SEPARATE_SPI
  1317. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  1318. #endif
  1319. // Init fans according to whether they're native PWM or Software PWM
  1320. #ifdef ALFAWISE_UX0
  1321. #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH)
  1322. #else
  1323. #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH)
  1324. #endif
  1325. #if ENABLED(FAN_SOFT_PWM)
  1326. #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P)
  1327. #else
  1328. #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
  1329. #endif
  1330. #if ENABLED(FAST_PWM_FAN)
  1331. #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY)
  1332. #else
  1333. #define SET_FAST_PWM_FREQ(P) NOOP
  1334. #endif
  1335. #define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0)
  1336. #if EXTRUDER_AUTO_FAN_SPEED != 255
  1337. #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)
  1338. #else
  1339. #define INIT_E_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1340. #endif
  1341. #if CHAMBER_AUTO_FAN_SPEED != 255
  1342. #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)
  1343. #else
  1344. #define INIT_CHAMBER_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1345. #endif
  1346. /**
  1347. * Initialize the temperature manager
  1348. * The manager is implemented by periodic calls to manage_heater()
  1349. */
  1350. void Temperature::init() {
  1351. TERN_(MAX6675_IS_MAX31865, max31865.begin(MAX31865_2WIRE)); // MAX31865_2WIRE, MAX31865_3WIRE, MAX31865_4WIRE
  1352. #if EARLY_WATCHDOG
  1353. // Flag that the thermalManager should be running
  1354. if (inited) return;
  1355. inited = true;
  1356. #endif
  1357. #if MB(RUMBA)
  1358. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  1359. #define _AD(N) ANY(HEATER_##N##_USES_AD595, HEATER_##N##_USES_AD8495)
  1360. #if _AD(0) || _AD(1) || _AD(2) || _AD(BED) || _AD(CHAMBER)
  1361. MCUCR = _BV(JTD);
  1362. MCUCR = _BV(JTD);
  1363. #endif
  1364. #endif
  1365. #if BOTH(PIDTEMP, PID_EXTRUSION_SCALING)
  1366. last_e_position = 0;
  1367. #endif
  1368. #if HAS_HEATER_0
  1369. #ifdef ALFAWISE_UX0
  1370. OUT_WRITE_OD(HEATER_0_PIN, HEATER_0_INVERTING);
  1371. #else
  1372. OUT_WRITE(HEATER_0_PIN, HEATER_0_INVERTING);
  1373. #endif
  1374. #endif
  1375. #if HAS_HEATER_1
  1376. OUT_WRITE(HEATER_1_PIN, HEATER_1_INVERTING);
  1377. #endif
  1378. #if HAS_HEATER_2
  1379. OUT_WRITE(HEATER_2_PIN, HEATER_2_INVERTING);
  1380. #endif
  1381. #if HAS_HEATER_3
  1382. OUT_WRITE(HEATER_3_PIN, HEATER_3_INVERTING);
  1383. #endif
  1384. #if HAS_HEATER_4
  1385. OUT_WRITE(HEATER_4_PIN, HEATER_4_INVERTING);
  1386. #endif
  1387. #if HAS_HEATER_5
  1388. OUT_WRITE(HEATER_5_PIN, HEATER_5_INVERTING);
  1389. #endif
  1390. #if HAS_HEATER_6
  1391. OUT_WRITE(HEATER_6_PIN, HEATER_6_INVERTING);
  1392. #endif
  1393. #if HAS_HEATER_7
  1394. OUT_WRITE(HEATER_7_PIN, HEATER_7_INVERTING);
  1395. #endif
  1396. #if HAS_HEATED_BED
  1397. #ifdef ALFAWISE_UX0
  1398. OUT_WRITE_OD(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1399. #else
  1400. OUT_WRITE(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1401. #endif
  1402. #endif
  1403. #if HAS_HEATED_CHAMBER
  1404. OUT_WRITE(HEATER_CHAMBER_PIN, HEATER_CHAMBER_INVERTING);
  1405. #endif
  1406. #if HAS_FAN0
  1407. INIT_FAN_PIN(FAN_PIN);
  1408. #endif
  1409. #if HAS_FAN1
  1410. INIT_FAN_PIN(FAN1_PIN);
  1411. #endif
  1412. #if HAS_FAN2
  1413. INIT_FAN_PIN(FAN2_PIN);
  1414. #endif
  1415. #if HAS_FAN3
  1416. INIT_FAN_PIN(FAN3_PIN);
  1417. #endif
  1418. #if HAS_FAN4
  1419. INIT_FAN_PIN(FAN4_PIN);
  1420. #endif
  1421. #if HAS_FAN5
  1422. INIT_FAN_PIN(FAN5_PIN);
  1423. #endif
  1424. #if HAS_FAN6
  1425. INIT_FAN_PIN(FAN6_PIN);
  1426. #endif
  1427. #if HAS_FAN7
  1428. INIT_FAN_PIN(FAN7_PIN);
  1429. #endif
  1430. #if ENABLED(USE_CONTROLLER_FAN)
  1431. INIT_FAN_PIN(CONTROLLER_FAN_PIN);
  1432. #endif
  1433. #if MAX6675_SEPARATE_SPI
  1434. OUT_WRITE(SCK_PIN, LOW);
  1435. OUT_WRITE(MOSI_PIN, HIGH);
  1436. SET_INPUT_PULLUP(MISO_PIN);
  1437. max6675_spi.init();
  1438. OUT_WRITE(SS_PIN, HIGH);
  1439. OUT_WRITE(MAX6675_SS_PIN, HIGH);
  1440. #endif
  1441. #if ENABLED(HEATER_1_USES_MAX6675)
  1442. OUT_WRITE(MAX6675_SS2_PIN, HIGH);
  1443. #endif
  1444. HAL_adc_init();
  1445. #if HAS_TEMP_ADC_0
  1446. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1447. #endif
  1448. #if HAS_TEMP_ADC_1
  1449. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1450. #endif
  1451. #if HAS_TEMP_ADC_2
  1452. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1453. #endif
  1454. #if HAS_TEMP_ADC_3
  1455. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1456. #endif
  1457. #if HAS_TEMP_ADC_4
  1458. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1459. #endif
  1460. #if HAS_TEMP_ADC_5
  1461. HAL_ANALOG_SELECT(TEMP_5_PIN);
  1462. #endif
  1463. #if HAS_TEMP_ADC_6
  1464. HAL_ANALOG_SELECT(TEMP_6_PIN);
  1465. #endif
  1466. #if HAS_TEMP_ADC_7
  1467. HAL_ANALOG_SELECT(TEMP_7_PIN);
  1468. #endif
  1469. #if HAS_JOY_ADC_X
  1470. HAL_ANALOG_SELECT(JOY_X_PIN);
  1471. #endif
  1472. #if HAS_JOY_ADC_Y
  1473. HAL_ANALOG_SELECT(JOY_Y_PIN);
  1474. #endif
  1475. #if HAS_JOY_ADC_Z
  1476. HAL_ANALOG_SELECT(JOY_Z_PIN);
  1477. #endif
  1478. #if HAS_JOY_ADC_EN
  1479. SET_INPUT_PULLUP(JOY_EN_PIN);
  1480. #endif
  1481. #if HAS_HEATED_BED
  1482. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1483. #endif
  1484. #if HAS_TEMP_CHAMBER
  1485. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1486. #endif
  1487. #if HAS_TEMP_PROBE
  1488. HAL_ANALOG_SELECT(TEMP_PROBE_PIN);
  1489. #endif
  1490. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1491. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1492. #endif
  1493. #if HAS_ADC_BUTTONS
  1494. HAL_ANALOG_SELECT(ADC_KEYPAD_PIN);
  1495. #endif
  1496. #if ENABLED(POWER_MONITOR_CURRENT)
  1497. HAL_ANALOG_SELECT(POWER_MONITOR_CURRENT_PIN);
  1498. #endif
  1499. #if ENABLED(POWER_MONITOR_VOLTAGE)
  1500. HAL_ANALOG_SELECT(POWER_MONITOR_VOLTAGE_PIN);
  1501. #endif
  1502. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1503. ENABLE_TEMPERATURE_INTERRUPT();
  1504. #if HAS_AUTO_FAN_0
  1505. INIT_E_AUTO_FAN_PIN(E0_AUTO_FAN_PIN);
  1506. #endif
  1507. #if HAS_AUTO_FAN_1 && !_EFANOVERLAP(1,0)
  1508. INIT_E_AUTO_FAN_PIN(E1_AUTO_FAN_PIN);
  1509. #endif
  1510. #if HAS_AUTO_FAN_2 && !(_EFANOVERLAP(2,0) || _EFANOVERLAP(2,1))
  1511. INIT_E_AUTO_FAN_PIN(E2_AUTO_FAN_PIN);
  1512. #endif
  1513. #if HAS_AUTO_FAN_3 && !(_EFANOVERLAP(3,0) || _EFANOVERLAP(3,1) || _EFANOVERLAP(3,2))
  1514. INIT_E_AUTO_FAN_PIN(E3_AUTO_FAN_PIN);
  1515. #endif
  1516. #if HAS_AUTO_FAN_4 && !(_EFANOVERLAP(4,0) || _EFANOVERLAP(4,1) || _EFANOVERLAP(4,2) || _EFANOVERLAP(4,3))
  1517. INIT_E_AUTO_FAN_PIN(E4_AUTO_FAN_PIN);
  1518. #endif
  1519. #if HAS_AUTO_FAN_5 && !(_EFANOVERLAP(5,0) || _EFANOVERLAP(5,1) || _EFANOVERLAP(5,2) || _EFANOVERLAP(5,3) || _EFANOVERLAP(5,4))
  1520. INIT_E_AUTO_FAN_PIN(E5_AUTO_FAN_PIN);
  1521. #endif
  1522. #if HAS_AUTO_FAN_6 && !(_EFANOVERLAP(6,0) || _EFANOVERLAP(6,1) || _EFANOVERLAP(6,2) || _EFANOVERLAP(6,3) || _EFANOVERLAP(6,4) || _EFANOVERLAP(6,5))
  1523. INIT_E_AUTO_FAN_PIN(E6_AUTO_FAN_PIN);
  1524. #endif
  1525. #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))
  1526. INIT_E_AUTO_FAN_PIN(E7_AUTO_FAN_PIN);
  1527. #endif
  1528. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  1529. INIT_CHAMBER_AUTO_FAN_PIN(CHAMBER_AUTO_FAN_PIN);
  1530. #endif
  1531. // Wait for temperature measurement to settle
  1532. delay(250);
  1533. #if HAS_HOTEND
  1534. #define _TEMP_MIN_E(NR) do{ \
  1535. temp_range[NR].mintemp = HEATER_ ##NR## _MINTEMP; \
  1536. while (analog_to_celsius_hotend(temp_range[NR].raw_min, NR) < HEATER_ ##NR## _MINTEMP) \
  1537. temp_range[NR].raw_min += TEMPDIR(NR) * (OVERSAMPLENR); \
  1538. }while(0)
  1539. #define _TEMP_MAX_E(NR) do{ \
  1540. temp_range[NR].maxtemp = HEATER_ ##NR## _MAXTEMP; \
  1541. while (analog_to_celsius_hotend(temp_range[NR].raw_max, NR) > HEATER_ ##NR## _MAXTEMP) \
  1542. temp_range[NR].raw_max -= TEMPDIR(NR) * (OVERSAMPLENR); \
  1543. }while(0)
  1544. #ifdef HEATER_0_MINTEMP
  1545. _TEMP_MIN_E(0);
  1546. #endif
  1547. #ifdef HEATER_0_MAXTEMP
  1548. _TEMP_MAX_E(0);
  1549. #endif
  1550. #if HAS_MULTI_HOTEND
  1551. #ifdef HEATER_1_MINTEMP
  1552. _TEMP_MIN_E(1);
  1553. #endif
  1554. #ifdef HEATER_1_MAXTEMP
  1555. _TEMP_MAX_E(1);
  1556. #endif
  1557. #if HOTENDS > 2
  1558. #ifdef HEATER_2_MINTEMP
  1559. _TEMP_MIN_E(2);
  1560. #endif
  1561. #ifdef HEATER_2_MAXTEMP
  1562. _TEMP_MAX_E(2);
  1563. #endif
  1564. #if HOTENDS > 3
  1565. #ifdef HEATER_3_MINTEMP
  1566. _TEMP_MIN_E(3);
  1567. #endif
  1568. #ifdef HEATER_3_MAXTEMP
  1569. _TEMP_MAX_E(3);
  1570. #endif
  1571. #if HOTENDS > 4
  1572. #ifdef HEATER_4_MINTEMP
  1573. _TEMP_MIN_E(4);
  1574. #endif
  1575. #ifdef HEATER_4_MAXTEMP
  1576. _TEMP_MAX_E(4);
  1577. #endif
  1578. #if HOTENDS > 5
  1579. #ifdef HEATER_5_MINTEMP
  1580. _TEMP_MIN_E(5);
  1581. #endif
  1582. #ifdef HEATER_5_MAXTEMP
  1583. _TEMP_MAX_E(5);
  1584. #endif
  1585. #if HOTENDS > 6
  1586. #ifdef HEATER_6_MINTEMP
  1587. _TEMP_MIN_E(6);
  1588. #endif
  1589. #ifdef HEATER_6_MAXTEMP
  1590. _TEMP_MAX_E(6);
  1591. #endif
  1592. #if HOTENDS > 7
  1593. #ifdef HEATER_7_MINTEMP
  1594. _TEMP_MIN_E(7);
  1595. #endif
  1596. #ifdef HEATER_7_MAXTEMP
  1597. _TEMP_MAX_E(7);
  1598. #endif
  1599. #endif // HOTENDS > 7
  1600. #endif // HOTENDS > 6
  1601. #endif // HOTENDS > 5
  1602. #endif // HOTENDS > 4
  1603. #endif // HOTENDS > 3
  1604. #endif // HOTENDS > 2
  1605. #endif // HAS_MULTI_HOTEND
  1606. #endif // HOTENDS
  1607. #if HAS_HEATED_BED
  1608. #ifdef BED_MINTEMP
  1609. while (analog_to_celsius_bed(mintemp_raw_BED) < BED_MINTEMP) mintemp_raw_BED += TEMPDIR(BED) * (OVERSAMPLENR);
  1610. #endif
  1611. #ifdef BED_MAXTEMP
  1612. while (analog_to_celsius_bed(maxtemp_raw_BED) > BED_MAXTEMP) maxtemp_raw_BED -= TEMPDIR(BED) * (OVERSAMPLENR);
  1613. #endif
  1614. #endif // HAS_HEATED_BED
  1615. #if HAS_HEATED_CHAMBER
  1616. #ifdef CHAMBER_MINTEMP
  1617. while (analog_to_celsius_chamber(mintemp_raw_CHAMBER) < CHAMBER_MINTEMP) mintemp_raw_CHAMBER += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1618. #endif
  1619. #ifdef CHAMBER_MAXTEMP
  1620. while (analog_to_celsius_chamber(maxtemp_raw_CHAMBER) > CHAMBER_MAXTEMP) maxtemp_raw_CHAMBER -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1621. #endif
  1622. #endif
  1623. TERN_(PROBING_HEATERS_OFF, paused = false);
  1624. }
  1625. #if WATCH_HOTENDS
  1626. /**
  1627. * Start Heating Sanity Check for hotends that are below
  1628. * their target temperature by a configurable margin.
  1629. * This is called when the temperature is set. (M104, M109)
  1630. */
  1631. void Temperature::start_watching_hotend(const uint8_t E_NAME) {
  1632. const uint8_t ee = HOTEND_INDEX;
  1633. watch_hotend[ee].restart(degHotend(ee), degTargetHotend(ee));
  1634. }
  1635. #endif
  1636. #if WATCH_BED
  1637. /**
  1638. * Start Heating Sanity Check for hotends that are below
  1639. * their target temperature by a configurable margin.
  1640. * This is called when the temperature is set. (M140, M190)
  1641. */
  1642. void Temperature::start_watching_bed() {
  1643. watch_bed.restart(degBed(), degTargetBed());
  1644. }
  1645. #endif
  1646. #if WATCH_CHAMBER
  1647. /**
  1648. * Start Heating Sanity Check for chamber that is below
  1649. * its target temperature by a configurable margin.
  1650. * This is called when the temperature is set. (M141, M191)
  1651. */
  1652. void Temperature::start_watching_chamber() {
  1653. watch_chamber.restart(degChamber(), degTargetChamber());
  1654. }
  1655. #endif
  1656. #if HAS_THERMAL_PROTECTION
  1657. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1658. Temperature::tr_state_machine_t Temperature::tr_state_machine[HOTENDS]; // = { { TRInactive, 0 } };
  1659. #endif
  1660. #if HAS_THERMALLY_PROTECTED_BED
  1661. Temperature::tr_state_machine_t Temperature::tr_state_machine_bed; // = { TRInactive, 0 };
  1662. #endif
  1663. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1664. Temperature::tr_state_machine_t Temperature::tr_state_machine_chamber; // = { TRInactive, 0 };
  1665. #endif
  1666. 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) {
  1667. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1668. /**
  1669. SERIAL_ECHO_START();
  1670. SERIAL_ECHOPGM("Thermal Runaway Running. Heater ID: ");
  1671. if (heater_id == H_CHAMBER) SERIAL_ECHOPGM("chamber");
  1672. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1673. SERIAL_ECHOPAIR(" ; State:", sm.state, " ; Timer:", sm.timer, " ; Temperature:", current, " ; Target Temp:", target);
  1674. if (heater_id >= 0)
  1675. SERIAL_ECHOPAIR(" ; Idle Timeout:", hotend_idle[heater_id].timed_out);
  1676. else
  1677. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle.timed_out);
  1678. SERIAL_EOL();
  1679. //*/
  1680. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1681. #if HEATER_IDLE_HANDLER
  1682. // If the heater idle timeout expires, restart
  1683. if ((heater_id >= 0 && hotend_idle[heater_id].timed_out)
  1684. || TERN0(HAS_HEATED_BED, (heater_id < 0 && bed_idle.timed_out))
  1685. ) {
  1686. sm.state = TRInactive;
  1687. tr_target_temperature[heater_index] = 0;
  1688. }
  1689. else
  1690. #endif
  1691. {
  1692. // If the target temperature changes, restart
  1693. if (tr_target_temperature[heater_index] != target) {
  1694. tr_target_temperature[heater_index] = target;
  1695. sm.state = target > 0 ? TRFirstHeating : TRInactive;
  1696. }
  1697. }
  1698. switch (sm.state) {
  1699. // Inactive state waits for a target temperature to be set
  1700. case TRInactive: break;
  1701. // When first heating, wait for the temperature to be reached then go to Stable state
  1702. case TRFirstHeating:
  1703. if (current < tr_target_temperature[heater_index]) break;
  1704. sm.state = TRStable;
  1705. // While the temperature is stable watch for a bad temperature
  1706. case TRStable:
  1707. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  1708. if (adaptive_fan_slowing && heater_id >= 0) {
  1709. const int fan_index = _MIN(heater_id, FAN_COUNT - 1);
  1710. if (fan_speed[fan_index] == 0 || current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.25f))
  1711. fan_speed_scaler[fan_index] = 128;
  1712. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.3335f))
  1713. fan_speed_scaler[fan_index] = 96;
  1714. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.5f))
  1715. fan_speed_scaler[fan_index] = 64;
  1716. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.8f))
  1717. fan_speed_scaler[fan_index] = 32;
  1718. else
  1719. fan_speed_scaler[fan_index] = 0;
  1720. }
  1721. #endif
  1722. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1723. sm.timer = millis() + SEC_TO_MS(period_seconds);
  1724. break;
  1725. }
  1726. else if (PENDING(millis(), sm.timer)) break;
  1727. sm.state = TRRunaway;
  1728. case TRRunaway:
  1729. TERN_(DWIN_CREALITY_LCD, Popup_Window_Temperature(0));
  1730. _temp_error(heater_id, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  1731. }
  1732. }
  1733. #endif // HAS_THERMAL_PROTECTION
  1734. void Temperature::disable_all_heaters() {
  1735. TERN_(AUTOTEMP, planner.autotemp_enabled = false);
  1736. #if HAS_HOTEND
  1737. HOTEND_LOOP() setTargetHotend(0, e);
  1738. #endif
  1739. TERN_(HAS_HEATED_BED, setTargetBed(0));
  1740. TERN_(HAS_HEATED_CHAMBER, setTargetChamber(0));
  1741. // Unpause and reset everything
  1742. TERN_(PROBING_HEATERS_OFF, pause(false));
  1743. #define DISABLE_HEATER(N) { \
  1744. setTargetHotend(0, N); \
  1745. temp_hotend[N].soft_pwm_amount = 0; \
  1746. WRITE_HEATER_##N(LOW); \
  1747. }
  1748. #if HAS_TEMP_HOTEND
  1749. REPEAT(HOTENDS, DISABLE_HEATER);
  1750. #endif
  1751. #if HAS_HEATED_BED
  1752. temp_bed.target = 0;
  1753. temp_bed.soft_pwm_amount = 0;
  1754. WRITE_HEATER_BED(LOW);
  1755. #endif
  1756. #if HAS_HEATED_CHAMBER
  1757. temp_chamber.target = 0;
  1758. temp_chamber.soft_pwm_amount = 0;
  1759. WRITE_HEATER_CHAMBER(LOW);
  1760. #endif
  1761. }
  1762. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  1763. bool Temperature::over_autostart_threshold() {
  1764. #if HAS_HOTEND
  1765. HOTEND_LOOP() if (degTargetHotend(e) > (EXTRUDE_MINTEMP) / 2) return true;
  1766. #endif
  1767. return TERN0(HAS_HEATED_BED, degTargetBed() > BED_MINTEMP)
  1768. || TERN0(HAS_HEATED_CHAMBER, degTargetChamber() > CHAMBER_MINTEMP);
  1769. }
  1770. void Temperature::check_timer_autostart(const bool can_start, const bool can_stop) {
  1771. if (over_autostart_threshold()) {
  1772. if (can_start) startOrResumeJob();
  1773. }
  1774. else if (can_stop) {
  1775. print_job_timer.stop();
  1776. ui.reset_status();
  1777. }
  1778. }
  1779. #endif
  1780. #if ENABLED(PROBING_HEATERS_OFF)
  1781. void Temperature::pause(const bool p) {
  1782. if (p != paused) {
  1783. paused = p;
  1784. if (p) {
  1785. HOTEND_LOOP() hotend_idle[e].expire(); // Timeout immediately
  1786. TERN_(HAS_HEATED_BED, bed_idle.expire()); // Timeout immediately
  1787. }
  1788. else {
  1789. HOTEND_LOOP() reset_hotend_idle_timer(e);
  1790. TERN_(HAS_HEATED_BED, reset_bed_idle_timer());
  1791. }
  1792. }
  1793. }
  1794. #endif // PROBING_HEATERS_OFF
  1795. #if HAS_MAX6675
  1796. #ifndef THERMOCOUPLE_MAX_ERRORS
  1797. #define THERMOCOUPLE_MAX_ERRORS 15
  1798. #endif
  1799. int Temperature::read_max6675(
  1800. #if COUNT_6675 > 1
  1801. const uint8_t hindex
  1802. #endif
  1803. ) {
  1804. #if COUNT_6675 == 1
  1805. constexpr uint8_t hindex = 0;
  1806. #else
  1807. // Needed to return the correct temp when this is called too soon
  1808. static uint16_t max6675_temp_previous[COUNT_6675] = { 0 };
  1809. #endif
  1810. static uint8_t max6675_errors[COUNT_6675] = { 0 };
  1811. #define MAX6675_HEAT_INTERVAL 250UL
  1812. #if ENABLED(MAX6675_IS_MAX31855)
  1813. static uint32_t max6675_temp = 2000;
  1814. #define MAX6675_ERROR_MASK 7
  1815. #define MAX6675_DISCARD_BITS 18
  1816. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1817. #else
  1818. static uint16_t max6675_temp = 2000;
  1819. #define MAX6675_ERROR_MASK 4
  1820. #define MAX6675_DISCARD_BITS 3
  1821. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1822. #endif
  1823. // Return last-read value between readings
  1824. static millis_t next_max6675_ms[COUNT_6675] = { 0 };
  1825. millis_t ms = millis();
  1826. if (PENDING(ms, next_max6675_ms[hindex]))
  1827. return int(
  1828. #if COUNT_6675 == 1
  1829. max6675_temp
  1830. #else
  1831. max6675_temp_previous[hindex] // Need to return the correct previous value
  1832. #endif
  1833. );
  1834. next_max6675_ms[hindex] = ms + MAX6675_HEAT_INTERVAL;
  1835. #if ENABLED(MAX6675_IS_MAX31865)
  1836. max6675_temp = int(max31865.temperature(100, 400)); // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1837. #endif
  1838. //
  1839. // TODO: spiBegin, spiRec and spiInit doesn't work when soft spi is used.
  1840. //
  1841. #if !MAX6675_SEPARATE_SPI
  1842. spiBegin();
  1843. spiInit(MAX6675_SPEED_BITS);
  1844. #endif
  1845. #if COUNT_6675 > 1
  1846. #define WRITE_MAX6675(V) do{ switch (hindex) { case 1: WRITE(MAX6675_SS2_PIN, V); break; default: WRITE(MAX6675_SS_PIN, V); } }while(0)
  1847. #define SET_OUTPUT_MAX6675() do{ switch (hindex) { case 1: SET_OUTPUT(MAX6675_SS2_PIN); break; default: SET_OUTPUT(MAX6675_SS_PIN); } }while(0)
  1848. #elif ENABLED(HEATER_1_USES_MAX6675)
  1849. #define WRITE_MAX6675(V) WRITE(MAX6675_SS2_PIN, V)
  1850. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS2_PIN)
  1851. #else
  1852. #define WRITE_MAX6675(V) WRITE(MAX6675_SS_PIN, V)
  1853. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS_PIN)
  1854. #endif
  1855. SET_OUTPUT_MAX6675();
  1856. WRITE_MAX6675(LOW); // enable TT_MAX6675
  1857. DELAY_NS(100); // Ensure 100ns delay
  1858. // Read a big-endian temperature value
  1859. max6675_temp = 0;
  1860. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1861. max6675_temp |= (
  1862. #if MAX6675_SEPARATE_SPI
  1863. max6675_spi.receive()
  1864. #else
  1865. spiRec()
  1866. #endif
  1867. );
  1868. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1869. }
  1870. WRITE_MAX6675(HIGH); // disable TT_MAX6675
  1871. if (max6675_temp & MAX6675_ERROR_MASK) {
  1872. max6675_errors[hindex] += 1;
  1873. if (max6675_errors[hindex] > THERMOCOUPLE_MAX_ERRORS) {
  1874. SERIAL_ERROR_START();
  1875. SERIAL_ECHOPGM("Temp measurement error! ");
  1876. #if MAX6675_ERROR_MASK == 7
  1877. SERIAL_ECHOPGM("MAX31855 ");
  1878. if (max6675_temp & 1)
  1879. SERIAL_ECHOLNPGM("Open Circuit");
  1880. else if (max6675_temp & 2)
  1881. SERIAL_ECHOLNPGM("Short to GND");
  1882. else if (max6675_temp & 4)
  1883. SERIAL_ECHOLNPGM("Short to VCC");
  1884. #else
  1885. SERIAL_ECHOLNPGM("MAX6675");
  1886. #endif
  1887. // Thermocouple open
  1888. max6675_temp = 4 * (
  1889. #if COUNT_6675 > 1
  1890. hindex ? HEATER_1_MAX6675_TMAX : HEATER_0_MAX6675_TMAX
  1891. #elif ENABLED(HEATER_1_USES_MAX6675)
  1892. HEATER_1_MAX6675_TMAX
  1893. #else
  1894. HEATER_0_MAX6675_TMAX
  1895. #endif
  1896. );
  1897. }
  1898. else {
  1899. max6675_temp >>= MAX6675_DISCARD_BITS;
  1900. }
  1901. }
  1902. else {
  1903. max6675_temp >>= MAX6675_DISCARD_BITS;
  1904. max6675_errors[hindex] = 0;
  1905. }
  1906. #if ENABLED(MAX6675_IS_MAX31855)
  1907. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000; // Support negative temperature
  1908. #endif
  1909. #if COUNT_6675 > 1
  1910. max6675_temp_previous[hindex] = max6675_temp;
  1911. #endif
  1912. return int(max6675_temp);
  1913. }
  1914. #endif // HAS_MAX6675
  1915. /**
  1916. * Update raw temperatures
  1917. */
  1918. void Temperature::update_raw_temperatures() {
  1919. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  1920. temp_hotend[0].update();
  1921. #endif
  1922. #if HAS_TEMP_ADC_1
  1923. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1924. redundant_temperature_raw = temp_hotend[1].acc;
  1925. #elif DISABLED(HEATER_1_USES_MAX6675)
  1926. temp_hotend[1].update();
  1927. #endif
  1928. #endif
  1929. TERN_(HAS_TEMP_ADC_2, temp_hotend[2].update());
  1930. TERN_(HAS_TEMP_ADC_3, temp_hotend[3].update());
  1931. TERN_(HAS_TEMP_ADC_4, temp_hotend[4].update());
  1932. TERN_(HAS_TEMP_ADC_5, temp_hotend[5].update());
  1933. TERN_(HAS_TEMP_ADC_6, temp_hotend[6].update());
  1934. TERN_(HAS_TEMP_ADC_7, temp_hotend[7].update());
  1935. TERN_(HAS_HEATED_BED, temp_bed.update());
  1936. TERN_(HAS_TEMP_CHAMBER, temp_chamber.update());
  1937. TERN_(HAS_TEMP_PROBE, temp_probe.update());
  1938. TERN_(HAS_JOY_ADC_X, joystick.x.update());
  1939. TERN_(HAS_JOY_ADC_Y, joystick.y.update());
  1940. TERN_(HAS_JOY_ADC_Z, joystick.z.update());
  1941. raw_temps_ready = true;
  1942. }
  1943. void Temperature::readings_ready() {
  1944. // Update the raw values if they've been read. Else we could be updating them during reading.
  1945. if (!raw_temps_ready) update_raw_temperatures();
  1946. // Filament Sensor - can be read any time since IIR filtering is used
  1947. TERN_(FILAMENT_WIDTH_SENSOR, filwidth.reading_ready());
  1948. #if HAS_HOTEND
  1949. HOTEND_LOOP() temp_hotend[e].reset();
  1950. TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_hotend[1].reset());
  1951. #endif
  1952. TERN_(HAS_HEATED_BED, temp_bed.reset());
  1953. TERN_(HAS_TEMP_CHAMBER, temp_chamber.reset());
  1954. TERN_(HAS_TEMP_PROBE, temp_probe.reset());
  1955. TERN_(HAS_JOY_ADC_X, joystick.x.reset());
  1956. TERN_(HAS_JOY_ADC_Y, joystick.y.reset());
  1957. TERN_(HAS_JOY_ADC_Z, joystick.z.reset());
  1958. #if HAS_HOTEND
  1959. static constexpr int8_t temp_dir[] = {
  1960. TERN(HEATER_0_USES_MAX6675, 0, TEMPDIR(0))
  1961. #if HAS_MULTI_HOTEND
  1962. , TERN(HEATER_1_USES_MAX6675, 0, TEMPDIR(1))
  1963. #if HOTENDS > 2
  1964. #define _TEMPDIR(N) , TEMPDIR(N)
  1965. REPEAT_S(2, HOTENDS, _TEMPDIR)
  1966. #endif
  1967. #endif
  1968. };
  1969. LOOP_L_N(e, COUNT(temp_dir)) {
  1970. const int8_t tdir = temp_dir[e];
  1971. if (tdir) {
  1972. const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
  1973. const bool heater_on = (temp_hotend[e].target > 0
  1974. || TERN0(PIDTEMP, temp_hotend[e].soft_pwm_amount) > 0
  1975. );
  1976. if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_ind_t)e);
  1977. if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
  1978. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1979. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1980. #endif
  1981. min_temp_error((heater_ind_t)e);
  1982. }
  1983. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1984. else
  1985. consecutive_low_temperature_error[e] = 0;
  1986. #endif
  1987. }
  1988. }
  1989. #endif // HOTENDS
  1990. #if HAS_HEATED_BED
  1991. #if TEMPDIR(BED) < 0
  1992. #define BEDCMP(A,B) ((A)<(B))
  1993. #else
  1994. #define BEDCMP(A,B) ((A)>(B))
  1995. #endif
  1996. const bool bed_on = temp_bed.target > 0
  1997. || TERN0(PIDTEMPBED, temp_bed.soft_pwm_amount) > 0
  1998. ;
  1999. if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
  2000. if (bed_on && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
  2001. #endif
  2002. #if HAS_HEATED_CHAMBER
  2003. #if TEMPDIR(CHAMBER) < 0
  2004. #define CHAMBERCMP(A,B) ((A)<(B))
  2005. #else
  2006. #define CHAMBERCMP(A,B) ((A)>(B))
  2007. #endif
  2008. const bool chamber_on = (temp_chamber.target > 0);
  2009. if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
  2010. if (chamber_on && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
  2011. #endif
  2012. }
  2013. /**
  2014. * Timer 0 is shared with millies so don't change the prescaler.
  2015. *
  2016. * On AVR this ISR uses the compare method so it runs at the base
  2017. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  2018. * in OCR0B above (128 or halfway between OVFs).
  2019. *
  2020. * - Manage PWM to all the heaters and fan
  2021. * - Prepare or Measure one of the raw ADC sensor values
  2022. * - Check new temperature values for MIN/MAX errors (kill on error)
  2023. * - Step the babysteps value for each axis towards 0
  2024. * - For PINS_DEBUGGING, monitor and report endstop pins
  2025. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  2026. * - Call planner.tick to count down its "ignore" time
  2027. */
  2028. HAL_TEMP_TIMER_ISR() {
  2029. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  2030. Temperature::tick();
  2031. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  2032. }
  2033. #if ENABLED(SLOW_PWM_HEATERS) && !defined(MIN_STATE_TIME)
  2034. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  2035. #endif
  2036. class SoftPWM {
  2037. public:
  2038. uint8_t count;
  2039. inline bool add(const uint8_t mask, const uint8_t amount) {
  2040. count = (count & mask) + amount; return (count > mask);
  2041. }
  2042. #if ENABLED(SLOW_PWM_HEATERS)
  2043. bool state_heater;
  2044. uint8_t state_timer_heater;
  2045. inline void dec() { if (state_timer_heater > 0) state_timer_heater--; }
  2046. inline bool ready(const bool v) {
  2047. const bool rdy = !state_timer_heater;
  2048. if (rdy && state_heater != v) {
  2049. state_heater = v;
  2050. state_timer_heater = MIN_STATE_TIME;
  2051. }
  2052. return rdy;
  2053. }
  2054. #endif
  2055. };
  2056. /**
  2057. * Handle various ~1KHz tasks associated with temperature
  2058. * - Heater PWM (~1KHz with scaler)
  2059. * - LCD Button polling (~500Hz)
  2060. * - Start / Read one ADC sensor
  2061. * - Advance Babysteps
  2062. * - Endstop polling
  2063. * - Planner clean buffer
  2064. */
  2065. void Temperature::tick() {
  2066. static int8_t temp_count = -1;
  2067. static ADCSensorState adc_sensor_state = StartupDelay;
  2068. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  2069. // avoid multiple loads of pwm_count
  2070. uint8_t pwm_count_tmp = pwm_count;
  2071. #if HAS_ADC_BUTTONS
  2072. static unsigned int raw_ADCKey_value = 0;
  2073. static bool ADCKey_pressed = false;
  2074. #endif
  2075. #if HAS_HOTEND
  2076. static SoftPWM soft_pwm_hotend[HOTENDS];
  2077. #endif
  2078. #if HAS_HEATED_BED
  2079. static SoftPWM soft_pwm_bed;
  2080. #endif
  2081. #if HAS_HEATED_CHAMBER
  2082. static SoftPWM soft_pwm_chamber;
  2083. #endif
  2084. #if DISABLED(SLOW_PWM_HEATERS)
  2085. #if HAS_HOTEND || HAS_HEATED_BED || HAS_HEATED_CHAMBER
  2086. constexpr uint8_t pwm_mask =
  2087. #if ENABLED(SOFT_PWM_DITHER)
  2088. _BV(SOFT_PWM_SCALE) - 1
  2089. #else
  2090. 0
  2091. #endif
  2092. ;
  2093. #define _PWM_MOD(N,S,T) do{ \
  2094. const bool on = S.add(pwm_mask, T.soft_pwm_amount); \
  2095. WRITE_HEATER_##N(on); \
  2096. }while(0)
  2097. #endif
  2098. /**
  2099. * Standard heater PWM modulation
  2100. */
  2101. if (pwm_count_tmp >= 127) {
  2102. pwm_count_tmp -= 127;
  2103. #if HAS_HOTEND
  2104. #define _PWM_MOD_E(N) _PWM_MOD(N,soft_pwm_hotend[N],temp_hotend[N]);
  2105. REPEAT(HOTENDS, _PWM_MOD_E);
  2106. #endif
  2107. #if HAS_HEATED_BED
  2108. _PWM_MOD(BED,soft_pwm_bed,temp_bed);
  2109. #endif
  2110. #if HAS_HEATED_CHAMBER
  2111. _PWM_MOD(CHAMBER,soft_pwm_chamber,temp_chamber);
  2112. #endif
  2113. #if ENABLED(FAN_SOFT_PWM)
  2114. #define _FAN_PWM(N) do{ \
  2115. uint8_t &spcf = soft_pwm_count_fan[N]; \
  2116. spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \
  2117. WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \
  2118. }while(0)
  2119. #if HAS_FAN0
  2120. _FAN_PWM(0);
  2121. #endif
  2122. #if HAS_FAN1
  2123. _FAN_PWM(1);
  2124. #endif
  2125. #if HAS_FAN2
  2126. _FAN_PWM(2);
  2127. #endif
  2128. #if HAS_FAN3
  2129. _FAN_PWM(3);
  2130. #endif
  2131. #if HAS_FAN4
  2132. _FAN_PWM(4);
  2133. #endif
  2134. #if HAS_FAN5
  2135. _FAN_PWM(5);
  2136. #endif
  2137. #if HAS_FAN6
  2138. _FAN_PWM(6);
  2139. #endif
  2140. #if HAS_FAN7
  2141. _FAN_PWM(7);
  2142. #endif
  2143. #endif
  2144. }
  2145. else {
  2146. #define _PWM_LOW(N,S) do{ if (S.count <= pwm_count_tmp) WRITE_HEATER_##N(LOW); }while(0)
  2147. #if HAS_HOTEND
  2148. #define _PWM_LOW_E(N) _PWM_LOW(N, soft_pwm_hotend[N]);
  2149. REPEAT(HOTENDS, _PWM_LOW_E);
  2150. #endif
  2151. #if HAS_HEATED_BED
  2152. _PWM_LOW(BED, soft_pwm_bed);
  2153. #endif
  2154. #if HAS_HEATED_CHAMBER
  2155. _PWM_LOW(CHAMBER, soft_pwm_chamber);
  2156. #endif
  2157. #if ENABLED(FAN_SOFT_PWM)
  2158. #if HAS_FAN0
  2159. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2160. #endif
  2161. #if HAS_FAN1
  2162. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2163. #endif
  2164. #if HAS_FAN2
  2165. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2166. #endif
  2167. #if HAS_FAN3
  2168. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2169. #endif
  2170. #if HAS_FAN4
  2171. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2172. #endif
  2173. #if HAS_FAN5
  2174. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2175. #endif
  2176. #if HAS_FAN6
  2177. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2178. #endif
  2179. #if HAS_FAN7
  2180. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2181. #endif
  2182. #endif
  2183. }
  2184. // SOFT_PWM_SCALE to frequency:
  2185. //
  2186. // 0: 16000000/64/256/128 = 7.6294 Hz
  2187. // 1: / 64 = 15.2588 Hz
  2188. // 2: / 32 = 30.5176 Hz
  2189. // 3: / 16 = 61.0352 Hz
  2190. // 4: / 8 = 122.0703 Hz
  2191. // 5: / 4 = 244.1406 Hz
  2192. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2193. #else // SLOW_PWM_HEATERS
  2194. /**
  2195. * SLOW PWM HEATERS
  2196. *
  2197. * For relay-driven heaters
  2198. */
  2199. #define _SLOW_SET(NR,PWM,V) do{ if (PWM.ready(V)) WRITE_HEATER_##NR(V); }while(0)
  2200. #define _SLOW_PWM(NR,PWM,SRC) do{ PWM.count = SRC.soft_pwm_amount; _SLOW_SET(NR,PWM,(PWM.count > 0)); }while(0)
  2201. #define _PWM_OFF(NR,PWM) do{ if (PWM.count < slow_pwm_count) _SLOW_SET(NR,PWM,0); }while(0)
  2202. static uint8_t slow_pwm_count = 0;
  2203. if (slow_pwm_count == 0) {
  2204. #if HAS_HOTEND
  2205. #define _SLOW_PWM_E(N) _SLOW_PWM(N, soft_pwm_hotend[N], temp_hotend[N]);
  2206. REPEAT(HOTENDS, _SLOW_PWM_E);
  2207. #endif
  2208. #if HAS_HEATED_BED
  2209. _SLOW_PWM(BED, soft_pwm_bed, temp_bed);
  2210. #endif
  2211. } // slow_pwm_count == 0
  2212. #if HAS_HOTEND
  2213. #define _PWM_OFF_E(N) _PWM_OFF(N, soft_pwm_hotend[N]);
  2214. REPEAT(HOTENDS, _PWM_OFF_E);
  2215. #endif
  2216. #if HAS_HEATED_BED
  2217. _PWM_OFF(BED, soft_pwm_bed);
  2218. #endif
  2219. #if ENABLED(FAN_SOFT_PWM)
  2220. if (pwm_count_tmp >= 127) {
  2221. pwm_count_tmp = 0;
  2222. #define _PWM_FAN(N) do{ \
  2223. soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \
  2224. WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \
  2225. }while(0)
  2226. #if HAS_FAN0
  2227. _PWM_FAN(0);
  2228. #endif
  2229. #if HAS_FAN1
  2230. _PWM_FAN(1);
  2231. #endif
  2232. #if HAS_FAN2
  2233. _PWM_FAN(2);
  2234. #endif
  2235. #if HAS_FAN3
  2236. _FAN_PWM(3);
  2237. #endif
  2238. #if HAS_FAN4
  2239. _FAN_PWM(4);
  2240. #endif
  2241. #if HAS_FAN5
  2242. _FAN_PWM(5);
  2243. #endif
  2244. #if HAS_FAN6
  2245. _FAN_PWM(6);
  2246. #endif
  2247. #if HAS_FAN7
  2248. _FAN_PWM(7);
  2249. #endif
  2250. }
  2251. #if HAS_FAN0
  2252. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2253. #endif
  2254. #if HAS_FAN1
  2255. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2256. #endif
  2257. #if HAS_FAN2
  2258. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2259. #endif
  2260. #if HAS_FAN3
  2261. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2262. #endif
  2263. #if HAS_FAN4
  2264. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2265. #endif
  2266. #if HAS_FAN5
  2267. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2268. #endif
  2269. #if HAS_FAN6
  2270. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2271. #endif
  2272. #if HAS_FAN7
  2273. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2274. #endif
  2275. #endif // FAN_SOFT_PWM
  2276. // SOFT_PWM_SCALE to frequency:
  2277. //
  2278. // 0: 16000000/64/256/128 = 7.6294 Hz
  2279. // 1: / 64 = 15.2588 Hz
  2280. // 2: / 32 = 30.5176 Hz
  2281. // 3: / 16 = 61.0352 Hz
  2282. // 4: / 8 = 122.0703 Hz
  2283. // 5: / 4 = 244.1406 Hz
  2284. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2285. // increment slow_pwm_count only every 64th pwm_count,
  2286. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  2287. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  2288. slow_pwm_count++;
  2289. slow_pwm_count &= 0x7F;
  2290. #if HAS_HOTEND
  2291. HOTEND_LOOP() soft_pwm_hotend[e].dec();
  2292. #endif
  2293. TERN_(HAS_HEATED_BED, soft_pwm_bed.dec());
  2294. }
  2295. #endif // SLOW_PWM_HEATERS
  2296. //
  2297. // Update lcd buttons 488 times per second
  2298. //
  2299. static bool do_buttons;
  2300. if ((do_buttons ^= true)) ui.update_buttons();
  2301. /**
  2302. * One sensor is sampled on every other call of the ISR.
  2303. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  2304. *
  2305. * On each Prepare pass, ADC is started for a sensor pin.
  2306. * On the next pass, the ADC value is read and accumulated.
  2307. *
  2308. * This gives each ADC 0.9765ms to charge up.
  2309. */
  2310. #define ACCUMULATE_ADC(obj) do{ \
  2311. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
  2312. else obj.sample(HAL_READ_ADC()); \
  2313. }while(0)
  2314. ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
  2315. switch (adc_sensor_state) {
  2316. case SensorsReady: {
  2317. // All sensors have been read. Stay in this state for a few
  2318. // ISRs to save on calls to temp update/checking code below.
  2319. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  2320. static uint8_t delay_count = 0;
  2321. if (extra_loops > 0) {
  2322. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  2323. if (--delay_count) // While delaying...
  2324. next_sensor_state = SensorsReady; // retain this state (else, next state will be 0)
  2325. break;
  2326. }
  2327. else {
  2328. adc_sensor_state = StartSampling; // Fall-through to start sampling
  2329. next_sensor_state = (ADCSensorState)(int(StartSampling) + 1);
  2330. }
  2331. }
  2332. case StartSampling: // Start of sampling loops. Do updates/checks.
  2333. if (++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  2334. temp_count = 0;
  2335. readings_ready();
  2336. }
  2337. break;
  2338. #if HAS_TEMP_ADC_0
  2339. case PrepareTemp_0: HAL_START_ADC(TEMP_0_PIN); break;
  2340. case MeasureTemp_0: ACCUMULATE_ADC(temp_hotend[0]); break;
  2341. #endif
  2342. #if HAS_HEATED_BED
  2343. case PrepareTemp_BED: HAL_START_ADC(TEMP_BED_PIN); break;
  2344. case MeasureTemp_BED: ACCUMULATE_ADC(temp_bed); break;
  2345. #endif
  2346. #if HAS_TEMP_CHAMBER
  2347. case PrepareTemp_CHAMBER: HAL_START_ADC(TEMP_CHAMBER_PIN); break;
  2348. case MeasureTemp_CHAMBER: ACCUMULATE_ADC(temp_chamber); break;
  2349. #endif
  2350. #if HAS_TEMP_PROBE
  2351. case PrepareTemp_PROBE: HAL_START_ADC(TEMP_PROBE_PIN); break;
  2352. case MeasureTemp_PROBE: ACCUMULATE_ADC(temp_probe); break;
  2353. #endif
  2354. #if HAS_TEMP_ADC_1
  2355. case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break;
  2356. case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break;
  2357. #endif
  2358. #if HAS_TEMP_ADC_2
  2359. case PrepareTemp_2: HAL_START_ADC(TEMP_2_PIN); break;
  2360. case MeasureTemp_2: ACCUMULATE_ADC(temp_hotend[2]); break;
  2361. #endif
  2362. #if HAS_TEMP_ADC_3
  2363. case PrepareTemp_3: HAL_START_ADC(TEMP_3_PIN); break;
  2364. case MeasureTemp_3: ACCUMULATE_ADC(temp_hotend[3]); break;
  2365. #endif
  2366. #if HAS_TEMP_ADC_4
  2367. case PrepareTemp_4: HAL_START_ADC(TEMP_4_PIN); break;
  2368. case MeasureTemp_4: ACCUMULATE_ADC(temp_hotend[4]); break;
  2369. #endif
  2370. #if HAS_TEMP_ADC_5
  2371. case PrepareTemp_5: HAL_START_ADC(TEMP_5_PIN); break;
  2372. case MeasureTemp_5: ACCUMULATE_ADC(temp_hotend[5]); break;
  2373. #endif
  2374. #if HAS_TEMP_ADC_6
  2375. case PrepareTemp_6: HAL_START_ADC(TEMP_6_PIN); break;
  2376. case MeasureTemp_6: ACCUMULATE_ADC(temp_hotend[6]); break;
  2377. #endif
  2378. #if HAS_TEMP_ADC_7
  2379. case PrepareTemp_7: HAL_START_ADC(TEMP_7_PIN); break;
  2380. case MeasureTemp_7: ACCUMULATE_ADC(temp_hotend[7]); break;
  2381. #endif
  2382. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  2383. case Prepare_FILWIDTH: HAL_START_ADC(FILWIDTH_PIN); break;
  2384. case Measure_FILWIDTH:
  2385. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2386. else filwidth.accumulate(HAL_READ_ADC());
  2387. break;
  2388. #endif
  2389. #if ENABLED(POWER_MONITOR_CURRENT)
  2390. case Prepare_POWER_MONITOR_CURRENT:
  2391. HAL_START_ADC(POWER_MONITOR_CURRENT_PIN);
  2392. break;
  2393. case Measure_POWER_MONITOR_CURRENT:
  2394. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2395. else power_monitor.add_current_sample(HAL_READ_ADC());
  2396. break;
  2397. #endif
  2398. #if ENABLED(POWER_MONITOR_VOLTAGE)
  2399. case Prepare_POWER_MONITOR_VOLTAGE:
  2400. HAL_START_ADC(POWER_MONITOR_VOLTAGE_PIN);
  2401. break;
  2402. case Measure_POWER_MONITOR_VOLTAGE:
  2403. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
  2404. else power_monitor.add_voltage_sample(HAL_READ_ADC());
  2405. break;
  2406. #endif
  2407. #if HAS_JOY_ADC_X
  2408. case PrepareJoy_X: HAL_START_ADC(JOY_X_PIN); break;
  2409. case MeasureJoy_X: ACCUMULATE_ADC(joystick.x); break;
  2410. #endif
  2411. #if HAS_JOY_ADC_Y
  2412. case PrepareJoy_Y: HAL_START_ADC(JOY_Y_PIN); break;
  2413. case MeasureJoy_Y: ACCUMULATE_ADC(joystick.y); break;
  2414. #endif
  2415. #if HAS_JOY_ADC_Z
  2416. case PrepareJoy_Z: HAL_START_ADC(JOY_Z_PIN); break;
  2417. case MeasureJoy_Z: ACCUMULATE_ADC(joystick.z); break;
  2418. #endif
  2419. #if HAS_ADC_BUTTONS
  2420. #ifndef ADC_BUTTON_DEBOUNCE_DELAY
  2421. #define ADC_BUTTON_DEBOUNCE_DELAY 16
  2422. #endif
  2423. case Prepare_ADC_KEY: HAL_START_ADC(ADC_KEYPAD_PIN); break;
  2424. case Measure_ADC_KEY:
  2425. if (!HAL_ADC_READY())
  2426. next_sensor_state = adc_sensor_state; // redo this state
  2427. else if (ADCKey_count < ADC_BUTTON_DEBOUNCE_DELAY) {
  2428. raw_ADCKey_value = HAL_READ_ADC();
  2429. if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
  2430. NOMORE(current_ADCKey_raw, raw_ADCKey_value);
  2431. ADCKey_count++;
  2432. }
  2433. else { //ADC Key release
  2434. if (ADCKey_count > 0) ADCKey_count++; else ADCKey_pressed = false;
  2435. if (ADCKey_pressed) {
  2436. ADCKey_count = 0;
  2437. current_ADCKey_raw = HAL_ADC_RANGE;
  2438. }
  2439. }
  2440. }
  2441. if (ADCKey_count == ADC_BUTTON_DEBOUNCE_DELAY) ADCKey_pressed = true;
  2442. break;
  2443. #endif // HAS_ADC_BUTTONS
  2444. case StartupDelay: break;
  2445. } // switch(adc_sensor_state)
  2446. // Go to the next state
  2447. adc_sensor_state = next_sensor_state;
  2448. //
  2449. // Additional ~1KHz Tasks
  2450. //
  2451. #if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
  2452. babystep.task();
  2453. #endif
  2454. // Poll endstops state, if required
  2455. endstops.poll();
  2456. // Periodically call the planner timer
  2457. planner.tick();
  2458. }
  2459. #if HAS_TEMP_SENSOR
  2460. #include "../gcode/gcode.h"
  2461. static void print_heater_state(const float &c, const float &t
  2462. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2463. , const float r
  2464. #endif
  2465. , const heater_ind_t e=INDEX_NONE
  2466. ) {
  2467. char k;
  2468. switch (e) {
  2469. #if HAS_TEMP_CHAMBER
  2470. case H_CHAMBER: k = 'C'; break;
  2471. #endif
  2472. #if HAS_TEMP_PROBE
  2473. case H_PROBE: k = 'P'; break;
  2474. #endif
  2475. #if HAS_TEMP_HOTEND
  2476. default: k = 'T'; break;
  2477. #if HAS_HEATED_BED
  2478. case H_BED: k = 'B'; break;
  2479. #endif
  2480. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2481. case H_REDUNDANT: k = 'R'; break;
  2482. #endif
  2483. #elif HAS_HEATED_BED
  2484. default: k = 'B'; break;
  2485. #endif
  2486. }
  2487. SERIAL_CHAR(' ');
  2488. SERIAL_CHAR(k);
  2489. #if HAS_MULTI_HOTEND
  2490. if (e >= 0) SERIAL_CHAR('0' + e);
  2491. #endif
  2492. SERIAL_CHAR(':');
  2493. SERIAL_ECHO(c);
  2494. SERIAL_ECHOPAIR(" /" , t);
  2495. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2496. SERIAL_ECHOPAIR(" (", r * RECIPROCAL(OVERSAMPLENR));
  2497. SERIAL_CHAR(')');
  2498. #endif
  2499. delay(2);
  2500. }
  2501. void Temperature::print_heater_states(const uint8_t target_extruder
  2502. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2503. , const bool include_r/*=false*/
  2504. #endif
  2505. ) {
  2506. #if HAS_TEMP_HOTEND
  2507. print_heater_state(degHotend(target_extruder), degTargetHotend(target_extruder)
  2508. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2509. , rawHotendTemp(target_extruder)
  2510. #endif
  2511. );
  2512. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2513. if (include_r) print_heater_state(redundant_temperature, degTargetHotend(target_extruder)
  2514. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2515. , redundant_temperature_raw
  2516. #endif
  2517. , H_REDUNDANT
  2518. );
  2519. #endif
  2520. #endif
  2521. #if HAS_HEATED_BED
  2522. print_heater_state(degBed(), degTargetBed()
  2523. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2524. , rawBedTemp()
  2525. #endif
  2526. , H_BED
  2527. );
  2528. #endif
  2529. #if HAS_TEMP_CHAMBER
  2530. print_heater_state(degChamber()
  2531. #if HAS_HEATED_CHAMBER
  2532. , degTargetChamber()
  2533. #else
  2534. , 0
  2535. #endif
  2536. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2537. , rawChamberTemp()
  2538. #endif
  2539. , H_CHAMBER
  2540. );
  2541. #endif // HAS_TEMP_CHAMBER
  2542. #if HAS_TEMP_PROBE
  2543. print_heater_state(degProbe(), 0
  2544. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2545. , rawProbeTemp()
  2546. #endif
  2547. , H_PROBE
  2548. );
  2549. #endif // HAS_TEMP_PROBE
  2550. #if HAS_MULTI_HOTEND
  2551. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2552. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2553. , rawHotendTemp(e)
  2554. #endif
  2555. , (heater_ind_t)e
  2556. );
  2557. #endif
  2558. SERIAL_ECHOPAIR(" @:", getHeaterPower((heater_ind_t)target_extruder));
  2559. #if HAS_HEATED_BED
  2560. SERIAL_ECHOPAIR(" B@:", getHeaterPower(H_BED));
  2561. #endif
  2562. #if HAS_HEATED_CHAMBER
  2563. SERIAL_ECHOPAIR(" C@:", getHeaterPower(H_CHAMBER));
  2564. #endif
  2565. #if HAS_MULTI_HOTEND
  2566. HOTEND_LOOP() {
  2567. SERIAL_ECHOPAIR(" @", e);
  2568. SERIAL_CHAR(':');
  2569. SERIAL_ECHO(getHeaterPower((heater_ind_t)e));
  2570. }
  2571. #endif
  2572. }
  2573. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2574. uint8_t Temperature::auto_report_temp_interval;
  2575. millis_t Temperature::next_temp_report_ms;
  2576. void Temperature::auto_report_temperatures() {
  2577. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2578. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2579. PORT_REDIRECT(SERIAL_BOTH);
  2580. print_heater_states(active_extruder);
  2581. SERIAL_EOL();
  2582. }
  2583. }
  2584. #endif // AUTO_REPORT_TEMPERATURES
  2585. #if HAS_HOTEND && HAS_DISPLAY
  2586. void Temperature::set_heating_message(const uint8_t e) {
  2587. const bool heating = isHeatingHotend(e);
  2588. ui.status_printf_P(0,
  2589. #if HAS_MULTI_HOTEND
  2590. PSTR("E%c " S_FMT), '1' + e
  2591. #else
  2592. PSTR("E " S_FMT)
  2593. #endif
  2594. , heating ? GET_TEXT(MSG_HEATING) : GET_TEXT(MSG_COOLING)
  2595. );
  2596. }
  2597. #endif
  2598. #if HAS_TEMP_HOTEND
  2599. #ifndef MIN_COOLING_SLOPE_DEG
  2600. #define MIN_COOLING_SLOPE_DEG 1.50
  2601. #endif
  2602. #ifndef MIN_COOLING_SLOPE_TIME
  2603. #define MIN_COOLING_SLOPE_TIME 60
  2604. #endif
  2605. bool Temperature::wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling/*=true*/
  2606. #if G26_CLICK_CAN_CANCEL
  2607. , const bool click_to_cancel/*=false*/
  2608. #endif
  2609. ) {
  2610. #if ENABLED(AUTOTEMP)
  2611. REMEMBER(1, planner.autotemp_enabled, false);
  2612. #endif
  2613. #if TEMP_RESIDENCY_TIME > 0
  2614. millis_t residency_start_ms = 0;
  2615. bool first_loop = true;
  2616. // Loop until the temperature has stabilized
  2617. #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_RESIDENCY_TIME)))
  2618. #else
  2619. // Loop until the temperature is very close target
  2620. #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
  2621. #endif
  2622. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2623. KEEPALIVE_STATE(NOT_BUSY);
  2624. #endif
  2625. #if ENABLED(PRINTER_EVENT_LEDS)
  2626. const float start_temp = degHotend(target_extruder);
  2627. printerEventLEDs.onHotendHeatingStart();
  2628. #endif
  2629. float target_temp = -1.0, old_temp = 9999.0;
  2630. bool wants_to_cool = false;
  2631. wait_for_heatup = true;
  2632. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2633. do {
  2634. // Target temperature might be changed during the loop
  2635. if (target_temp != degTargetHotend(target_extruder)) {
  2636. wants_to_cool = isCoolingHotend(target_extruder);
  2637. target_temp = degTargetHotend(target_extruder);
  2638. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2639. if (no_wait_for_cooling && wants_to_cool) break;
  2640. }
  2641. now = millis();
  2642. if (ELAPSED(now, next_temp_ms)) { // Print temp & remaining time every 1s while waiting
  2643. next_temp_ms = now + 1000UL;
  2644. print_heater_states(target_extruder);
  2645. #if TEMP_RESIDENCY_TIME > 0
  2646. SERIAL_ECHOPGM(" W:");
  2647. if (residency_start_ms)
  2648. SERIAL_ECHO(long((SEC_TO_MS(TEMP_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2649. else
  2650. SERIAL_CHAR('?');
  2651. #endif
  2652. SERIAL_EOL();
  2653. }
  2654. idle();
  2655. gcode.reset_stepper_timeout(); // Keep steppers powered
  2656. const float temp = degHotend(target_extruder);
  2657. #if ENABLED(PRINTER_EVENT_LEDS)
  2658. // Gradually change LED strip from violet to red as nozzle heats up
  2659. if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp);
  2660. #endif
  2661. #if TEMP_RESIDENCY_TIME > 0
  2662. const float temp_diff = ABS(target_temp - temp);
  2663. if (!residency_start_ms) {
  2664. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
  2665. if (temp_diff < TEMP_WINDOW) {
  2666. residency_start_ms = now;
  2667. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_RESIDENCY_TIME);
  2668. }
  2669. }
  2670. else if (temp_diff > TEMP_HYSTERESIS) {
  2671. // Restart the timer whenever the temperature falls outside the hysteresis.
  2672. residency_start_ms = now;
  2673. }
  2674. first_loop = false;
  2675. #endif
  2676. // Prevent a wait-forever situation if R is misused i.e. M109 R0
  2677. if (wants_to_cool) {
  2678. // break after MIN_COOLING_SLOPE_TIME seconds
  2679. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG
  2680. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2681. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG)) break;
  2682. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME;
  2683. old_temp = temp;
  2684. }
  2685. }
  2686. #if G26_CLICK_CAN_CANCEL
  2687. if (click_to_cancel && ui.use_click()) {
  2688. wait_for_heatup = false;
  2689. ui.quick_feedback();
  2690. }
  2691. #endif
  2692. } while (wait_for_heatup && TEMP_CONDITIONS);
  2693. if (wait_for_heatup) {
  2694. #if ENABLED(DWIN_CREALITY_LCD)
  2695. HMI_flag.heat_flag = 0;
  2696. duration_t elapsed = print_job_timer.duration(); // print timer
  2697. heat_time = elapsed.value;
  2698. #else
  2699. ui.reset_status();
  2700. #endif
  2701. TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onHeatingDone());
  2702. }
  2703. return wait_for_heatup;
  2704. }
  2705. #endif // HAS_TEMP_HOTEND
  2706. #if HAS_HEATED_BED
  2707. #ifndef MIN_COOLING_SLOPE_DEG_BED
  2708. #define MIN_COOLING_SLOPE_DEG_BED 1.00
  2709. #endif
  2710. #ifndef MIN_COOLING_SLOPE_TIME_BED
  2711. #define MIN_COOLING_SLOPE_TIME_BED 60
  2712. #endif
  2713. bool Temperature::wait_for_bed(const bool no_wait_for_cooling/*=true*/
  2714. #if G26_CLICK_CAN_CANCEL
  2715. , const bool click_to_cancel/*=false*/
  2716. #endif
  2717. ) {
  2718. #if TEMP_BED_RESIDENCY_TIME > 0
  2719. millis_t residency_start_ms = 0;
  2720. bool first_loop = true;
  2721. // Loop until the temperature has stabilized
  2722. #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_BED_RESIDENCY_TIME)))
  2723. #else
  2724. // Loop until the temperature is very close target
  2725. #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
  2726. #endif
  2727. float target_temp = -1, old_temp = 9999;
  2728. bool wants_to_cool = false;
  2729. wait_for_heatup = true;
  2730. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2731. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2732. KEEPALIVE_STATE(NOT_BUSY);
  2733. #endif
  2734. #if ENABLED(PRINTER_EVENT_LEDS)
  2735. const float start_temp = degBed();
  2736. printerEventLEDs.onBedHeatingStart();
  2737. #endif
  2738. do {
  2739. // Target temperature might be changed during the loop
  2740. if (target_temp != degTargetBed()) {
  2741. wants_to_cool = isCoolingBed();
  2742. target_temp = degTargetBed();
  2743. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2744. if (no_wait_for_cooling && wants_to_cool) break;
  2745. }
  2746. now = millis();
  2747. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2748. next_temp_ms = now + 1000UL;
  2749. print_heater_states(active_extruder);
  2750. #if TEMP_BED_RESIDENCY_TIME > 0
  2751. SERIAL_ECHOPGM(" W:");
  2752. if (residency_start_ms)
  2753. SERIAL_ECHO(long((SEC_TO_MS(TEMP_BED_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2754. else
  2755. SERIAL_CHAR('?');
  2756. #endif
  2757. SERIAL_EOL();
  2758. }
  2759. idle();
  2760. gcode.reset_stepper_timeout(); // Keep steppers powered
  2761. const float temp = degBed();
  2762. #if ENABLED(PRINTER_EVENT_LEDS)
  2763. // Gradually change LED strip from blue to violet as bed heats up
  2764. if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp);
  2765. #endif
  2766. #if TEMP_BED_RESIDENCY_TIME > 0
  2767. const float temp_diff = ABS(target_temp - temp);
  2768. if (!residency_start_ms) {
  2769. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
  2770. if (temp_diff < TEMP_BED_WINDOW) {
  2771. residency_start_ms = now;
  2772. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_BED_RESIDENCY_TIME);
  2773. }
  2774. }
  2775. else if (temp_diff > TEMP_BED_HYSTERESIS) {
  2776. // Restart the timer whenever the temperature falls outside the hysteresis.
  2777. residency_start_ms = now;
  2778. }
  2779. #endif // TEMP_BED_RESIDENCY_TIME > 0
  2780. // Prevent a wait-forever situation if R is misused i.e. M190 R0
  2781. if (wants_to_cool) {
  2782. // Break after MIN_COOLING_SLOPE_TIME_BED seconds
  2783. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED
  2784. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2785. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_BED)) break;
  2786. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED;
  2787. old_temp = temp;
  2788. }
  2789. }
  2790. #if G26_CLICK_CAN_CANCEL
  2791. if (click_to_cancel && ui.use_click()) {
  2792. wait_for_heatup = false;
  2793. ui.quick_feedback();
  2794. }
  2795. #endif
  2796. #if TEMP_BED_RESIDENCY_TIME > 0
  2797. first_loop = false;
  2798. #endif
  2799. } while (wait_for_heatup && TEMP_BED_CONDITIONS);
  2800. if (wait_for_heatup) ui.reset_status();
  2801. return wait_for_heatup;
  2802. }
  2803. void Temperature::wait_for_bed_heating() {
  2804. if (isHeatingBed()) {
  2805. SERIAL_ECHOLNPGM("Wait for bed heating...");
  2806. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2807. wait_for_bed();
  2808. ui.reset_status();
  2809. }
  2810. }
  2811. #endif // HAS_HEATED_BED
  2812. #if HAS_HEATED_CHAMBER
  2813. #ifndef MIN_COOLING_SLOPE_DEG_CHAMBER
  2814. #define MIN_COOLING_SLOPE_DEG_CHAMBER 1.50
  2815. #endif
  2816. #ifndef MIN_COOLING_SLOPE_TIME_CHAMBER
  2817. #define MIN_COOLING_SLOPE_TIME_CHAMBER 60
  2818. #endif
  2819. bool Temperature::wait_for_chamber(const bool no_wait_for_cooling/*=true*/) {
  2820. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2821. millis_t residency_start_ms = 0;
  2822. bool first_loop = true;
  2823. // Loop until the temperature has stabilized
  2824. #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME)))
  2825. #else
  2826. // Loop until the temperature is very close target
  2827. #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
  2828. #endif
  2829. float target_temp = -1, old_temp = 9999;
  2830. bool wants_to_cool = false;
  2831. wait_for_heatup = true;
  2832. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2833. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2834. KEEPALIVE_STATE(NOT_BUSY);
  2835. #endif
  2836. do {
  2837. // Target temperature might be changed during the loop
  2838. if (target_temp != degTargetChamber()) {
  2839. wants_to_cool = isCoolingChamber();
  2840. target_temp = degTargetChamber();
  2841. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2842. if (no_wait_for_cooling && wants_to_cool) break;
  2843. }
  2844. now = millis();
  2845. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2846. next_temp_ms = now + 1000UL;
  2847. print_heater_states(active_extruder);
  2848. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2849. SERIAL_ECHOPGM(" W:");
  2850. if (residency_start_ms)
  2851. SERIAL_ECHO(long((SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME) - (now - residency_start_ms)) / 1000UL));
  2852. else
  2853. SERIAL_CHAR('?');
  2854. #endif
  2855. SERIAL_EOL();
  2856. }
  2857. idle();
  2858. gcode.reset_stepper_timeout(); // Keep steppers powered
  2859. const float temp = degChamber();
  2860. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2861. const float temp_diff = ABS(target_temp - temp);
  2862. if (!residency_start_ms) {
  2863. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
  2864. if (temp_diff < TEMP_CHAMBER_WINDOW) {
  2865. residency_start_ms = now;
  2866. if (first_loop) residency_start_ms += SEC_TO_MS(TEMP_CHAMBER_RESIDENCY_TIME);
  2867. }
  2868. }
  2869. else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {
  2870. // Restart the timer whenever the temperature falls outside the hysteresis.
  2871. residency_start_ms = now;
  2872. }
  2873. first_loop = false;
  2874. #endif // TEMP_CHAMBER_RESIDENCY_TIME > 0
  2875. // Prevent a wait-forever situation if R is misused i.e. M191 R0
  2876. if (wants_to_cool) {
  2877. // Break after MIN_COOLING_SLOPE_TIME_CHAMBER seconds
  2878. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_CHAMBER
  2879. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2880. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_CHAMBER)) break;
  2881. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_CHAMBER;
  2882. old_temp = temp;
  2883. }
  2884. }
  2885. } while (wait_for_heatup && TEMP_CHAMBER_CONDITIONS);
  2886. if (wait_for_heatup) ui.reset_status();
  2887. return wait_for_heatup;
  2888. }
  2889. #endif // HAS_HEATED_CHAMBER
  2890. #endif // HAS_TEMP_SENSOR