My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 27KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "temperature.h"
  26. #include "watchdog.h"
  27. //===========================================================================
  28. //=============================public variables============================
  29. //===========================================================================
  30. int target_raw[EXTRUDERS] = { 0 };
  31. int target_raw_bed = 0;
  32. #ifdef BED_LIMIT_SWITCHING
  33. int target_bed_low_temp =0;
  34. int target_bed_high_temp =0;
  35. #endif
  36. int current_raw[EXTRUDERS] = { 0 };
  37. int current_raw_bed = 0;
  38. #ifdef PIDTEMP
  39. // used external
  40. float pid_setpoint[EXTRUDERS] = { 0.0 };
  41. float Kp=DEFAULT_Kp;
  42. float Ki=(DEFAULT_Ki*PID_dT);
  43. float Kd=(DEFAULT_Kd/PID_dT);
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. //===========================================================================
  49. //=============================private variables============================
  50. //===========================================================================
  51. static volatile bool temp_meas_ready = false;
  52. static unsigned long previous_millis_bed_heater;
  53. //static unsigned long previous_millis_heater;
  54. #ifdef PIDTEMP
  55. //static cannot be external:
  56. static float temp_iState[EXTRUDERS] = { 0 };
  57. static float temp_dState[EXTRUDERS] = { 0 };
  58. static float pTerm[EXTRUDERS];
  59. static float iTerm[EXTRUDERS];
  60. static float dTerm[EXTRUDERS];
  61. //int output;
  62. static float pid_error[EXTRUDERS];
  63. static float temp_iState_min[EXTRUDERS];
  64. static float temp_iState_max[EXTRUDERS];
  65. // static float pid_input[EXTRUDERS];
  66. // static float pid_output[EXTRUDERS];
  67. static bool pid_reset[EXTRUDERS];
  68. #endif //PIDTEMP
  69. static unsigned char soft_pwm[EXTRUDERS];
  70. #ifdef WATCHPERIOD
  71. int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
  72. int watch_oldtemp[3] = {0,0,0};
  73. unsigned long watchmillis = 0;
  74. #endif //WATCHPERIOD
  75. // Init min and max temp with extreme values to prevent false errors during startup
  76. static int minttemp[EXTRUDERS] = { 0 };
  77. static int maxttemp[EXTRUDERS] = { 16383 }; // the first value used for all
  78. static int bed_minttemp = 0;
  79. static int bed_maxttemp = 16383;
  80. static void *heater_ttbl_map[EXTRUDERS] = { (void *)heater_0_temptable
  81. #if EXTRUDERS > 1
  82. , (void *)heater_1_temptable
  83. #endif
  84. #if EXTRUDERS > 2
  85. , (void *)heater_2_temptable
  86. #endif
  87. #if EXTRUDERS > 3
  88. #error Unsupported number of extruders
  89. #endif
  90. };
  91. static int heater_ttbllen_map[EXTRUDERS] = { heater_0_temptable_len
  92. #if EXTRUDERS > 1
  93. , heater_1_temptable_len
  94. #endif
  95. #if EXTRUDERS > 2
  96. , heater_2_temptable_len
  97. #endif
  98. #if EXTRUDERS > 3
  99. #error Unsupported number of extruders
  100. #endif
  101. };
  102. //===========================================================================
  103. //============================= functions ============================
  104. //===========================================================================
  105. void PID_autotune(float temp)
  106. {
  107. float input;
  108. int cycles=0;
  109. bool heating = true;
  110. unsigned long temp_millis = millis();
  111. unsigned long t1=temp_millis;
  112. unsigned long t2=temp_millis;
  113. long t_high;
  114. long t_low;
  115. long bias=PID_MAX/2;
  116. long d = PID_MAX/2;
  117. float Ku, Tu;
  118. float Kp, Ki, Kd;
  119. float max, min;
  120. SERIAL_ECHOLN("PID Autotune start");
  121. disable_heater(); // switch off all heaters.
  122. soft_pwm[0] = PID_MAX/2;
  123. for(;;) {
  124. if(temp_meas_ready == true) { // temp sample ready
  125. CRITICAL_SECTION_START;
  126. temp_meas_ready = false;
  127. CRITICAL_SECTION_END;
  128. input = analog2temp(current_raw[0], 0);
  129. max=max(max,input);
  130. min=min(min,input);
  131. if(heating == true && input > temp) {
  132. if(millis() - t2 > 5000) {
  133. heating=false;
  134. soft_pwm[0] = (bias - d) >> 1;
  135. t1=millis();
  136. t_high=t1 - t2;
  137. max=temp;
  138. }
  139. }
  140. if(heating == false && input < temp) {
  141. if(millis() - t1 > 5000) {
  142. heating=true;
  143. t2=millis();
  144. t_low=t2 - t1;
  145. if(cycles > 0) {
  146. bias += (d*(t_high - t_low))/(t_low + t_high);
  147. bias = constrain(bias, 20 ,PID_MAX-20);
  148. if(bias > PID_MAX/2) d = PID_MAX - 1 - bias;
  149. else d = bias;
  150. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  151. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  152. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  153. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  154. if(cycles > 2) {
  155. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  156. Tu = ((float)(t_low + t_high)/1000.0);
  157. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  158. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  159. Kp = 0.6*Ku;
  160. Ki = 2*Kp/Tu;
  161. Kd = Kp*Tu/8;
  162. SERIAL_PROTOCOLLNPGM(" Clasic PID ")
  163. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  164. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  165. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  166. /*
  167. Kp = 0.33*Ku;
  168. Ki = Kp/Tu;
  169. Kd = Kp*Tu/3;
  170. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  171. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  172. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  173. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  174. Kp = 0.2*Ku;
  175. Ki = 2*Kp/Tu;
  176. Kd = Kp*Tu/3;
  177. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  178. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  179. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  180. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  181. */
  182. }
  183. }
  184. soft_pwm[0] = (bias + d) >> 1;
  185. cycles++;
  186. min=temp;
  187. }
  188. }
  189. }
  190. if(input > (temp + 20)) {
  191. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature to high");
  192. return;
  193. }
  194. if(millis() - temp_millis > 2000) {
  195. temp_millis = millis();
  196. SERIAL_PROTOCOLPGM("ok T:");
  197. SERIAL_PROTOCOL(degHotend(0));
  198. SERIAL_PROTOCOLPGM(" @:");
  199. SERIAL_PROTOCOLLN(getHeaterPower(0));
  200. }
  201. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  202. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  203. return;
  204. }
  205. if(cycles > 5) {
  206. SERIAL_PROTOCOLLNPGM("PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h");
  207. return;
  208. }
  209. LCD_STATUS;
  210. }
  211. }
  212. void updatePID()
  213. {
  214. #ifdef PIDTEMP
  215. for(int e = 0; e < EXTRUDERS; e++) {
  216. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  217. }
  218. #endif
  219. }
  220. int getHeaterPower(int heater) {
  221. return soft_pwm[heater];
  222. }
  223. void manage_heater()
  224. {
  225. #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
  226. static int bed_needs_heating=0;
  227. static int bed_is_on=0;
  228. #endif
  229. #ifdef USE_WATCHDOG
  230. wd_reset();
  231. #endif
  232. float pid_input;
  233. float pid_output;
  234. if(temp_meas_ready != true) //better readability
  235. return;
  236. CRITICAL_SECTION_START;
  237. temp_meas_ready = false;
  238. CRITICAL_SECTION_END;
  239. for(int e = 0; e < EXTRUDERS; e++)
  240. {
  241. #ifdef PIDTEMP
  242. pid_input = analog2temp(current_raw[e], e);
  243. #ifndef PID_OPENLOOP
  244. pid_error[e] = pid_setpoint[e] - pid_input;
  245. if(pid_error[e] > 10) {
  246. pid_output = PID_MAX;
  247. pid_reset[e] = true;
  248. }
  249. else if(pid_error[e] < -10) {
  250. pid_output = 0;
  251. pid_reset[e] = true;
  252. }
  253. else {
  254. if(pid_reset[e] == true) {
  255. temp_iState[e] = 0.0;
  256. pid_reset[e] = false;
  257. }
  258. pTerm[e] = Kp * pid_error[e];
  259. temp_iState[e] += pid_error[e];
  260. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  261. iTerm[e] = Ki * temp_iState[e];
  262. //K1 defined in Configuration.h in the PID settings
  263. #define K2 (1.0-K1)
  264. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  265. temp_dState[e] = pid_input;
  266. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  267. }
  268. #endif //PID_OPENLOOP
  269. #ifdef PID_DEBUG
  270. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  271. #endif //PID_DEBUG
  272. #else /* PID off */
  273. pid_output = 0;
  274. if(current_raw[e] < target_raw[e]) {
  275. pid_output = PID_MAX;
  276. }
  277. #endif
  278. // Check if temperature is within the correct range
  279. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  280. {
  281. soft_pwm[e] = (int)pid_output >> 1;
  282. }
  283. else {
  284. soft_pwm[e] = 0;
  285. }
  286. } // End extruder for loop
  287. #ifdef WATCHPERIOD
  288. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  289. if(watch_oldtemp[0] >= degHotend(active_extruder)){
  290. setTargetHotend(0,active_extruder);
  291. LCD_MESSAGEPGM("Heating failed");
  292. SERIAL_ECHO_START;
  293. SERIAL_ECHOLN("Heating failed");
  294. }else{
  295. watchmillis = 0;
  296. }
  297. }
  298. #endif
  299. #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
  300. if (bed_needs_heating){
  301. if (bed_is_on==0)
  302. WRITE(HEATER_BED_PIN,HIGH);
  303. if (bed_is_on==1)
  304. WRITE(HEATER_BED_PIN,LOW);
  305. bed_is_on=(bed_is_on+1) % HEATER_BED_DUTY_CYCLE_DIVIDER;
  306. }
  307. #endif
  308. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  309. return;
  310. previous_millis_bed_heater = millis();
  311. #if TEMP_BED_PIN > -1
  312. #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
  313. bed_needs_heating=0;
  314. #endif
  315. #ifndef BED_LIMIT_SWITCHING
  316. // Check if temperature is within the correct range
  317. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  318. if(current_raw_bed >= target_raw_bed)
  319. {
  320. WRITE(HEATER_BED_PIN,LOW);
  321. }
  322. else
  323. {
  324. #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
  325. bed_needs_heating=1;
  326. #endif
  327. WRITE(HEATER_BED_PIN,HIGH);
  328. }
  329. }
  330. else {
  331. WRITE(HEATER_BED_PIN,LOW);
  332. }
  333. #else //#ifdef BED_LIMIT_SWITCHING
  334. // Check if temperature is within the correct band
  335. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  336. if(current_raw_bed > target_bed_high_temp)
  337. {
  338. WRITE(HEATER_BED_PIN,LOW);
  339. }
  340. else
  341. if(current_raw_bed <= target_bed_low_temp)
  342. {
  343. #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
  344. bed_needs_heating=1;
  345. #endif
  346. WRITE(HEATER_BED_PIN,HIGH);
  347. }
  348. }
  349. else {
  350. WRITE(HEATER_BED_PIN,LOW);
  351. }
  352. #endif
  353. #endif
  354. }
  355. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  356. // Takes hot end temperature value as input and returns corresponding raw value.
  357. // For a thermistor, it uses the RepRap thermistor temp table.
  358. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  359. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  360. int temp2analog(int celsius, uint8_t e) {
  361. if(e >= EXTRUDERS)
  362. {
  363. SERIAL_ERROR_START;
  364. SERIAL_ERROR((int)e);
  365. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  366. kill();
  367. }
  368. #ifdef HEATER_0_USES_MAX6675
  369. if (e == 0)
  370. {
  371. return celsius * 4;
  372. }
  373. #endif
  374. if(heater_ttbl_map[e] != 0)
  375. {
  376. int raw = 0;
  377. byte i;
  378. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  379. for (i=1; i<heater_ttbllen_map[e]; i++)
  380. {
  381. if (PGM_RD_W((*tt)[i][1]) < celsius)
  382. {
  383. raw = PGM_RD_W((*tt)[i-1][0]) +
  384. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  385. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  386. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  387. break;
  388. }
  389. }
  390. // Overflow: Set to last value in the table
  391. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  392. return (1023 * OVERSAMPLENR) - raw;
  393. }
  394. return ((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  395. }
  396. // Takes bed temperature value as input and returns corresponding raw value.
  397. // For a thermistor, it uses the RepRap thermistor temp table.
  398. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  399. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  400. int temp2analogBed(int celsius) {
  401. #ifdef BED_USES_THERMISTOR
  402. int raw = 0;
  403. byte i;
  404. for (i=1; i<bedtemptable_len; i++)
  405. {
  406. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  407. {
  408. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  409. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  410. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  411. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  412. break;
  413. }
  414. }
  415. // Overflow: Set to last value in the table
  416. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  417. return (1023 * OVERSAMPLENR) - raw;
  418. #elif defined BED_USES_AD595
  419. return lround(((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  420. #else
  421. #warning No heater-type defined for the bed.
  422. return 0;
  423. #endif
  424. }
  425. // Derived from RepRap FiveD extruder::getTemperature()
  426. // For hot end temperature measurement.
  427. float analog2temp(int raw, uint8_t e) {
  428. if(e >= EXTRUDERS)
  429. {
  430. SERIAL_ERROR_START;
  431. SERIAL_ERROR((int)e);
  432. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  433. kill();
  434. }
  435. #ifdef HEATER_0_USES_MAX6675
  436. if (e == 0)
  437. {
  438. return 0.25 * raw;
  439. }
  440. #endif
  441. if(heater_ttbl_map[e] != 0)
  442. {
  443. float celsius = 0;
  444. byte i;
  445. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  446. raw = (1023 * OVERSAMPLENR) - raw;
  447. for (i=1; i<heater_ttbllen_map[e]; i++)
  448. {
  449. if (PGM_RD_W((*tt)[i][0]) > raw)
  450. {
  451. celsius = PGM_RD_W((*tt)[i-1][1]) +
  452. (raw - PGM_RD_W((*tt)[i-1][0])) *
  453. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  454. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  455. break;
  456. }
  457. }
  458. // Overflow: Set to last value in the table
  459. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  460. return celsius;
  461. }
  462. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  463. }
  464. // Derived from RepRap FiveD extruder::getTemperature()
  465. // For bed temperature measurement.
  466. float analog2tempBed(int raw) {
  467. #ifdef BED_USES_THERMISTOR
  468. float celsius = 0;
  469. byte i;
  470. raw = (1023 * OVERSAMPLENR) - raw;
  471. for (i=1; i<bedtemptable_len; i++)
  472. {
  473. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  474. {
  475. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  476. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  477. (float)(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  478. (float)(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  479. break;
  480. }
  481. }
  482. // Overflow: Set to last value in the table
  483. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  484. return celsius;
  485. #elif defined BED_USES_AD595
  486. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  487. #else
  488. #warning No heater-type defined for the bed.
  489. return 0;
  490. #endif
  491. }
  492. void tp_init()
  493. {
  494. // Finish init of mult extruder arrays
  495. for(int e = 0; e < EXTRUDERS; e++) {
  496. // populate with the first value
  497. #ifdef WATCHPERIOD
  498. watch_raw[e] = watch_raw[0];
  499. #endif
  500. maxttemp[e] = maxttemp[0];
  501. #ifdef PIDTEMP
  502. temp_iState_min[e] = 0.0;
  503. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  504. #endif //PIDTEMP
  505. }
  506. #if (HEATER_0_PIN > -1)
  507. SET_OUTPUT(HEATER_0_PIN);
  508. #endif
  509. #if (HEATER_1_PIN > -1)
  510. SET_OUTPUT(HEATER_1_PIN);
  511. #endif
  512. #if (HEATER_2_PIN > -1)
  513. SET_OUTPUT(HEATER_2_PIN);
  514. #endif
  515. #if (HEATER_BED_PIN > -1)
  516. SET_OUTPUT(HEATER_BED_PIN);
  517. #endif
  518. #if (FAN_PIN > -1)
  519. SET_OUTPUT(FAN_PIN);
  520. #ifdef FAST_PWM_FAN
  521. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  522. #endif
  523. #endif
  524. #ifdef HEATER_0_USES_MAX6675
  525. #ifndef SDSUPPORT
  526. SET_OUTPUT(MAX_SCK_PIN);
  527. WRITE(MAX_SCK_PIN,0);
  528. SET_OUTPUT(MAX_MOSI_PIN);
  529. WRITE(MAX_MOSI_PIN,1);
  530. SET_INPUT(MAX_MISO_PIN);
  531. WRITE(MAX_MISO_PIN,1);
  532. #endif
  533. SET_OUTPUT(MAX6675_SS);
  534. WRITE(MAX6675_SS,1);
  535. #endif
  536. // Set analog inputs
  537. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  538. DIDR0 = 0;
  539. #ifdef DIDR2
  540. DIDR2 = 0;
  541. #endif
  542. #if (TEMP_0_PIN > -1)
  543. #if TEMP_0_PIN < 8
  544. DIDR0 |= 1 << TEMP_0_PIN;
  545. #else
  546. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  547. #endif
  548. #endif
  549. #if (TEMP_1_PIN > -1)
  550. #if TEMP_1_PIN < 8
  551. DIDR0 |= 1<<TEMP_1_PIN;
  552. #else
  553. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  554. #endif
  555. #endif
  556. #if (TEMP_2_PIN > -1)
  557. #if TEMP_2_PIN < 8
  558. DIDR0 |= 1 << TEMP_2_PIN;
  559. #else
  560. DIDR2 = 1<<(TEMP_2_PIN - 8);
  561. #endif
  562. #endif
  563. #if (TEMP_BED_PIN > -1)
  564. #if TEMP_BED_PIN < 8
  565. DIDR0 |= 1<<TEMP_BED_PIN;
  566. #else
  567. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  568. #endif
  569. #endif
  570. // Use timer0 for temperature measurement
  571. // Interleave temperature interrupt with millies interrupt
  572. OCR0B = 128;
  573. TIMSK0 |= (1<<OCIE0B);
  574. // Wait for temperature measurement to settle
  575. delay(250);
  576. #ifdef HEATER_0_MINTEMP
  577. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  578. #endif //MINTEMP
  579. #ifdef HEATER_0_MAXTEMP
  580. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  581. #endif //MAXTEMP
  582. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  583. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  584. #endif // MINTEMP 1
  585. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  586. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  587. #endif //MAXTEMP 1
  588. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  589. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  590. #endif //MINTEMP 2
  591. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  592. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  593. #endif //MAXTEMP 2
  594. #ifdef BED_MINTEMP
  595. bed_minttemp = temp2analogBed(BED_MINTEMP);
  596. #endif //BED_MINTEMP
  597. #ifdef BED_MAXTEMP
  598. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  599. #endif //BED_MAXTEMP
  600. }
  601. void setWatch()
  602. {
  603. #ifdef WATCHPERIOD
  604. int t = 0;
  605. for (int e = 0; e < EXTRUDERS; e++)
  606. {
  607. if(isHeatingHotend(e))
  608. watch_oldtemp[0] = degHotend(0);
  609. {
  610. t = max(t,millis());
  611. watch_raw[e] = current_raw[e];
  612. }
  613. }
  614. watchmillis = t;
  615. #endif
  616. }
  617. void disable_heater()
  618. {
  619. for(int i=0;i<EXTRUDERS;i++)
  620. setTargetHotend(0,i);
  621. setTargetBed(0);
  622. #if TEMP_0_PIN > -1
  623. target_raw[0]=0;
  624. soft_pwm[0]=0;
  625. #if HEATER_0_PIN > -1
  626. WRITE(HEATER_0_PIN,LOW);
  627. #endif
  628. #endif
  629. #if TEMP_1_PIN > -1
  630. target_raw[1]=0;
  631. soft_pwm[1]=0;
  632. #if HEATER_1_PIN > -1
  633. WRITE(HEATER_1_PIN,LOW);
  634. #endif
  635. #endif
  636. #if TEMP_2_PIN > -1
  637. target_raw[2]=0;
  638. soft_pwm[2]=0;
  639. #if HEATER_2_PIN > -1
  640. WRITE(HEATER_2_PIN,LOW);
  641. #endif
  642. #endif
  643. #if TEMP_BED_PIN > -1
  644. target_raw_bed=0;
  645. #if HEATER_BED_PIN > -1
  646. WRITE(HEATER_BED_PIN,LOW);
  647. #endif
  648. #endif
  649. }
  650. void max_temp_error(uint8_t e) {
  651. disable_heater();
  652. if(IsStopped() == false) {
  653. SERIAL_ERROR_START;
  654. SERIAL_ERRORLN((int)e);
  655. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  656. }
  657. }
  658. void min_temp_error(uint8_t e) {
  659. disable_heater();
  660. if(IsStopped() == false) {
  661. SERIAL_ERROR_START;
  662. SERIAL_ERRORLN((int)e);
  663. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  664. }
  665. }
  666. void bed_max_temp_error(void) {
  667. #if HEATER_BED_PIN > -1
  668. WRITE(HEATER_BED_PIN, 0);
  669. #endif
  670. if(IsStopped() == false) {
  671. SERIAL_ERROR_START;
  672. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  673. }
  674. }
  675. #define HEAT_INTERVAL 250
  676. #ifdef HEATER_0_USES_MAX6675
  677. long max6675_previous_millis = -HEAT_INTERVAL;
  678. int max6675_temp = 2000;
  679. int read_max6675()
  680. {
  681. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  682. return max6675_temp;
  683. max6675_previous_millis = millis();
  684. max6675_temp = 0;
  685. #ifdef PRR
  686. PRR &= ~(1<<PRSPI);
  687. #elif defined PRR0
  688. PRR0 &= ~(1<<PRSPI);
  689. #endif
  690. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  691. // enable TT_MAX6675
  692. WRITE(MAX6675_SS, 0);
  693. // ensure 100ns delay - a bit extra is fine
  694. delay(1);
  695. // read MSB
  696. SPDR = 0;
  697. for (;(SPSR & (1<<SPIF)) == 0;);
  698. max6675_temp = SPDR;
  699. max6675_temp <<= 8;
  700. // read LSB
  701. SPDR = 0;
  702. for (;(SPSR & (1<<SPIF)) == 0;);
  703. max6675_temp |= SPDR;
  704. // disable TT_MAX6675
  705. WRITE(MAX6675_SS, 1);
  706. if (max6675_temp & 4)
  707. {
  708. // thermocouple open
  709. max6675_temp = 2000;
  710. }
  711. else
  712. {
  713. max6675_temp = max6675_temp >> 3;
  714. }
  715. return max6675_temp;
  716. }
  717. #endif
  718. // Timer 0 is shared with millies
  719. ISR(TIMER0_COMPB_vect)
  720. {
  721. //these variables are only accesible from the ISR, but static, so they don't loose their value
  722. static unsigned char temp_count = 0;
  723. static unsigned long raw_temp_0_value = 0;
  724. static unsigned long raw_temp_1_value = 0;
  725. static unsigned long raw_temp_2_value = 0;
  726. static unsigned long raw_temp_bed_value = 0;
  727. static unsigned char temp_state = 0;
  728. static unsigned char pwm_count = 1;
  729. static unsigned char soft_pwm_0;
  730. static unsigned char soft_pwm_1;
  731. static unsigned char soft_pwm_2;
  732. if(pwm_count == 0){
  733. soft_pwm_0 = soft_pwm[0];
  734. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  735. #if EXTRUDERS > 1
  736. soft_pwm_1 = soft_pwm[1];
  737. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  738. #endif
  739. #if EXTRUDERS > 2
  740. soft_pwm_2 = soft_pwm[2];
  741. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  742. #endif
  743. }
  744. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  745. #if EXTRUDERS > 1
  746. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  747. #endif
  748. #if EXTRUDERS > 2
  749. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  750. #endif
  751. pwm_count++;
  752. pwm_count &= 0x7f;
  753. switch(temp_state) {
  754. case 0: // Prepare TEMP_0
  755. #if (TEMP_0_PIN > -1)
  756. #if TEMP_0_PIN > 7
  757. ADCSRB = 1<<MUX5;
  758. #else
  759. ADCSRB = 0;
  760. #endif
  761. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  762. ADCSRA |= 1<<ADSC; // Start conversion
  763. #endif
  764. #ifdef ULTIPANEL
  765. buttons_check();
  766. #endif
  767. temp_state = 1;
  768. break;
  769. case 1: // Measure TEMP_0
  770. #if (TEMP_0_PIN > -1)
  771. raw_temp_0_value += ADC;
  772. #endif
  773. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  774. raw_temp_0_value = read_max6675();
  775. #endif
  776. temp_state = 2;
  777. break;
  778. case 2: // Prepare TEMP_BED
  779. #if (TEMP_BED_PIN > -1)
  780. #if TEMP_BED_PIN > 7
  781. ADCSRB = 1<<MUX5;
  782. #else
  783. ADCSRB = 0;
  784. #endif
  785. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  786. ADCSRA |= 1<<ADSC; // Start conversion
  787. #endif
  788. #ifdef ULTIPANEL
  789. buttons_check();
  790. #endif
  791. temp_state = 3;
  792. break;
  793. case 3: // Measure TEMP_BED
  794. #if (TEMP_BED_PIN > -1)
  795. raw_temp_bed_value += ADC;
  796. #endif
  797. temp_state = 4;
  798. break;
  799. case 4: // Prepare TEMP_1
  800. #if (TEMP_1_PIN > -1)
  801. #if TEMP_1_PIN > 7
  802. ADCSRB = 1<<MUX5;
  803. #else
  804. ADCSRB = 0;
  805. #endif
  806. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  807. ADCSRA |= 1<<ADSC; // Start conversion
  808. #endif
  809. #ifdef ULTIPANEL
  810. buttons_check();
  811. #endif
  812. temp_state = 5;
  813. break;
  814. case 5: // Measure TEMP_1
  815. #if (TEMP_1_PIN > -1)
  816. raw_temp_1_value += ADC;
  817. #endif
  818. temp_state = 6;
  819. break;
  820. case 6: // Prepare TEMP_2
  821. #if (TEMP_2_PIN > -1)
  822. #if TEMP_2_PIN > 7
  823. ADCSRB = 1<<MUX5;
  824. #else
  825. ADCSRB = 0;
  826. #endif
  827. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  828. ADCSRA |= 1<<ADSC; // Start conversion
  829. #endif
  830. #ifdef ULTIPANEL
  831. buttons_check();
  832. #endif
  833. temp_state = 7;
  834. break;
  835. case 7: // Measure TEMP_2
  836. #if (TEMP_2_PIN > -1)
  837. raw_temp_2_value += ADC;
  838. #endif
  839. temp_state = 0;
  840. temp_count++;
  841. break;
  842. // default:
  843. // SERIAL_ERROR_START;
  844. // SERIAL_ERRORLNPGM("Temp measurement error!");
  845. // break;
  846. }
  847. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  848. {
  849. #if defined(HEATER_0_USES_AD595) || defined(HEATER_0_USES_MAX6675)
  850. current_raw[0] = raw_temp_0_value;
  851. #else
  852. current_raw[0] = 16383 - raw_temp_0_value;
  853. #endif
  854. #if EXTRUDERS > 1
  855. #ifdef HEATER_1_USES_AD595
  856. current_raw[1] = raw_temp_1_value;
  857. #else
  858. current_raw[1] = 16383 - raw_temp_1_value;
  859. #endif
  860. #endif
  861. #if EXTRUDERS > 2
  862. #ifdef HEATER_2_USES_AD595
  863. current_raw[2] = raw_temp_2_value;
  864. #else
  865. current_raw[2] = 16383 - raw_temp_2_value;
  866. #endif
  867. #endif
  868. #ifdef BED_USES_AD595
  869. current_raw_bed = raw_temp_bed_value;
  870. #else
  871. current_raw_bed = 16383 - raw_temp_bed_value;
  872. #endif
  873. temp_meas_ready = true;
  874. temp_count = 0;
  875. raw_temp_0_value = 0;
  876. raw_temp_1_value = 0;
  877. raw_temp_2_value = 0;
  878. raw_temp_bed_value = 0;
  879. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  880. if(current_raw[e] >= maxttemp[e]) {
  881. target_raw[e] = 0;
  882. max_temp_error(e);
  883. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  884. {
  885. Stop();;
  886. }
  887. #endif
  888. }
  889. if(current_raw[e] <= minttemp[e]) {
  890. target_raw[e] = 0;
  891. min_temp_error(e);
  892. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  893. {
  894. Stop();
  895. }
  896. #endif
  897. }
  898. }
  899. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  900. if(current_raw_bed >= bed_maxttemp) {
  901. target_raw_bed = 0;
  902. bed_max_temp_error();
  903. Stop();
  904. }
  905. #endif
  906. }
  907. }