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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "Marlin.h"
  28. #include "planner.h"
  29. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  30. #include "stepper.h"
  31. #endif
  32. #ifndef SOFT_PWM_SCALE
  33. #define SOFT_PWM_SCALE 0
  34. #endif
  35. #if HOTENDS == 1
  36. #define HOTEND_LOOP() const uint8_t e = 0;
  37. #define HOTEND_INDEX 0
  38. #define EXTRUDER_IDX 0
  39. #else
  40. #define HOTEND_LOOP() for (int8_t e = 0; e < HOTENDS; e++)
  41. #define HOTEND_INDEX e
  42. #define EXTRUDER_IDX active_extruder
  43. #endif
  44. class Temperature {
  45. public:
  46. static int current_temperature_raw[HOTENDS];
  47. static float current_temperature[HOTENDS];
  48. static int target_temperature[HOTENDS];
  49. static int current_temperature_bed_raw;
  50. static float current_temperature_bed;
  51. static int target_temperature_bed;
  52. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  53. static float redundant_temperature;
  54. #endif
  55. static unsigned char soft_pwm_bed;
  56. #if ENABLED(FAN_SOFT_PWM)
  57. static unsigned char fanSpeedSoftPwm[FAN_COUNT];
  58. #endif
  59. #if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
  60. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  61. #endif
  62. #if ENABLED(PIDTEMP)
  63. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  64. static float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS];
  65. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  66. static float Kc[HOTENDS];
  67. #endif
  68. #define PID_PARAM(param, h) Temperature::param[h]
  69. #else
  70. static float Kp, Ki, Kd;
  71. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  72. static float Kc;
  73. #endif
  74. #define PID_PARAM(param, h) Temperature::param
  75. #endif // PID_PARAMS_PER_HOTEND
  76. // Apply the scale factors to the PID values
  77. #define scalePID_i(i) ( (i) * PID_dT )
  78. #define unscalePID_i(i) ( (i) / PID_dT )
  79. #define scalePID_d(d) ( (d) / PID_dT )
  80. #define unscalePID_d(d) ( (d) * PID_dT )
  81. #endif
  82. #if ENABLED(PIDTEMPBED)
  83. static float bedKp, bedKi, bedKd;
  84. #endif
  85. #if ENABLED(BABYSTEPPING)
  86. static volatile int babystepsTodo[3];
  87. #endif
  88. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  89. static int watch_target_temp[HOTENDS];
  90. static millis_t watch_heater_next_ms[HOTENDS];
  91. #endif
  92. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  93. static int watch_target_bed_temp;
  94. static millis_t watch_bed_next_ms;
  95. #endif
  96. #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
  97. static float extrude_min_temp;
  98. static bool tooColdToExtrude(uint8_t e) {
  99. #if HOTENDS == 1
  100. UNUSED(e);
  101. #endif
  102. return degHotend(HOTEND_INDEX) < extrude_min_temp;
  103. }
  104. #else
  105. static bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
  106. #endif
  107. private:
  108. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  109. static int redundant_temperature_raw;
  110. static float redundant_temperature;
  111. #endif
  112. static volatile bool temp_meas_ready;
  113. #if ENABLED(PIDTEMP)
  114. static float temp_iState[HOTENDS];
  115. static float temp_dState[HOTENDS];
  116. static float pTerm[HOTENDS];
  117. static float iTerm[HOTENDS];
  118. static float dTerm[HOTENDS];
  119. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  120. static float cTerm[HOTENDS];
  121. static long last_e_position;
  122. static long lpq[LPQ_MAX_LEN];
  123. static int lpq_ptr;
  124. #endif
  125. static float pid_error[HOTENDS];
  126. static float temp_iState_min[HOTENDS];
  127. static float temp_iState_max[HOTENDS];
  128. static bool pid_reset[HOTENDS];
  129. #endif
  130. #if ENABLED(PIDTEMPBED)
  131. static float temp_iState_bed;
  132. static float temp_dState_bed;
  133. static float pTerm_bed;
  134. static float iTerm_bed;
  135. static float dTerm_bed;
  136. static float pid_error_bed;
  137. static float temp_iState_min_bed;
  138. static float temp_iState_max_bed;
  139. #else
  140. static millis_t next_bed_check_ms;
  141. #endif
  142. static unsigned long raw_temp_value[4];
  143. static unsigned long raw_temp_bed_value;
  144. // Init min and max temp with extreme values to prevent false errors during startup
  145. static int minttemp_raw[HOTENDS];
  146. static int maxttemp_raw[HOTENDS];
  147. static int minttemp[HOTENDS];
  148. static int maxttemp[HOTENDS];
  149. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  150. static int consecutive_low_temperature_error[HOTENDS];
  151. #endif
  152. #ifdef MILLISECONDS_PREHEAT_TIME
  153. static unsigned long preheat_end_time[HOTENDS];
  154. #endif
  155. #ifdef BED_MINTEMP
  156. static int bed_minttemp_raw;
  157. #endif
  158. #ifdef BED_MAXTEMP
  159. static int bed_maxttemp_raw;
  160. #endif
  161. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  162. static int meas_shift_index; // Index of a delayed sample in buffer
  163. #endif
  164. #if HAS_AUTO_FAN
  165. static millis_t next_auto_fan_check_ms;
  166. #endif
  167. static unsigned char soft_pwm[HOTENDS];
  168. #if ENABLED(FAN_SOFT_PWM)
  169. static unsigned char soft_pwm_fan[FAN_COUNT];
  170. #endif
  171. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  172. static int current_raw_filwidth; //Holds measured filament diameter - one extruder only
  173. #endif
  174. public:
  175. /**
  176. * Instance Methods
  177. */
  178. Temperature();
  179. void init();
  180. /**
  181. * Static (class) methods
  182. */
  183. static float analog2temp(int raw, uint8_t e);
  184. static float analog2tempBed(int raw);
  185. /**
  186. * Called from the Temperature ISR
  187. */
  188. static void isr();
  189. /**
  190. * Call periodically to manage heaters
  191. */
  192. static void manage_heater();
  193. /**
  194. * Preheating hotends
  195. */
  196. #ifdef MILLISECONDS_PREHEAT_TIME
  197. static bool is_preheating(uint8_t e) {
  198. #if HOTENDS == 1
  199. UNUSED(e);
  200. #endif
  201. return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
  202. }
  203. static void start_preheat_time(uint8_t e) {
  204. #if HOTENDS == 1
  205. UNUSED(e);
  206. #endif
  207. preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
  208. }
  209. static void reset_preheat_time(uint8_t e) {
  210. #if HOTENDS == 1
  211. UNUSED(e);
  212. #endif
  213. preheat_end_time[HOTEND_INDEX] = 0;
  214. }
  215. #else
  216. #define is_preheating(n) (false)
  217. #endif
  218. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  219. static float analog2widthFil(); // Convert raw Filament Width to millimeters
  220. static int widthFil_to_size_ratio(); // Convert raw Filament Width to an extrusion ratio
  221. #endif
  222. //high level conversion routines, for use outside of temperature.cpp
  223. //inline so that there is no performance decrease.
  224. //deg=degreeCelsius
  225. static float degHotend(uint8_t e) {
  226. #if HOTENDS == 1
  227. UNUSED(e);
  228. #endif
  229. return current_temperature[HOTEND_INDEX];
  230. }
  231. static float degBed() { return current_temperature_bed; }
  232. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  233. static float rawHotendTemp(uint8_t e) {
  234. #if HOTENDS == 1
  235. UNUSED(e);
  236. #endif
  237. return current_temperature_raw[HOTEND_INDEX];
  238. }
  239. static float rawBedTemp() { return current_temperature_bed_raw; }
  240. #endif
  241. static float degTargetHotend(uint8_t e) {
  242. #if HOTENDS == 1
  243. UNUSED(e);
  244. #endif
  245. return target_temperature[HOTEND_INDEX];
  246. }
  247. static float degTargetBed() { return target_temperature_bed; }
  248. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  249. static void start_watching_heater(uint8_t e = 0);
  250. #endif
  251. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  252. static void start_watching_bed();
  253. #endif
  254. static void setTargetHotend(const float& celsius, uint8_t e) {
  255. #if HOTENDS == 1
  256. UNUSED(e);
  257. #endif
  258. #ifdef MILLISECONDS_PREHEAT_TIME
  259. if (celsius == 0.0f)
  260. reset_preheat_time(HOTEND_INDEX);
  261. else if (target_temperature[HOTEND_INDEX] == 0.0f)
  262. start_preheat_time(HOTEND_INDEX);
  263. #endif
  264. target_temperature[HOTEND_INDEX] = celsius;
  265. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  266. start_watching_heater(HOTEND_INDEX);
  267. #endif
  268. }
  269. static void setTargetBed(const float& celsius) {
  270. target_temperature_bed = celsius;
  271. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  272. start_watching_bed();
  273. #endif
  274. }
  275. static bool isHeatingHotend(uint8_t e) {
  276. #if HOTENDS == 1
  277. UNUSED(e);
  278. #endif
  279. return target_temperature[HOTEND_INDEX] > current_temperature[HOTEND_INDEX];
  280. }
  281. static bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
  282. static bool isCoolingHotend(uint8_t e) {
  283. #if HOTENDS == 1
  284. UNUSED(e);
  285. #endif
  286. return target_temperature[HOTEND_INDEX] < current_temperature[HOTEND_INDEX];
  287. }
  288. static bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
  289. /**
  290. * The software PWM power for a heater
  291. */
  292. static int getHeaterPower(int heater);
  293. /**
  294. * Switch off all heaters, set all target temperatures to 0
  295. */
  296. static void disable_all_heaters();
  297. /**
  298. * Perform auto-tuning for hotend or bed in response to M303
  299. */
  300. #if HAS_PID_HEATING
  301. static void PID_autotune(float temp, int hotend, int ncycles, bool set_result=false);
  302. #endif
  303. /**
  304. * Update the temp manager when PID values change
  305. */
  306. static void updatePID();
  307. static void autotempShutdown() {
  308. #if ENABLED(AUTOTEMP)
  309. if (planner.autotemp_enabled) {
  310. planner.autotemp_enabled = false;
  311. if (degTargetHotend(EXTRUDER_IDX) > planner.autotemp_min)
  312. setTargetHotend(0, EXTRUDER_IDX);
  313. }
  314. #endif
  315. }
  316. #if ENABLED(BABYSTEPPING)
  317. static void babystep_axis(AxisEnum axis, int distance) {
  318. #if ENABLED(COREXY) || ENABLED(COREXZ) || ENABLED(COREYZ)
  319. #if ENABLED(BABYSTEP_XY)
  320. switch (axis) {
  321. case CORE_AXIS_1: // X on CoreXY and CoreXZ, Y on CoreYZ
  322. babystepsTodo[CORE_AXIS_1] += distance * 2;
  323. babystepsTodo[CORE_AXIS_2] += distance * 2;
  324. break;
  325. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ and CoreYZ
  326. babystepsTodo[CORE_AXIS_1] += distance * 2;
  327. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  328. break;
  329. case NORMAL_AXIS: // Z on CoreXY, Y on CoreXZ, X on CoreYZ
  330. babystepsTodo[NORMAL_AXIS] += distance;
  331. break;
  332. }
  333. #elif ENABLED(COREXZ) || ENABLED(COREYZ)
  334. // Only Z stepping needs to be handled here
  335. babystepsTodo[CORE_AXIS_1] += distance * 2;
  336. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  337. #else
  338. babystepsTodo[Z_AXIS] += distance;
  339. #endif
  340. #else
  341. babystepsTodo[axis] += distance;
  342. #endif
  343. }
  344. #endif // BABYSTEPPING
  345. private:
  346. static void set_current_temp_raw();
  347. static void updateTemperaturesFromRawValues();
  348. #if ENABLED(HEATER_0_USES_MAX6675)
  349. static int read_max6675();
  350. #endif
  351. static void checkExtruderAutoFans();
  352. static float get_pid_output(int e);
  353. #if ENABLED(PIDTEMPBED)
  354. static float get_pid_output_bed();
  355. #endif
  356. static void _temp_error(int e, const char* serial_msg, const char* lcd_msg);
  357. static void min_temp_error(uint8_t e);
  358. static void max_temp_error(uint8_t e);
  359. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  360. typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
  361. static void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  362. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  363. static TRState thermal_runaway_state_machine[HOTENDS];
  364. static millis_t thermal_runaway_timer[HOTENDS];
  365. #endif
  366. #if HAS_THERMALLY_PROTECTED_BED
  367. static TRState thermal_runaway_bed_state_machine;
  368. static millis_t thermal_runaway_bed_timer;
  369. #endif
  370. #endif // THERMAL_PROTECTION
  371. };
  372. extern Temperature thermalManager;
  373. #endif // TEMPERATURE_H