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

temperature.cpp 13KB

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