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.

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