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

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