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

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