My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

temperature.h 14KB

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