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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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_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 void restart(const celsius_t curr, const celsius_t tgt) {
  213. if (tgt) {
  214. const celsius_t newtarget = curr + INCREASE;
  215. if (newtarget < tgt - HYSTERESIS - 1) {
  216. target = newtarget;
  217. next_ms = millis() + SEC_TO_MS(PERIOD);
  218. return;
  219. }
  220. }
  221. next_ms = 0;
  222. }
  223. };
  224. #if WATCH_HOTENDS
  225. typedef struct HeaterWatch<WATCH_TEMP_INCREASE, TEMP_HYSTERESIS, WATCH_TEMP_PERIOD> hotend_watch_t;
  226. #endif
  227. #if WATCH_BED
  228. typedef struct HeaterWatch<WATCH_BED_TEMP_INCREASE, TEMP_BED_HYSTERESIS, WATCH_BED_TEMP_PERIOD> bed_watch_t;
  229. #endif
  230. #if WATCH_CHAMBER
  231. typedef struct HeaterWatch<WATCH_CHAMBER_TEMP_INCREASE, TEMP_CHAMBER_HYSTERESIS, WATCH_CHAMBER_TEMP_PERIOD> chamber_watch_t;
  232. #endif
  233. #if WATCH_COOLER
  234. typedef struct HeaterWatch<WATCH_COOLER_TEMP_INCREASE, TEMP_COOLER_HYSTERESIS, WATCH_COOLER_TEMP_PERIOD> cooler_watch_t;
  235. #endif
  236. // Temperature sensor read value ranges
  237. typedef struct { int16_t raw_min, raw_max; } raw_range_t;
  238. typedef struct { celsius_t mintemp, maxtemp; } celsius_range_t;
  239. typedef struct { int16_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
  240. #define THERMISTOR_ABS_ZERO_C -273.15f // bbbbrrrrr cold !
  241. #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f // mmmmm comfortable
  242. #if HAS_USER_THERMISTORS
  243. enum CustomThermistorIndex : uint8_t {
  244. #if TEMP_SENSOR_0_IS_CUSTOM
  245. CTI_HOTEND_0,
  246. #endif
  247. #if TEMP_SENSOR_1_IS_CUSTOM
  248. CTI_HOTEND_1,
  249. #endif
  250. #if TEMP_SENSOR_2_IS_CUSTOM
  251. CTI_HOTEND_2,
  252. #endif
  253. #if TEMP_SENSOR_3_IS_CUSTOM
  254. CTI_HOTEND_3,
  255. #endif
  256. #if TEMP_SENSOR_4_IS_CUSTOM
  257. CTI_HOTEND_4,
  258. #endif
  259. #if TEMP_SENSOR_5_IS_CUSTOM
  260. CTI_HOTEND_5,
  261. #endif
  262. #if TEMP_SENSOR_BED_IS_CUSTOM
  263. CTI_BED,
  264. #endif
  265. #if TEMP_SENSOR_PROBE_IS_CUSTOM
  266. CTI_PROBE,
  267. #endif
  268. #if TEMP_SENSOR_CHAMBER_IS_CUSTOM
  269. CTI_CHAMBER,
  270. #endif
  271. #if COOLER_USER_THERMISTOR
  272. CTI_COOLER,
  273. #endif
  274. USER_THERMISTORS
  275. };
  276. // User-defined thermistor
  277. typedef struct {
  278. bool pre_calc; // true if pre-calculations update needed
  279. float sh_c_coeff, // Steinhart-Hart C coefficient .. defaults to '0.0'
  280. sh_alpha,
  281. series_res,
  282. res_25, res_25_recip,
  283. res_25_log,
  284. beta, beta_recip;
  285. } user_thermistor_t;
  286. #endif
  287. class Temperature {
  288. public:
  289. #if HAS_HOTEND
  290. #define HOTEND_TEMPS (HOTENDS + ENABLED(TEMP_SENSOR_1_AS_REDUNDANT))
  291. static hotend_info_t temp_hotend[HOTEND_TEMPS];
  292. static const celsius_t hotend_maxtemp[HOTENDS];
  293. FORCE_INLINE static celsius_t hotend_max_target(const uint8_t e) { return hotend_maxtemp[e] - (HOTEND_OVERSHOOT); }
  294. #endif
  295. TERN_(HAS_HEATED_BED, static bed_info_t temp_bed);
  296. TERN_(HAS_TEMP_PROBE, static probe_info_t temp_probe);
  297. TERN_(HAS_TEMP_CHAMBER, static chamber_info_t temp_chamber);
  298. TERN_(HAS_TEMP_COOLER, static cooler_info_t temp_cooler);
  299. TERN_(AUTO_POWER_E_FANS, static uint8_t autofan_speed[HOTENDS]);
  300. TERN_(AUTO_POWER_CHAMBER_FAN, static uint8_t chamberfan_speed);
  301. TERN_(AUTO_POWER_COOLER_FAN, static uint8_t coolerfan_speed);
  302. #if ENABLED(FAN_SOFT_PWM)
  303. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  304. soft_pwm_count_fan[FAN_COUNT];
  305. #endif
  306. #if ENABLED(PREVENT_COLD_EXTRUSION)
  307. static bool allow_cold_extrude;
  308. static celsius_t extrude_min_temp;
  309. FORCE_INLINE static bool tooCold(const celsius_t temp) { return allow_cold_extrude ? false : temp < extrude_min_temp - (TEMP_WINDOW); }
  310. FORCE_INLINE static bool tooColdToExtrude(const uint8_t E_NAME) {
  311. return tooCold(degHotend(HOTEND_INDEX));
  312. }
  313. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t E_NAME) {
  314. return tooCold(degTargetHotend(HOTEND_INDEX));
  315. }
  316. #else
  317. FORCE_INLINE static bool tooColdToExtrude(const uint8_t) { return false; }
  318. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t) { return false; }
  319. #endif
  320. FORCE_INLINE static bool hotEnoughToExtrude(const uint8_t e) { return !tooColdToExtrude(e); }
  321. FORCE_INLINE static bool targetHotEnoughToExtrude(const uint8_t e) { return !targetTooColdToExtrude(e); }
  322. #if ENABLED(SINGLENOZZLE_STANDBY_FAN)
  323. static celsius_t singlenozzle_temp[EXTRUDERS];
  324. #if HAS_FAN
  325. static uint8_t singlenozzle_fan_speed[EXTRUDERS];
  326. #endif
  327. static void singlenozzle_change(const uint8_t old_tool, const uint8_t new_tool);
  328. #endif
  329. #if HEATER_IDLE_HANDLER
  330. // Heater idle handling. Marlin creates one per hotend and one for the heated bed.
  331. typedef struct {
  332. millis_t timeout_ms;
  333. bool timed_out;
  334. inline void update(const millis_t &ms) { if (!timed_out && timeout_ms && ELAPSED(ms, timeout_ms)) timed_out = true; }
  335. inline void start(const millis_t &ms) { timeout_ms = millis() + ms; timed_out = false; }
  336. inline void reset() { timeout_ms = 0; timed_out = false; }
  337. inline void expire() { start(0); }
  338. } heater_idle_t;
  339. // Indices and size for the heater_idle array
  340. #define _ENUM_FOR_E(N) IDLE_INDEX_E##N,
  341. enum IdleIndex : uint8_t {
  342. REPEAT(HOTENDS, _ENUM_FOR_E)
  343. #if ENABLED(HAS_HEATED_BED)
  344. IDLE_INDEX_BED,
  345. #endif
  346. NR_HEATER_IDLE
  347. };
  348. #undef _ENUM_FOR_E
  349. // Convert the given heater_id_t to idle array index
  350. static inline IdleIndex idle_index_for_id(const int8_t heater_id) {
  351. #if HAS_HEATED_BED
  352. if (heater_id == H_BED) return IDLE_INDEX_BED;
  353. #endif
  354. return (IdleIndex)_MAX(heater_id, 0);
  355. }
  356. static heater_idle_t heater_idle[NR_HEATER_IDLE];
  357. #endif
  358. private:
  359. TERN_(EARLY_WATCHDOG, static bool inited); // If temperature controller is running
  360. static volatile bool raw_temps_ready;
  361. TERN_(WATCH_HOTENDS, static hotend_watch_t watch_hotend[HOTENDS]);
  362. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  363. static uint16_t redundant_temperature_raw;
  364. static celsius_t redundant_temperature;
  365. #endif
  366. #if ENABLED(PID_EXTRUSION_SCALING)
  367. static int32_t last_e_position, lpq[LPQ_MAX_LEN];
  368. static lpq_ptr_t lpq_ptr;
  369. #endif
  370. TERN_(HAS_HOTEND, static temp_range_t temp_range[HOTENDS]);
  371. #if HAS_HEATED_BED
  372. TERN_(WATCH_BED, static bed_watch_t watch_bed);
  373. IF_DISABLED(PIDTEMPBED, static millis_t next_bed_check_ms);
  374. static int16_t mintemp_raw_BED, maxtemp_raw_BED;
  375. #endif
  376. #if HAS_HEATED_CHAMBER
  377. TERN_(WATCH_CHAMBER, static chamber_watch_t watch_chamber);
  378. TERN(PIDTEMPCHAMBER,,static millis_t next_chamber_check_ms);
  379. static int16_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
  380. #endif
  381. #if HAS_COOLER
  382. TERN_(WATCH_COOLER, static cooler_watch_t watch_cooler);
  383. static millis_t next_cooler_check_ms, cooler_fan_flush_ms;
  384. static int16_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
  385. #endif
  386. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  387. static uint8_t consecutive_low_temperature_error[HOTENDS];
  388. #endif
  389. #ifdef MILLISECONDS_PREHEAT_TIME
  390. static millis_t preheat_end_time[HOTENDS];
  391. #endif
  392. TERN_(HAS_AUTO_FAN, static millis_t next_auto_fan_check_ms);
  393. TERN_(PROBING_HEATERS_OFF, static bool paused);
  394. public:
  395. #if HAS_ADC_BUTTONS
  396. static uint32_t current_ADCKey_raw;
  397. static uint16_t ADCKey_count;
  398. #endif
  399. TERN_(PID_EXTRUSION_SCALING, static int16_t lpq_len);
  400. /**
  401. * Instance Methods
  402. */
  403. void init();
  404. /**
  405. * Static (class) methods
  406. */
  407. #if HAS_USER_THERMISTORS
  408. static user_thermistor_t user_thermistor[USER_THERMISTORS];
  409. static void log_user_thermistor(const uint8_t t_index, const bool eprom=false);
  410. static void reset_user_thermistors();
  411. static celsius_t user_thermistor_to_deg_c(const uint8_t t_index, const int raw);
  412. static bool set_pull_up_res(int8_t t_index, float value) {
  413. //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
  414. if (!WITHIN(value, 1, 1000000)) return false;
  415. user_thermistor[t_index].series_res = value;
  416. return true;
  417. }
  418. static bool set_res25(int8_t t_index, float value) {
  419. if (!WITHIN(value, 1, 10000000)) return false;
  420. user_thermistor[t_index].res_25 = value;
  421. user_thermistor[t_index].pre_calc = true;
  422. return true;
  423. }
  424. static bool set_beta(int8_t t_index, float value) {
  425. if (!WITHIN(value, 1, 1000000)) return false;
  426. user_thermistor[t_index].beta = value;
  427. user_thermistor[t_index].pre_calc = true;
  428. return true;
  429. }
  430. static bool set_sh_coeff(int8_t t_index, float value) {
  431. if (!WITHIN(value, -0.01f, 0.01f)) return false;
  432. user_thermistor[t_index].sh_c_coeff = value;
  433. user_thermistor[t_index].pre_calc = true;
  434. return true;
  435. }
  436. #endif
  437. #if HAS_HOTEND
  438. static celsius_t analog_to_celsius_hotend(const int raw, const uint8_t e);
  439. #endif
  440. #if HAS_HEATED_BED
  441. static celsius_t analog_to_celsius_bed(const int raw);
  442. #endif
  443. #if HAS_TEMP_PROBE
  444. static celsius_t analog_to_celsius_probe(const int raw);
  445. #endif
  446. #if HAS_TEMP_CHAMBER
  447. static celsius_t analog_to_celsius_chamber(const int raw);
  448. #endif
  449. #if HAS_TEMP_COOLER
  450. static celsius_t analog_to_celsius_cooler(const int raw);
  451. #endif
  452. #if HAS_FAN
  453. static uint8_t fan_speed[FAN_COUNT];
  454. #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT)
  455. static void set_fan_speed(const uint8_t target, const uint16_t speed);
  456. #if ENABLED(REPORT_FAN_CHANGE)
  457. static void report_fan_speed(const uint8_t target);
  458. #endif
  459. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  460. static bool fans_paused;
  461. static uint8_t saved_fan_speed[FAN_COUNT];
  462. #endif
  463. static constexpr inline uint8_t fanPercent(const uint8_t speed) { return ui8_to_percent(speed); }
  464. TERN_(ADAPTIVE_FAN_SLOWING, static uint8_t fan_speed_scaler[FAN_COUNT]);
  465. static inline uint8_t scaledFanSpeed(const uint8_t target, const uint8_t fs) {
  466. UNUSED(target); // Potentially unused!
  467. return (fs * uint16_t(TERN(ADAPTIVE_FAN_SLOWING, fan_speed_scaler[target], 128))) >> 7;
  468. }
  469. static inline uint8_t scaledFanSpeed(const uint8_t target) {
  470. return scaledFanSpeed(target, fan_speed[target]);
  471. }
  472. #if ENABLED(EXTRA_FAN_SPEED)
  473. typedef struct { uint8_t saved, speed; } extra_fan_t;
  474. static extra_fan_t extra_fan_speed[FAN_COUNT];
  475. static void set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed);
  476. #endif
  477. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  478. void set_fans_paused(const bool p);
  479. #endif
  480. #endif // HAS_FAN
  481. static inline void zero_fan_speeds() {
  482. #if HAS_FAN
  483. FANS_LOOP(i) set_fan_speed(i, 0);
  484. #endif
  485. }
  486. /**
  487. * Called from the Temperature ISR
  488. */
  489. static void readings_ready();
  490. static void tick();
  491. /**
  492. * Call periodically to manage heaters
  493. */
  494. static void manage_heater() _O2; // Added _O2 to work around a compiler error
  495. /**
  496. * Preheating hotends
  497. */
  498. #ifdef MILLISECONDS_PREHEAT_TIME
  499. static bool is_preheating(const uint8_t E_NAME) {
  500. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  501. }
  502. static void start_preheat_time(const uint8_t E_NAME) {
  503. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  504. }
  505. static void reset_preheat_time(const uint8_t E_NAME) {
  506. preheat_end_time[HOTEND_INDEX] = 0;
  507. }
  508. #else
  509. #define is_preheating(n) (false)
  510. #endif
  511. //high level conversion routines, for use outside of temperature.cpp
  512. //inline so that there is no performance decrease.
  513. //deg=degreeCelsius
  514. FORCE_INLINE static float degHotend(const uint8_t E_NAME) {
  515. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].celsius);
  516. }
  517. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  518. FORCE_INLINE static int16_t rawHotendTemp(const uint8_t E_NAME) {
  519. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].raw);
  520. }
  521. #endif
  522. FORCE_INLINE static celsius_t degTargetHotend(const uint8_t E_NAME) {
  523. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].target);
  524. }
  525. #if WATCH_HOTENDS
  526. static void start_watching_hotend(const uint8_t e=0);
  527. #else
  528. static inline void start_watching_hotend(const uint8_t=0) {}
  529. #endif
  530. #if HAS_HOTEND
  531. static void setTargetHotend(const celsius_t celsius, const uint8_t E_NAME) {
  532. const uint8_t ee = HOTEND_INDEX;
  533. #ifdef MILLISECONDS_PREHEAT_TIME
  534. if (celsius == 0)
  535. reset_preheat_time(ee);
  536. else if (temp_hotend[ee].target == 0)
  537. start_preheat_time(ee);
  538. #endif
  539. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  540. temp_hotend[ee].target = _MIN(celsius, hotend_max_target(ee));
  541. start_watching_hotend(ee);
  542. }
  543. FORCE_INLINE static bool isHeatingHotend(const uint8_t E_NAME) {
  544. return temp_hotend[HOTEND_INDEX].target > temp_hotend[HOTEND_INDEX].celsius;
  545. }
  546. FORCE_INLINE static bool isCoolingHotend(const uint8_t E_NAME) {
  547. return temp_hotend[HOTEND_INDEX].target < temp_hotend[HOTEND_INDEX].celsius;
  548. }
  549. #if HAS_TEMP_HOTEND
  550. static bool wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling=true
  551. #if G26_CLICK_CAN_CANCEL
  552. , const bool click_to_cancel=false
  553. #endif
  554. );
  555. #if ENABLED(WAIT_FOR_HOTEND)
  556. static void wait_for_hotend_heating(const uint8_t target_extruder);
  557. #endif
  558. #endif
  559. FORCE_INLINE static bool still_heating(const uint8_t e) {
  560. return degTargetHotend(e) > TEMP_HYSTERESIS && ABS(degHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS;
  561. }
  562. FORCE_INLINE static bool degHotendNear(const uint8_t e, const float &temp) {
  563. return ABS(degHotend(e) - temp) < (TEMP_HYSTERESIS);
  564. }
  565. #endif // HAS_HOTEND
  566. #if HAS_HEATED_BED
  567. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  568. FORCE_INLINE static int16_t rawBedTemp() { return temp_bed.raw; }
  569. #endif
  570. FORCE_INLINE static celsius_t degBed() { return temp_bed.celsius; }
  571. FORCE_INLINE static celsius_t degTargetBed() { return temp_bed.target; }
  572. FORCE_INLINE static bool isHeatingBed() { return temp_bed.target > temp_bed.celsius; }
  573. FORCE_INLINE static bool isCoolingBed() { return temp_bed.target < temp_bed.celsius; }
  574. #if WATCH_BED
  575. static void start_watching_bed();
  576. #else
  577. static inline void start_watching_bed() {}
  578. #endif
  579. static void setTargetBed(const celsius_t celsius) {
  580. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  581. temp_bed.target = _MIN(celsius, BED_MAX_TARGET);
  582. start_watching_bed();
  583. }
  584. static bool wait_for_bed(const bool no_wait_for_cooling=true
  585. #if G26_CLICK_CAN_CANCEL
  586. , const bool click_to_cancel=false
  587. #endif
  588. );
  589. static void wait_for_bed_heating();
  590. FORCE_INLINE static bool degBedNear(const float &temp) {
  591. return ABS(degBed() - temp) < (TEMP_BED_HYSTERESIS);
  592. }
  593. #endif // HAS_HEATED_BED
  594. #if HAS_TEMP_PROBE
  595. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  596. FORCE_INLINE static int16_t rawProbeTemp() { return temp_probe.raw; }
  597. #endif
  598. FORCE_INLINE static float degProbe() { return temp_probe.celsius; }
  599. FORCE_INLINE static bool isProbeBelowTemp(const float target_temp) { return temp_probe.celsius < target_temp; }
  600. FORCE_INLINE static bool isProbeAboveTemp(const float target_temp) { return temp_probe.celsius > target_temp; }
  601. static bool wait_for_probe(const float target_temp, bool no_wait_for_cooling=true);
  602. #endif
  603. #if WATCH_PROBE
  604. static void start_watching_probe();
  605. #else
  606. static inline void start_watching_probe() {}
  607. #endif
  608. #if HAS_TEMP_CHAMBER
  609. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  610. FORCE_INLINE static int16_t rawChamberTemp() { return temp_chamber.raw; }
  611. #endif
  612. FORCE_INLINE static float degChamber() { return temp_chamber.celsius; }
  613. #if HAS_HEATED_CHAMBER
  614. FORCE_INLINE static celsius_t degTargetChamber() { return temp_chamber.target; }
  615. FORCE_INLINE static bool isHeatingChamber() { return temp_chamber.target > temp_chamber.celsius; }
  616. FORCE_INLINE static bool isCoolingChamber() { return temp_chamber.target < temp_chamber.celsius; }
  617. static bool wait_for_chamber(const bool no_wait_for_cooling=true);
  618. #endif
  619. #endif
  620. #if WATCH_CHAMBER
  621. static void start_watching_chamber();
  622. #else
  623. static inline void start_watching_chamber() {}
  624. #endif
  625. #if HAS_HEATED_CHAMBER
  626. static void setTargetChamber(const celsius_t celsius) {
  627. temp_chamber.target = _MIN(celsius, CHAMBER_MAX_TARGET);
  628. start_watching_chamber();
  629. }
  630. #endif
  631. #if HAS_TEMP_COOLER
  632. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  633. FORCE_INLINE static int16_t rawCoolerTemp() { return temp_cooler.raw; }
  634. #endif
  635. FORCE_INLINE static float degCooler() { return temp_cooler.celsius; }
  636. #if HAS_COOLER
  637. FORCE_INLINE static celsius_t degTargetCooler() { return temp_cooler.target; }
  638. FORCE_INLINE static bool isLaserHeating() { return temp_cooler.target > temp_cooler.celsius; }
  639. FORCE_INLINE static bool isLaserCooling() { return temp_cooler.target < temp_cooler.celsius; }
  640. static bool wait_for_cooler(const bool no_wait_for_cooling=true);
  641. #endif
  642. #endif
  643. #if WATCH_COOLER
  644. static void start_watching_cooler();
  645. #else
  646. static inline void start_watching_cooler() {}
  647. #endif
  648. #if HAS_COOLER
  649. static void setTargetCooler(const celsius_t celsius) {
  650. temp_cooler.target = constrain(celsius, COOLER_MIN_TARGET, COOLER_MAX_TARGET);
  651. start_watching_cooler();
  652. }
  653. #endif
  654. /**
  655. * The software PWM power for a heater
  656. */
  657. static int16_t getHeaterPower(const heater_id_t heater_id);
  658. /**
  659. * Switch off all heaters, set all target temperatures to 0
  660. */
  661. static void disable_all_heaters();
  662. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  663. /**
  664. * Methods to check if heaters are enabled, indicating an active job
  665. */
  666. static bool auto_job_over_threshold();
  667. static void auto_job_check_timer(const bool can_start, const bool can_stop);
  668. #endif
  669. /**
  670. * Perform auto-tuning for hotend or bed in response to M303
  671. */
  672. #if HAS_PID_HEATING
  673. #if ANY(PID_DEBUG, PID_BED_DEBUG, PID_CHAMBER_DEBUG)
  674. static bool pid_debug_flag;
  675. #endif
  676. static void PID_autotune(const float &target, const heater_id_t heater_id, const int8_t ncycles, const bool set_result=false);
  677. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  678. static bool adaptive_fan_slowing;
  679. #elif ENABLED(ADAPTIVE_FAN_SLOWING)
  680. static constexpr bool adaptive_fan_slowing = true;
  681. #endif
  682. /**
  683. * Update the temp manager when PID values change
  684. */
  685. #if ENABLED(PIDTEMP)
  686. FORCE_INLINE static void updatePID() {
  687. TERN_(PID_EXTRUSION_SCALING, last_e_position = 0);
  688. }
  689. #endif
  690. #endif
  691. #if ENABLED(PROBING_HEATERS_OFF)
  692. static void pause(const bool p);
  693. FORCE_INLINE static bool is_paused() { return paused; }
  694. #endif
  695. #if HEATER_IDLE_HANDLER
  696. static void reset_hotend_idle_timer(const uint8_t E_NAME) {
  697. heater_idle[HOTEND_INDEX].reset();
  698. start_watching_hotend(HOTEND_INDEX);
  699. }
  700. #if HAS_HEATED_BED
  701. static void reset_bed_idle_timer() {
  702. heater_idle[IDLE_INDEX_BED].reset();
  703. start_watching_bed();
  704. }
  705. #endif
  706. #endif // HEATER_IDLE_HANDLER
  707. #if HAS_TEMP_SENSOR
  708. static void print_heater_states(const uint8_t target_extruder
  709. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  710. , const bool include_r=false
  711. #endif
  712. );
  713. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  714. struct AutoReportTemp { static void report(); };
  715. static AutoReporter<AutoReportTemp> auto_reporter;
  716. #endif
  717. #endif
  718. TERN_(HAS_DISPLAY, static void set_heating_message(const uint8_t e));
  719. #if HAS_LCD_MENU && HAS_TEMPERATURE
  720. static void lcd_preheat(const uint8_t e, const int8_t indh, const int8_t indb);
  721. #endif
  722. private:
  723. static void update_raw_temperatures();
  724. static void updateTemperaturesFromRawValues();
  725. #if HAS_MAX_TC
  726. #define MAX_TC_COUNT 1 + BOTH(TEMP_SENSOR_0_IS_MAX_TC, TEMP_SENSOR_1_IS_MAX_TC)
  727. #if MAX_TC_COUNT > 1
  728. #define HAS_MULTI_MAX_TC 1
  729. #define READ_MAX_TC(N) read_max_tc(N)
  730. #else
  731. #define READ_MAX_TC(N) read_max_tc()
  732. #endif
  733. static int read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
  734. #endif
  735. static void checkExtruderAutoFans();
  736. TERN_(HAS_HOTEND, static float get_pid_output_hotend(const uint8_t e));
  737. TERN_(PIDTEMPBED, static float get_pid_output_bed());
  738. TERN_(PIDTEMPCHAMBER, static float get_pid_output_chamber());
  739. static void _temp_error(const heater_id_t e, PGM_P const serial_msg, PGM_P const lcd_msg);
  740. static void min_temp_error(const heater_id_t e);
  741. static void max_temp_error(const heater_id_t e);
  742. #define HAS_THERMAL_PROTECTION ANY(THERMAL_PROTECTION_HOTENDS, THERMAL_PROTECTION_CHAMBER, HAS_THERMALLY_PROTECTED_BED, THERMAL_PROTECTION_COOLER)
  743. #if HAS_THERMAL_PROTECTION
  744. // Indices and size for the tr_state_machine array. One for each protected heater.
  745. #define _ENUM_FOR_E(N) RUNAWAY_IND_E##N,
  746. enum RunawayIndex : uint8_t {
  747. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  748. REPEAT(HOTENDS, _ENUM_FOR_E)
  749. #endif
  750. #if ENABLED(HAS_THERMALLY_PROTECTED_BED)
  751. RUNAWAY_IND_BED,
  752. #endif
  753. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  754. RUNAWAY_IND_CHAMBER,
  755. #endif
  756. #if ENABLED(THERMAL_PROTECTION_COOLER)
  757. RUNAWAY_IND_COOLER,
  758. #endif
  759. NR_HEATER_RUNAWAY
  760. };
  761. #undef _ENUM_FOR_E
  762. // Convert the given heater_id_t to runaway state array index
  763. static inline RunawayIndex runaway_index_for_id(const int8_t heater_id) {
  764. #if HAS_THERMALLY_PROTECTED_CHAMBER
  765. if (heater_id == H_CHAMBER) return RUNAWAY_IND_CHAMBER;
  766. #endif
  767. #if HAS_THERMALLY_PROTECTED_CHAMBER
  768. if (heater_id == H_COOLER) return RUNAWAY_IND_COOLER;
  769. #endif
  770. #if HAS_THERMALLY_PROTECTED_BED
  771. if (heater_id == H_BED) return RUNAWAY_IND_BED;
  772. #endif
  773. return (RunawayIndex)_MAX(heater_id, 0);
  774. }
  775. enum TRState : char { TRInactive, TRFirstHeating, TRStable, TRRunaway };
  776. typedef struct {
  777. millis_t timer = 0;
  778. TRState state = TRInactive;
  779. float running_temp;
  780. void run(const float &current, const float &target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_t hysteresis_degc);
  781. } tr_state_machine_t;
  782. static tr_state_machine_t tr_state_machine[NR_HEATER_RUNAWAY];
  783. #endif // HAS_THERMAL_PROTECTION
  784. };
  785. extern Temperature thermalManager;