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