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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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=PID_MAX/2;
  134. long d = PID_MAX/2;
  135. float Ku, Tu;
  136. float Kp, Ki, Kd;
  137. float max, min;
  138. if ((extruder > EXTRUDERS)
  139. #if (TEMP_BED_PIN <= -1)
  140. ||(extruder < 0)
  141. #endif
  142. ){
  143. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  144. return;
  145. }
  146. SERIAL_ECHOLN("PID Autotune start");
  147. disable_heater(); // switch off all heaters.
  148. if (extruder<0)
  149. soft_pwm_bed = PID_MAX/2;
  150. else
  151. soft_pwm[extruder] = PID_MAX/2;
  152. for(;;) {
  153. if(temp_meas_ready == true) { // temp sample ready
  154. CRITICAL_SECTION_START;
  155. temp_meas_ready = false;
  156. CRITICAL_SECTION_END;
  157. input = (extruder<0)?analog2tempBed(current_raw_bed):analog2temp(current_raw[extruder], extruder);
  158. max=max(max,input);
  159. min=min(min,input);
  160. if(heating == true && input > temp) {
  161. if(millis() - t2 > 5000) {
  162. heating=false;
  163. if (extruder<0)
  164. soft_pwm_bed = (bias - d) >> 1;
  165. else
  166. soft_pwm[extruder] = (bias - d) >> 1;
  167. t1=millis();
  168. t_high=t1 - t2;
  169. max=temp;
  170. }
  171. }
  172. if(heating == false && input < temp) {
  173. if(millis() - t1 > 5000) {
  174. heating=true;
  175. t2=millis();
  176. t_low=t2 - t1;
  177. if(cycles > 0) {
  178. bias += (d*(t_high - t_low))/(t_low + t_high);
  179. bias = constrain(bias, 20 ,PID_MAX-20);
  180. if(bias > PID_MAX/2) d = PID_MAX - 1 - bias;
  181. else d = bias;
  182. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  183. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  184. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  185. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  186. if(cycles > 2) {
  187. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  188. Tu = ((float)(t_low + t_high)/1000.0);
  189. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  190. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  191. Kp = 0.6*Ku;
  192. Ki = 2*Kp/Tu;
  193. Kd = Kp*Tu/8;
  194. SERIAL_PROTOCOLLNPGM(" Clasic PID ")
  195. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  196. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  197. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  198. /*
  199. Kp = 0.33*Ku;
  200. Ki = Kp/Tu;
  201. Kd = Kp*Tu/3;
  202. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  203. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  204. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  205. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  206. Kp = 0.2*Ku;
  207. Ki = 2*Kp/Tu;
  208. Kd = Kp*Tu/3;
  209. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  210. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  211. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  212. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  213. */
  214. }
  215. }
  216. if (extruder<0)
  217. soft_pwm_bed = (bias + d) >> 1;
  218. else
  219. soft_pwm[extruder] = (bias + d) >> 1;
  220. cycles++;
  221. min=temp;
  222. }
  223. }
  224. }
  225. if(input > (temp + 20)) {
  226. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature to high");
  227. return;
  228. }
  229. if(millis() - temp_millis > 2000) {
  230. int p;
  231. if (extruder<0){
  232. p=soft_pwm_bed;
  233. SERIAL_PROTOCOLPGM("ok B:");
  234. }else{
  235. p=soft_pwm[extruder];
  236. SERIAL_PROTOCOLPGM("ok T:");
  237. }
  238. SERIAL_PROTOCOL(input);
  239. SERIAL_PROTOCOLPGM(" @:");
  240. SERIAL_PROTOCOLLN(p);
  241. temp_millis = millis();
  242. }
  243. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  244. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  245. return;
  246. }
  247. if(cycles > ncycles) {
  248. SERIAL_PROTOCOLLNPGM("PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h");
  249. return;
  250. }
  251. LCD_STATUS;
  252. }
  253. }
  254. void updatePID()
  255. {
  256. #ifdef PIDTEMP
  257. for(int e = 0; e < EXTRUDERS; e++) {
  258. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  259. }
  260. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  261. #endif
  262. }
  263. int getHeaterPower(int heater) {
  264. if (heater<0)
  265. return soft_pwm_bed;
  266. return soft_pwm[heater];
  267. }
  268. void manage_heater()
  269. {
  270. #ifdef USE_WATCHDOG
  271. wd_reset();
  272. #endif
  273. float pid_input;
  274. float pid_output;
  275. if(temp_meas_ready != true) //better readability
  276. return;
  277. CRITICAL_SECTION_START;
  278. temp_meas_ready = false;
  279. CRITICAL_SECTION_END;
  280. for(int e = 0; e < EXTRUDERS; e++)
  281. {
  282. #ifdef PIDTEMP
  283. pid_input = analog2temp(current_raw[e], e);
  284. #ifndef PID_OPENLOOP
  285. pid_error[e] = pid_setpoint[e] - pid_input;
  286. if(pid_error[e] > 10) {
  287. pid_output = PID_MAX;
  288. pid_reset[e] = true;
  289. }
  290. else if(pid_error[e] < -10) {
  291. pid_output = 0;
  292. pid_reset[e] = true;
  293. }
  294. else {
  295. if(pid_reset[e] == true) {
  296. temp_iState[e] = 0.0;
  297. pid_reset[e] = false;
  298. }
  299. pTerm[e] = Kp * pid_error[e];
  300. temp_iState[e] += pid_error[e];
  301. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  302. iTerm[e] = Ki * temp_iState[e];
  303. //K1 defined in Configuration.h in the PID settings
  304. #define K2 (1.0-K1)
  305. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  306. temp_dState[e] = pid_input;
  307. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  308. }
  309. #else
  310. pid_output = constrain(pid_setpoint[e], 0, PID_MAX);
  311. #endif //PID_OPENLOOP
  312. #ifdef PID_DEBUG
  313. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  314. #endif //PID_DEBUG
  315. #else /* PID off */
  316. pid_output = 0;
  317. if(current_raw[e] < target_raw[e]) {
  318. pid_output = PID_MAX;
  319. }
  320. #endif
  321. // Check if temperature is within the correct range
  322. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  323. {
  324. soft_pwm[e] = (int)pid_output >> 1;
  325. }
  326. else {
  327. soft_pwm[e] = 0;
  328. }
  329. } // End extruder for loop
  330. #ifdef WATCHPERIOD
  331. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  332. if(watch_oldtemp[0] >= degHotend(active_extruder)){
  333. setTargetHotend(0,active_extruder);
  334. LCD_MESSAGEPGM("Heating failed");
  335. SERIAL_ECHO_START;
  336. SERIAL_ECHOLN("Heating failed");
  337. }else{
  338. watchmillis = 0;
  339. }
  340. }
  341. #endif
  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, PID_MAX);
  361. #else
  362. pid_output = constrain(pid_setpoint_bed, 0, PID_MAX);
  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 = 100;
  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 = 100;
  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. #warning No heater-type defined for the bed.
  474. return 0;
  475. #endif
  476. }
  477. // Derived from RepRap FiveD extruder::getTemperature()
  478. // For hot end temperature measurement.
  479. float analog2temp(int raw, uint8_t e) {
  480. if(e >= EXTRUDERS)
  481. {
  482. SERIAL_ERROR_START;
  483. SERIAL_ERROR((int)e);
  484. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  485. kill();
  486. }
  487. #ifdef HEATER_0_USES_MAX6675
  488. if (e == 0)
  489. {
  490. return 0.25 * raw;
  491. }
  492. #endif
  493. if(heater_ttbl_map[e] != 0)
  494. {
  495. float celsius = 0;
  496. byte i;
  497. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  498. raw = (1023 * OVERSAMPLENR) - raw;
  499. for (i=1; i<heater_ttbllen_map[e]; i++)
  500. {
  501. if (PGM_RD_W((*tt)[i][0]) > raw)
  502. {
  503. celsius = PGM_RD_W((*tt)[i-1][1]) +
  504. (raw - PGM_RD_W((*tt)[i-1][0])) *
  505. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  506. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  507. break;
  508. }
  509. }
  510. // Overflow: Set to last value in the table
  511. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  512. return celsius;
  513. }
  514. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  515. }
  516. // Derived from RepRap FiveD extruder::getTemperature()
  517. // For bed temperature measurement.
  518. float analog2tempBed(int raw) {
  519. #ifdef BED_USES_THERMISTOR
  520. float celsius = 0;
  521. byte i;
  522. raw = (1023 * OVERSAMPLENR) - raw;
  523. for (i=1; i<bedtemptable_len; i++)
  524. {
  525. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  526. {
  527. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  528. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  529. (float)(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  530. (float)(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  531. break;
  532. }
  533. }
  534. // Overflow: Set to last value in the table
  535. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  536. return celsius;
  537. #elif defined BED_USES_AD595
  538. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  539. #else
  540. #warning No heater-type defined for the bed.
  541. return 0;
  542. #endif
  543. }
  544. void tp_init()
  545. {
  546. // Finish init of mult extruder arrays
  547. for(int e = 0; e < EXTRUDERS; e++) {
  548. // populate with the first value
  549. #ifdef WATCHPERIOD
  550. watch_raw[e] = watch_raw[0];
  551. #endif
  552. maxttemp[e] = maxttemp[0];
  553. #ifdef PIDTEMP
  554. temp_iState_min[e] = 0.0;
  555. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  556. temp_iState_min_bed = 0.0;
  557. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  558. #endif //PIDTEMP
  559. }
  560. #if (HEATER_0_PIN > -1)
  561. SET_OUTPUT(HEATER_0_PIN);
  562. #endif
  563. #if (HEATER_1_PIN > -1)
  564. SET_OUTPUT(HEATER_1_PIN);
  565. #endif
  566. #if (HEATER_2_PIN > -1)
  567. SET_OUTPUT(HEATER_2_PIN);
  568. #endif
  569. #if (HEATER_BED_PIN > -1)
  570. SET_OUTPUT(HEATER_BED_PIN);
  571. #endif
  572. #if (FAN_PIN > -1)
  573. SET_OUTPUT(FAN_PIN);
  574. #ifdef FAST_PWM_FAN
  575. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  576. #endif
  577. #endif
  578. #ifdef HEATER_0_USES_MAX6675
  579. #ifndef SDSUPPORT
  580. SET_OUTPUT(MAX_SCK_PIN);
  581. WRITE(MAX_SCK_PIN,0);
  582. SET_OUTPUT(MAX_MOSI_PIN);
  583. WRITE(MAX_MOSI_PIN,1);
  584. SET_INPUT(MAX_MISO_PIN);
  585. WRITE(MAX_MISO_PIN,1);
  586. #endif
  587. SET_OUTPUT(MAX6675_SS);
  588. WRITE(MAX6675_SS,1);
  589. #endif
  590. // Set analog inputs
  591. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  592. DIDR0 = 0;
  593. #ifdef DIDR2
  594. DIDR2 = 0;
  595. #endif
  596. #if (TEMP_0_PIN > -1)
  597. #if TEMP_0_PIN < 8
  598. DIDR0 |= 1 << TEMP_0_PIN;
  599. #else
  600. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  601. #endif
  602. #endif
  603. #if (TEMP_1_PIN > -1)
  604. #if TEMP_1_PIN < 8
  605. DIDR0 |= 1<<TEMP_1_PIN;
  606. #else
  607. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  608. #endif
  609. #endif
  610. #if (TEMP_2_PIN > -1)
  611. #if TEMP_2_PIN < 8
  612. DIDR0 |= 1 << TEMP_2_PIN;
  613. #else
  614. DIDR2 = 1<<(TEMP_2_PIN - 8);
  615. #endif
  616. #endif
  617. #if (TEMP_BED_PIN > -1)
  618. #if TEMP_BED_PIN < 8
  619. DIDR0 |= 1<<TEMP_BED_PIN;
  620. #else
  621. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  622. #endif
  623. #endif
  624. // Use timer0 for temperature measurement
  625. // Interleave temperature interrupt with millies interrupt
  626. OCR0B = 128;
  627. TIMSK0 |= (1<<OCIE0B);
  628. // Wait for temperature measurement to settle
  629. delay(250);
  630. #ifdef HEATER_0_MINTEMP
  631. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  632. #endif //MINTEMP
  633. #ifdef HEATER_0_MAXTEMP
  634. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  635. #endif //MAXTEMP
  636. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  637. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  638. #endif // MINTEMP 1
  639. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  640. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  641. #endif //MAXTEMP 1
  642. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  643. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  644. #endif //MINTEMP 2
  645. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  646. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  647. #endif //MAXTEMP 2
  648. #ifdef BED_MINTEMP
  649. bed_minttemp = temp2analogBed(BED_MINTEMP);
  650. #endif //BED_MINTEMP
  651. #ifdef BED_MAXTEMP
  652. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  653. #endif //BED_MAXTEMP
  654. }
  655. void setWatch()
  656. {
  657. #ifdef WATCHPERIOD
  658. int t = 0;
  659. for (int e = 0; e < EXTRUDERS; e++)
  660. {
  661. if(isHeatingHotend(e))
  662. watch_oldtemp[0] = degHotend(0);
  663. {
  664. t = max(t,millis());
  665. watch_raw[e] = current_raw[e];
  666. }
  667. }
  668. watchmillis = t;
  669. #endif
  670. }
  671. void disable_heater()
  672. {
  673. for(int i=0;i<EXTRUDERS;i++)
  674. setTargetHotend(0,i);
  675. setTargetBed(0);
  676. #if TEMP_0_PIN > -1
  677. target_raw[0]=0;
  678. soft_pwm[0]=0;
  679. #if HEATER_0_PIN > -1
  680. WRITE(HEATER_0_PIN,LOW);
  681. #endif
  682. #endif
  683. #if TEMP_1_PIN > -1
  684. target_raw[1]=0;
  685. soft_pwm[1]=0;
  686. #if HEATER_1_PIN > -1
  687. WRITE(HEATER_1_PIN,LOW);
  688. #endif
  689. #endif
  690. #if TEMP_2_PIN > -1
  691. target_raw[2]=0;
  692. soft_pwm[2]=0;
  693. #if HEATER_2_PIN > -1
  694. WRITE(HEATER_2_PIN,LOW);
  695. #endif
  696. #endif
  697. #if TEMP_BED_PIN > -1
  698. target_raw_bed=0;
  699. soft_pwm_bed=0;
  700. #if HEATER_BED_PIN > -1
  701. WRITE(HEATER_BED_PIN,LOW);
  702. #endif
  703. #endif
  704. }
  705. void max_temp_error(uint8_t e) {
  706. disable_heater();
  707. if(IsStopped() == false) {
  708. SERIAL_ERROR_START;
  709. SERIAL_ERRORLN((int)e);
  710. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  711. }
  712. }
  713. void min_temp_error(uint8_t e) {
  714. disable_heater();
  715. if(IsStopped() == false) {
  716. SERIAL_ERROR_START;
  717. SERIAL_ERRORLN((int)e);
  718. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  719. }
  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. }
  730. #define HEAT_INTERVAL 250
  731. #ifdef HEATER_0_USES_MAX6675
  732. long max6675_previous_millis = -HEAT_INTERVAL;
  733. int max6675_temp = 2000;
  734. int read_max6675()
  735. {
  736. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  737. return max6675_temp;
  738. max6675_previous_millis = millis();
  739. max6675_temp = 0;
  740. #ifdef PRR
  741. PRR &= ~(1<<PRSPI);
  742. #elif defined PRR0
  743. PRR0 &= ~(1<<PRSPI);
  744. #endif
  745. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  746. // enable TT_MAX6675
  747. WRITE(MAX6675_SS, 0);
  748. // ensure 100ns delay - a bit extra is fine
  749. delay(1);
  750. // read MSB
  751. SPDR = 0;
  752. for (;(SPSR & (1<<SPIF)) == 0;);
  753. max6675_temp = SPDR;
  754. max6675_temp <<= 8;
  755. // read LSB
  756. SPDR = 0;
  757. for (;(SPSR & (1<<SPIF)) == 0;);
  758. max6675_temp |= SPDR;
  759. // disable TT_MAX6675
  760. WRITE(MAX6675_SS, 1);
  761. if (max6675_temp & 4)
  762. {
  763. // thermocouple open
  764. max6675_temp = 2000;
  765. }
  766. else
  767. {
  768. max6675_temp = max6675_temp >> 3;
  769. }
  770. return max6675_temp;
  771. }
  772. #endif
  773. // Timer 0 is shared with millies
  774. ISR(TIMER0_COMPB_vect)
  775. {
  776. //these variables are only accesible from the ISR, but static, so they don't loose their value
  777. static unsigned char temp_count = 0;
  778. static unsigned long raw_temp_0_value = 0;
  779. static unsigned long raw_temp_1_value = 0;
  780. static unsigned long raw_temp_2_value = 0;
  781. static unsigned long raw_temp_bed_value = 0;
  782. static unsigned char temp_state = 0;
  783. static unsigned char pwm_count = 1;
  784. static unsigned char soft_pwm_0;
  785. static unsigned char soft_pwm_1;
  786. static unsigned char soft_pwm_2;
  787. static unsigned char soft_pwm_b;
  788. if(pwm_count == 0){
  789. soft_pwm_0 = soft_pwm[0];
  790. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  791. #if EXTRUDERS > 1
  792. soft_pwm_1 = soft_pwm[1];
  793. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  794. #endif
  795. #if EXTRUDERS > 2
  796. soft_pwm_2 = soft_pwm[2];
  797. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  798. #endif
  799. #if HEATER_BED_PIN > -1
  800. soft_pwm_b = soft_pwm_bed;
  801. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1);
  802. #endif
  803. }
  804. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  805. #if EXTRUDERS > 1
  806. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  807. #endif
  808. #if EXTRUDERS > 2
  809. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  810. #endif
  811. #if HEATER_BED_PIN > -1
  812. if(soft_pwm_b <= pwm_count) WRITE(HEATER_BED_PIN,0);
  813. #endif
  814. pwm_count++;
  815. pwm_count &= 0x7f;
  816. switch(temp_state) {
  817. case 0: // Prepare TEMP_0
  818. #if (TEMP_0_PIN > -1)
  819. #if TEMP_0_PIN > 7
  820. ADCSRB = 1<<MUX5;
  821. #else
  822. ADCSRB = 0;
  823. #endif
  824. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  825. ADCSRA |= 1<<ADSC; // Start conversion
  826. #endif
  827. #ifdef ULTIPANEL
  828. buttons_check();
  829. #endif
  830. temp_state = 1;
  831. break;
  832. case 1: // Measure TEMP_0
  833. #if (TEMP_0_PIN > -1)
  834. raw_temp_0_value += ADC;
  835. #endif
  836. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  837. raw_temp_0_value = read_max6675();
  838. #endif
  839. temp_state = 2;
  840. break;
  841. case 2: // Prepare TEMP_BED
  842. #if (TEMP_BED_PIN > -1)
  843. #if TEMP_BED_PIN > 7
  844. ADCSRB = 1<<MUX5;
  845. #else
  846. ADCSRB = 0;
  847. #endif
  848. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  849. ADCSRA |= 1<<ADSC; // Start conversion
  850. #endif
  851. #ifdef ULTIPANEL
  852. buttons_check();
  853. #endif
  854. temp_state = 3;
  855. break;
  856. case 3: // Measure TEMP_BED
  857. #if (TEMP_BED_PIN > -1)
  858. raw_temp_bed_value += ADC;
  859. #endif
  860. temp_state = 4;
  861. break;
  862. case 4: // Prepare TEMP_1
  863. #if (TEMP_1_PIN > -1)
  864. #if TEMP_1_PIN > 7
  865. ADCSRB = 1<<MUX5;
  866. #else
  867. ADCSRB = 0;
  868. #endif
  869. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  870. ADCSRA |= 1<<ADSC; // Start conversion
  871. #endif
  872. #ifdef ULTIPANEL
  873. buttons_check();
  874. #endif
  875. temp_state = 5;
  876. break;
  877. case 5: // Measure TEMP_1
  878. #if (TEMP_1_PIN > -1)
  879. raw_temp_1_value += ADC;
  880. #endif
  881. temp_state = 6;
  882. break;
  883. case 6: // Prepare TEMP_2
  884. #if (TEMP_2_PIN > -1)
  885. #if TEMP_2_PIN > 7
  886. ADCSRB = 1<<MUX5;
  887. #else
  888. ADCSRB = 0;
  889. #endif
  890. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  891. ADCSRA |= 1<<ADSC; // Start conversion
  892. #endif
  893. #ifdef ULTIPANEL
  894. buttons_check();
  895. #endif
  896. temp_state = 7;
  897. break;
  898. case 7: // Measure TEMP_2
  899. #if (TEMP_2_PIN > -1)
  900. raw_temp_2_value += ADC;
  901. #endif
  902. temp_state = 0;
  903. temp_count++;
  904. break;
  905. // default:
  906. // SERIAL_ERROR_START;
  907. // SERIAL_ERRORLNPGM("Temp measurement error!");
  908. // break;
  909. }
  910. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  911. {
  912. #if defined(HEATER_0_USES_AD595) || defined(HEATER_0_USES_MAX6675)
  913. current_raw[0] = raw_temp_0_value;
  914. #else
  915. current_raw[0] = 16383 - raw_temp_0_value;
  916. #endif
  917. #if EXTRUDERS > 1
  918. #ifdef HEATER_1_USES_AD595
  919. current_raw[1] = raw_temp_1_value;
  920. #else
  921. current_raw[1] = 16383 - raw_temp_1_value;
  922. #endif
  923. #endif
  924. #if EXTRUDERS > 2
  925. #ifdef HEATER_2_USES_AD595
  926. current_raw[2] = raw_temp_2_value;
  927. #else
  928. current_raw[2] = 16383 - raw_temp_2_value;
  929. #endif
  930. #endif
  931. #ifdef BED_USES_AD595
  932. current_raw_bed = raw_temp_bed_value;
  933. #else
  934. current_raw_bed = 16383 - raw_temp_bed_value;
  935. #endif
  936. temp_meas_ready = true;
  937. temp_count = 0;
  938. raw_temp_0_value = 0;
  939. raw_temp_1_value = 0;
  940. raw_temp_2_value = 0;
  941. raw_temp_bed_value = 0;
  942. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  943. if(current_raw[e] >= maxttemp[e]) {
  944. target_raw[e] = 0;
  945. max_temp_error(e);
  946. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  947. {
  948. Stop();;
  949. }
  950. #endif
  951. }
  952. if(current_raw[e] <= minttemp[e]) {
  953. target_raw[e] = 0;
  954. min_temp_error(e);
  955. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  956. {
  957. Stop();
  958. }
  959. #endif
  960. }
  961. }
  962. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  963. if(current_raw_bed >= bed_maxttemp) {
  964. target_raw_bed = 0;
  965. bed_max_temp_error();
  966. Stop();
  967. }
  968. #endif
  969. }
  970. }