My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 14KB

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