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

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