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

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