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

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