My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

temperature.cpp 27KB

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