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

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