My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.h 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * temperature.h - temperature controller
  25. */
  26. #include "thermistor/thermistors.h"
  27. #include "../inc/MarlinConfig.h"
  28. #if ENABLED(AUTO_POWER_CONTROL)
  29. #include "../feature/power.h"
  30. #endif
  31. #ifndef SOFT_PWM_SCALE
  32. #define SOFT_PWM_SCALE 0
  33. #endif
  34. #define HOTEND_INDEX TERN(HAS_MULTI_HOTEND, e, 0)
  35. #define E_NAME TERN_(HAS_MULTI_HOTEND, e)
  36. // Heater identifiers. Positive values are hotends. Negative values are other heaters.
  37. typedef enum : int8_t {
  38. INDEX_NONE = -5,
  39. H_PROBE, H_REDUNDANT, H_CHAMBER, H_BED,
  40. H_E0, H_E1, H_E2, H_E3, H_E4, H_E5, H_E6, H_E7
  41. } heater_id_t;
  42. // PID storage
  43. typedef struct { float Kp, Ki, Kd; } PID_t;
  44. typedef struct { float Kp, Ki, Kd, Kc; } PIDC_t;
  45. typedef struct { float Kp, Ki, Kd, Kf; } PIDF_t;
  46. typedef struct { float Kp, Ki, Kd, Kc, Kf; } PIDCF_t;
  47. typedef
  48. #if BOTH(PID_EXTRUSION_SCALING, PID_FAN_SCALING)
  49. PIDCF_t
  50. #elif ENABLED(PID_EXTRUSION_SCALING)
  51. PIDC_t
  52. #elif ENABLED(PID_FAN_SCALING)
  53. PIDF_t
  54. #else
  55. PID_t
  56. #endif
  57. hotend_pid_t;
  58. #if ENABLED(PID_EXTRUSION_SCALING)
  59. typedef IF<(LPQ_MAX_LEN > 255), uint16_t, uint8_t>::type lpq_ptr_t;
  60. #endif
  61. #define PID_PARAM(F,H) _PID_##F(TERN(PID_PARAMS_PER_HOTEND, H, 0))
  62. #define _PID_Kp(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kp, NAN)
  63. #define _PID_Ki(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Ki, NAN)
  64. #define _PID_Kd(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kd, NAN)
  65. #if ENABLED(PIDTEMP)
  66. #define _PID_Kc(H) TERN(PID_EXTRUSION_SCALING, Temperature::temp_hotend[H].pid.Kc, 1)
  67. #define _PID_Kf(H) TERN(PID_FAN_SCALING, Temperature::temp_hotend[H].pid.Kf, 0)
  68. #else
  69. #define _PID_Kc(H) 1
  70. #define _PID_Kf(H) 0
  71. #endif
  72. /**
  73. * States for ADC reading in the ISR
  74. */
  75. enum ADCSensorState : char {
  76. StartSampling,
  77. #if HAS_TEMP_ADC_0
  78. PrepareTemp_0, MeasureTemp_0,
  79. #endif
  80. #if HAS_HEATED_BED
  81. PrepareTemp_BED, MeasureTemp_BED,
  82. #endif
  83. #if HAS_TEMP_CHAMBER
  84. PrepareTemp_CHAMBER, MeasureTemp_CHAMBER,
  85. #endif
  86. #if HAS_TEMP_PROBE
  87. PrepareTemp_PROBE, MeasureTemp_PROBE,
  88. #endif
  89. #if HAS_TEMP_ADC_1
  90. PrepareTemp_1, MeasureTemp_1,
  91. #endif
  92. #if HAS_TEMP_ADC_2
  93. PrepareTemp_2, MeasureTemp_2,
  94. #endif
  95. #if HAS_TEMP_ADC_3
  96. PrepareTemp_3, MeasureTemp_3,
  97. #endif
  98. #if HAS_TEMP_ADC_4
  99. PrepareTemp_4, MeasureTemp_4,
  100. #endif
  101. #if HAS_TEMP_ADC_5
  102. PrepareTemp_5, MeasureTemp_5,
  103. #endif
  104. #if HAS_TEMP_ADC_6
  105. PrepareTemp_6, MeasureTemp_6,
  106. #endif
  107. #if HAS_TEMP_ADC_7
  108. PrepareTemp_7, MeasureTemp_7,
  109. #endif
  110. #if HAS_JOY_ADC_X
  111. PrepareJoy_X, MeasureJoy_X,
  112. #endif
  113. #if HAS_JOY_ADC_Y
  114. PrepareJoy_Y, MeasureJoy_Y,
  115. #endif
  116. #if HAS_JOY_ADC_Z
  117. PrepareJoy_Z, MeasureJoy_Z,
  118. #endif
  119. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  120. Prepare_FILWIDTH, Measure_FILWIDTH,
  121. #endif
  122. #if ENABLED(POWER_MONITOR_CURRENT)
  123. Prepare_POWER_MONITOR_CURRENT,
  124. Measure_POWER_MONITOR_CURRENT,
  125. #endif
  126. #if ENABLED(POWER_MONITOR_VOLTAGE)
  127. Prepare_POWER_MONITOR_VOLTAGE,
  128. Measure_POWER_MONITOR_VOLTAGE,
  129. #endif
  130. #if HAS_ADC_BUTTONS
  131. Prepare_ADC_KEY, Measure_ADC_KEY,
  132. #endif
  133. SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
  134. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  135. };
  136. // Minimum number of Temperature::ISR loops between sensor readings.
  137. // Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
  138. // get all oversampled sensor readings
  139. #define MIN_ADC_ISR_LOOPS 10
  140. #define ACTUAL_ADC_SAMPLES _MAX(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
  141. #if HAS_PID_HEATING
  142. #define PID_K2 (1-float(PID_K1))
  143. #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / TEMP_TIMER_FREQUENCY)
  144. // Apply the scale factors to the PID values
  145. #define scalePID_i(i) ( float(i) * PID_dT )
  146. #define unscalePID_i(i) ( float(i) / PID_dT )
  147. #define scalePID_d(d) ( float(d) / PID_dT )
  148. #define unscalePID_d(d) ( float(d) * PID_dT )
  149. #endif
  150. #if BOTH(HAS_LCD_MENU, G26_MESH_VALIDATION)
  151. #define G26_CLICK_CAN_CANCEL 1
  152. #endif
  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 watch handling
  193. template <int INCREASE, int HYSTERESIS, millis_t PERIOD>
  194. struct HeaterWatch {
  195. uint16_t target;
  196. millis_t next_ms;
  197. inline bool elapsed(const millis_t &ms) { return next_ms && ELAPSED(ms, next_ms); }
  198. inline bool elapsed() { return elapsed(millis()); }
  199. inline void restart(const int16_t curr, const int16_t tgt) {
  200. if (tgt) {
  201. const int16_t newtarget = curr + INCREASE;
  202. if (newtarget < tgt - HYSTERESIS - 1) {
  203. target = newtarget;
  204. next_ms = millis() + SEC_TO_MS(PERIOD);
  205. return;
  206. }
  207. }
  208. next_ms = 0;
  209. }
  210. };
  211. #if WATCH_HOTENDS
  212. typedef struct HeaterWatch<WATCH_TEMP_INCREASE, TEMP_HYSTERESIS, WATCH_TEMP_PERIOD> hotend_watch_t;
  213. #endif
  214. #if WATCH_BED
  215. typedef struct HeaterWatch<WATCH_BED_TEMP_INCREASE, TEMP_BED_HYSTERESIS, WATCH_BED_TEMP_PERIOD> bed_watch_t;
  216. #endif
  217. #if WATCH_CHAMBER
  218. typedef struct HeaterWatch<WATCH_CHAMBER_TEMP_INCREASE, TEMP_CHAMBER_HYSTERESIS, WATCH_CHAMBER_TEMP_PERIOD> chamber_watch_t;
  219. #endif
  220. // Temperature sensor read value ranges
  221. typedef struct { int16_t raw_min, raw_max; } raw_range_t;
  222. typedef struct { int16_t mintemp, maxtemp; } celsius_range_t;
  223. typedef struct { int16_t raw_min, raw_max, mintemp, maxtemp; } temp_range_t;
  224. #define THERMISTOR_ABS_ZERO_C -273.15f // bbbbrrrrr cold !
  225. #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f // mmmmm comfortable
  226. #if HAS_USER_THERMISTORS
  227. enum CustomThermistorIndex : uint8_t {
  228. #if ENABLED(HEATER_0_USER_THERMISTOR)
  229. CTI_HOTEND_0,
  230. #endif
  231. #if ENABLED(HEATER_1_USER_THERMISTOR)
  232. CTI_HOTEND_1,
  233. #endif
  234. #if ENABLED(HEATER_2_USER_THERMISTOR)
  235. CTI_HOTEND_2,
  236. #endif
  237. #if ENABLED(HEATER_3_USER_THERMISTOR)
  238. CTI_HOTEND_3,
  239. #endif
  240. #if ENABLED(HEATER_4_USER_THERMISTOR)
  241. CTI_HOTEND_4,
  242. #endif
  243. #if ENABLED(HEATER_5_USER_THERMISTOR)
  244. CTI_HOTEND_5,
  245. #endif
  246. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  247. CTI_BED,
  248. #endif
  249. #if ENABLED(HEATER_PROBE_USER_THERMISTOR)
  250. CTI_PROBE,
  251. #endif
  252. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  253. CTI_CHAMBER,
  254. #endif
  255. USER_THERMISTORS
  256. };
  257. // User-defined thermistor
  258. typedef struct {
  259. bool pre_calc; // true if pre-calculations update needed
  260. float sh_c_coeff, // Steinhart-Hart C coefficient .. defaults to '0.0'
  261. sh_alpha,
  262. series_res,
  263. res_25, res_25_recip,
  264. res_25_log,
  265. beta, beta_recip;
  266. } user_thermistor_t;
  267. #endif
  268. class Temperature {
  269. public:
  270. #if HAS_HOTEND
  271. #define HOTEND_TEMPS (HOTENDS + ENABLED(TEMP_SENSOR_1_AS_REDUNDANT))
  272. static hotend_info_t temp_hotend[HOTEND_TEMPS];
  273. static const uint16_t heater_maxtemp[HOTENDS];
  274. #endif
  275. TERN_(HAS_HEATED_BED, static bed_info_t temp_bed);
  276. TERN_(HAS_TEMP_PROBE, static probe_info_t temp_probe);
  277. TERN_(HAS_TEMP_CHAMBER, static chamber_info_t temp_chamber);
  278. TERN_(AUTO_POWER_E_FANS, static uint8_t autofan_speed[HOTENDS]);
  279. TERN_(AUTO_POWER_CHAMBER_FAN, static uint8_t chamberfan_speed);
  280. #if ENABLED(FAN_SOFT_PWM)
  281. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  282. soft_pwm_count_fan[FAN_COUNT];
  283. #endif
  284. #if ENABLED(PREVENT_COLD_EXTRUSION)
  285. static bool allow_cold_extrude;
  286. static int16_t extrude_min_temp;
  287. FORCE_INLINE static bool tooCold(const int16_t temp) { return allow_cold_extrude ? false : temp < extrude_min_temp - (TEMP_WINDOW); }
  288. FORCE_INLINE static bool tooColdToExtrude(const uint8_t E_NAME) {
  289. return tooCold(degHotend(HOTEND_INDEX));
  290. }
  291. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t E_NAME) {
  292. return tooCold(degTargetHotend(HOTEND_INDEX));
  293. }
  294. #else
  295. FORCE_INLINE static bool tooColdToExtrude(const uint8_t) { return false; }
  296. FORCE_INLINE static bool targetTooColdToExtrude(const uint8_t) { return false; }
  297. #endif
  298. FORCE_INLINE static bool hotEnoughToExtrude(const uint8_t e) { return !tooColdToExtrude(e); }
  299. FORCE_INLINE static bool targetHotEnoughToExtrude(const uint8_t e) { return !targetTooColdToExtrude(e); }
  300. #if HEATER_IDLE_HANDLER
  301. // Heater idle handling. Marlin creates one per hotend and one for the heated bed.
  302. typedef struct {
  303. millis_t timeout_ms;
  304. bool timed_out;
  305. inline void update(const millis_t &ms) { if (!timed_out && timeout_ms && ELAPSED(ms, timeout_ms)) timed_out = true; }
  306. inline void start(const millis_t &ms) { timeout_ms = millis() + ms; timed_out = false; }
  307. inline void reset() { timeout_ms = 0; timed_out = false; }
  308. inline void expire() { start(0); }
  309. } heater_idle_t;
  310. // Indices and size for the heater_idle array
  311. #define _ENUM_FOR_E(N) IDLE_INDEX_E##N,
  312. enum IdleIndex : uint8_t {
  313. REPEAT(HOTENDS, _ENUM_FOR_E)
  314. #if ENABLED(HAS_HEATED_BED)
  315. IDLE_INDEX_BED,
  316. #endif
  317. NR_HEATER_IDLE
  318. };
  319. #undef _ENUM_FOR_E
  320. // Convert the given heater_id_t to idle array index
  321. static inline IdleIndex idle_index_for_id(const int8_t heater_id) {
  322. #if HAS_HEATED_BED
  323. if (heater_id == H_BED) return IDLE_INDEX_BED;
  324. #endif
  325. return (IdleIndex)_MAX(heater_id, 0);
  326. }
  327. static heater_idle_t heater_idle[NR_HEATER_IDLE];
  328. #endif
  329. private:
  330. TERN_(EARLY_WATCHDOG, static bool inited); // If temperature controller is running
  331. static volatile bool raw_temps_ready;
  332. TERN_(WATCH_HOTENDS, static hotend_watch_t watch_hotend[HOTENDS]);
  333. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  334. static uint16_t redundant_temperature_raw;
  335. static float redundant_temperature;
  336. #endif
  337. #if ENABLED(PID_EXTRUSION_SCALING)
  338. static int32_t last_e_position, lpq[LPQ_MAX_LEN];
  339. static lpq_ptr_t lpq_ptr;
  340. #endif
  341. TERN_(HAS_HOTEND, static temp_range_t temp_range[HOTENDS]);
  342. #if HAS_HEATED_BED
  343. TERN_(WATCH_BED, static bed_watch_t watch_bed);
  344. TERN(PIDTEMPBED,,static millis_t next_bed_check_ms);
  345. #ifdef BED_MINTEMP
  346. static int16_t mintemp_raw_BED;
  347. #endif
  348. #ifdef BED_MAXTEMP
  349. static int16_t maxtemp_raw_BED;
  350. #endif
  351. #endif
  352. #if HAS_HEATED_CHAMBER
  353. TERN_(WATCH_CHAMBER, static chamber_watch_t watch_chamber);
  354. static millis_t next_chamber_check_ms;
  355. #ifdef CHAMBER_MINTEMP
  356. static int16_t mintemp_raw_CHAMBER;
  357. #endif
  358. #ifdef CHAMBER_MAXTEMP
  359. static int16_t maxtemp_raw_CHAMBER;
  360. #endif
  361. #endif
  362. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  363. static uint8_t consecutive_low_temperature_error[HOTENDS];
  364. #endif
  365. #ifdef MILLISECONDS_PREHEAT_TIME
  366. static millis_t preheat_end_time[HOTENDS];
  367. #endif
  368. TERN_(HAS_AUTO_FAN, static millis_t next_auto_fan_check_ms);
  369. TERN_(PROBING_HEATERS_OFF, static bool paused);
  370. public:
  371. #if HAS_ADC_BUTTONS
  372. static uint32_t current_ADCKey_raw;
  373. static uint16_t ADCKey_count;
  374. #endif
  375. TERN_(PID_EXTRUSION_SCALING, static int16_t lpq_len);
  376. /**
  377. * Instance Methods
  378. */
  379. void init();
  380. /**
  381. * Static (class) methods
  382. */
  383. #if HAS_USER_THERMISTORS
  384. static user_thermistor_t user_thermistor[USER_THERMISTORS];
  385. static void log_user_thermistor(const uint8_t t_index, const bool eprom=false);
  386. static void reset_user_thermistors();
  387. static float user_thermistor_to_deg_c(const uint8_t t_index, const int raw);
  388. static bool set_pull_up_res(int8_t t_index, float value) {
  389. //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
  390. if (!WITHIN(value, 1, 1000000)) return false;
  391. user_thermistor[t_index].series_res = value;
  392. return true;
  393. }
  394. static bool set_res25(int8_t t_index, float value) {
  395. if (!WITHIN(value, 1, 10000000)) return false;
  396. user_thermistor[t_index].res_25 = value;
  397. user_thermistor[t_index].pre_calc = true;
  398. return true;
  399. }
  400. static bool set_beta(int8_t t_index, float value) {
  401. if (!WITHIN(value, 1, 1000000)) return false;
  402. user_thermistor[t_index].beta = value;
  403. user_thermistor[t_index].pre_calc = true;
  404. return true;
  405. }
  406. static bool set_sh_coeff(int8_t t_index, float value) {
  407. if (!WITHIN(value, -0.01f, 0.01f)) return false;
  408. user_thermistor[t_index].sh_c_coeff = value;
  409. user_thermistor[t_index].pre_calc = true;
  410. return true;
  411. }
  412. #endif
  413. #if HAS_HOTEND
  414. static float analog_to_celsius_hotend(const int raw, const uint8_t e);
  415. #endif
  416. #if HAS_HEATED_BED
  417. static float analog_to_celsius_bed(const int raw);
  418. #endif
  419. #if HAS_TEMP_PROBE
  420. static float analog_to_celsius_probe(const int raw);
  421. #endif
  422. #if HAS_TEMP_CHAMBER
  423. static float analog_to_celsius_chamber(const int raw);
  424. #endif
  425. #if HAS_FAN
  426. static uint8_t fan_speed[FAN_COUNT];
  427. #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT)
  428. static void set_fan_speed(const uint8_t target, const uint16_t speed);
  429. #if ENABLED(REPORT_FAN_CHANGE)
  430. static void report_fan_speed(const uint8_t target);
  431. #endif
  432. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  433. static bool fans_paused;
  434. static uint8_t saved_fan_speed[FAN_COUNT];
  435. #endif
  436. static constexpr inline uint8_t fanPercent(const uint8_t speed) { return ui8_to_percent(speed); }
  437. TERN_(ADAPTIVE_FAN_SLOWING, static uint8_t fan_speed_scaler[FAN_COUNT]);
  438. static inline uint8_t scaledFanSpeed(const uint8_t target, const uint8_t fs) {
  439. UNUSED(target); // Potentially unused!
  440. return (fs * uint16_t(TERN(ADAPTIVE_FAN_SLOWING, fan_speed_scaler[target], 128))) >> 7;
  441. }
  442. static inline uint8_t scaledFanSpeed(const uint8_t target) {
  443. return scaledFanSpeed(target, fan_speed[target]);
  444. }
  445. #if ENABLED(EXTRA_FAN_SPEED)
  446. static uint8_t old_fan_speed[FAN_COUNT], new_fan_speed[FAN_COUNT];
  447. static void set_temp_fan_speed(const uint8_t fan, const uint16_t tmp_temp);
  448. #endif
  449. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  450. void set_fans_paused(const bool p);
  451. #endif
  452. #endif // HAS_FAN
  453. static inline void zero_fan_speeds() {
  454. #if HAS_FAN
  455. FANS_LOOP(i) set_fan_speed(i, 0);
  456. #endif
  457. }
  458. /**
  459. * Called from the Temperature ISR
  460. */
  461. static void readings_ready();
  462. static void tick();
  463. /**
  464. * Call periodically to manage heaters
  465. */
  466. static void manage_heater() _O2; // Added _O2 to work around a compiler error
  467. /**
  468. * Preheating hotends
  469. */
  470. #ifdef MILLISECONDS_PREHEAT_TIME
  471. static bool is_preheating(const uint8_t E_NAME) {
  472. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  473. }
  474. static void start_preheat_time(const uint8_t E_NAME) {
  475. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  476. }
  477. static void reset_preheat_time(const uint8_t E_NAME) {
  478. preheat_end_time[HOTEND_INDEX] = 0;
  479. }
  480. #else
  481. #define is_preheating(n) (false)
  482. #endif
  483. //high level conversion routines, for use outside of temperature.cpp
  484. //inline so that there is no performance decrease.
  485. //deg=degreeCelsius
  486. FORCE_INLINE static float degHotend(const uint8_t E_NAME) {
  487. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].celsius);
  488. }
  489. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  490. FORCE_INLINE static int16_t rawHotendTemp(const uint8_t E_NAME) {
  491. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].raw);
  492. }
  493. #endif
  494. FORCE_INLINE static int16_t degTargetHotend(const uint8_t E_NAME) {
  495. return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].target);
  496. }
  497. #if WATCH_HOTENDS
  498. static void start_watching_hotend(const uint8_t e=0);
  499. #else
  500. static inline void start_watching_hotend(const uint8_t=0) {}
  501. #endif
  502. #if HAS_HOTEND
  503. static void setTargetHotend(const int16_t celsius, const uint8_t E_NAME) {
  504. const uint8_t ee = HOTEND_INDEX;
  505. #ifdef MILLISECONDS_PREHEAT_TIME
  506. if (celsius == 0)
  507. reset_preheat_time(ee);
  508. else if (temp_hotend[ee].target == 0)
  509. start_preheat_time(ee);
  510. #endif
  511. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  512. temp_hotend[ee].target = _MIN(celsius, temp_range[ee].maxtemp - HOTEND_OVERSHOOT);
  513. start_watching_hotend(ee);
  514. }
  515. FORCE_INLINE static bool isHeatingHotend(const uint8_t E_NAME) {
  516. return temp_hotend[HOTEND_INDEX].target > temp_hotend[HOTEND_INDEX].celsius;
  517. }
  518. FORCE_INLINE static bool isCoolingHotend(const uint8_t E_NAME) {
  519. return temp_hotend[HOTEND_INDEX].target < temp_hotend[HOTEND_INDEX].celsius;
  520. }
  521. #if HAS_TEMP_HOTEND
  522. static bool wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling=true
  523. #if G26_CLICK_CAN_CANCEL
  524. , const bool click_to_cancel=false
  525. #endif
  526. );
  527. #endif
  528. FORCE_INLINE static bool still_heating(const uint8_t e) {
  529. return degTargetHotend(e) > TEMP_HYSTERESIS && ABS(degHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS;
  530. }
  531. FORCE_INLINE static bool degHotendNear(const uint8_t e, const float &temp) {
  532. return ABS(degHotend(e) - temp) < (TEMP_HYSTERESIS);
  533. }
  534. #endif // HAS_HOTEND
  535. #if HAS_HEATED_BED
  536. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  537. FORCE_INLINE static int16_t rawBedTemp() { return temp_bed.raw; }
  538. #endif
  539. FORCE_INLINE static float degBed() { return temp_bed.celsius; }
  540. FORCE_INLINE static int16_t degTargetBed() { return temp_bed.target; }
  541. FORCE_INLINE static bool isHeatingBed() { return temp_bed.target > temp_bed.celsius; }
  542. FORCE_INLINE static bool isCoolingBed() { return temp_bed.target < temp_bed.celsius; }
  543. #if WATCH_BED
  544. static void start_watching_bed();
  545. #else
  546. static inline void start_watching_bed() {}
  547. #endif
  548. static void setTargetBed(const int16_t celsius) {
  549. TERN_(AUTO_POWER_CONTROL, if (celsius) powerManager.power_on());
  550. temp_bed.target =
  551. #ifdef BED_MAX_TARGET
  552. _MIN(celsius, BED_MAX_TARGET)
  553. #else
  554. celsius
  555. #endif
  556. ;
  557. start_watching_bed();
  558. }
  559. static bool wait_for_bed(const bool no_wait_for_cooling=true
  560. #if G26_CLICK_CAN_CANCEL
  561. , const bool click_to_cancel=false
  562. #endif
  563. );
  564. static void wait_for_bed_heating();
  565. FORCE_INLINE static bool degBedNear(const float &temp) {
  566. return ABS(degBed() - temp) < (TEMP_BED_HYSTERESIS);
  567. }
  568. #endif // HAS_HEATED_BED
  569. #if HAS_TEMP_PROBE
  570. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  571. FORCE_INLINE static int16_t rawProbeTemp() { return temp_probe.raw; }
  572. #endif
  573. FORCE_INLINE static float degProbe() { return temp_probe.celsius; }
  574. FORCE_INLINE static bool isProbeBelowTemp(const float target_temp) { return temp_probe.celsius < target_temp; }
  575. FORCE_INLINE static bool isProbeAboveTemp(const float target_temp) { return temp_probe.celsius > target_temp; }
  576. static bool wait_for_probe(const float target_temp, bool no_wait_for_cooling=true);
  577. #endif
  578. #if WATCH_PROBE
  579. static void start_watching_probe();
  580. #else
  581. static inline void start_watching_probe() {}
  582. #endif
  583. #if HAS_TEMP_CHAMBER
  584. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  585. FORCE_INLINE static int16_t rawChamberTemp() { return temp_chamber.raw; }
  586. #endif
  587. FORCE_INLINE static float degChamber() { return temp_chamber.celsius; }
  588. #if HAS_HEATED_CHAMBER
  589. FORCE_INLINE static int16_t degTargetChamber() { return temp_chamber.target; }
  590. FORCE_INLINE static bool isHeatingChamber() { return temp_chamber.target > temp_chamber.celsius; }
  591. FORCE_INLINE static bool isCoolingChamber() { return temp_chamber.target < temp_chamber.celsius; }
  592. static bool wait_for_chamber(const bool no_wait_for_cooling=true);
  593. #endif
  594. #endif // HAS_TEMP_CHAMBER
  595. #if WATCH_CHAMBER
  596. static void start_watching_chamber();
  597. #else
  598. static inline void start_watching_chamber() {}
  599. #endif
  600. #if HAS_HEATED_CHAMBER
  601. static void setTargetChamber(const int16_t celsius) {
  602. temp_chamber.target =
  603. #ifdef CHAMBER_MAXTEMP
  604. _MIN(celsius, CHAMBER_MAXTEMP - 10)
  605. #else
  606. celsius
  607. #endif
  608. ;
  609. start_watching_chamber();
  610. }
  611. #endif // HAS_HEATED_CHAMBER
  612. /**
  613. * The software PWM power for a heater
  614. */
  615. static int16_t getHeaterPower(const heater_id_t heater_id);
  616. /**
  617. * Switch off all heaters, set all target temperatures to 0
  618. */
  619. static void disable_all_heaters();
  620. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  621. /**
  622. * Methods to check if heaters are enabled, indicating an active job
  623. */
  624. static bool over_autostart_threshold();
  625. static void check_timer_autostart(const bool can_start, const bool can_stop);
  626. #endif
  627. /**
  628. * Perform auto-tuning for hotend or bed in response to M303
  629. */
  630. #if HAS_PID_HEATING
  631. static void PID_autotune(const float &target, const heater_id_t heater_id, const int8_t ncycles, const bool set_result=false);
  632. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  633. static bool adaptive_fan_slowing;
  634. #elif ENABLED(ADAPTIVE_FAN_SLOWING)
  635. static constexpr bool adaptive_fan_slowing = true;
  636. #endif
  637. /**
  638. * Update the temp manager when PID values change
  639. */
  640. #if ENABLED(PIDTEMP)
  641. FORCE_INLINE static void updatePID() {
  642. TERN_(PID_EXTRUSION_SCALING, last_e_position = 0);
  643. }
  644. #endif
  645. #endif
  646. #if ENABLED(PROBING_HEATERS_OFF)
  647. static void pause(const bool p);
  648. FORCE_INLINE static bool is_paused() { return paused; }
  649. #endif
  650. #if HEATER_IDLE_HANDLER
  651. static void reset_hotend_idle_timer(const uint8_t E_NAME) {
  652. heater_idle[HOTEND_INDEX].reset();
  653. start_watching_hotend(HOTEND_INDEX);
  654. }
  655. #if HAS_HEATED_BED
  656. static void reset_bed_idle_timer() {
  657. heater_idle[IDLE_INDEX_BED].reset();
  658. start_watching_bed();
  659. }
  660. #endif
  661. #endif // HEATER_IDLE_HANDLER
  662. #if HAS_TEMP_SENSOR
  663. static void print_heater_states(const uint8_t target_extruder
  664. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  665. , const bool include_r=false
  666. #endif
  667. );
  668. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  669. static uint8_t auto_report_temp_interval;
  670. static millis_t next_temp_report_ms;
  671. static void auto_report_temperatures();
  672. static inline void set_auto_report_interval(uint8_t v) {
  673. NOMORE(v, 60);
  674. auto_report_temp_interval = v;
  675. next_temp_report_ms = millis() + 1000UL * v;
  676. }
  677. #endif
  678. #endif
  679. TERN_(HAS_DISPLAY, static void set_heating_message(const uint8_t e));
  680. #if HAS_LCD_MENU && HAS_TEMPERATURE
  681. static void lcd_preheat(const int16_t e, const int8_t indh, const int8_t indb);
  682. #endif
  683. private:
  684. static void update_raw_temperatures();
  685. static void updateTemperaturesFromRawValues();
  686. #define HAS_MAX6675 EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675)
  687. #if HAS_MAX6675
  688. #define COUNT_6675 1 + BOTH(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675)
  689. #if COUNT_6675 > 1
  690. #define READ_MAX6675(N) read_max6675(N)
  691. #else
  692. #define READ_MAX6675(N) read_max6675()
  693. #endif
  694. static int read_max6675(
  695. #if COUNT_6675 > 1
  696. const uint8_t hindex=0
  697. #endif
  698. );
  699. #endif
  700. static void checkExtruderAutoFans();
  701. static float get_pid_output_hotend(const uint8_t e);
  702. TERN_(PIDTEMPBED, static float get_pid_output_bed());
  703. TERN_(HAS_HEATED_CHAMBER, static float get_pid_output_chamber());
  704. static void _temp_error(const heater_id_t e, PGM_P const serial_msg, PGM_P const lcd_msg);
  705. static void min_temp_error(const heater_id_t e);
  706. static void max_temp_error(const heater_id_t e);
  707. #define HAS_THERMAL_PROTECTION ANY(THERMAL_PROTECTION_HOTENDS, THERMAL_PROTECTION_CHAMBER, HAS_THERMALLY_PROTECTED_BED)
  708. #if HAS_THERMAL_PROTECTION
  709. // Indices and size for the tr_state_machine array. One for each protected heater.
  710. #define _ENUM_FOR_E(N) RUNAWAY_IND_E##N,
  711. enum RunawayIndex : uint8_t {
  712. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  713. REPEAT(HOTENDS, _ENUM_FOR_E)
  714. #endif
  715. #if ENABLED(HAS_THERMALLY_PROTECTED_BED)
  716. RUNAWAY_IND_BED,
  717. #endif
  718. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  719. RUNAWAY_IND_CHAMBER,
  720. #endif
  721. NR_HEATER_RUNAWAY
  722. };
  723. #undef _ENUM_FOR_E
  724. // Convert the given heater_id_t to runaway state array index
  725. static inline RunawayIndex runaway_index_for_id(const int8_t heater_id) {
  726. #if HAS_THERMALLY_PROTECTED_CHAMBER
  727. if (heater_id == H_CHAMBER) return RUNAWAY_IND_CHAMBER;
  728. #endif
  729. #if HAS_THERMALLY_PROTECTED_BED
  730. if (heater_id == H_BED) return RUNAWAY_IND_BED;
  731. #endif
  732. return (RunawayIndex)_MAX(heater_id, 0);
  733. }
  734. enum TRState : char { TRInactive, TRFirstHeating, TRStable, TRRunaway };
  735. typedef struct {
  736. millis_t timer = 0;
  737. TRState state = TRInactive;
  738. float running_temp;
  739. void run(const float &current, const float &target, const heater_id_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc);
  740. } tr_state_machine_t;
  741. static tr_state_machine_t tr_state_machine[NR_HEATER_RUNAWAY];
  742. #endif // HAS_THERMAL_PROTECTION
  743. };
  744. extern Temperature thermalManager;