My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.h 12KB

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