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

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