My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

temperature.h 13KB

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