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

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