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

temperature.cpp 157KB

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