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.h 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * temperature.h - temperature controller
  25. */
  26. #include "thermistor/thermistors.h"
  27. #include "../inc/MarlinConfig.h"
  28. #if ENABLED(AUTO_POWER_CONTROL)
  29. #include "../feature/power.h"
  30. #endif
  31. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  32. #include "../libs/autoreport.h"
  33. #endif
  34. #if HAS_FANCHECK
  35. #include "../feature/fancheck.h"
  36. #endif
  37. #ifndef SOFT_PWM_SCALE
  38. #define SOFT_PWM_SCALE 0
  39. #endif
  40. #define HOTEND_INDEX TERN(HAS_MULTI_HOTEND, e, 0)
  41. #define E_NAME TERN_(HAS_MULTI_HOTEND, e)
  42. // Element identifiers. Positive values are hotends. Negative values are other heaters or coolers.
  43. typedef enum : int8_t {
  44. H_REDUNDANT = HID_REDUNDANT,
  45. H_COOLER = HID_COOLER,
  46. H_PROBE = HID_PROBE,
  47. H_BOARD = HID_BOARD,
  48. H_CHAMBER = HID_CHAMBER,
  49. H_BED = HID_BED,
  50. H_E0 = HID_E0, H_E1, H_E2, H_E3, H_E4, H_E5, H_E6, H_E7,
  51. H_NONE = -128
  52. } heater_id_t;
  53. // PID storage
  54. typedef struct { float Kp, Ki, Kd; } PID_t;
  55. typedef struct { float Kp, Ki, Kd, Kc; } PIDC_t;
  56. typedef struct { float Kp, Ki, Kd, Kf; } PIDF_t;
  57. typedef struct { float Kp, Ki, Kd, Kc, Kf; } PIDCF_t;
  58. typedef
  59. #if BOTH(PID_EXTRUSION_SCALING, PID_FAN_SCALING)
  60. PIDCF_t
  61. #elif ENABLED(PID_EXTRUSION_SCALING)
  62. PIDC_t
  63. #elif ENABLED(PID_FAN_SCALING)
  64. PIDF_t
  65. #else
  66. PID_t
  67. #endif
  68. hotend_pid_t;
  69. #if ENABLED(PID_EXTRUSION_SCALING)
  70. typedef IF<(LPQ_MAX_LEN > 255), uint16_t, uint8_t>::type lpq_ptr_t;
  71. #endif
  72. #define PID_PARAM(F,H) _PID_##F(TERN(PID_PARAMS_PER_HOTEND, H, 0 & H)) // Always use 'H' to suppress warning
  73. #define _PID_Kp(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kp, NAN)
  74. #define _PID_Ki(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Ki, NAN)
  75. #define _PID_Kd(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kd, NAN)
  76. #if ENABLED(PIDTEMP)
  77. #define _PID_Kc(H) TERN(PID_EXTRUSION_SCALING, Temperature::temp_hotend[H].pid.Kc, 1)
  78. #define _PID_Kf(H) TERN(PID_FAN_SCALING, Temperature::temp_hotend[H].pid.Kf, 0)
  79. #else
  80. #define _PID_Kc(H) 1
  81. #define _PID_Kf(H) 0
  82. #endif
  83. #if ENABLED(MPCTEMP)
  84. typedef struct {
  85. float heater_power; // M306 P
  86. float block_heat_capacity; // M306 C
  87. float sensor_responsiveness; // M306 R
  88. float ambient_xfer_coeff_fan0; // M306 A
  89. #if ENABLED(MPC_INCLUDE_FAN)
  90. float fan255_adjustment; // M306 F
  91. #endif
  92. float filament_heat_capacity_permm; // M306 H
  93. } MPC_t;
  94. #endif
  95. /**
  96. * States for ADC reading in the ISR
  97. */
  98. enum ADCSensorState : char {
  99. StartSampling,
  100. #if HAS_TEMP_ADC_0
  101. PrepareTemp_0, MeasureTemp_0,
  102. #endif
  103. #if HAS_TEMP_ADC_BED
  104. PrepareTemp_BED, MeasureTemp_BED,
  105. #endif
  106. #if HAS_TEMP_ADC_CHAMBER
  107. PrepareTemp_CHAMBER, MeasureTemp_CHAMBER,
  108. #endif
  109. #if HAS_TEMP_ADC_COOLER
  110. PrepareTemp_COOLER, MeasureTemp_COOLER,
  111. #endif
  112. #if HAS_TEMP_ADC_PROBE
  113. PrepareTemp_PROBE, MeasureTemp_PROBE,
  114. #endif
  115. #if HAS_TEMP_ADC_BOARD
  116. PrepareTemp_BOARD, MeasureTemp_BOARD,
  117. #endif
  118. #if HAS_TEMP_ADC_REDUNDANT
  119. PrepareTemp_REDUNDANT, MeasureTemp_REDUNDANT,
  120. #endif
  121. #if HAS_TEMP_ADC_1
  122. PrepareTemp_1, MeasureTemp_1,
  123. #endif
  124. #if HAS_TEMP_ADC_2
  125. PrepareTemp_2, MeasureTemp_2,
  126. #endif
  127. #if HAS_TEMP_ADC_3
  128. PrepareTemp_3, MeasureTemp_3,
  129. #endif
  130. #if HAS_TEMP_ADC_4
  131. PrepareTemp_4, MeasureTemp_4,
  132. #endif
  133. #if HAS_TEMP_ADC_5
  134. PrepareTemp_5, MeasureTemp_5,
  135. #endif
  136. #if HAS_TEMP_ADC_6
  137. PrepareTemp_6, MeasureTemp_6,
  138. #endif
  139. #if HAS_TEMP_ADC_7
  140. PrepareTemp_7, MeasureTemp_7,
  141. #endif
  142. #if HAS_JOY_ADC_X
  143. PrepareJoy_X, MeasureJoy_X,
  144. #endif
  145. #if HAS_JOY_ADC_Y
  146. PrepareJoy_Y, MeasureJoy_Y,
  147. #endif
  148. #if HAS_JOY_ADC_Z
  149. PrepareJoy_Z, MeasureJoy_Z,
  150. #endif
  151. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  152. Prepare_FILWIDTH, Measure_FILWIDTH,
  153. #endif
  154. #if ENABLED(POWER_MONITOR_CURRENT)
  155. Prepare_POWER_MONITOR_CURRENT,
  156. Measure_POWER_MONITOR_CURRENT,
  157. #endif
  158. #if ENABLED(POWER_MONITOR_VOLTAGE)
  159. Prepare_POWER_MONITOR_VOLTAGE,
  160. Measure_POWER_MONITOR_VOLTAGE,
  161. #endif
  162. #if HAS_ADC_BUTTONS
  163. Prepare_ADC_KEY, Measure_ADC_KEY,
  164. #endif
  165. SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
  166. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  167. };
  168. // Minimum number of Temperature::ISR loops between sensor readings.
  169. // Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
  170. // get all oversampled sensor readings
  171. #define MIN_ADC_ISR_LOOPS 10
  172. #define ACTUAL_ADC_SAMPLES _MAX(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
  173. #if HAS_PID_HEATING
  174. #define PID_K2 (1-float(PID_K1))
  175. #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (TEMP_TIMER_FREQUENCY))
  176. // Apply the scale factors to the PID values
  177. #define scalePID_i(i) ( float(i) * PID_dT )
  178. #define unscalePID_i(i) ( float(i) / PID_dT )
  179. #define scalePID_d(d) ( float(d) / PID_dT )
  180. #define unscalePID_d(d) ( float(d) * PID_dT )
  181. #endif
  182. #if ENABLED(MPCTEMP)
  183. #define MPC_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (TEMP_TIMER_FREQUENCY))
  184. #endif
  185. #if ENABLED(G26_MESH_VALIDATION) && EITHER(HAS_MARLINUI_MENU, EXTENSIBLE_UI)
  186. #define G26_CLICK_CAN_CANCEL 1
  187. #endif
  188. // A temperature sensor
  189. typedef struct TempInfo {
  190. private:
  191. raw_adc_t acc;
  192. raw_adc_t raw;
  193. public:
  194. celsius_float_t celsius;
  195. inline void reset() { acc = 0; }
  196. inline void sample(const raw_adc_t s) { acc += s; }
  197. inline void update() { raw = acc; }
  198. void setraw(const raw_adc_t r) { raw = r; }
  199. raw_adc_t getraw() { return raw; }
  200. } temp_info_t;
  201. #if HAS_TEMP_REDUNDANT
  202. // A redundant temperature sensor
  203. typedef struct RedundantTempInfo : public TempInfo {
  204. temp_info_t* target;
  205. } redundant_info_t;
  206. #endif
  207. // A PWM heater with temperature sensor
  208. typedef struct HeaterInfo : public TempInfo {
  209. celsius_t target;
  210. uint8_t soft_pwm_amount;
  211. bool is_below_target(const celsius_t offs=0) const { return (celsius < (target + offs)); }
  212. } heater_info_t;
  213. // A heater with PID stabilization
  214. template<typename T>
  215. struct PIDHeaterInfo : public HeaterInfo {
  216. T pid; // Initialized by settings.load()
  217. };
  218. #if ENABLED(MPCTEMP)
  219. struct MPCHeaterInfo : public HeaterInfo {
  220. MPC_t constants;
  221. float modeled_ambient_temp,
  222. modeled_block_temp,
  223. modeled_sensor_temp;
  224. };
  225. #endif
  226. #if ENABLED(PIDTEMP)
  227. typedef struct PIDHeaterInfo<hotend_pid_t> hotend_info_t;
  228. #elif ENABLED(MPCTEMP)
  229. typedef struct MPCHeaterInfo hotend_info_t;
  230. #else
  231. typedef heater_info_t hotend_info_t;
  232. #endif
  233. #if HAS_HEATED_BED
  234. #if ENABLED(PIDTEMPBED)
  235. typedef struct PIDHeaterInfo<PID_t> bed_info_t;
  236. #else
  237. typedef heater_info_t bed_info_t;
  238. #endif
  239. #endif
  240. #if HAS_HEATED_CHAMBER
  241. #if ENABLED(PIDTEMPCHAMBER)
  242. typedef struct PIDHeaterInfo<PID_t> chamber_info_t;
  243. #else
  244. typedef heater_info_t chamber_info_t;
  245. #endif
  246. #elif HAS_TEMP_CHAMBER
  247. typedef temp_info_t chamber_info_t;
  248. #endif
  249. #if HAS_TEMP_PROBE
  250. typedef temp_info_t probe_info_t;
  251. #endif
  252. #if EITHER(HAS_COOLER, HAS_TEMP_COOLER)
  253. typedef heater_info_t cooler_info_t;
  254. #endif
  255. #if HAS_TEMP_BOARD
  256. typedef temp_info_t board_info_t;
  257. #endif
  258. // Heater watch handling
  259. template <int INCREASE, int HYSTERESIS, millis_t PERIOD>
  260. struct HeaterWatch {
  261. celsius_t target;
  262. millis_t next_ms;
  263. inline bool elapsed(const millis_t &ms) { return next_ms && ELAPSED(ms, next_ms); }
  264. inline bool elapsed() { return elapsed(millis()); }
  265. inline bool check(const celsius_t curr) { return curr >= target; }
  266. inline void restart(const celsius_t curr, const celsius_t tgt) {
  267. if (tgt) {
  268. const celsius_t newtarget = curr + INCREASE;
  269. if (newtarget < tgt - HYSTERESIS - 1) {
  270. target = newtarget;
  271. next_ms = millis() + SEC_TO_MS(PERIOD);
  272. return;
  273. }
  274. }
  275. next_ms = 0;
  276. }
  277. };
  278. #if WATCH_HOTENDS
  279. typedef struct HeaterWatch<WATCH_TEMP_INCREASE, TEMP_HYSTERESIS, WATCH_TEMP_PERIOD> hotend_watch_t;
  280. #endif
  281. #if WATCH_BED
  282. typedef struct HeaterWatch<WATCH_BED_TEMP_INCREASE, TEMP_BED_HYSTERESIS, WATCH_BED_TEMP_PERIOD> bed_watch_t;
  283. #endif
  284. #if WATCH_CHAMBER
  285. typedef struct HeaterWatch<WATCH_CHAMBER_TEMP_INCREASE, TEMP_CHAMBER_HYSTERESIS, WATCH_CHAMBER_TEMP_PERIOD> chamber_watch_t;
  286. #endif
  287. #if WATCH_COOLER
  288. typedef struct HeaterWatch<WATCH_COOLER_TEMP_INCREASE, TEMP_COOLER_HYSTERESIS, WATCH_COOLER_TEMP_PERIOD> cooler_watch_t;
  289. #endif
  290. // Temperature sensor read value ranges
  291. typedef struct { raw_adc_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
  292. #define THERMISTOR_ABS_ZERO_C -273.15f // bbbbrrrrr cold !
  293. #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f // mmmmm comfortable
  294. #if HAS_USER_THERMISTORS
  295. enum CustomThermistorIndex : uint8_t {
  296. #if TEMP_SENSOR_0_IS_CUSTOM
  297. CTI_HOTEND_0,
  298. #endif
  299. #if TEMP_SENSOR_1_IS_CUSTOM
  300. CTI_HOTEND_1,
  301. #endif
  302. #if TEMP_SENSOR_2_IS_CUSTOM
  303. CTI_HOTEND_2,
  304. #endif
  305. #if TEMP_SENSOR_3_IS_CUSTOM
  306. CTI_HOTEND_3,
  307. #endif
  308. #if TEMP_SENSOR_4_IS_CUSTOM
  309. CTI_HOTEND_4,
  310. #endif
  311. #if TEMP_SENSOR_5_IS_CUSTOM
  312. CTI_HOTEND_5,
  313. #endif
  314. #if TEMP_SENSOR_BED_IS_CUSTOM
  315. CTI_BED,
  316. #endif
  317. #if TEMP_SENSOR_CHAMBER_IS_CUSTOM
  318. CTI_CHAMBER,
  319. #endif
  320. #if TEMP_SENSOR_PROBE_IS_CUSTOM
  321. CTI_PROBE,
  322. #endif
  323. #if TEMP_SENSOR_COOLER_IS_CUSTOM
  324. CTI_COOLER,
  325. #endif
  326. #if TEMP_SENSOR_BOARD_IS_CUSTOM
  327. CTI_BOARD,
  328. #endif
  329. #if TEMP_SENSOR_REDUNDANT_IS_CUSTOM
  330. CTI_REDUNDANT,
  331. #endif
  332. USER_THERMISTORS
  333. };
  334. // User-defined thermistor
  335. typedef struct {
  336. bool pre_calc; // true if pre-calculations update needed
  337. float sh_c_coeff, // Steinhart-Hart C coefficient .. defaults to '0.0'
  338. sh_alpha,
  339. series_res,
  340. res_25, res_25_recip,
  341. res_25_log,
  342. beta, beta_recip;
  343. } user_thermistor_t;
  344. #endif
  345. #if HAS_AUTO_FAN || HAS_FANCHECK
  346. #define HAS_FAN_LOGIC 1
  347. #endif
  348. class Temperature {
  349. public:
  350. #if HAS_HOTEND
  351. static hotend_info_t temp_hotend[HOTENDS];
  352. static const celsius_t hotend_maxtemp[HOTENDS];
  353. static celsius_t hotend_max_target(const uint8_t e) { return hotend_maxtemp[e] - (HOTEND_OVERSHOOT); }
  354. #endif
  355. #if HAS_HEATED_BED
  356. static bed_info_t temp_bed;
  357. #endif
  358. #if HAS_TEMP_PROBE
  359. static probe_info_t temp_probe;
  360. #endif
  361. #if HAS_TEMP_CHAMBER
  362. static chamber_info_t temp_chamber;
  363. #endif
  364. #if HAS_TEMP_COOLER
  365. static cooler_info_t temp_cooler;
  366. #endif
  367. #if HAS_TEMP_BOARD
  368. static board_info_t temp_board;
  369. #endif
  370. #if HAS_TEMP_REDUNDANT
  371. static redundant_info_t temp_redundant;
  372. #endif
  373. #if EITHER(AUTO_POWER_E_FANS, HAS_FANCHECK)
  374. static uint8_t autofan_speed[HOTENDS];
  375. #endif
  376. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  377. static uint8_t chamberfan_speed;
  378. #endif
  379. #if ENABLED(AUTO_POWER_COOLER_FAN)
  380. static uint8_t coolerfan_speed;
  381. #endif
  382. #if ENABLED(FAN_SOFT_PWM)
  383. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  384. soft_pwm_count_fan[FAN_COUNT];
  385. #endif
  386. #if BOTH(FAN_SOFT_PWM, USE_CONTROLLER_FAN)
  387. static uint8_t soft_pwm_controller_speed;
  388. #endif
  389. #if BOTH(HAS_MARLINUI_MENU, PREVENT_COLD_EXTRUSION) && E_MANUAL > 0
  390. static bool allow_cold_extrude_override;
  391. static void set_menu_cold_override(const bool allow) { allow_cold_extrude_override = allow; }
  392. #else
  393. static constexpr bool allow_cold_extrude_override = false;
  394. static void set_menu_cold_override(const bool) {}
  395. #endif
  396. #if ENABLED(PREVENT_COLD_EXTRUSION)
  397. static bool allow_cold_extrude;
  398. static celsius_t extrude_min_temp;
  399. static bool tooCold(const celsius_t temp) { return !allow_cold_extrude && !allow_cold_extrude_override && temp < extrude_min_temp - (TEMP_WINDOW); }
  400. static bool tooColdToExtrude(const uint8_t E_NAME) { return tooCold(wholeDegHotend(HOTEND_INDEX)); }
  401. static bool targetTooColdToExtrude(const uint8_t E_NAME) { return tooCold(degTargetHotend(HOTEND_INDEX)); }
  402. #else
  403. static bool tooColdToExtrude(const uint8_t) { return false; }
  404. static bool targetTooColdToExtrude(const uint8_t) { return false; }
  405. #endif
  406. static bool hotEnoughToExtrude(const uint8_t e) { return !tooColdToExtrude(e); }
  407. static bool targetHotEnoughToExtrude(const uint8_t e) { return !targetTooColdToExtrude(e); }
  408. #if EITHER(SINGLENOZZLE_STANDBY_TEMP, SINGLENOZZLE_STANDBY_FAN)
  409. #if ENABLED(SINGLENOZZLE_STANDBY_TEMP)
  410. static celsius_t singlenozzle_temp[EXTRUDERS];
  411. #endif
  412. #if ENABLED(SINGLENOZZLE_STANDBY_FAN)
  413. static uint8_t singlenozzle_fan_speed[EXTRUDERS];
  414. #endif
  415. static void singlenozzle_change(const uint8_t old_tool, const uint8_t new_tool);
  416. #endif
  417. #if HEATER_IDLE_HANDLER
  418. // Heater idle handling. Marlin creates one per hotend and one for the heated bed.
  419. typedef struct {
  420. millis_t timeout_ms;
  421. bool timed_out;
  422. inline void update(const millis_t &ms) { if (!timed_out && timeout_ms && ELAPSED(ms, timeout_ms)) timed_out = true; }
  423. inline void start(const millis_t &ms) { timeout_ms = millis() + ms; timed_out = false; }
  424. inline void reset() { timeout_ms = 0; timed_out = false; }
  425. inline void expire() { start(0); }
  426. } heater_idle_t;
  427. // Indices and size for the heater_idle array
  428. enum IdleIndex : int8_t {
  429. _II = -1
  430. #define _IDLE_INDEX_E(N) ,IDLE_INDEX_E##N
  431. REPEAT(HOTENDS, _IDLE_INDEX_E)
  432. #undef _IDLE_INDEX_E
  433. OPTARG(HAS_HEATED_BED, IDLE_INDEX_BED)
  434. , NR_HEATER_IDLE
  435. };
  436. // Convert the given heater_id_t to idle array index
  437. static IdleIndex idle_index_for_id(const int8_t heater_id) {
  438. TERN_(HAS_HEATED_BED, if (heater_id == H_BED) return IDLE_INDEX_BED);
  439. return (IdleIndex)_MAX(heater_id, 0);
  440. }
  441. static heater_idle_t heater_idle[NR_HEATER_IDLE];
  442. #endif // HEATER_IDLE_TIMER
  443. #if HAS_ADC_BUTTONS
  444. static uint32_t current_ADCKey_raw;
  445. static uint16_t ADCKey_count;
  446. #endif
  447. #if ENABLED(PID_EXTRUSION_SCALING)
  448. static int16_t lpq_len;
  449. #endif
  450. #if HAS_FAN_LOGIC
  451. static constexpr millis_t fan_update_interval_ms = TERN(HAS_PWMFANCHECK, 5000, TERN(HAS_FANCHECK, 1000, 2500));
  452. #endif
  453. private:
  454. #if ENABLED(WATCH_HOTENDS)
  455. static hotend_watch_t watch_hotend[HOTENDS];
  456. #endif
  457. #if ENABLED(PID_EXTRUSION_SCALING)
  458. static int32_t pes_e_position, lpq[LPQ_MAX_LEN];
  459. static lpq_ptr_t lpq_ptr;
  460. #endif
  461. #if ENABLED(MPCTEMP)
  462. static int32_t mpc_e_position;
  463. #endif
  464. #if HAS_HOTEND
  465. static temp_range_t temp_range[HOTENDS];
  466. #endif
  467. #if HAS_HEATED_BED
  468. #if ENABLED(WATCH_BED)
  469. static bed_watch_t watch_bed;
  470. #endif
  471. IF_DISABLED(PIDTEMPBED, static millis_t next_bed_check_ms);
  472. static raw_adc_t mintemp_raw_BED, maxtemp_raw_BED;
  473. #endif
  474. #if HAS_HEATED_CHAMBER
  475. #if ENABLED(WATCH_CHAMBER)
  476. static chamber_watch_t watch_chamber;
  477. #endif
  478. TERN(PIDTEMPCHAMBER,,static millis_t next_chamber_check_ms);
  479. static raw_adc_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
  480. #endif
  481. #if HAS_COOLER
  482. #if ENABLED(WATCH_COOLER)
  483. static cooler_watch_t watch_cooler;
  484. #endif
  485. static millis_t next_cooler_check_ms, cooler_fan_flush_ms;
  486. static raw_adc_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
  487. #endif
  488. #if HAS_TEMP_BOARD && ENABLED(THERMAL_PROTECTION_BOARD)
  489. static raw_adc_t mintemp_raw_BOARD, maxtemp_raw_BOARD;
  490. #endif
  491. #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
  492. static uint8_t consecutive_low_temperature_error[HOTENDS];
  493. #endif
  494. #if MILLISECONDS_PREHEAT_TIME > 0
  495. static millis_t preheat_end_time[HOTENDS];
  496. #endif
  497. #if HAS_FAN_LOGIC
  498. static millis_t fan_update_ms;
  499. static void manage_extruder_fans(millis_t ms) {
  500. if (ELAPSED(ms, fan_update_ms)) { // only need to check fan state very infrequently
  501. const millis_t next_ms = ms + fan_update_interval_ms;
  502. #if HAS_PWMFANCHECK
  503. #define FAN_CHECK_DURATION 100
  504. if (fan_check.is_measuring()) {
  505. fan_check.compute_speed(ms + FAN_CHECK_DURATION - fan_update_ms);
  506. fan_update_ms = next_ms;
  507. }
  508. else
  509. fan_update_ms = ms + FAN_CHECK_DURATION;
  510. fan_check.toggle_measuring();
  511. #else
  512. TERN_(HAS_FANCHECK, fan_check.compute_speed(next_ms - fan_update_ms));
  513. fan_update_ms = next_ms;
  514. #endif
  515. TERN_(HAS_AUTO_FAN, update_autofans()); // Needed as last when HAS_PWMFANCHECK to properly force fan speed
  516. }
  517. }
  518. #endif
  519. #if ENABLED(PROBING_HEATERS_OFF)
  520. static bool paused_for_probing;
  521. #endif
  522. public:
  523. /**
  524. * Instance Methods
  525. */
  526. void init();
  527. /**
  528. * Static (class) methods
  529. */
  530. #if HAS_USER_THERMISTORS
  531. static user_thermistor_t user_thermistor[USER_THERMISTORS];
  532. static void M305_report(const uint8_t t_index, const bool forReplay=true);
  533. static void reset_user_thermistors();
  534. static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const raw_adc_t raw);
  535. static bool set_pull_up_res(int8_t t_index, float value) {
  536. //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
  537. if (!WITHIN(value, 1, 1000000)) return false;
  538. user_thermistor[t_index].series_res = value;
  539. return true;
  540. }
  541. static bool set_res25(int8_t t_index, float value) {
  542. if (!WITHIN(value, 1, 10000000)) return false;
  543. user_thermistor[t_index].res_25 = value;
  544. user_thermistor[t_index].pre_calc = true;
  545. return true;
  546. }
  547. static bool set_beta(int8_t t_index, float value) {
  548. if (!WITHIN(value, 1, 1000000)) return false;
  549. user_thermistor[t_index].beta = value;
  550. user_thermistor[t_index].pre_calc = true;
  551. return true;
  552. }
  553. static bool set_sh_coeff(int8_t t_index, float value) {
  554. if (!WITHIN(value, -0.01f, 0.01f)) return false;
  555. user_thermistor[t_index].sh_c_coeff = value;
  556. user_thermistor[t_index].pre_calc = true;
  557. return true;
  558. }
  559. #endif
  560. #if HAS_HOTEND
  561. static celsius_float_t analog_to_celsius_hotend(const raw_adc_t raw, const uint8_t e);
  562. #endif
  563. #if HAS_HEATED_BED
  564. static celsius_float_t analog_to_celsius_bed(const raw_adc_t raw);
  565. #endif
  566. #if HAS_TEMP_CHAMBER
  567. static celsius_float_t analog_to_celsius_chamber(const raw_adc_t raw);
  568. #endif
  569. #if HAS_TEMP_PROBE
  570. static celsius_float_t analog_to_celsius_probe(const raw_adc_t raw);
  571. #endif
  572. #if HAS_TEMP_COOLER
  573. static celsius_float_t analog_to_celsius_cooler(const raw_adc_t raw);
  574. #endif
  575. #if HAS_TEMP_BOARD
  576. static celsius_float_t analog_to_celsius_board(const raw_adc_t raw);
  577. #endif
  578. #if HAS_TEMP_REDUNDANT
  579. static celsius_float_t analog_to_celsius_redundant(const raw_adc_t raw);
  580. #endif
  581. #if HAS_FAN
  582. static uint8_t fan_speed[FAN_COUNT];
  583. #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT)
  584. static void set_fan_speed(const uint8_t fan, const uint16_t speed);
  585. #if ENABLED(REPORT_FAN_CHANGE)
  586. static void report_fan_speed(const uint8_t fan);
  587. #endif
  588. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  589. static bool fans_paused;
  590. static uint8_t saved_fan_speed[FAN_COUNT];
  591. #endif
  592. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  593. static uint8_t fan_speed_scaler[FAN_COUNT];
  594. #endif
  595. static uint8_t scaledFanSpeed(const uint8_t fan, const uint8_t fs) {
  596. UNUSED(fan); // Potentially unused!
  597. return (fs * uint16_t(TERN(ADAPTIVE_FAN_SLOWING, fan_speed_scaler[fan], 128))) >> 7;
  598. }
  599. static uint8_t scaledFanSpeed(const uint8_t fan) {
  600. return scaledFanSpeed(fan, fan_speed[fan]);
  601. }
  602. static constexpr inline uint8_t pwmToPercent(const uint8_t speed) { return ui8_to_percent(speed); }
  603. static uint8_t fanSpeedPercent(const uint8_t fan) { return ui8_to_percent(fan_speed[fan]); }
  604. static uint8_t scaledFanSpeedPercent(const uint8_t fan) { return ui8_to_percent(scaledFanSpeed(fan)); }
  605. #if ENABLED(EXTRA_FAN_SPEED)
  606. typedef struct { uint8_t saved, speed; } extra_fan_t;
  607. static extra_fan_t extra_fan_speed[FAN_COUNT];
  608. static void set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed);
  609. #endif
  610. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  611. void set_fans_paused(const bool p);
  612. #endif
  613. #endif // HAS_FAN
  614. static void zero_fan_speeds() {
  615. #if HAS_FAN
  616. FANS_LOOP(i) set_fan_speed(i, 0);
  617. #endif
  618. }
  619. /**
  620. * Called from the Temperature ISR
  621. */
  622. static void isr();
  623. static void readings_ready();
  624. /**
  625. * Call periodically to manage heaters and keep the watchdog fed
  626. */
  627. static void task();
  628. /**
  629. * Preheating hotends
  630. */
  631. #if MILLISECONDS_PREHEAT_TIME > 0
  632. static bool is_preheating(const uint8_t E_NAME) {
  633. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  634. }
  635. static void start_preheat_time(const uint8_t E_NAME) {
  636. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  637. }
  638. static void reset_preheat_time(const uint8_t E_NAME) {
  639. preheat_end_time[HOTEND_INDEX] = 0;
  640. }
  641. #else
  642. #define is_preheating(n) (false)
  643. #endif
  644. //high level conversion routines, for use outside of temperature.cpp
  645. //inline so that there is no performance decrease.
  646. //deg=degreeCelsius
  647. static celsius_float_t degHotend(const uint8_t E_NAME) {
  648. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].celsius);
  649. }
  650. static celsius_t wholeDegHotend(const uint8_t E_NAME) {
  651. return TERN0(HAS_HOTEND, static_cast<celsius_t>(temp_hotend[HOTEND_INDEX].celsius + 0.5f));
  652. }
  653. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  654. static raw_adc_t rawHotendTemp(const uint8_t E_NAME) {
  655. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].getraw());
  656. }
  657. #endif
  658. static celsius_t degTargetHotend(const uint8_t E_NAME) {
  659. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].target);
  660. }
  661. #if HAS_HOTEND
  662. static void setTargetHotend(const celsius_t celsius, const uint8_t E_NAME) {
  663. const uint8_t ee = HOTEND_INDEX;
  664. #if MILLISECONDS_PREHEAT_TIME > 0
  665. if (celsius == 0)
  666. reset_preheat_time(ee);
  667. else if (temp_hotend[ee].target == 0)
  668. start_preheat_time(ee);
  669. #endif
  670. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  671. temp_hotend[ee].target = _MIN(celsius, hotend_max_target(ee));
  672. start_watching_hotend(ee);
  673. }
  674. static bool isHeatingHotend(const uint8_t E_NAME) {
  675. return temp_hotend[HOTEND_INDEX].target > temp_hotend[HOTEND_INDEX].celsius;
  676. }
  677. static bool isCoolingHotend(const uint8_t E_NAME) {
  678. return temp_hotend[HOTEND_INDEX].target < temp_hotend[HOTEND_INDEX].celsius;
  679. }
  680. #if HAS_TEMP_HOTEND
  681. static bool wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling=true
  682. OPTARG(G26_CLICK_CAN_CANCEL, const bool click_to_cancel=false)
  683. );
  684. #if ENABLED(WAIT_FOR_HOTEND)
  685. static void wait_for_hotend_heating(const uint8_t target_extruder);
  686. #endif
  687. #endif
  688. static bool still_heating(const uint8_t e) {
  689. return degTargetHotend(e) > TEMP_HYSTERESIS && ABS(wholeDegHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS;
  690. }
  691. static bool degHotendNear(const uint8_t e, const celsius_t temp) {
  692. return ABS(wholeDegHotend(e) - temp) < (TEMP_HYSTERESIS);
  693. }
  694. // Start watching a Hotend to make sure it's really heating up
  695. static void start_watching_hotend(const uint8_t E_NAME) {
  696. UNUSED(HOTEND_INDEX);
  697. #if WATCH_HOTENDS
  698. watch_hotend[HOTEND_INDEX].restart(degHotend(HOTEND_INDEX), degTargetHotend(HOTEND_INDEX));
  699. #endif
  700. }
  701. static void manage_hotends(const millis_t &ms);
  702. #endif // HAS_HOTEND
  703. #if HAS_HEATED_BED
  704. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  705. static raw_adc_t rawBedTemp() { return temp_bed.getraw(); }
  706. #endif
  707. static celsius_float_t degBed() { return temp_bed.celsius; }
  708. static celsius_t wholeDegBed() { return static_cast<celsius_t>(degBed() + 0.5f); }
  709. static celsius_t degTargetBed() { return temp_bed.target; }
  710. static bool isHeatingBed() { return temp_bed.target > temp_bed.celsius; }
  711. static bool isCoolingBed() { return temp_bed.target < temp_bed.celsius; }
  712. static bool degBedNear(const celsius_t temp) {
  713. return ABS(wholeDegBed() - temp) < (TEMP_BED_HYSTERESIS);
  714. }
  715. // Start watching the Bed to make sure it's really heating up
  716. static void start_watching_bed() { TERN_(WATCH_BED, watch_bed.restart(degBed(), degTargetBed())); }
  717. static void setTargetBed(const celsius_t celsius) {
  718. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  719. temp_bed.target = _MIN(celsius, BED_MAX_TARGET);
  720. start_watching_bed();
  721. }
  722. static bool wait_for_bed(const bool no_wait_for_cooling=true
  723. OPTARG(G26_CLICK_CAN_CANCEL, const bool click_to_cancel=false)
  724. );
  725. static void wait_for_bed_heating();
  726. static void manage_heated_bed(const millis_t &ms);
  727. #endif // HAS_HEATED_BED
  728. #if HAS_TEMP_PROBE
  729. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  730. static raw_adc_t rawProbeTemp() { return temp_probe.getraw(); }
  731. #endif
  732. static celsius_float_t degProbe() { return temp_probe.celsius; }
  733. static celsius_t wholeDegProbe() { return static_cast<celsius_t>(degProbe() + 0.5f); }
  734. static bool isProbeBelowTemp(const celsius_t target_temp) { return wholeDegProbe() < target_temp; }
  735. static bool isProbeAboveTemp(const celsius_t target_temp) { return wholeDegProbe() > target_temp; }
  736. static bool wait_for_probe(const celsius_t target_temp, bool no_wait_for_cooling=true);
  737. #endif
  738. #if HAS_TEMP_CHAMBER
  739. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  740. static raw_adc_t rawChamberTemp() { return temp_chamber.getraw(); }
  741. #endif
  742. static celsius_float_t degChamber() { return temp_chamber.celsius; }
  743. static celsius_t wholeDegChamber() { return static_cast<celsius_t>(degChamber() + 0.5f); }
  744. #if HAS_HEATED_CHAMBER
  745. static celsius_t degTargetChamber() { return temp_chamber.target; }
  746. static bool isHeatingChamber() { return temp_chamber.target > temp_chamber.celsius; }
  747. static bool isCoolingChamber() { return temp_chamber.target < temp_chamber.celsius; }
  748. static bool wait_for_chamber(const bool no_wait_for_cooling=true);
  749. static void manage_heated_chamber(const millis_t &ms);
  750. #endif
  751. #endif
  752. #if HAS_HEATED_CHAMBER
  753. static void setTargetChamber(const celsius_t celsius) {
  754. temp_chamber.target = _MIN(celsius, CHAMBER_MAX_TARGET);
  755. start_watching_chamber();
  756. }
  757. // Start watching the Chamber to make sure it's really heating up
  758. static void start_watching_chamber() { TERN_(WATCH_CHAMBER, watch_chamber.restart(degChamber(), degTargetChamber())); }
  759. #endif
  760. #if HAS_TEMP_COOLER
  761. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  762. static raw_adc_t rawCoolerTemp() { return temp_cooler.getraw(); }
  763. #endif
  764. static celsius_float_t degCooler() { return temp_cooler.celsius; }
  765. static celsius_t wholeDegCooler() { return static_cast<celsius_t>(temp_cooler.celsius + 0.5f); }
  766. #if HAS_COOLER
  767. static celsius_t degTargetCooler() { return temp_cooler.target; }
  768. static bool isLaserHeating() { return temp_cooler.target > temp_cooler.celsius; }
  769. static bool isLaserCooling() { return temp_cooler.target < temp_cooler.celsius; }
  770. static bool wait_for_cooler(const bool no_wait_for_cooling=true);
  771. static void manage_cooler(const millis_t &ms);
  772. #endif
  773. #endif
  774. #if HAS_TEMP_BOARD
  775. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  776. static raw_adc_t rawBoardTemp() { return temp_board.getraw(); }
  777. #endif
  778. static celsius_float_t degBoard() { return temp_board.celsius; }
  779. static celsius_t wholeDegBoard() { return static_cast<celsius_t>(temp_board.celsius + 0.5f); }
  780. #endif
  781. #if HAS_TEMP_REDUNDANT
  782. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  783. static raw_adc_t rawRedundantTemp() { return temp_redundant.getraw(); }
  784. #endif
  785. static celsius_float_t degRedundant() { return temp_redundant.celsius; }
  786. static celsius_float_t degRedundantTarget() { return (*temp_redundant.target).celsius; }
  787. static celsius_t wholeDegRedundant() { return static_cast<celsius_t>(temp_redundant.celsius + 0.5f); }
  788. static celsius_t wholeDegRedundantTarget() { return static_cast<celsius_t>((*temp_redundant.target).celsius + 0.5f); }
  789. #endif
  790. #if HAS_COOLER
  791. static void setTargetCooler(const celsius_t celsius) {
  792. temp_cooler.target = constrain(celsius, COOLER_MIN_TARGET, COOLER_MAX_TARGET);
  793. start_watching_cooler();
  794. }
  795. // Start watching the Cooler to make sure it's really cooling down
  796. static void start_watching_cooler() { TERN_(WATCH_COOLER, watch_cooler.restart(degCooler(), degTargetCooler())); }
  797. #endif
  798. /**
  799. * The software PWM power for a heater
  800. */
  801. static int16_t getHeaterPower(const heater_id_t heater_id);
  802. /**
  803. * Switch off all heaters, set all target temperatures to 0
  804. */
  805. static void disable_all_heaters();
  806. /**
  807. * Cooldown, as from the LCD. Disables all heaters and fans.
  808. */
  809. static void cooldown() {
  810. zero_fan_speeds();
  811. disable_all_heaters();
  812. }
  813. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  814. /**
  815. * Methods to check if heaters are enabled, indicating an active job
  816. */
  817. static bool auto_job_over_threshold();
  818. static void auto_job_check_timer(const bool can_start, const bool can_stop);
  819. #endif
  820. /**
  821. * Perform auto-tuning for hotend or bed in response to M303
  822. */
  823. #if HAS_PID_HEATING
  824. #if HAS_PID_DEBUG
  825. static bool pid_debug_flag;
  826. #endif
  827. static void PID_autotune(const celsius_t target, const heater_id_t heater_id, const int8_t ncycles, const bool set_result=false);
  828. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  829. static bool adaptive_fan_slowing;
  830. #elif ENABLED(ADAPTIVE_FAN_SLOWING)
  831. static constexpr bool adaptive_fan_slowing = true;
  832. #endif
  833. /**
  834. * Update the temp manager when PID values change
  835. */
  836. #if ENABLED(PIDTEMP)
  837. static void updatePID() {
  838. TERN_(PID_EXTRUSION_SCALING, pes_e_position = 0);
  839. }
  840. #endif
  841. #endif
  842. #if ENABLED(MPCTEMP)
  843. void MPC_autotune();
  844. #endif
  845. #if ENABLED(PROBING_HEATERS_OFF)
  846. static void pause_heaters(const bool p);
  847. #endif
  848. #if HEATER_IDLE_HANDLER
  849. static void reset_hotend_idle_timer(const uint8_t E_NAME) {
  850. heater_idle[HOTEND_INDEX].reset();
  851. start_watching_hotend(HOTEND_INDEX);
  852. }
  853. #if HAS_HEATED_BED
  854. static void reset_bed_idle_timer() {
  855. heater_idle[IDLE_INDEX_BED].reset();
  856. start_watching_bed();
  857. }
  858. #endif
  859. #endif // HEATER_IDLE_HANDLER
  860. #if HAS_TEMP_SENSOR
  861. static void print_heater_states(const int8_t target_extruder
  862. OPTARG(HAS_TEMP_REDUNDANT, const bool include_r=false)
  863. );
  864. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  865. struct AutoReportTemp { static void report(); };
  866. static AutoReporter<AutoReportTemp> auto_reporter;
  867. #endif
  868. #endif
  869. #if HAS_HOTEND && HAS_STATUS_MESSAGE
  870. static void set_heating_message(const uint8_t e, const bool isM104=false);
  871. #else
  872. static void set_heating_message(const uint8_t, const bool=false) {}
  873. #endif
  874. #if HAS_MARLINUI_MENU && HAS_TEMPERATURE && HAS_PREHEAT
  875. static void lcd_preheat(const uint8_t e, const int8_t indh, const int8_t indb);
  876. #endif
  877. private:
  878. // Reading raw temperatures and converting to Celsius when ready
  879. static volatile bool raw_temps_ready;
  880. static void update_raw_temperatures();
  881. static void updateTemperaturesFromRawValues();
  882. static bool updateTemperaturesIfReady() {
  883. if (!raw_temps_ready) return false;
  884. updateTemperaturesFromRawValues();
  885. raw_temps_ready = false;
  886. return true;
  887. }
  888. // MAX Thermocouples
  889. #if HAS_MAX_TC
  890. #define MAX_TC_COUNT TEMP_SENSOR_IS_MAX_TC(0) + TEMP_SENSOR_IS_MAX_TC(1) + TEMP_SENSOR_IS_MAX_TC(REDUNDANT)
  891. #if MAX_TC_COUNT > 1
  892. #define HAS_MULTI_MAX_TC 1
  893. #define READ_MAX_TC(N) read_max_tc(N)
  894. #else
  895. #define READ_MAX_TC(N) read_max_tc()
  896. #endif
  897. static raw_adc_t read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
  898. #endif
  899. #if HAS_AUTO_FAN
  900. #if ENABLED(POWER_OFF_WAIT_FOR_COOLDOWN)
  901. static bool autofans_on;
  902. #endif
  903. static void update_autofans();
  904. #endif
  905. #if HAS_HOTEND
  906. static float get_pid_output_hotend(const uint8_t e);
  907. #endif
  908. #if ENABLED(PIDTEMPBED)
  909. static float get_pid_output_bed();
  910. #endif
  911. #if ENABLED(PIDTEMPCHAMBER)
  912. static float get_pid_output_chamber();
  913. #endif
  914. static void _temp_error(const heater_id_t e, FSTR_P const serial_msg, FSTR_P const lcd_msg);
  915. static void min_temp_error(const heater_id_t e);
  916. static void max_temp_error(const heater_id_t e);
  917. #define HAS_THERMAL_PROTECTION ANY(THERMAL_PROTECTION_HOTENDS, THERMAL_PROTECTION_CHAMBER, THERMAL_PROTECTION_BED, THERMAL_PROTECTION_COOLER)
  918. #if HAS_THERMAL_PROTECTION
  919. // Indices and size for the tr_state_machine array. One for each protected heater.
  920. enum RunawayIndex : int8_t {
  921. _RI = -1
  922. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  923. #define _RUNAWAY_IND_E(N) ,RUNAWAY_IND_E##N
  924. REPEAT(HOTENDS, _RUNAWAY_IND_E)
  925. #undef _RUNAWAY_IND_E
  926. #endif
  927. OPTARG(THERMAL_PROTECTION_BED, RUNAWAY_IND_BED)
  928. OPTARG(THERMAL_PROTECTION_CHAMBER, RUNAWAY_IND_CHAMBER)
  929. OPTARG(THERMAL_PROTECTION_COOLER, RUNAWAY_IND_COOLER)
  930. , NR_HEATER_RUNAWAY
  931. };
  932. // Convert the given heater_id_t to runaway state array index
  933. static RunawayIndex runaway_index_for_id(const int8_t heater_id) {
  934. TERN_(THERMAL_PROTECTION_CHAMBER, if (heater_id == H_CHAMBER) return RUNAWAY_IND_CHAMBER);
  935. TERN_(THERMAL_PROTECTION_COOLER, if (heater_id == H_COOLER) return RUNAWAY_IND_COOLER);
  936. TERN_(THERMAL_PROTECTION_BED, if (heater_id == H_BED) return RUNAWAY_IND_BED);
  937. return (RunawayIndex)_MAX(heater_id, 0);
  938. }
  939. enum TRState : char { TRInactive, TRFirstHeating, TRStable, TRRunaway
  940. OPTARG(THERMAL_PROTECTION_VARIANCE_MONITOR, TRMalfunction)
  941. };
  942. typedef struct {
  943. millis_t timer = 0;
  944. TRState state = TRInactive;
  945. float running_temp;
  946. #if ENABLED(THERMAL_PROTECTION_VARIANCE_MONITOR)
  947. millis_t variance_timer = 0;
  948. celsius_float_t last_temp = 0.0, variance = 0.0;
  949. #endif
  950. void run(const_celsius_float_t current, const_celsius_float_t target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_t hysteresis_degc);
  951. } tr_state_machine_t;
  952. static tr_state_machine_t tr_state_machine[NR_HEATER_RUNAWAY];
  953. #endif // HAS_THERMAL_PROTECTION
  954. };
  955. extern Temperature thermalManager;