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

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