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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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[EXTRUDERS];
  38. static float current_temperature[EXTRUDERS];
  39. static int target_temperature[EXTRUDERS];
  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_EXTRUDER)
  55. static float Kp[EXTRUDERS], Ki[EXTRUDERS], Kd[EXTRUDERS];
  56. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  57. static 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. 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[EXTRUDERS];
  81. static millis_t watch_heater_next_ms[EXTRUDERS];
  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[EXTRUDERS];
  101. static float temp_dState[EXTRUDERS];
  102. static float pTerm[EXTRUDERS];
  103. static float iTerm[EXTRUDERS];
  104. static float dTerm[EXTRUDERS];
  105. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  106. static float cTerm[EXTRUDERS];
  107. static long last_position[EXTRUDERS];
  108. static long lpq[LPQ_MAX_LEN];
  109. static int lpq_ptr;
  110. #endif
  111. static float pid_error[EXTRUDERS];
  112. static float temp_iState_min[EXTRUDERS];
  113. static float temp_iState_max[EXTRUDERS];
  114. static bool pid_reset[EXTRUDERS];
  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[EXTRUDERS];
  132. static int maxttemp_raw[EXTRUDERS];
  133. static int minttemp[EXTRUDERS];
  134. static int maxttemp[EXTRUDERS];
  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[EXTRUDERS];
  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. static float degHotend(uint8_t extruder) { return current_temperature[extruder]; }
  181. static float degBed() { return current_temperature_bed; }
  182. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  183. static float rawHotendTemp(uint8_t extruder) { return current_temperature_raw[extruder]; }
  184. static float rawBedTemp() { return current_temperature_bed_raw; }
  185. #endif
  186. static float degTargetHotend(uint8_t extruder) { return target_temperature[extruder]; }
  187. static float degTargetBed() { return target_temperature_bed; }
  188. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  189. static void start_watching_heater(int e = 0);
  190. #endif
  191. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  192. static void start_watching_bed();
  193. #endif
  194. static void setTargetHotend(const float& celsius, uint8_t extruder) {
  195. target_temperature[extruder] = celsius;
  196. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  197. start_watching_heater(extruder);
  198. #endif
  199. }
  200. static void setTargetBed(const float& celsius) {
  201. target_temperature_bed = celsius;
  202. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  203. start_watching_bed();
  204. #endif
  205. }
  206. static bool isHeatingHotend(uint8_t extruder) { return target_temperature[extruder] > current_temperature[extruder]; }
  207. static bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
  208. static bool isCoolingHotend(uint8_t extruder) { return target_temperature[extruder] < current_temperature[extruder]; }
  209. static bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
  210. /**
  211. * The software PWM power for a heater
  212. */
  213. static int getHeaterPower(int heater);
  214. /**
  215. * Switch off all heaters, set all target temperatures to 0
  216. */
  217. static void disable_all_heaters();
  218. /**
  219. * Perform auto-tuning for hotend or bed in response to M303
  220. */
  221. #if HAS_PID_HEATING
  222. static void PID_autotune(float temp, int extruder, int ncycles, bool set_result=false);
  223. #endif
  224. /**
  225. * Update the temp manager when PID values change
  226. */
  227. static void updatePID();
  228. static void autotempShutdown() {
  229. #if ENABLED(AUTOTEMP)
  230. if (planner.autotemp_enabled) {
  231. planner.autotemp_enabled = false;
  232. if (degTargetHotend(active_extruder) > planner.autotemp_min)
  233. setTargetHotend(0, active_extruder);
  234. }
  235. #endif
  236. }
  237. #if ENABLED(BABYSTEPPING)
  238. static void babystep_axis(AxisEnum axis, int distance) {
  239. #if ENABLED(COREXY) || ENABLED(COREXZ) || ENABLED(COREYZ)
  240. #if ENABLED(BABYSTEP_XY)
  241. switch (axis) {
  242. case CORE_AXIS_1: // X on CoreXY and CoreXZ, Y on CoreYZ
  243. babystepsTodo[CORE_AXIS_1] += distance * 2;
  244. babystepsTodo[CORE_AXIS_2] += distance * 2;
  245. break;
  246. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ and CoreYZ
  247. babystepsTodo[CORE_AXIS_1] += distance * 2;
  248. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  249. break;
  250. case NORMAL_AXIS: // Z on CoreXY, Y on CoreXZ, X on CoreYZ
  251. babystepsTodo[NORMAL_AXIS] += distance;
  252. break;
  253. }
  254. #elif ENABLED(COREXZ) || ENABLED(COREYZ)
  255. // Only Z stepping needs to be handled here
  256. babystepsTodo[CORE_AXIS_1] += distance * 2;
  257. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  258. #else
  259. babystepsTodo[Z_AXIS] += distance;
  260. #endif
  261. #else
  262. babystepsTodo[axis] += distance;
  263. #endif
  264. }
  265. #endif // BABYSTEPPING
  266. private:
  267. static void set_current_temp_raw();
  268. static void updateTemperaturesFromRawValues();
  269. #if ENABLED(HEATER_0_USES_MAX6675)
  270. static int read_max6675();
  271. #endif
  272. static void checkExtruderAutoFans();
  273. static float get_pid_output(int e);
  274. #if ENABLED(PIDTEMPBED)
  275. static float get_pid_output_bed();
  276. #endif
  277. static void _temp_error(int e, const char* serial_msg, const char* lcd_msg);
  278. static void min_temp_error(uint8_t e);
  279. static void max_temp_error(uint8_t e);
  280. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  281. typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
  282. static void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  283. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  284. static TRState thermal_runaway_state_machine[EXTRUDERS];
  285. static millis_t thermal_runaway_timer[EXTRUDERS];
  286. #endif
  287. #if HAS_THERMALLY_PROTECTED_BED
  288. static TRState thermal_runaway_bed_state_machine;
  289. static millis_t thermal_runaway_bed_timer;
  290. #endif
  291. #endif // THERMAL_PROTECTION
  292. };
  293. extern Temperature thermalManager;
  294. #endif // TEMPERATURE_H