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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
  76. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  77. };
  78. // Minimum number of Temperature::ISR loops between sensor readings.
  79. // Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
  80. // get all oversampled sensor readings
  81. #define MIN_ADC_ISR_LOOPS 10
  82. #define ACTUAL_ADC_SAMPLES max(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
  83. class Temperature {
  84. public:
  85. static float current_temperature[HOTENDS],
  86. current_temperature_bed;
  87. static int16_t current_temperature_raw[HOTENDS],
  88. target_temperature[HOTENDS],
  89. current_temperature_bed_raw,
  90. target_temperature_bed;
  91. static volatile bool in_temp_isr;
  92. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  93. static float redundant_temperature;
  94. #endif
  95. static uint8_t soft_pwm_amount[HOTENDS],
  96. soft_pwm_amount_bed;
  97. #if ENABLED(FAN_SOFT_PWM)
  98. static uint8_t soft_pwm_amount_fan[FAN_COUNT],
  99. soft_pwm_count_fan[FAN_COUNT];
  100. #endif
  101. #if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
  102. #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (F_CPU / 64.0 / 256.0))
  103. #endif
  104. #if ENABLED(PIDTEMP)
  105. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  106. static float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS];
  107. #if ENABLED(PID_EXTRUSION_SCALING)
  108. static float Kc[HOTENDS];
  109. #endif
  110. #define PID_PARAM(param, h) Temperature::param[h]
  111. #else
  112. static float Kp, Ki, Kd;
  113. #if ENABLED(PID_EXTRUSION_SCALING)
  114. static float Kc;
  115. #endif
  116. #define PID_PARAM(param, h) Temperature::param
  117. #endif // PID_PARAMS_PER_HOTEND
  118. // Apply the scale factors to the PID values
  119. #define scalePID_i(i) ( (i) * PID_dT )
  120. #define unscalePID_i(i) ( (i) / PID_dT )
  121. #define scalePID_d(d) ( (d) / PID_dT )
  122. #define unscalePID_d(d) ( (d) * PID_dT )
  123. #endif
  124. #if ENABLED(PIDTEMPBED)
  125. static float bedKp, bedKi, bedKd;
  126. #endif
  127. #if ENABLED(BABYSTEPPING)
  128. static volatile int babystepsTodo[3];
  129. #endif
  130. #if WATCH_HOTENDS
  131. static uint16_t watch_target_temp[HOTENDS];
  132. static millis_t watch_heater_next_ms[HOTENDS];
  133. #endif
  134. #if WATCH_THE_BED
  135. static uint16_t watch_target_bed_temp;
  136. static millis_t watch_bed_next_ms;
  137. #endif
  138. #if ENABLED(PREVENT_COLD_EXTRUSION)
  139. static bool allow_cold_extrude;
  140. static uint16_t extrude_min_temp;
  141. static bool tooColdToExtrude(uint8_t e) {
  142. #if HOTENDS == 1
  143. UNUSED(e);
  144. #endif
  145. return allow_cold_extrude ? false : degHotend(HOTEND_INDEX) < extrude_min_temp;
  146. }
  147. #else
  148. static bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
  149. #endif
  150. private:
  151. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  152. static uint16_t redundant_temperature_raw;
  153. static float redundant_temperature;
  154. #endif
  155. static volatile bool temp_meas_ready;
  156. #if ENABLED(PIDTEMP)
  157. static float temp_iState[HOTENDS],
  158. temp_dState[HOTENDS],
  159. pTerm[HOTENDS],
  160. iTerm[HOTENDS],
  161. dTerm[HOTENDS];
  162. #if ENABLED(PID_EXTRUSION_SCALING)
  163. static float cTerm[HOTENDS];
  164. static long last_e_position;
  165. static long lpq[LPQ_MAX_LEN];
  166. static int lpq_ptr;
  167. #endif
  168. static float pid_error[HOTENDS];
  169. static bool pid_reset[HOTENDS];
  170. #endif
  171. #if ENABLED(PIDTEMPBED)
  172. static float temp_iState_bed,
  173. temp_dState_bed,
  174. pTerm_bed,
  175. iTerm_bed,
  176. dTerm_bed,
  177. pid_error_bed;
  178. #else
  179. static millis_t next_bed_check_ms;
  180. #endif
  181. static uint16_t raw_temp_value[MAX_EXTRUDERS],
  182. raw_temp_bed_value;
  183. // Init min and max temp with extreme values to prevent false errors during startup
  184. static int16_t minttemp_raw[HOTENDS],
  185. maxttemp_raw[HOTENDS],
  186. minttemp[HOTENDS],
  187. maxttemp[HOTENDS];
  188. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  189. static uint8_t consecutive_low_temperature_error[HOTENDS];
  190. #endif
  191. #ifdef MILLISECONDS_PREHEAT_TIME
  192. static millis_t preheat_end_time[HOTENDS];
  193. #endif
  194. #ifdef BED_MINTEMP
  195. static int16_t bed_minttemp_raw;
  196. #endif
  197. #ifdef BED_MAXTEMP
  198. static int16_t bed_maxttemp_raw;
  199. #endif
  200. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  201. static int16_t meas_shift_index; // Index of a delayed sample in buffer
  202. #endif
  203. #if HAS_AUTO_FAN
  204. static millis_t next_auto_fan_check_ms;
  205. #endif
  206. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  207. static int current_raw_filwidth; //Holds measured filament diameter - one extruder only
  208. #endif
  209. #if ENABLED(PROBING_HEATERS_OFF)
  210. static bool paused;
  211. static int16_t paused_hotend_temp[HOTENDS];
  212. #if HAS_TEMP_BED
  213. static int16_t paused_bed_temp;
  214. #endif
  215. #endif
  216. public:
  217. /**
  218. * Instance Methods
  219. */
  220. Temperature();
  221. void init();
  222. /**
  223. * Static (class) methods
  224. */
  225. static float analog2temp(int raw, uint8_t e);
  226. static float analog2tempBed(int raw);
  227. /**
  228. * Called from the Temperature ISR
  229. */
  230. static void isr();
  231. /**
  232. * Call periodically to manage heaters
  233. */
  234. static void manage_heater() _O2; // Added _O2 to work around a compiler error
  235. /**
  236. * Preheating hotends
  237. */
  238. #ifdef MILLISECONDS_PREHEAT_TIME
  239. static bool is_preheating(uint8_t e) {
  240. #if HOTENDS == 1
  241. UNUSED(e);
  242. #endif
  243. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  244. }
  245. static void start_preheat_time(uint8_t e) {
  246. #if HOTENDS == 1
  247. UNUSED(e);
  248. #endif
  249. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  250. }
  251. static void reset_preheat_time(uint8_t e) {
  252. #if HOTENDS == 1
  253. UNUSED(e);
  254. #endif
  255. preheat_end_time[HOTEND_INDEX] = 0;
  256. }
  257. #else
  258. #define is_preheating(n) (false)
  259. #endif
  260. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  261. static float analog2widthFil(); // Convert raw Filament Width to millimeters
  262. static int widthFil_to_size_ratio(); // Convert raw Filament Width to an extrusion ratio
  263. #endif
  264. //high level conversion routines, for use outside of temperature.cpp
  265. //inline so that there is no performance decrease.
  266. //deg=degreeCelsius
  267. static float degHotend(uint8_t e) {
  268. #if HOTENDS == 1
  269. UNUSED(e);
  270. #endif
  271. return current_temperature[HOTEND_INDEX];
  272. }
  273. static float degBed() { return current_temperature_bed; }
  274. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  275. static int16_t rawHotendTemp(uint8_t e) {
  276. #if HOTENDS == 1
  277. UNUSED(e);
  278. #endif
  279. return current_temperature_raw[HOTEND_INDEX];
  280. }
  281. static int16_t rawBedTemp() { return current_temperature_bed_raw; }
  282. #endif
  283. static int16_t degTargetHotend(uint8_t e) {
  284. #if HOTENDS == 1
  285. UNUSED(e);
  286. #endif
  287. return target_temperature[HOTEND_INDEX];
  288. }
  289. static int16_t degTargetBed() { return target_temperature_bed; }
  290. #if WATCH_HOTENDS
  291. static void start_watching_heater(uint8_t e = 0);
  292. #endif
  293. #if WATCH_THE_BED
  294. static void start_watching_bed();
  295. #endif
  296. static void setTargetHotend(const int16_t celsius, uint8_t e) {
  297. #if HOTENDS == 1
  298. UNUSED(e);
  299. #endif
  300. #ifdef MILLISECONDS_PREHEAT_TIME
  301. if (celsius == 0)
  302. reset_preheat_time(HOTEND_INDEX);
  303. else if (target_temperature[HOTEND_INDEX] == 0)
  304. start_preheat_time(HOTEND_INDEX);
  305. #endif
  306. target_temperature[HOTEND_INDEX] = celsius;
  307. #if WATCH_HOTENDS
  308. start_watching_heater(HOTEND_INDEX);
  309. #endif
  310. }
  311. static void setTargetBed(const int16_t celsius) {
  312. target_temperature_bed = celsius;
  313. #if WATCH_THE_BED
  314. start_watching_bed();
  315. #endif
  316. }
  317. static bool isHeatingHotend(uint8_t e) {
  318. #if HOTENDS == 1
  319. UNUSED(e);
  320. #endif
  321. return target_temperature[HOTEND_INDEX] > current_temperature[HOTEND_INDEX];
  322. }
  323. static bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
  324. static bool isCoolingHotend(uint8_t e) {
  325. #if HOTENDS == 1
  326. UNUSED(e);
  327. #endif
  328. return target_temperature[HOTEND_INDEX] < current_temperature[HOTEND_INDEX];
  329. }
  330. static bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
  331. /**
  332. * The software PWM power for a heater
  333. */
  334. static int getHeaterPower(int heater);
  335. /**
  336. * Switch off all heaters, set all target temperatures to 0
  337. */
  338. static void disable_all_heaters();
  339. /**
  340. * Perform auto-tuning for hotend or bed in response to M303
  341. */
  342. #if HAS_PID_HEATING
  343. static void PID_autotune(float temp, int hotend, int ncycles, bool set_result=false);
  344. #endif
  345. /**
  346. * Update the temp manager when PID values change
  347. */
  348. static void updatePID();
  349. #if ENABLED(BABYSTEPPING)
  350. static void babystep_axis(const AxisEnum axis, const int distance) {
  351. if (axis_known_position[axis]) {
  352. #if IS_CORE
  353. #if ENABLED(BABYSTEP_XY)
  354. switch (axis) {
  355. case CORE_AXIS_1: // X on CoreXY and CoreXZ, Y on CoreYZ
  356. babystepsTodo[CORE_AXIS_1] += distance * 2;
  357. babystepsTodo[CORE_AXIS_2] += distance * 2;
  358. break;
  359. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ and CoreYZ
  360. babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
  361. babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
  362. break;
  363. case NORMAL_AXIS: // Z on CoreXY, Y on CoreXZ, X on CoreYZ
  364. babystepsTodo[NORMAL_AXIS] += distance;
  365. break;
  366. }
  367. #elif CORE_IS_XZ || CORE_IS_YZ
  368. // Only Z stepping needs to be handled here
  369. babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
  370. babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
  371. #else
  372. babystepsTodo[Z_AXIS] += distance;
  373. #endif
  374. #else
  375. babystepsTodo[axis] += distance;
  376. #endif
  377. }
  378. }
  379. #endif // BABYSTEPPING
  380. #if ENABLED(PROBING_HEATERS_OFF)
  381. static void pause(const bool p);
  382. #endif
  383. private:
  384. static void set_current_temp_raw();
  385. static void updateTemperaturesFromRawValues();
  386. #if ENABLED(HEATER_0_USES_MAX6675)
  387. static int read_max6675();
  388. #endif
  389. static void checkExtruderAutoFans();
  390. static float get_pid_output(int e);
  391. #if ENABLED(PIDTEMPBED)
  392. static float get_pid_output_bed();
  393. #endif
  394. static void _temp_error(int e, const char* serial_msg, const char* lcd_msg);
  395. static void min_temp_error(int8_t e);
  396. static void max_temp_error(int8_t e);
  397. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  398. typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
  399. static void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  400. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  401. static TRState thermal_runaway_state_machine[HOTENDS];
  402. static millis_t thermal_runaway_timer[HOTENDS];
  403. #endif
  404. #if HAS_THERMALLY_PROTECTED_BED
  405. static TRState thermal_runaway_bed_state_machine;
  406. static millis_t thermal_runaway_bed_timer;
  407. #endif
  408. #endif // THERMAL_PROTECTION
  409. };
  410. extern Temperature thermalManager;
  411. #endif // TEMPERATURE_H