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

temperature.cpp 134KB

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