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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. class Temperature {
  36. public:
  37. int current_temperature_raw[EXTRUDERS] = { 0 };
  38. float current_temperature[EXTRUDERS] = { 0.0 };
  39. int target_temperature[EXTRUDERS] = { 0 };
  40. int current_temperature_bed_raw = 0;
  41. float current_temperature_bed = 0.0;
  42. int target_temperature_bed = 0;
  43. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  44. float redundant_temperature = 0.0;
  45. #endif
  46. unsigned char soft_pwm_bed;
  47. #if ENABLED(FAN_SOFT_PWM)
  48. unsigned char fanSpeedSoftPwm[FAN_COUNT];
  49. #endif
  50. #if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
  51. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  52. #endif
  53. #if ENABLED(PIDTEMP)
  54. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  55. static float Kp[EXTRUDERS], Ki[EXTRUDERS], Kd[EXTRUDERS];
  56. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  57. float Kc[EXTRUDERS];
  58. #endif
  59. #define PID_PARAM(param, e) Temperature::param[e]
  60. #else
  61. static float Kp, Ki, Kd;
  62. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  63. static float Kc;
  64. #endif
  65. #define PID_PARAM(param, e) Temperature::param
  66. #endif // PID_PARAMS_PER_EXTRUDER
  67. // Apply the scale factors to the PID values
  68. #define scalePID_i(i) ( (i) * PID_dT )
  69. #define unscalePID_i(i) ( (i) / PID_dT )
  70. #define scalePID_d(d) ( (d) / PID_dT )
  71. #define unscalePID_d(d) ( (d) * PID_dT )
  72. #endif
  73. #if ENABLED(PIDTEMPBED)
  74. float bedKp = DEFAULT_bedKp,
  75. bedKi = ((DEFAULT_bedKi) * PID_dT),
  76. bedKd = ((DEFAULT_bedKd) / PID_dT);
  77. #endif
  78. #if ENABLED(BABYSTEPPING)
  79. volatile int babystepsTodo[3] = { 0 };
  80. #endif
  81. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  82. int watch_target_temp[EXTRUDERS] = { 0 };
  83. millis_t watch_heater_next_ms[EXTRUDERS] = { 0 };
  84. #endif
  85. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_BED_TEMP_PERIOD > 0
  86. int watch_target_bed_temp = 0;
  87. millis_t watch_bed_next_ms = 0;
  88. #endif
  89. #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
  90. float extrude_min_temp = EXTRUDE_MINTEMP;
  91. FORCE_INLINE bool tooColdToExtrude(uint8_t e) { return degHotend(e) < extrude_min_temp; }
  92. #else
  93. FORCE_INLINE bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
  94. #endif
  95. private:
  96. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  97. int redundant_temperature_raw = 0;
  98. float redundant_temperature = 0.0;
  99. #endif
  100. volatile bool temp_meas_ready = false;
  101. #if ENABLED(PIDTEMP)
  102. float temp_iState[EXTRUDERS] = { 0 };
  103. float temp_dState[EXTRUDERS] = { 0 };
  104. float pTerm[EXTRUDERS];
  105. float iTerm[EXTRUDERS];
  106. float dTerm[EXTRUDERS];
  107. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  108. float cTerm[EXTRUDERS];
  109. long last_position[EXTRUDERS];
  110. long lpq[LPQ_MAX_LEN];
  111. int lpq_ptr = 0;
  112. #endif
  113. float pid_error[EXTRUDERS];
  114. float temp_iState_min[EXTRUDERS];
  115. float temp_iState_max[EXTRUDERS];
  116. bool pid_reset[EXTRUDERS];
  117. #endif
  118. #if ENABLED(PIDTEMPBED)
  119. float temp_iState_bed = { 0 };
  120. float temp_dState_bed = { 0 };
  121. float pTerm_bed;
  122. float iTerm_bed;
  123. float dTerm_bed;
  124. float pid_error_bed;
  125. float temp_iState_min_bed;
  126. float temp_iState_max_bed;
  127. #else
  128. millis_t next_bed_check_ms;
  129. #endif
  130. unsigned long raw_temp_value[4] = { 0 };
  131. unsigned long raw_temp_bed_value = 0;
  132. // Init min and max temp with extreme values to prevent false errors during startup
  133. int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  134. int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  135. int minttemp[EXTRUDERS] = { 0 };
  136. int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(16383);
  137. #ifdef BED_MINTEMP
  138. int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  139. #endif
  140. #ifdef BED_MAXTEMP
  141. int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  142. #endif
  143. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  144. int meas_shift_index; // Index of a delayed sample in buffer
  145. #endif
  146. #if HAS_AUTO_FAN
  147. millis_t next_auto_fan_check_ms;
  148. #endif
  149. unsigned char soft_pwm[EXTRUDERS];
  150. #if ENABLED(FAN_SOFT_PWM)
  151. unsigned char soft_pwm_fan[FAN_COUNT];
  152. #endif
  153. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  154. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  155. #endif
  156. public:
  157. /**
  158. * Static (class) methods
  159. */
  160. static float analog2temp(int raw, uint8_t e);
  161. static float analog2tempBed(int raw);
  162. /**
  163. * Instance Methods
  164. */
  165. Temperature();
  166. void init();
  167. /**
  168. * Called from the Temperature ISR
  169. */
  170. void isr();
  171. /**
  172. * Call periodically to manage heaters
  173. */
  174. void manage_heater();
  175. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  176. float analog2widthFil(); // Convert raw Filament Width to millimeters
  177. int widthFil_to_size_ratio(); // Convert raw Filament Width to an extrusion ratio
  178. #endif
  179. //high level conversion routines, for use outside of temperature.cpp
  180. //inline so that there is no performance decrease.
  181. //deg=degreeCelsius
  182. FORCE_INLINE float degHotend(uint8_t extruder) { return current_temperature[extruder]; }
  183. FORCE_INLINE float degBed() { return current_temperature_bed; }
  184. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  185. FORCE_INLINE float rawHotendTemp(uint8_t extruder) { return current_temperature_raw[extruder]; }
  186. FORCE_INLINE float rawBedTemp() { return current_temperature_bed_raw; }
  187. #endif
  188. FORCE_INLINE float degTargetHotend(uint8_t extruder) { return target_temperature[extruder]; }
  189. FORCE_INLINE float degTargetBed() { return target_temperature_bed; }
  190. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  191. void start_watching_heater(int e = 0);
  192. #endif
  193. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  194. void start_watching_bed();
  195. #endif
  196. FORCE_INLINE void setTargetHotend(const float& celsius, uint8_t extruder) {
  197. target_temperature[extruder] = celsius;
  198. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  199. start_watching_heater(extruder);
  200. #endif
  201. }
  202. FORCE_INLINE void setTargetBed(const float& celsius) {
  203. target_temperature_bed = celsius;
  204. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  205. start_watching_bed();
  206. #endif
  207. }
  208. FORCE_INLINE bool isHeatingHotend(uint8_t extruder) { return target_temperature[extruder] > current_temperature[extruder]; }
  209. FORCE_INLINE bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
  210. FORCE_INLINE bool isCoolingHotend(uint8_t extruder) { return target_temperature[extruder] < current_temperature[extruder]; }
  211. FORCE_INLINE bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
  212. /**
  213. * The software PWM power for a heater
  214. */
  215. int getHeaterPower(int heater);
  216. /**
  217. * Switch off all heaters, set all target temperatures to 0
  218. */
  219. void disable_all_heaters();
  220. /**
  221. * Perform auto-tuning for hotend or bed in response to M303
  222. */
  223. #if HAS_PID_HEATING
  224. void PID_autotune(float temp, int extruder, int ncycles, bool set_result=false);
  225. #endif
  226. /**
  227. * Update the temp manager when PID values change
  228. */
  229. void updatePID();
  230. FORCE_INLINE void autotempShutdown() {
  231. #if ENABLED(AUTOTEMP)
  232. if (planner.autotemp_enabled) {
  233. planner.autotemp_enabled = false;
  234. if (degTargetHotend(active_extruder) > planner.autotemp_min)
  235. setTargetHotend(0, active_extruder);
  236. }
  237. #endif
  238. }
  239. #if ENABLED(BABYSTEPPING)
  240. FORCE_INLINE void babystep_axis(AxisEnum axis, int distance) {
  241. #if ENABLED(COREXY) || ENABLED(COREXZ)
  242. #if ENABLED(BABYSTEP_XY)
  243. switch (axis) {
  244. case X_AXIS: // X on CoreXY and CoreXZ
  245. babystepsTodo[A_AXIS] += distance * 2;
  246. babystepsTodo[CORE_AXIS_2] += distance * 2;
  247. break;
  248. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ
  249. babystepsTodo[A_AXIS] += distance * 2;
  250. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  251. break;
  252. case CORE_AXIS_3: // Z on CoreXY, Y on CoreXZ
  253. babystepsTodo[CORE_AXIS_3] += distance;
  254. break;
  255. }
  256. #elif ENABLED(COREXZ)
  257. babystepsTodo[A_AXIS] += distance * 2;
  258. babystepsTodo[C_AXIS] -= distance * 2;
  259. #else
  260. babystepsTodo[Z_AXIS] += distance;
  261. #endif
  262. #else
  263. babystepsTodo[axis] += distance;
  264. #endif
  265. }
  266. #endif // BABYSTEPPING
  267. private:
  268. void set_current_temp_raw();
  269. void updateTemperaturesFromRawValues();
  270. #if ENABLED(HEATER_0_USES_MAX6675)
  271. int read_max6675();
  272. #endif
  273. void checkExtruderAutoFans();
  274. float get_pid_output(int e);
  275. #if ENABLED(PIDTEMPBED)
  276. float get_pid_output_bed();
  277. #endif
  278. void _temp_error(int e, const char* serial_msg, const char* lcd_msg);
  279. void min_temp_error(uint8_t e);
  280. void max_temp_error(uint8_t e);
  281. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  282. typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
  283. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  284. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  285. TRState thermal_runaway_state_machine[EXTRUDERS] = { TRInactive };
  286. millis_t thermal_runaway_timer[EXTRUDERS] = { 0 };
  287. #endif
  288. #if HAS_THERMALLY_PROTECTED_BED
  289. TRState thermal_runaway_bed_state_machine = TRInactive;
  290. millis_t thermal_runaway_bed_timer;
  291. #endif
  292. #endif // THERMAL_PROTECTION
  293. };
  294. extern Temperature thermalManager;
  295. #endif // TEMPERATURE_H