My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 13KB

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