My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temperature.cpp 16KB

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