My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 29KB

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