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

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