My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 118KB

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