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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://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. #ifndef SOFT_PWM_SCALE
  32. #define SOFT_PWM_SCALE 0
  33. #endif
  34. #if HOTENDS <= 1
  35. #define HOTEND_INDEX 0
  36. #define E_UNUSED() UNUSED(e)
  37. #else
  38. #define HOTEND_INDEX e
  39. #define E_UNUSED()
  40. #endif
  41. // Identifiers for other heaters
  42. typedef enum : int8_t {
  43. INDEX_NONE = -4,
  44. H_REDUNDANT, H_CHAMBER, H_BED,
  45. H_E0, H_E1, H_E2, H_E3, H_E4, H_E5
  46. } heater_ind_t;
  47. // PID storage
  48. typedef struct { float Kp, Ki, Kd; } PID_t;
  49. typedef struct { float Kp, Ki, Kd, Kc; } PIDC_t;
  50. #if ENABLED(PID_EXTRUSION_SCALING)
  51. typedef PIDC_t hotend_pid_t;
  52. #if LPQ_MAX_LEN > 255
  53. typedef uint16_t lpq_ptr_t;
  54. #else
  55. typedef uint8_t lpq_ptr_t;
  56. #endif
  57. #else
  58. typedef PID_t hotend_pid_t;
  59. #endif
  60. #define DUMMY_PID_VALUE 3000.0f
  61. #if ENABLED(PIDTEMP)
  62. #define _PID_Kp(H) Temperature::temp_hotend[H].pid.Kp
  63. #define _PID_Ki(H) Temperature::temp_hotend[H].pid.Ki
  64. #define _PID_Kd(H) Temperature::temp_hotend[H].pid.Kd
  65. #if ENABLED(PID_EXTRUSION_SCALING)
  66. #define _PID_Kc(H) Temperature::temp_hotend[H].pid.Kc
  67. #else
  68. #define _PID_Kc(H) 1
  69. #endif
  70. #else
  71. #define _PID_Kp(H) DUMMY_PID_VALUE
  72. #define _PID_Ki(H) DUMMY_PID_VALUE
  73. #define _PID_Kd(H) DUMMY_PID_VALUE
  74. #define _PID_Kc(H) 1
  75. #endif
  76. #define PID_PARAM(F,H) _PID_##F(H)
  77. /**
  78. * States for ADC reading in the ISR
  79. */
  80. enum ADCSensorState : char {
  81. StartSampling,
  82. #if HAS_TEMP_ADC_0
  83. PrepareTemp_0, MeasureTemp_0,
  84. #endif
  85. #if HAS_HEATED_BED
  86. PrepareTemp_BED, MeasureTemp_BED,
  87. #endif
  88. #if HAS_TEMP_CHAMBER
  89. PrepareTemp_CHAMBER, MeasureTemp_CHAMBER,
  90. #endif
  91. #if HAS_TEMP_ADC_1
  92. PrepareTemp_1, MeasureTemp_1,
  93. #endif
  94. #if HAS_TEMP_ADC_2
  95. PrepareTemp_2, MeasureTemp_2,
  96. #endif
  97. #if HAS_TEMP_ADC_3
  98. PrepareTemp_3, MeasureTemp_3,
  99. #endif
  100. #if HAS_TEMP_ADC_4
  101. PrepareTemp_4, MeasureTemp_4,
  102. #endif
  103. #if HAS_TEMP_ADC_5
  104. PrepareTemp_5, MeasureTemp_5,
  105. #endif
  106. #if HAS_JOY_ADC_X
  107. PrepareJoy_X, MeasureJoy_X,
  108. #endif
  109. #if HAS_JOY_ADC_Y
  110. PrepareJoy_Y, MeasureJoy_Y,
  111. #endif
  112. #if HAS_JOY_ADC_Z
  113. PrepareJoy_Z, MeasureJoy_Z,
  114. #endif
  115. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  116. Prepare_FILWIDTH, Measure_FILWIDTH,
  117. #endif
  118. #if HAS_ADC_BUTTONS
  119. Prepare_ADC_KEY, Measure_ADC_KEY,
  120. #endif
  121. SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
  122. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  123. };
  124. // Minimum number of Temperature::ISR loops between sensor readings.
  125. // Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
  126. // get all oversampled sensor readings
  127. #define MIN_ADC_ISR_LOOPS 10
  128. #define ACTUAL_ADC_SAMPLES _MAX(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
  129. #if HAS_PID_HEATING
  130. #define PID_K2 (1-float(PID_K1))
  131. #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / TEMP_TIMER_FREQUENCY)
  132. // Apply the scale factors to the PID values
  133. #define scalePID_i(i) ( float(i) * PID_dT )
  134. #define unscalePID_i(i) ( float(i) / PID_dT )
  135. #define scalePID_d(d) ( float(d) / PID_dT )
  136. #define unscalePID_d(d) ( float(d) * PID_dT )
  137. #endif
  138. #define G26_CLICK_CAN_CANCEL (HAS_LCD_MENU && ENABLED(G26_MESH_VALIDATION))
  139. // A temperature sensor
  140. typedef struct TempInfo {
  141. uint16_t acc;
  142. int16_t raw;
  143. float celsius;
  144. inline void reset() { acc = 0; }
  145. inline void sample(const uint16_t s) { acc += s; }
  146. inline void update() { raw = acc; }
  147. } temp_info_t;
  148. // A PWM heater with temperature sensor
  149. typedef struct HeaterInfo : public TempInfo {
  150. int16_t target;
  151. uint8_t soft_pwm_amount;
  152. } heater_info_t;
  153. // A heater with PID stabilization
  154. template<typename T>
  155. struct PIDHeaterInfo : public HeaterInfo {
  156. T pid; // Initialized by settings.load()
  157. };
  158. #if ENABLED(PIDTEMP)
  159. typedef struct PIDHeaterInfo<hotend_pid_t> hotend_info_t;
  160. #else
  161. typedef heater_info_t hotend_info_t;
  162. #endif
  163. #if HAS_HEATED_BED
  164. #if ENABLED(PIDTEMPBED)
  165. typedef struct PIDHeaterInfo<PID_t> bed_info_t;
  166. #else
  167. typedef heater_info_t bed_info_t;
  168. #endif
  169. #endif
  170. #if HAS_HEATED_CHAMBER
  171. typedef heater_info_t chamber_info_t;
  172. #elif HAS_TEMP_CHAMBER
  173. typedef temp_info_t chamber_info_t;
  174. #endif
  175. // Heater idle handling
  176. typedef struct {
  177. millis_t timeout_ms;
  178. bool timed_out;
  179. inline void update(const millis_t &ms) { if (!timed_out && timeout_ms && ELAPSED(ms, timeout_ms)) timed_out = true; }
  180. inline void start(const millis_t &ms) { timeout_ms = millis() + ms; timed_out = false; }
  181. inline void reset() { timeout_ms = 0; timed_out = false; }
  182. inline void expire() { start(0); }
  183. } heater_idle_t;
  184. // Heater watch handling
  185. typedef struct {
  186. uint16_t target;
  187. millis_t next_ms;
  188. inline bool elapsed(const millis_t &ms) { return next_ms && ELAPSED(ms, next_ms); }
  189. inline bool elapsed() { return elapsed(millis()); }
  190. } heater_watch_t;
  191. // Temperature sensor read value ranges
  192. typedef struct { int16_t raw_min, raw_max; } raw_range_t;
  193. typedef struct { int16_t mintemp, maxtemp; } celsius_range_t;
  194. typedef struct { int16_t raw_min, raw_max, mintemp, maxtemp; } temp_range_t;
  195. #define THERMISTOR_ADC_RESOLUTION 1024 // 10-bit ADC .. shame to waste 12-bits of resolution on 32-bit
  196. #define THERMISTOR_ABS_ZERO_C -273.15f // bbbbrrrrr cold !
  197. #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f // mmmmm comfortable
  198. #if HAS_USER_THERMISTORS
  199. enum CustomThermistorIndex : uint8_t {
  200. #if ENABLED(HEATER_0_USER_THERMISTOR)
  201. CTI_HOTEND_0,
  202. #endif
  203. #if ENABLED(HEATER_1_USER_THERMISTOR)
  204. CTI_HOTEND_1,
  205. #endif
  206. #if ENABLED(HEATER_2_USER_THERMISTOR)
  207. CTI_HOTEND_2,
  208. #endif
  209. #if ENABLED(HEATER_3_USER_THERMISTOR)
  210. CTI_HOTEND_3,
  211. #endif
  212. #if ENABLED(HEATER_4_USER_THERMISTOR)
  213. CTI_HOTEND_4,
  214. #endif
  215. #if ENABLED(HEATER_5_USER_THERMISTOR)
  216. CTI_HOTEND_5,
  217. #endif
  218. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  219. CTI_BED,
  220. #endif
  221. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  222. CTI_CHAMBER,
  223. #endif
  224. USER_THERMISTORS
  225. };
  226. // User-defined thermistor
  227. typedef struct {
  228. bool pre_calc; // true if pre-calculations update needed
  229. float sh_c_coeff, // Steinhart-Hart C coefficient .. defaults to '0.0'
  230. sh_alpha,
  231. series_res,
  232. res_25, res_25_recip,
  233. res_25_log,
  234. beta, beta_recip;
  235. } user_thermistor_t;
  236. #endif
  237. class Temperature {
  238. public:
  239. static volatile bool in_temp_isr;
  240. #if HOTENDS
  241. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  242. #define HOTEND_TEMPS (HOTENDS + 1)
  243. #else
  244. #define HOTEND_TEMPS HOTENDS
  245. #endif
  246. static hotend_info_t temp_hotend[HOTEND_TEMPS];
  247. #endif
  248. #if HAS_HEATED_BED
  249. static bed_info_t temp_bed;
  250. #endif
  251. #if HAS_TEMP_CHAMBER
  252. static chamber_info_t temp_chamber;
  253. #endif
  254. #if ENABLED(AUTO_POWER_E_FANS)
  255. static uint8_t autofan_speed[HOTENDS];
  256. #endif
  257. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  258. static uint8_t chamberfan_speed;
  259. #endif
  260. #if ENABLED(FAN_SOFT_PWM)
  261. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  262. soft_pwm_count_fan[FAN_COUNT];
  263. #endif
  264. #if ENABLED(PREVENT_COLD_EXTRUSION)
  265. static bool allow_cold_extrude;
  266. static int16_t extrude_min_temp;
  267. FORCE_INLINE static bool tooCold(const int16_t temp) { return allow_cold_extrude ? false : temp < extrude_min_temp; }
  268. FORCE_INLINE static bool tooColdToExtrude(const uint8_t e) {
  269. E_UNUSED();
  270. return tooCold(degHotend(HOTEND_INDEX));
  271. }
  272. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t e) {
  273. E_UNUSED();
  274. return tooCold(degTargetHotend(HOTEND_INDEX));
  275. }
  276. #else
  277. FORCE_INLINE static bool tooColdToExtrude(const uint8_t e) { UNUSED(e); return false; }
  278. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t e) { UNUSED(e); return false; }
  279. #endif
  280. FORCE_INLINE static bool hotEnoughToExtrude(const uint8_t e) { return !tooColdToExtrude(e); }
  281. FORCE_INLINE static bool targetHotEnoughToExtrude(const uint8_t e) { return !targetTooColdToExtrude(e); }
  282. #if HEATER_IDLE_HANDLER
  283. static heater_idle_t hotend_idle[HOTENDS];
  284. #if HAS_HEATED_BED
  285. static heater_idle_t bed_idle;
  286. #endif
  287. #if HAS_HEATED_CHAMBER
  288. static heater_idle_t chamber_idle;
  289. #endif
  290. #endif
  291. private:
  292. #if EARLY_WATCHDOG
  293. static bool inited; // If temperature controller is running
  294. #endif
  295. static volatile bool temp_meas_ready;
  296. #if WATCH_HOTENDS
  297. static heater_watch_t watch_hotend[HOTENDS];
  298. #endif
  299. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  300. static uint16_t redundant_temperature_raw;
  301. static float redundant_temperature;
  302. #endif
  303. #if ENABLED(PID_EXTRUSION_SCALING)
  304. static int32_t last_e_position, lpq[LPQ_MAX_LEN];
  305. static lpq_ptr_t lpq_ptr;
  306. #endif
  307. #if HOTENDS
  308. static temp_range_t temp_range[HOTENDS];
  309. #endif
  310. #if HAS_HEATED_BED
  311. #if WATCH_BED
  312. static heater_watch_t watch_bed;
  313. #endif
  314. #if DISABLED(PIDTEMPBED)
  315. static millis_t next_bed_check_ms;
  316. #endif
  317. #ifdef BED_MINTEMP
  318. static int16_t mintemp_raw_BED;
  319. #endif
  320. #ifdef BED_MAXTEMP
  321. static int16_t maxtemp_raw_BED;
  322. #endif
  323. #endif
  324. #if HAS_HEATED_CHAMBER
  325. #if WATCH_CHAMBER
  326. static heater_watch_t watch_chamber;
  327. #endif
  328. static millis_t next_chamber_check_ms;
  329. #ifdef CHAMBER_MINTEMP
  330. static int16_t mintemp_raw_CHAMBER;
  331. #endif
  332. #ifdef CHAMBER_MAXTEMP
  333. static int16_t maxtemp_raw_CHAMBER;
  334. #endif
  335. #endif
  336. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  337. static uint8_t consecutive_low_temperature_error[HOTENDS];
  338. #endif
  339. #ifdef MILLISECONDS_PREHEAT_TIME
  340. static millis_t preheat_end_time[HOTENDS];
  341. #endif
  342. #if HAS_AUTO_FAN
  343. static millis_t next_auto_fan_check_ms;
  344. #endif
  345. #if ENABLED(PROBING_HEATERS_OFF)
  346. static bool paused;
  347. #endif
  348. public:
  349. #if HAS_ADC_BUTTONS
  350. static uint32_t current_ADCKey_raw;
  351. static uint8_t ADCKey_count;
  352. #endif
  353. #if ENABLED(PID_EXTRUSION_SCALING)
  354. static int16_t lpq_len;
  355. #endif
  356. /**
  357. * Instance Methods
  358. */
  359. void init();
  360. /**
  361. * Static (class) methods
  362. */
  363. #if HAS_USER_THERMISTORS
  364. static user_thermistor_t user_thermistor[USER_THERMISTORS];
  365. static void log_user_thermistor(const uint8_t t_index, const bool eprom=false);
  366. static void reset_user_thermistors();
  367. static float user_thermistor_to_deg_c(const uint8_t t_index, const int raw);
  368. static bool set_pull_up_res(int8_t t_index, float value) {
  369. //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
  370. if (!WITHIN(value, 1, 1000000)) return false;
  371. user_thermistor[t_index].series_res = value;
  372. return true;
  373. }
  374. static bool set_res25(int8_t t_index, float value) {
  375. if (!WITHIN(value, 1, 10000000)) return false;
  376. user_thermistor[t_index].res_25 = value;
  377. user_thermistor[t_index].pre_calc = true;
  378. return true;
  379. }
  380. static bool set_beta(int8_t t_index, float value) {
  381. if (!WITHIN(value, 1, 1000000)) return false;
  382. user_thermistor[t_index].beta = value;
  383. user_thermistor[t_index].pre_calc = true;
  384. return true;
  385. }
  386. static bool set_sh_coeff(int8_t t_index, float value) {
  387. if (!WITHIN(value, -0.01f, 0.01f)) return false;
  388. user_thermistor[t_index].sh_c_coeff = value;
  389. user_thermistor[t_index].pre_calc = true;
  390. return true;
  391. }
  392. #endif
  393. #if HOTENDS
  394. static float analog_to_celsius_hotend(const int raw, const uint8_t e);
  395. #endif
  396. #if HAS_HEATED_BED
  397. static float analog_to_celsius_bed(const int raw);
  398. #endif
  399. #if HAS_TEMP_CHAMBER
  400. static float analog_to_celsius_chamber(const int raw);
  401. #endif
  402. #if FAN_COUNT > 0
  403. static uint8_t fan_speed[FAN_COUNT];
  404. #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT)
  405. static void set_fan_speed(const uint8_t target, const uint16_t speed);
  406. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  407. static bool fans_paused;
  408. static uint8_t saved_fan_speed[FAN_COUNT];
  409. #endif
  410. static constexpr inline uint8_t fanPercent(const uint8_t speed) { return ui8_to_percent(speed); }
  411. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  412. static uint8_t fan_speed_scaler[FAN_COUNT];
  413. #else
  414. static constexpr uint8_t fan_speed_scaler[FAN_COUNT] = ARRAY_N(FAN_COUNT, 128, 128, 128, 128, 128, 128);
  415. #endif
  416. static inline uint8_t scaledFanSpeed(const uint8_t target, const uint8_t fs) {
  417. return (fs * uint16_t(fan_speed_scaler[target])) >> 7;
  418. }
  419. static inline uint8_t scaledFanSpeed(const uint8_t target) {
  420. return scaledFanSpeed(target, fan_speed[target]);
  421. }
  422. #if ENABLED(EXTRA_FAN_SPEED)
  423. static uint8_t old_fan_speed[FAN_COUNT], new_fan_speed[FAN_COUNT];
  424. static void set_temp_fan_speed(const uint8_t fan, const uint16_t tmp_temp);
  425. #endif
  426. #if HAS_LCD_MENU
  427. static uint8_t lcd_tmpfan_speed[
  428. #if ENABLED(SINGLENOZZLE)
  429. _MAX(EXTRUDERS, FAN_COUNT)
  430. #else
  431. FAN_COUNT
  432. #endif
  433. ];
  434. static inline void lcd_setFanSpeed(const uint8_t target) { set_fan_speed(target, lcd_tmpfan_speed[target]); }
  435. #if HAS_FAN0
  436. FORCE_INLINE static void lcd_setFanSpeed0() { lcd_setFanSpeed(0); }
  437. #endif
  438. #if HAS_FAN1 || (ENABLED(SINGLENOZZLE) && EXTRUDERS > 1)
  439. FORCE_INLINE static void lcd_setFanSpeed1() { lcd_setFanSpeed(1); }
  440. #endif
  441. #if HAS_FAN2 || (ENABLED(SINGLENOZZLE) && EXTRUDERS > 2)
  442. FORCE_INLINE static void lcd_setFanSpeed2() { lcd_setFanSpeed(2); }
  443. #endif
  444. #endif // HAS_LCD_MENU
  445. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  446. void set_fans_paused(const bool p);
  447. #endif
  448. #endif // FAN_COUNT > 0
  449. static inline void zero_fan_speeds() {
  450. #if FAN_COUNT > 0
  451. FANS_LOOP(i) set_fan_speed(i, 0);
  452. #endif
  453. }
  454. /**
  455. * Called from the Temperature ISR
  456. */
  457. static void readings_ready();
  458. static void isr();
  459. /**
  460. * Call periodically to manage heaters
  461. */
  462. static void manage_heater() _O2; // Added _O2 to work around a compiler error
  463. /**
  464. * Preheating hotends
  465. */
  466. #ifdef MILLISECONDS_PREHEAT_TIME
  467. static bool is_preheating(const uint8_t e) {
  468. E_UNUSED();
  469. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  470. }
  471. static void start_preheat_time(const uint8_t e) {
  472. E_UNUSED();
  473. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  474. }
  475. static void reset_preheat_time(const uint8_t e) {
  476. E_UNUSED();
  477. preheat_end_time[HOTEND_INDEX] = 0;
  478. }
  479. #else
  480. #define is_preheating(n) (false)
  481. #endif
  482. //high level conversion routines, for use outside of temperature.cpp
  483. //inline so that there is no performance decrease.
  484. //deg=degreeCelsius
  485. FORCE_INLINE static float degHotend(const uint8_t e) {
  486. E_UNUSED();
  487. #if HOTENDS
  488. return temp_hotend[HOTEND_INDEX].celsius;
  489. #else
  490. return 0;
  491. #endif
  492. }
  493. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  494. FORCE_INLINE static int16_t rawHotendTemp(const uint8_t e) {
  495. E_UNUSED();
  496. #if HOTENDS
  497. return temp_hotend[HOTEND_INDEX].raw;
  498. #else
  499. return 0;
  500. #endif
  501. }
  502. #endif
  503. FORCE_INLINE static int16_t degTargetHotend(const uint8_t e) {
  504. E_UNUSED();
  505. #if HOTENDS
  506. return temp_hotend[HOTEND_INDEX].target;
  507. #else
  508. return 0;
  509. #endif
  510. }
  511. #if WATCH_HOTENDS
  512. static void start_watching_hotend(const uint8_t e=0);
  513. #else
  514. static inline void start_watching_hotend(const uint8_t e=0) { UNUSED(e); }
  515. #endif
  516. #if HOTENDS
  517. #if HAS_LCD_MENU
  518. static inline void start_watching_E0() { start_watching_hotend(0); }
  519. static inline void start_watching_E1() { start_watching_hotend(1); }
  520. static inline void start_watching_E2() { start_watching_hotend(2); }
  521. static inline void start_watching_E3() { start_watching_hotend(3); }
  522. static inline void start_watching_E4() { start_watching_hotend(4); }
  523. static inline void start_watching_E5() { start_watching_hotend(5); }
  524. #endif
  525. static void setTargetHotend(const int16_t celsius, const uint8_t e) {
  526. E_UNUSED();
  527. const uint8_t ee = HOTEND_INDEX;
  528. #ifdef MILLISECONDS_PREHEAT_TIME
  529. if (celsius == 0)
  530. reset_preheat_time(ee);
  531. else if (temp_hotend[ee].target == 0)
  532. start_preheat_time(ee);
  533. #endif
  534. #if ENABLED(AUTO_POWER_CONTROL)
  535. powerManager.power_on();
  536. #endif
  537. temp_hotend[ee].target = _MIN(celsius, temp_range[ee].maxtemp - 15);
  538. start_watching_hotend(ee);
  539. }
  540. FORCE_INLINE static bool isHeatingHotend(const uint8_t e) {
  541. E_UNUSED();
  542. return temp_hotend[HOTEND_INDEX].target > temp_hotend[HOTEND_INDEX].celsius;
  543. }
  544. FORCE_INLINE static bool isCoolingHotend(const uint8_t e) {
  545. E_UNUSED();
  546. return temp_hotend[HOTEND_INDEX].target < temp_hotend[HOTEND_INDEX].celsius;
  547. }
  548. #if HAS_TEMP_HOTEND
  549. static bool wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling=true
  550. #if G26_CLICK_CAN_CANCEL
  551. , const bool click_to_cancel=false
  552. #endif
  553. );
  554. #endif
  555. FORCE_INLINE static bool still_heating(const uint8_t e) {
  556. return degTargetHotend(e) > TEMP_HYSTERESIS && ABS(degHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS;
  557. }
  558. #endif // HOTENDS
  559. #if HAS_HEATED_BED
  560. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  561. FORCE_INLINE static int16_t rawBedTemp() { return temp_bed.raw; }
  562. #endif
  563. FORCE_INLINE static float degBed() { return temp_bed.celsius; }
  564. FORCE_INLINE static int16_t degTargetBed() { return temp_bed.target; }
  565. FORCE_INLINE static bool isHeatingBed() { return temp_bed.target > temp_bed.celsius; }
  566. FORCE_INLINE static bool isCoolingBed() { return temp_bed.target < temp_bed.celsius; }
  567. #if WATCH_BED
  568. static void start_watching_bed();
  569. #else
  570. static inline void start_watching_bed() {}
  571. #endif
  572. static void setTargetBed(const int16_t celsius) {
  573. #if ENABLED(AUTO_POWER_CONTROL)
  574. powerManager.power_on();
  575. #endif
  576. temp_bed.target =
  577. #ifdef BED_MAXTEMP
  578. _MIN(celsius, BED_MAXTEMP - 10)
  579. #else
  580. celsius
  581. #endif
  582. ;
  583. start_watching_bed();
  584. }
  585. static bool wait_for_bed(const bool no_wait_for_cooling=true
  586. #if G26_CLICK_CAN_CANCEL
  587. , const bool click_to_cancel=false
  588. #endif
  589. );
  590. #endif // HAS_HEATED_BED
  591. #if HAS_TEMP_CHAMBER
  592. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  593. FORCE_INLINE static int16_t rawChamberTemp() { return temp_chamber.raw; }
  594. #endif
  595. FORCE_INLINE static float degChamber() { return temp_chamber.celsius; }
  596. #if HAS_HEATED_CHAMBER
  597. FORCE_INLINE static int16_t degTargetChamber() { return temp_chamber.target; }
  598. FORCE_INLINE static bool isHeatingChamber() { return temp_chamber.target > temp_chamber.celsius; }
  599. FORCE_INLINE static bool isCoolingChamber() { return temp_chamber.target < temp_chamber.celsius; }
  600. static bool wait_for_chamber(const bool no_wait_for_cooling=true);
  601. #endif
  602. #endif // HAS_TEMP_CHAMBER
  603. #if WATCH_CHAMBER
  604. static void start_watching_chamber();
  605. #else
  606. static inline void start_watching_chamber() {}
  607. #endif
  608. #if HAS_HEATED_CHAMBER
  609. static void setTargetChamber(const int16_t celsius) {
  610. temp_chamber.target =
  611. #ifdef CHAMBER_MAXTEMP
  612. _MIN(celsius, CHAMBER_MAXTEMP)
  613. #else
  614. celsius
  615. #endif
  616. ;
  617. start_watching_chamber();
  618. }
  619. #endif // HAS_HEATED_CHAMBER
  620. /**
  621. * The software PWM power for a heater
  622. */
  623. static int16_t getHeaterPower(const heater_ind_t heater);
  624. /**
  625. * Switch off all heaters, set all target temperatures to 0
  626. */
  627. static void disable_all_heaters();
  628. /**
  629. * Perform auto-tuning for hotend or bed in response to M303
  630. */
  631. #if HAS_PID_HEATING
  632. static void PID_autotune(const float &target, const heater_ind_t hotend, const int8_t ncycles, const bool set_result=false);
  633. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  634. static bool adaptive_fan_slowing;
  635. #elif ENABLED(ADAPTIVE_FAN_SLOWING)
  636. static constexpr bool adaptive_fan_slowing = true;
  637. #endif
  638. /**
  639. * Update the temp manager when PID values change
  640. */
  641. #if ENABLED(PIDTEMP)
  642. FORCE_INLINE static void updatePID() {
  643. #if ENABLED(PID_EXTRUSION_SCALING)
  644. last_e_position = 0;
  645. #endif
  646. }
  647. #endif
  648. #endif
  649. #if ENABLED(PROBING_HEATERS_OFF)
  650. static void pause(const bool p);
  651. FORCE_INLINE static bool is_paused() { return paused; }
  652. #endif
  653. #if HEATER_IDLE_HANDLER
  654. static void reset_heater_idle_timer(const uint8_t e) {
  655. E_UNUSED();
  656. hotend_idle[HOTEND_INDEX].reset();
  657. start_watching_hotend(HOTEND_INDEX);
  658. }
  659. #if HAS_HEATED_BED
  660. static void reset_bed_idle_timer() {
  661. bed_idle.reset();
  662. start_watching_bed();
  663. }
  664. #endif
  665. #endif // HEATER_IDLE_HANDLER
  666. #if HAS_TEMP_SENSOR
  667. static void print_heater_states(const uint8_t target_extruder
  668. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  669. , const bool include_r=false
  670. #endif
  671. );
  672. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  673. static uint8_t auto_report_temp_interval;
  674. static millis_t next_temp_report_ms;
  675. static void auto_report_temperatures();
  676. static inline void set_auto_report_interval(uint8_t v) {
  677. NOMORE(v, 60);
  678. auto_report_temp_interval = v;
  679. next_temp_report_ms = millis() + 1000UL * v;
  680. }
  681. #endif
  682. #endif
  683. #if HAS_DISPLAY
  684. static void set_heating_message(const uint8_t e);
  685. #endif
  686. private:
  687. static void set_current_temp_raw();
  688. static void updateTemperaturesFromRawValues();
  689. #define HAS_MAX6675 EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675)
  690. #if HAS_MAX6675
  691. #if BOTH(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675)
  692. #define COUNT_6675 2
  693. #else
  694. #define COUNT_6675 1
  695. #endif
  696. #if COUNT_6675 > 1
  697. #define READ_MAX6675(N) read_max6675(N)
  698. #else
  699. #define READ_MAX6675(N) read_max6675()
  700. #endif
  701. static int read_max6675(
  702. #if COUNT_6675 > 1
  703. const uint8_t hindex=0
  704. #endif
  705. );
  706. #endif
  707. static void checkExtruderAutoFans();
  708. static float get_pid_output_hotend(const uint8_t e);
  709. #if ENABLED(PIDTEMPBED)
  710. static float get_pid_output_bed();
  711. #endif
  712. #if HAS_HEATED_CHAMBER
  713. static float get_pid_output_chamber();
  714. #endif
  715. static void _temp_error(const heater_ind_t e, PGM_P const serial_msg, PGM_P const lcd_msg);
  716. static void min_temp_error(const heater_ind_t e);
  717. static void max_temp_error(const heater_ind_t e);
  718. #define HAS_THERMAL_PROTECTION (EITHER(THERMAL_PROTECTION_HOTENDS, THERMAL_PROTECTION_CHAMBER) || HAS_THERMALLY_PROTECTED_BED)
  719. #if HAS_THERMAL_PROTECTION
  720. enum TRState : char { TRInactive, TRFirstHeating, TRStable, TRRunaway };
  721. typedef struct {
  722. millis_t timer = 0;
  723. TRState state = TRInactive;
  724. } tr_state_machine_t;
  725. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  726. static tr_state_machine_t tr_state_machine[HOTENDS];
  727. #endif
  728. #if HAS_THERMALLY_PROTECTED_BED
  729. static tr_state_machine_t tr_state_machine_bed;
  730. #endif
  731. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  732. static tr_state_machine_t tr_state_machine_chamber;
  733. #endif
  734. static void thermal_runaway_protection(tr_state_machine_t &state, const float &current, const float &target, const heater_ind_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc);
  735. #endif // HAS_THERMAL_PROTECTION
  736. };
  737. extern Temperature thermalManager;