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

temperature.cpp 112KB

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