My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 123KB

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