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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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;
  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;
  134. long t_low;
  135. long bias, d;
  136. float Ku, Tu;
  137. float Kp, Ki, Kd;
  138. float max, min;
  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. #ifdef USE_WATCHDOG
  280. wd_reset();
  281. #endif
  282. float pid_input;
  283. float pid_output;
  284. if(temp_meas_ready != true) //better readability
  285. return;
  286. CRITICAL_SECTION_START;
  287. temp_meas_ready = false;
  288. CRITICAL_SECTION_END;
  289. for(int e = 0; e < EXTRUDERS; e++)
  290. {
  291. #ifdef PIDTEMP
  292. pid_input = analog2temp(current_raw[e], e);
  293. #ifndef PID_OPENLOOP
  294. pid_error[e] = pid_setpoint[e] - pid_input;
  295. if(pid_error[e] > 10) {
  296. pid_output = PID_MAX;
  297. pid_reset[e] = true;
  298. }
  299. else if(pid_error[e] < -10) {
  300. pid_output = 0;
  301. pid_reset[e] = true;
  302. }
  303. else {
  304. if(pid_reset[e] == true) {
  305. temp_iState[e] = 0.0;
  306. pid_reset[e] = false;
  307. }
  308. pTerm[e] = Kp * pid_error[e];
  309. temp_iState[e] += pid_error[e];
  310. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  311. iTerm[e] = Ki * temp_iState[e];
  312. //K1 defined in Configuration.h in the PID settings
  313. #define K2 (1.0-K1)
  314. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  315. temp_dState[e] = pid_input;
  316. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  317. }
  318. #else
  319. pid_output = constrain(pid_setpoint[e], 0, PID_MAX);
  320. #endif //PID_OPENLOOP
  321. #ifdef PID_DEBUG
  322. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  323. #endif //PID_DEBUG
  324. #else /* PID off */
  325. pid_output = 0;
  326. if(current_raw[e] < target_raw[e]) {
  327. pid_output = PID_MAX;
  328. }
  329. #endif
  330. // Check if temperature is within the correct range
  331. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  332. {
  333. soft_pwm[e] = (int)pid_output >> 1;
  334. }
  335. else {
  336. soft_pwm[e] = 0;
  337. }
  338. } // End extruder for loop
  339. #ifdef WATCHPERIOD
  340. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  341. if(watch_oldtemp[0] >= degHotend(active_extruder)){
  342. setTargetHotend(0,active_extruder);
  343. LCD_MESSAGEPGM("Heating failed");
  344. SERIAL_ECHO_START;
  345. SERIAL_ECHOLN("Heating failed");
  346. }else{
  347. watchmillis = 0;
  348. }
  349. }
  350. #endif
  351. #ifndef PIDTEMPBED
  352. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  353. return;
  354. previous_millis_bed_heater = millis();
  355. #endif
  356. #if TEMP_BED_PIN > -1
  357. #ifdef PIDTEMPBED
  358. pid_input = analog2tempBed(current_raw_bed);
  359. #ifndef PID_OPENLOOP
  360. pid_error_bed = pid_setpoint_bed - pid_input;
  361. pTerm_bed = bedKp * pid_error_bed;
  362. temp_iState_bed += pid_error_bed;
  363. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  364. iTerm_bed = bedKi * temp_iState_bed;
  365. //K1 defined in Configuration.h in the PID settings
  366. #define K2 (1.0-K1)
  367. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  368. temp_dState_bed = pid_input;
  369. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  370. #else
  371. pid_output = constrain(pid_setpoint_bed, 0, MAX_BED_POWER);
  372. #endif //PID_OPENLOOP
  373. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp))
  374. {
  375. soft_pwm_bed = (int)pid_output >> 1;
  376. }
  377. else {
  378. soft_pwm_bed = 0;
  379. }
  380. #elif not defined BED_LIMIT_SWITCHING
  381. // Check if temperature is within the correct range
  382. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  383. if(current_raw_bed >= target_raw_bed)
  384. {
  385. soft_pwm_bed = 0;
  386. }
  387. else
  388. {
  389. soft_pwm_bed = MAX_BED_POWER>>1;
  390. }
  391. }
  392. else {
  393. soft_pwm_bed = 0;
  394. WRITE(HEATER_BED_PIN,LOW);
  395. }
  396. #else //#ifdef BED_LIMIT_SWITCHING
  397. // Check if temperature is within the correct band
  398. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  399. if(current_raw_bed > target_bed_high_temp)
  400. {
  401. soft_pwm_bed = 0;
  402. }
  403. else
  404. if(current_raw_bed <= target_bed_low_temp)
  405. {
  406. soft_pwm_bed = MAX_BED_POWER>>1;
  407. }
  408. }
  409. else {
  410. soft_pwm_bed = 0;
  411. WRITE(HEATER_BED_PIN,LOW);
  412. }
  413. #endif
  414. #endif
  415. }
  416. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  417. // Takes hot end temperature value as input and returns corresponding raw value.
  418. // For a thermistor, it uses the RepRap thermistor temp table.
  419. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  420. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  421. int temp2analog(int celsius, uint8_t e) {
  422. if(e >= EXTRUDERS)
  423. {
  424. SERIAL_ERROR_START;
  425. SERIAL_ERROR((int)e);
  426. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  427. kill();
  428. }
  429. #ifdef HEATER_0_USES_MAX6675
  430. if (e == 0)
  431. {
  432. return celsius * 4;
  433. }
  434. #endif
  435. if(heater_ttbl_map[e] != 0)
  436. {
  437. int raw = 0;
  438. byte i;
  439. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  440. for (i=1; i<heater_ttbllen_map[e]; i++)
  441. {
  442. if (PGM_RD_W((*tt)[i][1]) < celsius)
  443. {
  444. raw = PGM_RD_W((*tt)[i-1][0]) +
  445. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  446. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  447. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  448. break;
  449. }
  450. }
  451. // Overflow: Set to last value in the table
  452. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  453. return (1023 * OVERSAMPLENR) - raw;
  454. }
  455. return ((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  456. }
  457. // Takes bed temperature value as input and returns corresponding raw value.
  458. // For a thermistor, it uses the RepRap thermistor temp table.
  459. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  460. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  461. int temp2analogBed(int celsius) {
  462. #ifdef BED_USES_THERMISTOR
  463. int raw = 0;
  464. byte i;
  465. for (i=1; i<bedtemptable_len; i++)
  466. {
  467. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  468. {
  469. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  470. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  471. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  472. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  473. break;
  474. }
  475. }
  476. // Overflow: Set to last value in the table
  477. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  478. return (1023 * OVERSAMPLENR) - raw;
  479. #elif defined BED_USES_AD595
  480. return lround(((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  481. #else
  482. #warning No heater-type defined for the bed.
  483. return 0;
  484. #endif
  485. }
  486. // Derived from RepRap FiveD extruder::getTemperature()
  487. // For hot end temperature measurement.
  488. float analog2temp(int raw, uint8_t e) {
  489. if(e >= EXTRUDERS)
  490. {
  491. SERIAL_ERROR_START;
  492. SERIAL_ERROR((int)e);
  493. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  494. kill();
  495. }
  496. #ifdef HEATER_0_USES_MAX6675
  497. if (e == 0)
  498. {
  499. return 0.25 * raw;
  500. }
  501. #endif
  502. if(heater_ttbl_map[e] != 0)
  503. {
  504. float celsius = 0;
  505. byte i;
  506. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  507. raw = (1023 * OVERSAMPLENR) - raw;
  508. for (i=1; i<heater_ttbllen_map[e]; i++)
  509. {
  510. if (PGM_RD_W((*tt)[i][0]) > raw)
  511. {
  512. celsius = PGM_RD_W((*tt)[i-1][1]) +
  513. (raw - PGM_RD_W((*tt)[i-1][0])) *
  514. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  515. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  516. break;
  517. }
  518. }
  519. // Overflow: Set to last value in the table
  520. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  521. return celsius;
  522. }
  523. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  524. }
  525. // Derived from RepRap FiveD extruder::getTemperature()
  526. // For bed temperature measurement.
  527. float analog2tempBed(int raw) {
  528. #ifdef BED_USES_THERMISTOR
  529. float celsius = 0;
  530. byte i;
  531. raw = (1023 * OVERSAMPLENR) - raw;
  532. for (i=1; i<bedtemptable_len; i++)
  533. {
  534. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  535. {
  536. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  537. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  538. (float)(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  539. (float)(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  540. break;
  541. }
  542. }
  543. // Overflow: Set to last value in the table
  544. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  545. return celsius;
  546. #elif defined BED_USES_AD595
  547. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  548. #else
  549. #warning No heater-type defined for the bed.
  550. return 0;
  551. #endif
  552. }
  553. void tp_init()
  554. {
  555. // Finish init of mult extruder arrays
  556. for(int e = 0; e < EXTRUDERS; e++) {
  557. // populate with the first value
  558. #ifdef WATCHPERIOD
  559. watch_raw[e] = watch_raw[0];
  560. #endif
  561. maxttemp[e] = maxttemp[0];
  562. #ifdef PIDTEMP
  563. temp_iState_min[e] = 0.0;
  564. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  565. #endif //PIDTEMP
  566. #ifdef PIDTEMPBED
  567. temp_iState_min_bed = 0.0;
  568. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  569. #endif //PIDTEMPBED
  570. }
  571. #if (HEATER_0_PIN > -1)
  572. SET_OUTPUT(HEATER_0_PIN);
  573. #endif
  574. #if (HEATER_1_PIN > -1)
  575. SET_OUTPUT(HEATER_1_PIN);
  576. #endif
  577. #if (HEATER_2_PIN > -1)
  578. SET_OUTPUT(HEATER_2_PIN);
  579. #endif
  580. #if (HEATER_BED_PIN > -1)
  581. SET_OUTPUT(HEATER_BED_PIN);
  582. #endif
  583. #if (FAN_PIN > -1)
  584. SET_OUTPUT(FAN_PIN);
  585. #ifdef FAST_PWM_FAN
  586. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  587. #endif
  588. #endif
  589. #ifdef HEATER_0_USES_MAX6675
  590. #ifndef SDSUPPORT
  591. SET_OUTPUT(MAX_SCK_PIN);
  592. WRITE(MAX_SCK_PIN,0);
  593. SET_OUTPUT(MAX_MOSI_PIN);
  594. WRITE(MAX_MOSI_PIN,1);
  595. SET_INPUT(MAX_MISO_PIN);
  596. WRITE(MAX_MISO_PIN,1);
  597. #endif
  598. SET_OUTPUT(MAX6675_SS);
  599. WRITE(MAX6675_SS,1);
  600. #endif
  601. // Set analog inputs
  602. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  603. DIDR0 = 0;
  604. #ifdef DIDR2
  605. DIDR2 = 0;
  606. #endif
  607. #if (TEMP_0_PIN > -1)
  608. #if TEMP_0_PIN < 8
  609. DIDR0 |= 1 << TEMP_0_PIN;
  610. #else
  611. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  612. #endif
  613. #endif
  614. #if (TEMP_1_PIN > -1)
  615. #if TEMP_1_PIN < 8
  616. DIDR0 |= 1<<TEMP_1_PIN;
  617. #else
  618. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  619. #endif
  620. #endif
  621. #if (TEMP_2_PIN > -1)
  622. #if TEMP_2_PIN < 8
  623. DIDR0 |= 1 << TEMP_2_PIN;
  624. #else
  625. DIDR2 = 1<<(TEMP_2_PIN - 8);
  626. #endif
  627. #endif
  628. #if (TEMP_BED_PIN > -1)
  629. #if TEMP_BED_PIN < 8
  630. DIDR0 |= 1<<TEMP_BED_PIN;
  631. #else
  632. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  633. #endif
  634. #endif
  635. // Use timer0 for temperature measurement
  636. // Interleave temperature interrupt with millies interrupt
  637. OCR0B = 128;
  638. TIMSK0 |= (1<<OCIE0B);
  639. // Wait for temperature measurement to settle
  640. delay(250);
  641. #ifdef HEATER_0_MINTEMP
  642. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  643. #endif //MINTEMP
  644. #ifdef HEATER_0_MAXTEMP
  645. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  646. #endif //MAXTEMP
  647. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  648. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  649. #endif // MINTEMP 1
  650. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  651. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  652. #endif //MAXTEMP 1
  653. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  654. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  655. #endif //MINTEMP 2
  656. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  657. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  658. #endif //MAXTEMP 2
  659. #ifdef BED_MINTEMP
  660. bed_minttemp = temp2analogBed(BED_MINTEMP);
  661. #endif //BED_MINTEMP
  662. #ifdef BED_MAXTEMP
  663. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  664. #endif //BED_MAXTEMP
  665. }
  666. void setWatch()
  667. {
  668. #ifdef WATCHPERIOD
  669. int t = 0;
  670. for (int e = 0; e < EXTRUDERS; e++)
  671. {
  672. if(isHeatingHotend(e))
  673. watch_oldtemp[0] = degHotend(0);
  674. {
  675. t = max(t,millis());
  676. watch_raw[e] = current_raw[e];
  677. }
  678. }
  679. watchmillis = t;
  680. #endif
  681. }
  682. void disable_heater()
  683. {
  684. for(int i=0;i<EXTRUDERS;i++)
  685. setTargetHotend(0,i);
  686. setTargetBed(0);
  687. #if TEMP_0_PIN > -1
  688. target_raw[0]=0;
  689. soft_pwm[0]=0;
  690. #if HEATER_0_PIN > -1
  691. WRITE(HEATER_0_PIN,LOW);
  692. #endif
  693. #endif
  694. #if TEMP_1_PIN > -1
  695. target_raw[1]=0;
  696. soft_pwm[1]=0;
  697. #if HEATER_1_PIN > -1
  698. WRITE(HEATER_1_PIN,LOW);
  699. #endif
  700. #endif
  701. #if TEMP_2_PIN > -1
  702. target_raw[2]=0;
  703. soft_pwm[2]=0;
  704. #if HEATER_2_PIN > -1
  705. WRITE(HEATER_2_PIN,LOW);
  706. #endif
  707. #endif
  708. #if TEMP_BED_PIN > -1
  709. target_raw_bed=0;
  710. soft_pwm_bed=0;
  711. #if HEATER_BED_PIN > -1
  712. WRITE(HEATER_BED_PIN,LOW);
  713. #endif
  714. #endif
  715. }
  716. void max_temp_error(uint8_t e) {
  717. disable_heater();
  718. if(IsStopped() == false) {
  719. SERIAL_ERROR_START;
  720. SERIAL_ERRORLN((int)e);
  721. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  722. }
  723. }
  724. void min_temp_error(uint8_t e) {
  725. disable_heater();
  726. if(IsStopped() == false) {
  727. SERIAL_ERROR_START;
  728. SERIAL_ERRORLN((int)e);
  729. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  730. }
  731. }
  732. void bed_max_temp_error(void) {
  733. #if HEATER_BED_PIN > -1
  734. WRITE(HEATER_BED_PIN, 0);
  735. #endif
  736. if(IsStopped() == false) {
  737. SERIAL_ERROR_START;
  738. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  739. }
  740. }
  741. #define HEAT_INTERVAL 250
  742. #ifdef HEATER_0_USES_MAX6675
  743. long max6675_previous_millis = -HEAT_INTERVAL;
  744. int max6675_temp = 2000;
  745. int read_max6675()
  746. {
  747. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  748. return max6675_temp;
  749. max6675_previous_millis = millis();
  750. max6675_temp = 0;
  751. #ifdef PRR
  752. PRR &= ~(1<<PRSPI);
  753. #elif defined PRR0
  754. PRR0 &= ~(1<<PRSPI);
  755. #endif
  756. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  757. // enable TT_MAX6675
  758. WRITE(MAX6675_SS, 0);
  759. // ensure 100ns delay - a bit extra is fine
  760. delay(1);
  761. // read MSB
  762. SPDR = 0;
  763. for (;(SPSR & (1<<SPIF)) == 0;);
  764. max6675_temp = SPDR;
  765. max6675_temp <<= 8;
  766. // read LSB
  767. SPDR = 0;
  768. for (;(SPSR & (1<<SPIF)) == 0;);
  769. max6675_temp |= SPDR;
  770. // disable TT_MAX6675
  771. WRITE(MAX6675_SS, 1);
  772. if (max6675_temp & 4)
  773. {
  774. // thermocouple open
  775. max6675_temp = 2000;
  776. }
  777. else
  778. {
  779. max6675_temp = max6675_temp >> 3;
  780. }
  781. return max6675_temp;
  782. }
  783. #endif
  784. // Timer 0 is shared with millies
  785. ISR(TIMER0_COMPB_vect)
  786. {
  787. //these variables are only accesible from the ISR, but static, so they don't loose their value
  788. static unsigned char temp_count = 0;
  789. static unsigned long raw_temp_0_value = 0;
  790. static unsigned long raw_temp_1_value = 0;
  791. static unsigned long raw_temp_2_value = 0;
  792. static unsigned long raw_temp_bed_value = 0;
  793. static unsigned char temp_state = 0;
  794. static unsigned char pwm_count = 1;
  795. static unsigned char soft_pwm_0;
  796. static unsigned char soft_pwm_1;
  797. static unsigned char soft_pwm_2;
  798. static unsigned char soft_pwm_b;
  799. if(pwm_count == 0){
  800. soft_pwm_0 = soft_pwm[0];
  801. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  802. #if EXTRUDERS > 1
  803. soft_pwm_1 = soft_pwm[1];
  804. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  805. #endif
  806. #if EXTRUDERS > 2
  807. soft_pwm_2 = soft_pwm[2];
  808. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  809. #endif
  810. #if HEATER_BED_PIN > -1
  811. soft_pwm_b = soft_pwm_bed;
  812. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1);
  813. #endif
  814. }
  815. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  816. #if EXTRUDERS > 1
  817. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  818. #endif
  819. #if EXTRUDERS > 2
  820. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  821. #endif
  822. #if HEATER_BED_PIN > -1
  823. if(soft_pwm_b <= pwm_count) WRITE(HEATER_BED_PIN,0);
  824. #endif
  825. pwm_count++;
  826. pwm_count &= 0x7f;
  827. switch(temp_state) {
  828. case 0: // Prepare TEMP_0
  829. #if (TEMP_0_PIN > -1)
  830. #if TEMP_0_PIN > 7
  831. ADCSRB = 1<<MUX5;
  832. #else
  833. ADCSRB = 0;
  834. #endif
  835. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  836. ADCSRA |= 1<<ADSC; // Start conversion
  837. #endif
  838. #ifdef ULTIPANEL
  839. buttons_check();
  840. #endif
  841. temp_state = 1;
  842. break;
  843. case 1: // Measure TEMP_0
  844. #if (TEMP_0_PIN > -1)
  845. raw_temp_0_value += ADC;
  846. #endif
  847. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  848. raw_temp_0_value = read_max6675();
  849. #endif
  850. temp_state = 2;
  851. break;
  852. case 2: // Prepare TEMP_BED
  853. #if (TEMP_BED_PIN > -1)
  854. #if TEMP_BED_PIN > 7
  855. ADCSRB = 1<<MUX5;
  856. #else
  857. ADCSRB = 0;
  858. #endif
  859. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  860. ADCSRA |= 1<<ADSC; // Start conversion
  861. #endif
  862. #ifdef ULTIPANEL
  863. buttons_check();
  864. #endif
  865. temp_state = 3;
  866. break;
  867. case 3: // Measure TEMP_BED
  868. #if (TEMP_BED_PIN > -1)
  869. raw_temp_bed_value += ADC;
  870. #endif
  871. temp_state = 4;
  872. break;
  873. case 4: // Prepare TEMP_1
  874. #if (TEMP_1_PIN > -1)
  875. #if TEMP_1_PIN > 7
  876. ADCSRB = 1<<MUX5;
  877. #else
  878. ADCSRB = 0;
  879. #endif
  880. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  881. ADCSRA |= 1<<ADSC; // Start conversion
  882. #endif
  883. #ifdef ULTIPANEL
  884. buttons_check();
  885. #endif
  886. temp_state = 5;
  887. break;
  888. case 5: // Measure TEMP_1
  889. #if (TEMP_1_PIN > -1)
  890. raw_temp_1_value += ADC;
  891. #endif
  892. temp_state = 6;
  893. break;
  894. case 6: // Prepare TEMP_2
  895. #if (TEMP_2_PIN > -1)
  896. #if TEMP_2_PIN > 7
  897. ADCSRB = 1<<MUX5;
  898. #else
  899. ADCSRB = 0;
  900. #endif
  901. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  902. ADCSRA |= 1<<ADSC; // Start conversion
  903. #endif
  904. #ifdef ULTIPANEL
  905. buttons_check();
  906. #endif
  907. temp_state = 7;
  908. break;
  909. case 7: // Measure TEMP_2
  910. #if (TEMP_2_PIN > -1)
  911. raw_temp_2_value += ADC;
  912. #endif
  913. temp_state = 0;
  914. temp_count++;
  915. break;
  916. // default:
  917. // SERIAL_ERROR_START;
  918. // SERIAL_ERRORLNPGM("Temp measurement error!");
  919. // break;
  920. }
  921. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  922. {
  923. #if defined(HEATER_0_USES_AD595) || defined(HEATER_0_USES_MAX6675)
  924. current_raw[0] = raw_temp_0_value;
  925. #else
  926. current_raw[0] = 16383 - raw_temp_0_value;
  927. #endif
  928. #if EXTRUDERS > 1
  929. #ifdef HEATER_1_USES_AD595
  930. current_raw[1] = raw_temp_1_value;
  931. #else
  932. current_raw[1] = 16383 - raw_temp_1_value;
  933. #endif
  934. #endif
  935. #if EXTRUDERS > 2
  936. #ifdef HEATER_2_USES_AD595
  937. current_raw[2] = raw_temp_2_value;
  938. #else
  939. current_raw[2] = 16383 - raw_temp_2_value;
  940. #endif
  941. #endif
  942. #ifdef BED_USES_AD595
  943. current_raw_bed = raw_temp_bed_value;
  944. #else
  945. current_raw_bed = 16383 - raw_temp_bed_value;
  946. #endif
  947. temp_meas_ready = true;
  948. temp_count = 0;
  949. raw_temp_0_value = 0;
  950. raw_temp_1_value = 0;
  951. raw_temp_2_value = 0;
  952. raw_temp_bed_value = 0;
  953. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  954. if(current_raw[e] >= maxttemp[e]) {
  955. target_raw[e] = 0;
  956. max_temp_error(e);
  957. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  958. {
  959. Stop();;
  960. }
  961. #endif
  962. }
  963. if(current_raw[e] <= minttemp[e]) {
  964. target_raw[e] = 0;
  965. min_temp_error(e);
  966. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  967. {
  968. Stop();
  969. }
  970. #endif
  971. }
  972. }
  973. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  974. if(current_raw_bed >= bed_maxttemp) {
  975. target_raw_bed = 0;
  976. bed_max_temp_error();
  977. Stop();
  978. }
  979. #endif
  980. }
  981. }