My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

temperature.cpp 26KB

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