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

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