My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

temperature.h 24KB

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