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

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