My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 141KB

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