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

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