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

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