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

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