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

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