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

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