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

temperature.cpp 118KB

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