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

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