My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

temperature.cpp 143KB

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