My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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