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

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