My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.cpp 13KB

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