My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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_ECHOLN(" PIDDEBUG Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm<<" iTerm "<<iTerm<<" dTerm "<<dTerm);
  123. #endif //PID_DEBUG
  124. analogWrite(HEATER_0_PIN, pid_output);
  125. #endif //PIDTEMP
  126. #ifndef PIDTEMP
  127. if(current_raw[0] >= target_raw[0])
  128. {
  129. WRITE(HEATER_0_PIN,LOW);
  130. }
  131. else
  132. {
  133. WRITE(HEATER_0_PIN,HIGH);
  134. }
  135. #endif
  136. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  137. return;
  138. previous_millis_bed_heater = millis();
  139. #if TEMP_1_PIN > -1
  140. if(current_raw[TEMPSENSOR_BED] >= target_raw[TEMPSENSOR_BED])
  141. {
  142. WRITE(HEATER_1_PIN,LOW);
  143. }
  144. else
  145. {
  146. WRITE(HEATER_1_PIN,HIGH);
  147. }
  148. #endif
  149. }
  150. // Takes hot end temperature value as input and returns corresponding raw value.
  151. // For a thermistor, it uses the RepRap thermistor temp table.
  152. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  153. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  154. int temp2analog(int celsius) {
  155. #ifdef HEATER_0_USES_THERMISTOR
  156. int raw = 0;
  157. byte i;
  158. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  159. {
  160. if (heater_0_temptable[i][1] < celsius)
  161. {
  162. raw = heater_0_temptable[i-1][0] +
  163. (celsius - heater_0_temptable[i-1][1]) *
  164. (heater_0_temptable[i][0] - heater_0_temptable[i-1][0]) /
  165. (heater_0_temptable[i][1] - heater_0_temptable[i-1][1]);
  166. break;
  167. }
  168. }
  169. // Overflow: Set to last value in the table
  170. if (i == NUMTEMPS_0) raw = heater_0_temptable[i-1][0];
  171. return (1023 * OVERSAMPLENR) - raw;
  172. #elif defined HEATER_0_USES_AD595
  173. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  174. #endif
  175. }
  176. // Takes bed temperature value as input and returns corresponding raw value.
  177. // For a thermistor, it uses the RepRap thermistor temp table.
  178. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  179. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  180. int temp2analogBed(int celsius) {
  181. #ifdef BED_USES_THERMISTOR
  182. int raw = 0;
  183. byte i;
  184. for (i=1; i<BNUMTEMPS; i++)
  185. {
  186. if (bedtemptable[i][1] < celsius)
  187. {
  188. raw = bedtemptable[i-1][0] +
  189. (celsius - bedtemptable[i-1][1]) *
  190. (bedtemptable[i][0] - bedtemptable[i-1][0]) /
  191. (bedtemptable[i][1] - bedtemptable[i-1][1]);
  192. break;
  193. }
  194. }
  195. // Overflow: Set to last value in the table
  196. if (i == BNUMTEMPS) raw = bedtemptable[i-1][0];
  197. return (1023 * OVERSAMPLENR) - raw;
  198. #elif defined BED_USES_AD595
  199. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  200. #endif
  201. }
  202. // Derived from RepRap FiveD extruder::getTemperature()
  203. // For hot end temperature measurement.
  204. float analog2temp(int raw) {
  205. #ifdef HEATER_0_USES_THERMISTOR
  206. float celsius = 0;
  207. byte i;
  208. raw = (1023 * OVERSAMPLENR) - raw;
  209. for (i=1; i<NUMTEMPS_HEATER_0; i++)
  210. {
  211. if (heater_0_temptable[i][0] > raw)
  212. {
  213. celsius = heater_0_temptable[i-1][1] +
  214. (raw - heater_0_temptable[i-1][0]) *
  215. (float)(heater_0_temptable[i][1] - heater_0_temptable[i-1][1]) /
  216. (float)(heater_0_temptable[i][0] - heater_0_temptable[i-1][0]);
  217. break;
  218. }
  219. }
  220. // Overflow: Set to last value in the table
  221. if (i == NUMTEMPS_HEATER_0) celsius = heater_0_temptable[i-1][1];
  222. return celsius;
  223. #elif defined HEATER_0_USES_AD595
  224. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  225. #endif
  226. }
  227. // Derived from RepRap FiveD extruder::getTemperature()
  228. // For bed temperature measurement.
  229. float analog2tempBed(int raw) {
  230. #ifdef BED_USES_THERMISTOR
  231. int celsius = 0;
  232. byte i;
  233. raw = (1023 * OVERSAMPLENR) - raw;
  234. for (i=1; i<BNUMTEMPS; i++)
  235. {
  236. if (bedtemptable[i][0] > raw)
  237. {
  238. celsius = bedtemptable[i-1][1] +
  239. (raw - bedtemptable[i-1][0]) *
  240. (bedtemptable[i][1] - bedtemptable[i-1][1]) /
  241. (bedtemptable[i][0] - bedtemptable[i-1][0]);
  242. break;
  243. }
  244. }
  245. // Overflow: Set to last value in the table
  246. if (i == BNUMTEMPS) celsius = bedtemptable[i-1][1];
  247. return celsius;
  248. #elif defined BED_USES_AD595
  249. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  250. #endif
  251. }
  252. void tp_init()
  253. {
  254. #if (HEATER_0_PIN > -1)
  255. SET_OUTPUT(HEATER_0_PIN);
  256. #endif
  257. #if (HEATER_1_PIN > -1)
  258. SET_OUTPUT(HEATER_1_PIN);
  259. #endif
  260. #if (HEATER_2_PIN > -1)
  261. SET_OUTPUT(HEATER_2_PIN);
  262. #endif
  263. #ifdef PIDTEMP
  264. temp_iState_min = 0.0;
  265. temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
  266. #endif //PIDTEMP
  267. // Set analog inputs
  268. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  269. // Use timer0 for temperature measurement
  270. // Interleave temperature interrupt with millies interrupt
  271. OCR0B = 128;
  272. TIMSK0 |= (1<<OCIE0B);
  273. }
  274. void setWatch()
  275. {
  276. #ifdef WATCHPERIOD
  277. if(isHeatingHotend0())
  278. {
  279. watchmillis = max(1,millis());
  280. watch_raw[TEMPSENSOR_HOTEND_0] = current_raw[TEMPSENSOR_HOTEND_0];
  281. }
  282. else
  283. {
  284. watchmillis = 0;
  285. }
  286. #endif
  287. }
  288. void disable_heater()
  289. {
  290. #if TEMP_0_PIN > -1
  291. target_raw[0]=0;
  292. #if HEATER_0_PIN > -1
  293. WRITE(HEATER_0_PIN,LOW);
  294. #endif
  295. #endif
  296. #if TEMP_1_PIN > -1
  297. target_raw[1]=0;
  298. #if HEATER_1_PIN > -1
  299. WRITE(HEATER_1_PIN,LOW);
  300. #endif
  301. #endif
  302. #if TEMP_2_PIN > -1
  303. target_raw[2]=0;
  304. #if HEATER_2_PIN > -1
  305. WRITE(HEATER_2_PIN,LOW);
  306. #endif
  307. #endif
  308. }
  309. // Timer 0 is shared with millies
  310. ISR(TIMER0_COMPB_vect)
  311. {
  312. //these variables are only accesible from the ISR, but static, so they don't loose their value
  313. static unsigned char temp_count = 0;
  314. static unsigned long raw_temp_0_value = 0;
  315. static unsigned long raw_temp_1_value = 0;
  316. static unsigned long raw_temp_2_value = 0;
  317. static unsigned char temp_state = 0;
  318. switch(temp_state) {
  319. case 0: // Prepare TEMP_0
  320. #if (TEMP_0_PIN > -1)
  321. #if TEMP_0_PIN < 8
  322. DIDR0 = 1 << TEMP_0_PIN;
  323. #else
  324. DIDR2 = 1<<(TEMP_0_PIN - 8);
  325. ADCSRB = 1<<MUX5;
  326. #endif
  327. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  328. ADCSRA |= 1<<ADSC; // Start conversion
  329. #endif
  330. #ifdef ULTIPANEL
  331. buttons_check();
  332. #endif
  333. temp_state = 1;
  334. break;
  335. case 1: // Measure TEMP_0
  336. #if (TEMP_0_PIN > -1)
  337. raw_temp_0_value += ADC;
  338. #endif
  339. temp_state = 2;
  340. break;
  341. case 2: // Prepare TEMP_1
  342. #if (TEMP_1_PIN > -1)
  343. #if TEMP_1_PIN < 7
  344. DIDR0 = 1<<TEMP_1_PIN;
  345. #else
  346. DIDR2 = 1<<(TEMP_1_PIN - 8);
  347. ADCSRB = 1<<MUX5;
  348. #endif
  349. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  350. ADCSRA |= 1<<ADSC; // Start conversion
  351. #endif
  352. #ifdef ULTIPANEL
  353. buttons_check();
  354. #endif
  355. temp_state = 3;
  356. break;
  357. case 3: // Measure TEMP_1
  358. #if (TEMP_1_PIN > -1)
  359. raw_temp_1_value += ADC;
  360. #endif
  361. temp_state = 4;
  362. break;
  363. case 4: // Prepare TEMP_2
  364. #if (TEMP_2_PIN > -1)
  365. #if TEMP_2_PIN < 7
  366. DIDR0 = 1 << TEMP_2_PIN;
  367. #else
  368. DIDR2 = 1<<(TEMP_2_PIN - 8);
  369. ADCSRB = 1<<MUX5;
  370. #endif
  371. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  372. ADCSRA |= 1<<ADSC; // Start conversion
  373. #endif
  374. #ifdef ULTIPANEL
  375. buttons_check();
  376. #endif
  377. temp_state = 5;
  378. break;
  379. case 5: // Measure TEMP_2
  380. #if (TEMP_2_PIN > -1)
  381. raw_temp_2_value += ADC;
  382. #endif
  383. temp_state = 0;
  384. temp_count++;
  385. break;
  386. default:
  387. SERIAL_ERRORLN("Temp measurement error!");
  388. break;
  389. }
  390. if(temp_count >= 16) // 6 ms * 16 = 96ms.
  391. {
  392. #ifdef HEATER_0_USES_AD595
  393. current_raw[0] = raw_temp_0_value;
  394. #else
  395. current_raw[0] = 16383 - raw_temp_0_value;
  396. #endif
  397. #ifdef HEATER_1_USES_AD595
  398. current_raw[2] = raw_temp_2_value;
  399. #else
  400. current_raw[2] = 16383 - raw_temp_2_value;
  401. #endif
  402. #ifdef BED_USES_AD595
  403. current_raw[1] = raw_temp_1_value;
  404. #else
  405. current_raw[1] = 16383 - raw_temp_1_value;
  406. #endif
  407. temp_meas_ready = true;
  408. temp_count = 0;
  409. raw_temp_0_value = 0;
  410. raw_temp_1_value = 0;
  411. raw_temp_2_value = 0;
  412. #ifdef HEATER_0_MAXTEMP
  413. #if (HEATER_0_PIN > -1)
  414. if(current_raw[TEMPSENSOR_HOTEND_0] >= maxttemp_0) {
  415. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  416. analogWrite(HEATER_0_PIN, 0);
  417. SERIAL_ERRORLN("Temperature extruder 0 switched off. MAXTEMP triggered !!");
  418. kill();
  419. }
  420. #endif
  421. #endif
  422. #ifdef HEATER_1_MAXTEMP
  423. #if (HEATER_1_PIN > -1)
  424. if(current_raw[TEMPSENSOR_HOTEND_1] >= maxttemp_1) {
  425. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  426. if(current_raw[2] >= maxttemp_1) {
  427. analogWrite(HEATER_2_PIN, 0);
  428. SERIAL_ERRORLN("Temperature extruder 1 switched off. MAXTEMP triggered !!");
  429. kill()
  430. }
  431. #endif
  432. #endif //MAXTEMP
  433. #ifdef HEATER_0_MINTEMP
  434. #if (HEATER_0_PIN > -1)
  435. if(current_raw[TEMPSENSOR_HOTEND_0] <= minttemp_0) {
  436. target_raw[TEMPSENSOR_HOTEND_0] = 0;
  437. analogWrite(HEATER_0_PIN, 0);
  438. SERIAL_ERRORLN("Temperature extruder 0 switched off. MINTEMP triggered !!");
  439. kill();
  440. }
  441. #endif
  442. #endif
  443. #ifdef HEATER_1_MINTEMP
  444. #if (HEATER_2_PIN > -1)
  445. if(current_raw[TEMPSENSOR_HOTEND_1] <= minttemp_1) {
  446. target_raw[TEMPSENSOR_HOTEND_1] = 0;
  447. analogWrite(HEATER_2_PIN, 0);
  448. SERIAL_ERRORLN("Temperature extruder 1 switched off. MINTEMP triggered !!");
  449. kill();
  450. }
  451. #endif
  452. #endif //MAXTEMP
  453. #ifdef BED_MINTEMP
  454. #if (HEATER_1_PIN > -1)
  455. if(current_raw[1] <= bed_minttemp) {
  456. target_raw[1] = 0;
  457. WRITE(HEATER_1_PIN, 0);
  458. SERIAL_ERRORLN("Temperatur heated bed switched off. MINTEMP triggered !!");
  459. kill();
  460. }
  461. #endif
  462. #endif
  463. #ifdef BED_MAXTEMP
  464. #if (HEATER_1_PIN > -1)
  465. if(current_raw[1] >= bed_maxttemp) {
  466. target_raw[1] = 0;
  467. WRITE(HEATER_1_PIN, 0);
  468. SERIAL_ERRORLN("Temperature heated bed switched off. MAXTEMP triggered !!");
  469. kill();
  470. }
  471. #endif
  472. #endif
  473. }
  474. }