My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temperature.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. temperature.c - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. This firmware is optimized for gen6 electronics.
  23. */
  24. #include <avr/pgmspace.h>
  25. #include "fastio.h"
  26. #include "Configuration.h"
  27. #include "pins.h"
  28. #include "Marlin.h"
  29. #include "ultralcd.h"
  30. #include "temperature.h"
  31. #include "watchdog.h"
  32. //===========================================================================
  33. //=============================public variables============================
  34. //===========================================================================
  35. int target_raw[3] = {0, 0, 0};
  36. int current_raw[3] = {0, 0, 0};
  37. #ifdef PIDTEMP
  38. // probably used external
  39. float HeaterPower;
  40. float pid_setpoint = 0.0;
  41. float Kp=DEFAULT_Kp;
  42. float Ki=DEFAULT_Ki;
  43. float Kd=DEFAULT_Kd;
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. //===========================================================================
  49. //=============================private variables============================
  50. //===========================================================================
  51. static bool temp_meas_ready = false;
  52. static unsigned long previous_millis_heater, previous_millis_bed_heater;
  53. #ifdef PIDTEMP
  54. //static cannot be external:
  55. static float temp_iState = 0;
  56. static float temp_dState = 0;
  57. static float pTerm;
  58. static float iTerm;
  59. static float dTerm;
  60. //int output;
  61. static float pid_error;
  62. static float temp_iState_min;
  63. static float temp_iState_max;
  64. static float pid_input;
  65. static float pid_output;
  66. static bool pid_reset;
  67. #endif //PIDTEMP
  68. #ifdef WATCHPERIOD
  69. static int watch_raw[3] = {-1000,-1000,-1000};
  70. static unsigned long watchmillis = 0;
  71. #endif //WATCHPERIOD
  72. #ifdef HEATER_0_MINTEMP
  73. static int minttemp_0 = temp2analog(HEATER_0_MINTEMP);
  74. #endif //MINTEMP
  75. #ifdef HEATER_0_MAXTEMP
  76. static int maxttemp_0 = temp2analog(HEATER_0_MAXTEMP);
  77. #endif //MAXTEMP
  78. #ifdef HEATER_1_MINTEMP
  79. static int minttemp_1 = temp2analog(HEATER_1_MINTEMP);
  80. #endif //MINTEMP
  81. #ifdef HEATER_1_MAXTEMP
  82. static int maxttemp_1 = temp2analog(HEATER_1_MAXTEMP);
  83. #endif //MAXTEMP
  84. #ifdef BED_MINTEMP
  85. static int bed_minttemp = temp2analog(BED_MINTEMP);
  86. #endif //BED_MINTEMP
  87. #ifdef BED_MAXTEMP
  88. static int bed_maxttemp = temp2analog(BED_MAXTEMP);
  89. #endif //BED_MAXTEMP
  90. //===========================================================================
  91. //=============================functions ============================
  92. //===========================================================================
  93. void updatePID()
  94. {
  95. #ifdef PIDTEMP
  96. temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
  97. #endif
  98. }
  99. void manage_heater()
  100. {
  101. #ifdef USE_WATCHDOG
  102. wd_reset();
  103. #endif
  104. float pid_input;
  105. float pid_output;
  106. if(temp_meas_ready != true) //better readability
  107. return;
  108. CRITICAL_SECTION_START;
  109. temp_meas_ready = false;
  110. CRITICAL_SECTION_END;
  111. #ifdef PIDTEMP
  112. pid_input = analog2temp(current_raw[TEMPSENSOR_HOTEND_0]);
  113. #ifndef PID_OPENLOOP
  114. pid_error = pid_setpoint - pid_input;
  115. if(pid_error > 10){
  116. pid_output = PID_MAX;
  117. pid_reset = true;
  118. }
  119. else if(pid_error < -10) {
  120. pid_output = 0;
  121. pid_reset = true;
  122. }
  123. else {
  124. if(pid_reset == true) {
  125. temp_iState = 0.0;
  126. pid_reset = false;
  127. }
  128. pTerm = Kp * pid_error;
  129. temp_iState += pid_error;
  130. temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);
  131. iTerm = Ki * temp_iState;
  132. //K1 defined in Configuration.h in the PID settings
  133. #define K2 (1.0-K1)
  134. dTerm = (Kd * (pid_input - temp_dState))*K2 + (K1 * dTerm);
  135. temp_dState = pid_input;
  136. // #ifdef PID_ADD_EXTRUSION_RATE
  137. // pTerm+=Kc*current_block->speed_e; //additional heating if extrusion speed is high
  138. // #endif
  139. pid_output = constrain(pTerm + iTerm - dTerm, 0, PID_MAX);
  140. }
  141. #endif //PID_OPENLOOP
  142. #ifdef PID_DEBUG
  143. //SERIAL_ECHOLN(" PIDDEBUG Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm<<" iTerm "<<iTerm<<" dTerm "<<dTerm);
  144. #endif //PID_DEBUG
  145. HeaterPower=pid_output;
  146. analogWrite(HEATER_0_PIN, pid_output);
  147. #endif //PIDTEMP
  148. #ifndef PIDTEMP
  149. if(current_raw[0] >= target_raw[0])
  150. {
  151. WRITE(HEATER_0_PIN,LOW);
  152. }
  153. else
  154. {
  155. WRITE(HEATER_0_PIN,HIGH);
  156. }
  157. #endif
  158. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  159. return;
  160. previous_millis_bed_heater = millis();
  161. #if TEMP_1_PIN > -1
  162. if(current_raw[TEMPSENSOR_BED] >= target_raw[TEMPSENSOR_BED])
  163. {
  164. WRITE(HEATER_1_PIN,LOW);
  165. }
  166. else
  167. {
  168. WRITE(HEATER_1_PIN,HIGH);
  169. }
  170. #endif
  171. }
  172. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  173. // Takes hot end temperature value as input and returns corresponding raw value.
  174. // For a thermistor, it uses the RepRap thermistor temp table.
  175. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  176. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  177. int temp2analog(int celsius) {
  178. #ifdef HEATER_0_USES_THERMISTOR
  179. int raw = 0;
  180. byte i;
  181. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  182. {
  183. if (PGM_RD_W(heater_0_temptable[i][1]) < celsius)
  184. {
  185. raw = PGM_RD_W(heater_0_temptable[i-1][0]) +
  186. (celsius - PGM_RD_W(heater_0_temptable[i-1][1])) *
  187. (PGM_RD_W(heater_0_temptable[i][0]) - PGM_RD_W(heater_0_temptable[i-1][0])) /
  188. (PGM_RD_W(heater_0_temptable[i][1]) - PGM_RD_W(heater_0_temptable[i-1][1]));
  189. break;
  190. }
  191. }
  192. // Overflow: Set to last value in the table
  193. if (i == NUMTEMPS_HEATER_0) raw = PGM_RD_W(heater_0_temptable[i-1][0]);
  194. return (1023 * OVERSAMPLENR) - raw;
  195. #elif defined HEATER_0_USES_AD595
  196. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  197. #endif
  198. }
  199. // Takes bed temperature value as input and returns corresponding raw value.
  200. // For a thermistor, it uses the RepRap thermistor temp table.
  201. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  202. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  203. int temp2analogBed(int celsius) {
  204. #ifdef BED_USES_THERMISTOR
  205. int raw = 0;
  206. byte i;
  207. for (i=1; i<BNUMTEMPS; i++)
  208. {
  209. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  210. {
  211. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  212. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  213. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  214. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  215. break;
  216. }
  217. }
  218. // Overflow: Set to last value in the table
  219. if (i == BNUMTEMPS) raw = PGM_RD_W(bedtemptable[i-1][0]);
  220. return (1023 * OVERSAMPLENR) - raw;
  221. #elif defined BED_USES_AD595
  222. return lround(celsius * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  223. #endif
  224. }
  225. // Derived from RepRap FiveD extruder::getTemperature()
  226. // For hot end temperature measurement.
  227. float analog2temp(int raw) {
  228. #ifdef HEATER_0_USES_THERMISTOR
  229. float celsius = 0;
  230. byte i;
  231. raw = (1023 * OVERSAMPLENR) - raw;
  232. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  233. {
  234. if (PGM_RD_W(heater_0_temptable[i][0]) > raw)
  235. {
  236. celsius = PGM_RD_W(heater_0_temptable[i-1][1]) +
  237. (raw - PGM_RD_W(heater_0_temptable[i-1][0])) *
  238. (float)(PGM_RD_W(heater_0_temptable[i][1]) - PGM_RD_W(heater_0_temptable[i-1][1])) /
  239. (float)(PGM_RD_W(heater_0_temptable[i][0]) - PGM_RD_W(heater_0_temptable[i-1][0]));
  240. break;
  241. }
  242. }
  243. // Overflow: Set to last value in the table
  244. if (i == NUMTEMPS_HEATER_0) celsius = PGM_RD_W(heater_0_temptable[i-1][1]);
  245. return celsius;
  246. #elif defined HEATER_0_USES_AD595
  247. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  248. #endif
  249. }
  250. // Derived from RepRap FiveD extruder::getTemperature()
  251. // For bed temperature measurement.
  252. float analog2tempBed(int raw) {
  253. #ifdef BED_USES_THERMISTOR
  254. int celsius = 0;
  255. byte i;
  256. raw = (1023 * OVERSAMPLENR) - raw;
  257. for (i=1; i<BNUMTEMPS; i++)
  258. {
  259. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  260. {
  261. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  262. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  263. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  264. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  265. break;
  266. }
  267. }
  268. // Overflow: Set to last value in the table
  269. if (i == BNUMTEMPS) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  270. return celsius;
  271. #elif defined BED_USES_AD595
  272. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  273. #endif
  274. }
  275. void tp_init()
  276. {
  277. #if (HEATER_0_PIN > -1)
  278. SET_OUTPUT(HEATER_0_PIN);
  279. #endif
  280. #if (HEATER_1_PIN > -1)
  281. SET_OUTPUT(HEATER_1_PIN);
  282. #endif
  283. #if (HEATER_2_PIN > -1)
  284. SET_OUTPUT(HEATER_2_PIN);
  285. #endif
  286. #ifdef PIDTEMP
  287. temp_iState_min = 0.0;
  288. temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
  289. #endif //PIDTEMP
  290. // Set analog inputs
  291. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  292. // Use timer0 for temperature measurement
  293. // Interleave temperature interrupt with millies interrupt
  294. OCR0B = 128;
  295. TIMSK0 |= (1<<OCIE0B);
  296. }
  297. void setWatch()
  298. {
  299. #ifdef WATCHPERIOD
  300. if(isHeatingHotend0())
  301. {
  302. watchmillis = max(1,millis());
  303. watch_raw[TEMPSENSOR_HOTEND_0] = current_raw[TEMPSENSOR_HOTEND_0];
  304. }
  305. else
  306. {
  307. watchmillis = 0;
  308. }
  309. #endif
  310. }
  311. void disable_heater()
  312. {
  313. #if TEMP_0_PIN > -1
  314. target_raw[0]=0;
  315. #if HEATER_0_PIN > -1
  316. WRITE(HEATER_0_PIN,LOW);
  317. #endif
  318. #endif
  319. #if TEMP_1_PIN > -1
  320. target_raw[1]=0;
  321. #if HEATER_1_PIN > -1
  322. WRITE(HEATER_1_PIN,LOW);
  323. #endif
  324. #endif
  325. #if TEMP_2_PIN > -1
  326. target_raw[2]=0;
  327. #if HEATER_2_PIN > -1
  328. WRITE(HEATER_2_PIN,LOW);
  329. #endif
  330. #endif
  331. }
  332. // Timer 0 is shared with millies
  333. ISR(TIMER0_COMPB_vect)
  334. {
  335. //these variables are only accesible from the ISR, but static, so they don't loose their value
  336. static unsigned char temp_count = 0;
  337. static unsigned long raw_temp_0_value = 0;
  338. static unsigned long raw_temp_1_value = 0;
  339. static unsigned long raw_temp_2_value = 0;
  340. static unsigned char temp_state = 0;
  341. switch(temp_state) {
  342. case 0: // Prepare TEMP_0
  343. #if (TEMP_0_PIN > -1)
  344. #if TEMP_0_PIN < 8
  345. DIDR0 = 1 << TEMP_0_PIN;
  346. #else
  347. DIDR2 = 1<<(TEMP_0_PIN - 8);
  348. ADCSRB = 1<<MUX5;
  349. #endif
  350. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  351. ADCSRA |= 1<<ADSC; // Start conversion
  352. #endif
  353. #ifdef ULTIPANEL
  354. buttons_check();
  355. #endif
  356. temp_state = 1;
  357. break;
  358. case 1: // Measure TEMP_0
  359. #if (TEMP_0_PIN > -1)
  360. raw_temp_0_value += ADC;
  361. #endif
  362. temp_state = 2;
  363. break;
  364. case 2: // Prepare TEMP_1
  365. #if (TEMP_1_PIN > -1)
  366. #if TEMP_1_PIN < 7
  367. DIDR0 = 1<<TEMP_1_PIN;
  368. #else
  369. DIDR2 = 1<<(TEMP_1_PIN - 8);
  370. ADCSRB = 1<<MUX5;
  371. #endif
  372. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  373. ADCSRA |= 1<<ADSC; // Start conversion
  374. #endif
  375. #ifdef ULTIPANEL
  376. buttons_check();
  377. #endif
  378. temp_state = 3;
  379. break;
  380. case 3: // Measure TEMP_1
  381. #if (TEMP_1_PIN > -1)
  382. raw_temp_1_value += ADC;
  383. #endif
  384. temp_state = 4;
  385. break;
  386. case 4: // Prepare TEMP_2
  387. #if (TEMP_2_PIN > -1)
  388. #if TEMP_2_PIN < 7
  389. DIDR0 = 1 << TEMP_2_PIN;
  390. #else
  391. DIDR2 = 1<<(TEMP_2_PIN - 8);
  392. ADCSRB = 1<<MUX5;
  393. #endif
  394. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  395. ADCSRA |= 1<<ADSC; // Start conversion
  396. #endif
  397. #ifdef ULTIPANEL
  398. buttons_check();
  399. #endif
  400. temp_state = 5;
  401. break;
  402. case 5: // Measure TEMP_2
  403. #if (TEMP_2_PIN > -1)
  404. raw_temp_2_value += ADC;
  405. #endif
  406. temp_state = 0;
  407. temp_count++;
  408. break;
  409. default:
  410. SERIAL_ERROR_START;
  411. SERIAL_ERRORLNPGM("Temp measurement error!");
  412. break;
  413. }
  414. if(temp_count >= 16) // 6 ms * 16 = 96ms.
  415. {
  416. #ifdef HEATER_0_USES_AD595
  417. current_raw[0] = raw_temp_0_value;
  418. #else
  419. current_raw[0] = 16383 - raw_temp_0_value;
  420. #endif
  421. #ifdef HEATER_1_USES_AD595
  422. current_raw[2] = raw_temp_2_value;
  423. #else
  424. current_raw[2] = 16383 - raw_temp_2_value;
  425. #endif
  426. #ifdef BED_USES_AD595
  427. current_raw[1] = raw_temp_1_value;
  428. #else
  429. current_raw[1] = 16383 - raw_temp_1_value;
  430. #endif
  431. temp_meas_ready = true;
  432. temp_count = 0;
  433. raw_temp_0_value = 0;
  434. raw_temp_1_value = 0;
  435. raw_temp_2_value = 0;
  436. #ifdef HEATER_0_MAXTEMP
  437. #if (HEATER_0_PIN > -1)
  438. if(current_raw[TEMPSENSOR_HOTEND_0] >= maxttemp_0) {
  439. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  440. analogWrite(HEATER_0_PIN, 0);
  441. SERIAL_ERROR_START;
  442. SERIAL_ERRORLNPGM("Temperature extruder 0 switched off. MAXTEMP triggered !!");
  443. kill();
  444. }
  445. #endif
  446. #endif
  447. #ifdef HEATER_1_MAXTEMP
  448. #if (HEATER_1_PIN > -1)
  449. if(current_raw[TEMPSENSOR_HOTEND_1] >= maxttemp_1) {
  450. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  451. if(current_raw[2] >= maxttemp_1) {
  452. analogWrite(HEATER_2_PIN, 0);
  453. SERIAL_ERROR_START;
  454. SERIAL_ERRORLNPGM("Temperature extruder 1 switched off. MAXTEMP triggered !!");
  455. kill()
  456. }
  457. #endif
  458. #endif //MAXTEMP
  459. #ifdef HEATER_0_MINTEMP
  460. #if (HEATER_0_PIN > -1)
  461. if(current_raw[TEMPSENSOR_HOTEND_0] <= minttemp_0) {
  462. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  463. analogWrite(HEATER_0_PIN, 0);
  464. SERIAL_ERROR_START;
  465. SERIAL_ERRORLNPGM("Temperature extruder 0 switched off. MINTEMP triggered !!");
  466. kill();
  467. }
  468. #endif
  469. #endif
  470. #ifdef HEATER_1_MINTEMP
  471. #if (HEATER_2_PIN > -1)
  472. if(current_raw[TEMPSENSOR_HOTEND_1] <= minttemp_1) {
  473. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  474. analogWrite(HEATER_2_PIN, 0);
  475. SERIAL_ERROR_START;
  476. SERIAL_ERRORLNPGM("Temperature extruder 1 switched off. MINTEMP triggered !!");
  477. kill();
  478. }
  479. #endif
  480. #endif //MAXTEMP
  481. #ifdef BED_MINTEMP
  482. #if (HEATER_1_PIN > -1)
  483. if(current_raw[1] <= bed_minttemp) {
  484. target_raw[1] = 0;
  485. WRITE(HEATER_1_PIN, 0);
  486. SERIAL_ERROR_START;
  487. SERIAL_ERRORLNPGM("Temperatur heated bed switched off. MINTEMP triggered !!");
  488. kill();
  489. }
  490. #endif
  491. #endif
  492. #ifdef BED_MAXTEMP
  493. #if (HEATER_1_PIN > -1)
  494. if(current_raw[1] >= bed_maxttemp) {
  495. target_raw[1] = 0;
  496. WRITE(HEATER_1_PIN, 0);
  497. SERIAL_ERROR_START;
  498. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  499. kill();
  500. }
  501. #endif
  502. #endif
  503. }
  504. }