My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.cpp 136KB

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