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

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