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

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