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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * temperature.h - temperature controller
  24. */
  25. #ifndef TEMPERATURE_H
  26. #define TEMPERATURE_H
  27. #include "thermistortables.h"
  28. #include "MarlinConfig.h"
  29. #if ENABLED(PID_EXTRUSION_SCALING)
  30. #include "stepper.h"
  31. #endif
  32. #ifndef SOFT_PWM_SCALE
  33. #define SOFT_PWM_SCALE 0
  34. #endif
  35. #define HOTEND_LOOP() for (int8_t e = 0; e < HOTENDS; e++)
  36. #if HOTENDS == 1
  37. #define HOTEND_INDEX 0
  38. #define EXTRUDER_IDX 0
  39. #else
  40. #define HOTEND_INDEX e
  41. #define EXTRUDER_IDX active_extruder
  42. #endif
  43. /**
  44. * States for ADC reading in the ISR
  45. */
  46. enum ADCSensorState {
  47. #if HAS_TEMP_0
  48. PrepareTemp_0,
  49. MeasureTemp_0,
  50. #endif
  51. #if HAS_TEMP_1
  52. PrepareTemp_1,
  53. MeasureTemp_1,
  54. #endif
  55. #if HAS_TEMP_2
  56. PrepareTemp_2,
  57. MeasureTemp_2,
  58. #endif
  59. #if HAS_TEMP_3
  60. PrepareTemp_3,
  61. MeasureTemp_3,
  62. #endif
  63. #if HAS_TEMP_4
  64. PrepareTemp_4,
  65. MeasureTemp_4,
  66. #endif
  67. #if HAS_TEMP_BED
  68. PrepareTemp_BED,
  69. MeasureTemp_BED,
  70. #endif
  71. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  72. Prepare_FILWIDTH,
  73. Measure_FILWIDTH,
  74. #endif
  75. #if ENABLED(ADC_KEYPAD)
  76. Prepare_ADC_KEY,
  77. Measure_ADC_KEY,
  78. #endif
  79. SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
  80. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  81. };
  82. // Minimum number of Temperature::ISR loops between sensor readings.
  83. // Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
  84. // get all oversampled sensor readings
  85. #define MIN_ADC_ISR_LOOPS 10
  86. #define ACTUAL_ADC_SAMPLES max(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
  87. #if HAS_PID_HEATING
  88. #define PID_K2 (1.0-PID_K1)
  89. #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (F_CPU / 64.0 / 256.0))
  90. // Apply the scale factors to the PID values
  91. #define scalePID_i(i) ( (i) * PID_dT )
  92. #define unscalePID_i(i) ( (i) / PID_dT )
  93. #define scalePID_d(d) ( (d) / PID_dT )
  94. #define unscalePID_d(d) ( (d) * PID_dT )
  95. #endif
  96. #if !HAS_HEATER_BED
  97. constexpr int16_t target_temperature_bed = 0;
  98. #endif
  99. class Temperature {
  100. public:
  101. static float current_temperature[HOTENDS],
  102. current_temperature_bed;
  103. static int16_t current_temperature_raw[HOTENDS],
  104. target_temperature[HOTENDS],
  105. current_temperature_bed_raw;
  106. #if HAS_HEATER_BED
  107. static int16_t target_temperature_bed;
  108. #endif
  109. static volatile bool in_temp_isr;
  110. static uint8_t soft_pwm_amount[HOTENDS],
  111. soft_pwm_amount_bed;
  112. #if ENABLED(FAN_SOFT_PWM)
  113. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  114. soft_pwm_count_fan[FAN_COUNT];
  115. #endif
  116. #if ENABLED(PIDTEMP)
  117. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  118. static float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS];
  119. #if ENABLED(PID_EXTRUSION_SCALING)
  120. static float Kc[HOTENDS];
  121. #endif
  122. #define PID_PARAM(param, h) Temperature::param[h]
  123. #else
  124. static float Kp, Ki, Kd;
  125. #if ENABLED(PID_EXTRUSION_SCALING)
  126. static float Kc;
  127. #endif
  128. #define PID_PARAM(param, h) Temperature::param
  129. #endif // PID_PARAMS_PER_HOTEND
  130. #endif
  131. #if ENABLED(PIDTEMPBED)
  132. static float bedKp, bedKi, bedKd;
  133. #endif
  134. #if ENABLED(BABYSTEPPING)
  135. static volatile int babystepsTodo[3];
  136. #endif
  137. #if WATCH_HOTENDS
  138. static uint16_t watch_target_temp[HOTENDS];
  139. static millis_t watch_heater_next_ms[HOTENDS];
  140. #endif
  141. #if WATCH_THE_BED
  142. static uint16_t watch_target_bed_temp;
  143. static millis_t watch_bed_next_ms;
  144. #endif
  145. #if ENABLED(PREVENT_COLD_EXTRUSION)
  146. static bool allow_cold_extrude;
  147. static int16_t extrude_min_temp;
  148. static bool tooColdToExtrude(uint8_t e) {
  149. #if HOTENDS == 1
  150. UNUSED(e);
  151. #endif
  152. return allow_cold_extrude ? false : degHotend(HOTEND_INDEX) < extrude_min_temp;
  153. }
  154. #else
  155. static bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
  156. #endif
  157. private:
  158. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  159. static uint16_t redundant_temperature_raw;
  160. static float redundant_temperature;
  161. #endif
  162. static volatile bool temp_meas_ready;
  163. #if ENABLED(PIDTEMP)
  164. static float temp_iState[HOTENDS],
  165. temp_dState[HOTENDS],
  166. pTerm[HOTENDS],
  167. iTerm[HOTENDS],
  168. dTerm[HOTENDS];
  169. #if ENABLED(PID_EXTRUSION_SCALING)
  170. static float cTerm[HOTENDS];
  171. static long last_e_position;
  172. static long lpq[LPQ_MAX_LEN];
  173. static int lpq_ptr;
  174. #endif
  175. static float pid_error[HOTENDS];
  176. static bool pid_reset[HOTENDS];
  177. #endif
  178. #if ENABLED(PIDTEMPBED)
  179. static float temp_iState_bed,
  180. temp_dState_bed,
  181. pTerm_bed,
  182. iTerm_bed,
  183. dTerm_bed,
  184. pid_error_bed;
  185. #else
  186. static millis_t next_bed_check_ms;
  187. #endif
  188. static uint16_t raw_temp_value[MAX_EXTRUDERS],
  189. raw_temp_bed_value;
  190. // Init min and max temp with extreme values to prevent false errors during startup
  191. static int16_t minttemp_raw[HOTENDS],
  192. maxttemp_raw[HOTENDS],
  193. minttemp[HOTENDS],
  194. maxttemp[HOTENDS];
  195. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  196. static uint8_t consecutive_low_temperature_error[HOTENDS];
  197. #endif
  198. #ifdef MILLISECONDS_PREHEAT_TIME
  199. static millis_t preheat_end_time[HOTENDS];
  200. #endif
  201. #ifdef BED_MINTEMP
  202. static int16_t bed_minttemp_raw;
  203. #endif
  204. #ifdef BED_MAXTEMP
  205. static int16_t bed_maxttemp_raw;
  206. #endif
  207. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  208. static int8_t meas_shift_index; // Index of a delayed sample in buffer
  209. #endif
  210. #if HAS_AUTO_FAN
  211. static millis_t next_auto_fan_check_ms;
  212. #endif
  213. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  214. static uint16_t current_raw_filwidth; // Measured filament diameter - one extruder only
  215. #endif
  216. #if ENABLED(PROBING_HEATERS_OFF)
  217. static bool paused;
  218. #endif
  219. #if HEATER_IDLE_HANDLER
  220. static millis_t heater_idle_timeout_ms[HOTENDS];
  221. static bool heater_idle_timeout_exceeded[HOTENDS];
  222. #if HAS_TEMP_BED
  223. static millis_t bed_idle_timeout_ms;
  224. static bool bed_idle_timeout_exceeded;
  225. #endif
  226. #endif
  227. public:
  228. #if ENABLED(ADC_KEYPAD)
  229. static uint32_t current_ADCKey_raw;
  230. static uint8_t ADCKey_count;
  231. #endif
  232. /**
  233. * Instance Methods
  234. */
  235. Temperature();
  236. void init();
  237. /**
  238. * Static (class) methods
  239. */
  240. static float analog2temp(const int raw, const uint8_t e);
  241. #if HAS_TEMP_BED
  242. static float analog2tempBed(const int raw);
  243. #endif
  244. /**
  245. * Called from the Temperature ISR
  246. */
  247. static void isr();
  248. /**
  249. * Call periodically to manage heaters
  250. */
  251. static void manage_heater() _O2; // Added _O2 to work around a compiler error
  252. /**
  253. * Preheating hotends
  254. */
  255. #ifdef MILLISECONDS_PREHEAT_TIME
  256. static bool is_preheating(uint8_t e) {
  257. #if HOTENDS == 1
  258. UNUSED(e);
  259. #endif
  260. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  261. }
  262. static void start_preheat_time(uint8_t e) {
  263. #if HOTENDS == 1
  264. UNUSED(e);
  265. #endif
  266. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  267. }
  268. static void reset_preheat_time(uint8_t e) {
  269. #if HOTENDS == 1
  270. UNUSED(e);
  271. #endif
  272. preheat_end_time[HOTEND_INDEX] = 0;
  273. }
  274. #else
  275. #define is_preheating(n) (false)
  276. #endif
  277. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  278. static float analog2widthFil(); // Convert raw Filament Width to millimeters
  279. static int8_t widthFil_to_size_ratio(); // Convert Filament Width (mm) to an extrusion ratio
  280. #endif
  281. //high level conversion routines, for use outside of temperature.cpp
  282. //inline so that there is no performance decrease.
  283. //deg=degreeCelsius
  284. static float degHotend(uint8_t e) {
  285. #if HOTENDS == 1
  286. UNUSED(e);
  287. #endif
  288. return current_temperature[HOTEND_INDEX];
  289. }
  290. static float degBed() { return current_temperature_bed; }
  291. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  292. static int16_t rawHotendTemp(uint8_t e) {
  293. #if HOTENDS == 1
  294. UNUSED(e);
  295. #endif
  296. return current_temperature_raw[HOTEND_INDEX];
  297. }
  298. static int16_t rawBedTemp() { return current_temperature_bed_raw; }
  299. #endif
  300. static int16_t degTargetHotend(uint8_t e) {
  301. #if HOTENDS == 1
  302. UNUSED(e);
  303. #endif
  304. return target_temperature[HOTEND_INDEX];
  305. }
  306. static int16_t degTargetBed() { return target_temperature_bed; }
  307. #if WATCH_HOTENDS
  308. static void start_watching_heater(const uint8_t e = 0);
  309. #endif
  310. #if WATCH_THE_BED
  311. static void start_watching_bed();
  312. #endif
  313. static void setTargetHotend(const int16_t celsius, const uint8_t e) {
  314. #if HOTENDS == 1
  315. UNUSED(e);
  316. #endif
  317. #ifdef MILLISECONDS_PREHEAT_TIME
  318. if (celsius == 0)
  319. reset_preheat_time(HOTEND_INDEX);
  320. else if (target_temperature[HOTEND_INDEX] == 0)
  321. start_preheat_time(HOTEND_INDEX);
  322. #endif
  323. target_temperature[HOTEND_INDEX] = celsius;
  324. #if WATCH_HOTENDS
  325. start_watching_heater(HOTEND_INDEX);
  326. #endif
  327. }
  328. static void setTargetBed(const int16_t celsius) {
  329. #if HAS_HEATER_BED
  330. target_temperature_bed =
  331. #ifdef BED_MAXTEMP
  332. min(celsius, BED_MAXTEMP)
  333. #else
  334. celsius
  335. #endif
  336. ;
  337. #if WATCH_THE_BED
  338. start_watching_bed();
  339. #endif
  340. #endif
  341. }
  342. static bool isHeatingHotend(uint8_t e) {
  343. #if HOTENDS == 1
  344. UNUSED(e);
  345. #endif
  346. return target_temperature[HOTEND_INDEX] > current_temperature[HOTEND_INDEX];
  347. }
  348. static bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
  349. static bool isCoolingHotend(uint8_t e) {
  350. #if HOTENDS == 1
  351. UNUSED(e);
  352. #endif
  353. return target_temperature[HOTEND_INDEX] < current_temperature[HOTEND_INDEX];
  354. }
  355. static bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
  356. /**
  357. * The software PWM power for a heater
  358. */
  359. static int getHeaterPower(int heater);
  360. /**
  361. * Switch off all heaters, set all target temperatures to 0
  362. */
  363. static void disable_all_heaters();
  364. /**
  365. * Perform auto-tuning for hotend or bed in response to M303
  366. */
  367. #if HAS_PID_HEATING
  368. static void PID_autotune(const float temp, const int8_t hotend, const int8_t ncycles, const bool set_result=false);
  369. /**
  370. * Update the temp manager when PID values change
  371. */
  372. #if ENABLED(PIDTEMP)
  373. FORCE_INLINE static void updatePID() {
  374. #if ENABLED(PID_EXTRUSION_SCALING)
  375. last_e_position = 0;
  376. #endif
  377. }
  378. #endif
  379. #endif
  380. #if ENABLED(BABYSTEPPING)
  381. static void babystep_axis(const AxisEnum axis, const int16_t distance) {
  382. if (axis_known_position[axis]) {
  383. #if IS_CORE
  384. #if ENABLED(BABYSTEP_XY)
  385. switch (axis) {
  386. case CORE_AXIS_1: // X on CoreXY and CoreXZ, Y on CoreYZ
  387. babystepsTodo[CORE_AXIS_1] += distance * 2;
  388. babystepsTodo[CORE_AXIS_2] += distance * 2;
  389. break;
  390. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ and CoreYZ
  391. babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
  392. babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
  393. break;
  394. case NORMAL_AXIS: // Z on CoreXY, Y on CoreXZ, X on CoreYZ
  395. babystepsTodo[NORMAL_AXIS] += distance;
  396. break;
  397. }
  398. #elif CORE_IS_XZ || CORE_IS_YZ
  399. // Only Z stepping needs to be handled here
  400. babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
  401. babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
  402. #else
  403. babystepsTodo[Z_AXIS] += distance;
  404. #endif
  405. #else
  406. babystepsTodo[axis] += distance;
  407. #endif
  408. }
  409. }
  410. #endif // BABYSTEPPING
  411. #if ENABLED(PROBING_HEATERS_OFF)
  412. static void pause(const bool p);
  413. static bool is_paused() { return paused; }
  414. #endif
  415. #if HEATER_IDLE_HANDLER
  416. static void start_heater_idle_timer(uint8_t e, millis_t timeout_ms) {
  417. #if HOTENDS == 1
  418. UNUSED(e);
  419. #endif
  420. heater_idle_timeout_ms[HOTEND_INDEX] = millis() + timeout_ms;
  421. heater_idle_timeout_exceeded[HOTEND_INDEX] = false;
  422. }
  423. static void reset_heater_idle_timer(uint8_t e) {
  424. #if HOTENDS == 1
  425. UNUSED(e);
  426. #endif
  427. heater_idle_timeout_ms[HOTEND_INDEX] = 0;
  428. heater_idle_timeout_exceeded[HOTEND_INDEX] = false;
  429. #if WATCH_HOTENDS
  430. start_watching_heater(HOTEND_INDEX);
  431. #endif
  432. }
  433. static bool is_heater_idle(uint8_t e) {
  434. #if HOTENDS == 1
  435. UNUSED(e);
  436. #endif
  437. return heater_idle_timeout_exceeded[HOTEND_INDEX];
  438. }
  439. #if HAS_TEMP_BED
  440. static void start_bed_idle_timer(millis_t timeout_ms) {
  441. bed_idle_timeout_ms = millis() + timeout_ms;
  442. bed_idle_timeout_exceeded = false;
  443. }
  444. static void reset_bed_idle_timer() {
  445. bed_idle_timeout_ms = 0;
  446. bed_idle_timeout_exceeded = false;
  447. #if WATCH_THE_BED
  448. start_watching_bed();
  449. #endif
  450. }
  451. static bool is_bed_idle() {
  452. return bed_idle_timeout_exceeded;
  453. }
  454. #endif
  455. #endif
  456. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  457. static void print_heaterstates();
  458. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  459. static uint8_t auto_report_temp_interval;
  460. static millis_t next_temp_report_ms;
  461. static void auto_report_temperatures(void);
  462. FORCE_INLINE void set_auto_report_interval(uint8_t v) {
  463. NOMORE(v, 60);
  464. auto_report_temp_interval = v;
  465. next_temp_report_ms = millis() + 1000UL * v;
  466. }
  467. #endif
  468. #endif
  469. private:
  470. static void set_current_temp_raw();
  471. static void updateTemperaturesFromRawValues();
  472. #if ENABLED(HEATER_0_USES_MAX6675)
  473. static int read_max6675();
  474. #endif
  475. static void checkExtruderAutoFans();
  476. static float get_pid_output(const int8_t e);
  477. #if ENABLED(PIDTEMPBED)
  478. static float get_pid_output_bed();
  479. #endif
  480. static void _temp_error(const int8_t e, const char * const serial_msg, const char * const lcd_msg);
  481. static void min_temp_error(const int8_t e);
  482. static void max_temp_error(const int8_t e);
  483. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  484. typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
  485. static void thermal_runaway_protection(TRState * const state, millis_t * const timer, const float current, const float target, const int8_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc);
  486. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  487. static TRState thermal_runaway_state_machine[HOTENDS];
  488. static millis_t thermal_runaway_timer[HOTENDS];
  489. #endif
  490. #if HAS_THERMALLY_PROTECTED_BED
  491. static TRState thermal_runaway_bed_state_machine;
  492. static millis_t thermal_runaway_bed_timer;
  493. #endif
  494. #endif // THERMAL_PROTECTION
  495. };
  496. extern Temperature thermalManager;
  497. #endif // TEMPERATURE_H