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

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