My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 135KB

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