My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 29KB

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