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

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