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.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. // Takes hot end temperature value as input and returns corresponding raw value.
  173. // For a thermistor, it uses the RepRap thermistor temp table.
  174. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  175. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  176. int temp2analog(int celsius) {
  177. #ifdef HEATER_0_USES_THERMISTOR
  178. int raw = 0;
  179. byte i;
  180. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  181. {
  182. if (pgm_read_word(&(heater_0_temptable[i][1])) < celsius)
  183. {
  184. raw = pgm_read_word(&(heater_0_temptable[i-1][0])) +
  185. (celsius - pgm_read_word(&(heater_0_temptable[i-1][1]))) *
  186. (pgm_read_word(&(heater_0_temptable[i][0])) - pgm_read_word(&(heater_0_temptable[i-1][0]))) /
  187. (pgm_read_word(&(heater_0_temptable[i][1])) - pgm_read_word(&(heater_0_temptable[i-1][1])));
  188. break;
  189. }
  190. }
  191. // Overflow: Set to last value in the table
  192. if (i == NUMTEMPS_HEATER_0) raw = pgm_read_word(&(heater_0_temptable[i-1][0]));
  193. return (1023 * OVERSAMPLENR) - raw;
  194. #elif defined HEATER_0_USES_AD595
  195. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  196. #endif
  197. }
  198. // Takes bed temperature value as input and returns corresponding raw value.
  199. // For a thermistor, it uses the RepRap thermistor temp table.
  200. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  201. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  202. int temp2analogBed(int celsius) {
  203. #ifdef BED_USES_THERMISTOR
  204. int raw = 0;
  205. byte i;
  206. for (i=1; i<BNUMTEMPS; i++)
  207. {
  208. if (pgm_read_word(&)bedtemptable[i][1])) < celsius)
  209. {
  210. raw = pgm_read_word(&(bedtemptable[i-1][0])) +
  211. (celsius - pgm_read_word(&(bedtemptable[i-1][1]))) *
  212. (pgm_read_word(&(bedtemptable[i][0])) - pgm_read_word(&(bedtemptable[i-1][0]))) /
  213. (pgm_read_word(&(bedtemptable[i][1])) - pgm_read_word(&(bedtemptable[i-1][1])));
  214. break;
  215. }
  216. }
  217. // Overflow: Set to last value in the table
  218. if (i == BNUMTEMPS) raw = pgm_read_word(&(bedtemptable[i-1][0]));
  219. return (1023 * OVERSAMPLENR) - raw;
  220. #elif defined BED_USES_AD595
  221. return lround(celsius * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  222. #endif
  223. }
  224. // Derived from RepRap FiveD extruder::getTemperature()
  225. // For hot end temperature measurement.
  226. float analog2temp(int raw) {
  227. #ifdef HEATER_0_USES_THERMISTOR
  228. float celsius = 0;
  229. byte i;
  230. raw = (1023 * OVERSAMPLENR) - raw;
  231. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  232. {
  233. if ((short)pgm_read_word(&heater_0_temptable[i][0]) > raw)
  234. {
  235. celsius = (short)pgm_read_word(&heater_0_temptable[i-1][1]) +
  236. (raw - (short)pgm_read_word(&heater_0_temptable[i-1][0])) *
  237. (float)((short)pgm_read_word(&heater_0_temptable[i][1]) - (short)pgm_read_word(&heater_0_temptable[i-1][1])) /
  238. (float)((short)pgm_read_word(&heater_0_temptable[i][0]) - (short)pgm_read_word(&heater_0_temptable[i-1][0]));
  239. break;
  240. }
  241. }
  242. // Overflow: Set to last value in the table
  243. if (i == NUMTEMPS_HEATER_0) celsius = (short)pgm_read_word(&(heater_0_temptable[i-1][1]));
  244. return celsius;
  245. #elif defined HEATER_0_USES_AD595
  246. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  247. #endif
  248. }
  249. // Derived from RepRap FiveD extruder::getTemperature()
  250. // For bed temperature measurement.
  251. float analog2tempBed(int raw) {
  252. #ifdef BED_USES_THERMISTOR
  253. int celsius = 0;
  254. byte i;
  255. raw = (1023 * OVERSAMPLENR) - raw;
  256. for (i=1; i<BNUMTEMPS; i++)
  257. {
  258. if (pgm_read_word(&(bedtemptable[i][0])) > raw)
  259. {
  260. celsius = pgm_read_word(&(bedtemptable[i-1][1])) +
  261. (raw - pgm_read_word(&(bedtemptable[i-1][0]))) *
  262. (pgm_read_word(&(bedtemptable[i][1])) - pgm_read_word(&(bedtemptable[i-1][1]))) /
  263. (pgm_read_word(&(bedtemptable[i][0])) - pgm_read_word(&(bedtemptable[i-1][0])));
  264. break;
  265. }
  266. }
  267. // Overflow: Set to last value in the table
  268. if (i == BNUMTEMPS) celsius = pgm_read_word(&(bedtemptable[i-1][1]));
  269. return celsius;
  270. #elif defined BED_USES_AD595
  271. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  272. #endif
  273. }
  274. void tp_init()
  275. {
  276. #if (HEATER_0_PIN > -1)
  277. SET_OUTPUT(HEATER_0_PIN);
  278. #endif
  279. #if (HEATER_1_PIN > -1)
  280. SET_OUTPUT(HEATER_1_PIN);
  281. #endif
  282. #if (HEATER_2_PIN > -1)
  283. SET_OUTPUT(HEATER_2_PIN);
  284. #endif
  285. #ifdef PIDTEMP
  286. temp_iState_min = 0.0;
  287. temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
  288. #endif //PIDTEMP
  289. // Set analog inputs
  290. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  291. // Use timer0 for temperature measurement
  292. // Interleave temperature interrupt with millies interrupt
  293. OCR0B = 128;
  294. TIMSK0 |= (1<<OCIE0B);
  295. }
  296. void setWatch()
  297. {
  298. #ifdef WATCHPERIOD
  299. if(isHeatingHotend0())
  300. {
  301. watchmillis = max(1,millis());
  302. watch_raw[TEMPSENSOR_HOTEND_0] = current_raw[TEMPSENSOR_HOTEND_0];
  303. }
  304. else
  305. {
  306. watchmillis = 0;
  307. }
  308. #endif
  309. }
  310. void disable_heater()
  311. {
  312. #if TEMP_0_PIN > -1
  313. target_raw[0]=0;
  314. #if HEATER_0_PIN > -1
  315. WRITE(HEATER_0_PIN,LOW);
  316. #endif
  317. #endif
  318. #if TEMP_1_PIN > -1
  319. target_raw[1]=0;
  320. #if HEATER_1_PIN > -1
  321. WRITE(HEATER_1_PIN,LOW);
  322. #endif
  323. #endif
  324. #if TEMP_2_PIN > -1
  325. target_raw[2]=0;
  326. #if HEATER_2_PIN > -1
  327. WRITE(HEATER_2_PIN,LOW);
  328. #endif
  329. #endif
  330. }
  331. // Timer 0 is shared with millies
  332. ISR(TIMER0_COMPB_vect)
  333. {
  334. //these variables are only accesible from the ISR, but static, so they don't loose their value
  335. static unsigned char temp_count = 0;
  336. static unsigned long raw_temp_0_value = 0;
  337. static unsigned long raw_temp_1_value = 0;
  338. static unsigned long raw_temp_2_value = 0;
  339. static unsigned char temp_state = 0;
  340. switch(temp_state) {
  341. case 0: // Prepare TEMP_0
  342. #if (TEMP_0_PIN > -1)
  343. #if TEMP_0_PIN < 8
  344. DIDR0 = 1 << TEMP_0_PIN;
  345. #else
  346. DIDR2 = 1<<(TEMP_0_PIN - 8);
  347. ADCSRB = 1<<MUX5;
  348. #endif
  349. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  350. ADCSRA |= 1<<ADSC; // Start conversion
  351. #endif
  352. #ifdef ULTIPANEL
  353. buttons_check();
  354. #endif
  355. temp_state = 1;
  356. break;
  357. case 1: // Measure TEMP_0
  358. #if (TEMP_0_PIN > -1)
  359. raw_temp_0_value += ADC;
  360. #endif
  361. temp_state = 2;
  362. break;
  363. case 2: // Prepare TEMP_1
  364. #if (TEMP_1_PIN > -1)
  365. #if TEMP_1_PIN < 7
  366. DIDR0 = 1<<TEMP_1_PIN;
  367. #else
  368. DIDR2 = 1<<(TEMP_1_PIN - 8);
  369. ADCSRB = 1<<MUX5;
  370. #endif
  371. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  372. ADCSRA |= 1<<ADSC; // Start conversion
  373. #endif
  374. #ifdef ULTIPANEL
  375. buttons_check();
  376. #endif
  377. temp_state = 3;
  378. break;
  379. case 3: // Measure TEMP_1
  380. #if (TEMP_1_PIN > -1)
  381. raw_temp_1_value += ADC;
  382. #endif
  383. temp_state = 4;
  384. break;
  385. case 4: // Prepare TEMP_2
  386. #if (TEMP_2_PIN > -1)
  387. #if TEMP_2_PIN < 7
  388. DIDR0 = 1 << TEMP_2_PIN;
  389. #else
  390. DIDR2 = 1<<(TEMP_2_PIN - 8);
  391. ADCSRB = 1<<MUX5;
  392. #endif
  393. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  394. ADCSRA |= 1<<ADSC; // Start conversion
  395. #endif
  396. #ifdef ULTIPANEL
  397. buttons_check();
  398. #endif
  399. temp_state = 5;
  400. break;
  401. case 5: // Measure TEMP_2
  402. #if (TEMP_2_PIN > -1)
  403. raw_temp_2_value += ADC;
  404. #endif
  405. temp_state = 0;
  406. temp_count++;
  407. break;
  408. default:
  409. SERIAL_ERROR_START;
  410. SERIAL_ERRORLNPGM("Temp measurement error!");
  411. break;
  412. }
  413. if(temp_count >= 16) // 6 ms * 16 = 96ms.
  414. {
  415. #ifdef HEATER_0_USES_AD595
  416. current_raw[0] = raw_temp_0_value;
  417. #else
  418. current_raw[0] = 16383 - raw_temp_0_value;
  419. #endif
  420. #ifdef HEATER_1_USES_AD595
  421. current_raw[2] = raw_temp_2_value;
  422. #else
  423. current_raw[2] = 16383 - raw_temp_2_value;
  424. #endif
  425. #ifdef BED_USES_AD595
  426. current_raw[1] = raw_temp_1_value;
  427. #else
  428. current_raw[1] = 16383 - raw_temp_1_value;
  429. #endif
  430. temp_meas_ready = true;
  431. temp_count = 0;
  432. raw_temp_0_value = 0;
  433. raw_temp_1_value = 0;
  434. raw_temp_2_value = 0;
  435. #ifdef HEATER_0_MAXTEMP
  436. #if (HEATER_0_PIN > -1)
  437. if(current_raw[TEMPSENSOR_HOTEND_0] >= maxttemp_0) {
  438. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  439. analogWrite(HEATER_0_PIN, 0);
  440. SERIAL_ERROR_START;
  441. SERIAL_ERRORLNPGM("Temperature extruder 0 switched off. MAXTEMP triggered !!");
  442. kill();
  443. }
  444. #endif
  445. #endif
  446. #ifdef HEATER_1_MAXTEMP
  447. #if (HEATER_1_PIN > -1)
  448. if(current_raw[TEMPSENSOR_HOTEND_1] >= maxttemp_1) {
  449. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  450. if(current_raw[2] >= maxttemp_1) {
  451. analogWrite(HEATER_2_PIN, 0);
  452. SERIAL_ERROR_START;
  453. SERIAL_ERRORLNPGM("Temperature extruder 1 switched off. MAXTEMP triggered !!");
  454. kill()
  455. }
  456. #endif
  457. #endif //MAXTEMP
  458. #ifdef HEATER_0_MINTEMP
  459. #if (HEATER_0_PIN > -1)
  460. if(current_raw[TEMPSENSOR_HOTEND_0] <= minttemp_0) {
  461. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  462. analogWrite(HEATER_0_PIN, 0);
  463. SERIAL_ERROR_START;
  464. SERIAL_ERRORLNPGM("Temperature extruder 0 switched off. MINTEMP triggered !!");
  465. kill();
  466. }
  467. #endif
  468. #endif
  469. #ifdef HEATER_1_MINTEMP
  470. #if (HEATER_2_PIN > -1)
  471. if(current_raw[TEMPSENSOR_HOTEND_1] <= minttemp_1) {
  472. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  473. analogWrite(HEATER_2_PIN, 0);
  474. SERIAL_ERROR_START;
  475. SERIAL_ERRORLNPGM("Temperature extruder 1 switched off. MINTEMP triggered !!");
  476. kill();
  477. }
  478. #endif
  479. #endif //MAXTEMP
  480. #ifdef BED_MINTEMP
  481. #if (HEATER_1_PIN > -1)
  482. if(current_raw[1] <= bed_minttemp) {
  483. target_raw[1] = 0;
  484. WRITE(HEATER_1_PIN, 0);
  485. SERIAL_ERROR_START;
  486. SERIAL_ERRORLNPGM("Temperatur heated bed switched off. MINTEMP triggered !!");
  487. kill();
  488. }
  489. #endif
  490. #endif
  491. #ifdef BED_MAXTEMP
  492. #if (HEATER_1_PIN > -1)
  493. if(current_raw[1] >= bed_maxttemp) {
  494. target_raw[1] = 0;
  495. WRITE(HEATER_1_PIN, 0);
  496. SERIAL_ERROR_START;
  497. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  498. kill();
  499. }
  500. #endif
  501. #endif
  502. }
  503. }