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

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