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

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