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 140KB

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