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 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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_temperature[EXTRUDERS] = { 0 };
  31. int target_temperature_bed = 0;
  32. int current_temperature_raw[EXTRUDERS] = { 0 };
  33. float current_temperature[EXTRUDERS] = { 0.0 };
  34. int current_temperature_bed_raw = 0;
  35. float current_temperature_bed = 0.0;
  36. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  37. int redundant_temperature_raw = 0;
  38. float redundant_temperature = 0.0;
  39. #endif
  40. #ifdef PIDTEMP
  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. float bedKp=DEFAULT_bedKp;
  50. float bedKi=(DEFAULT_bedKi*PID_dT);
  51. float bedKd=(DEFAULT_bedKd/PID_dT);
  52. #endif //PIDTEMPBED
  53. #ifdef FAN_SOFT_PWM
  54. unsigned char fanSpeedSoftPwm;
  55. #endif
  56. unsigned char soft_pwm_bed;
  57. #ifdef BABYSTEPPING
  58. volatile int babystepsTodo[3]={0,0,0};
  59. #endif
  60. //===========================================================================
  61. //=============================private variables============================
  62. //===========================================================================
  63. static volatile bool temp_meas_ready = false;
  64. #ifdef PIDTEMP
  65. //static cannot be external:
  66. static float temp_iState[EXTRUDERS] = { 0 };
  67. static float temp_dState[EXTRUDERS] = { 0 };
  68. static float pTerm[EXTRUDERS];
  69. static float iTerm[EXTRUDERS];
  70. static float dTerm[EXTRUDERS];
  71. //int output;
  72. static float pid_error[EXTRUDERS];
  73. static float temp_iState_min[EXTRUDERS];
  74. static float temp_iState_max[EXTRUDERS];
  75. // static float pid_input[EXTRUDERS];
  76. // static float pid_output[EXTRUDERS];
  77. static bool pid_reset[EXTRUDERS];
  78. #endif //PIDTEMP
  79. #ifdef PIDTEMPBED
  80. //static cannot be external:
  81. static float temp_iState_bed = { 0 };
  82. static float temp_dState_bed = { 0 };
  83. static float pTerm_bed;
  84. static float iTerm_bed;
  85. static float dTerm_bed;
  86. //int output;
  87. static float pid_error_bed;
  88. static float temp_iState_min_bed;
  89. static float temp_iState_max_bed;
  90. #else //PIDTEMPBED
  91. static unsigned long previous_millis_bed_heater;
  92. #endif //PIDTEMPBED
  93. static unsigned char soft_pwm[EXTRUDERS];
  94. #ifdef FAN_SOFT_PWM
  95. static unsigned char soft_pwm_fan;
  96. #endif
  97. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  98. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  99. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  100. static unsigned long extruder_autofan_last_check;
  101. #endif
  102. #if EXTRUDERS > 3
  103. # error Unsupported number of extruders
  104. #elif EXTRUDERS > 2
  105. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  106. #elif EXTRUDERS > 1
  107. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  108. #else
  109. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  110. #endif
  111. // Init min and max temp with extreme values to prevent false errors during startup
  112. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  113. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  114. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  115. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  116. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  117. #ifdef BED_MAXTEMP
  118. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  119. #endif
  120. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  121. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  122. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  123. #else
  124. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  125. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  126. #endif
  127. static float analog2temp(int raw, uint8_t e);
  128. static float analog2tempBed(int raw);
  129. static void updateTemperaturesFromRawValues();
  130. #ifdef WATCH_TEMP_PERIOD
  131. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  132. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  133. #endif //WATCH_TEMP_PERIOD
  134. #ifndef SOFT_PWM_SCALE
  135. #define SOFT_PWM_SCALE 0
  136. #endif
  137. //===========================================================================
  138. //============================= functions ============================
  139. //===========================================================================
  140. void PID_autotune(float temp, int extruder, int ncycles)
  141. {
  142. float input = 0.0;
  143. int cycles=0;
  144. bool heating = true;
  145. unsigned long temp_millis = millis();
  146. unsigned long t1=temp_millis;
  147. unsigned long t2=temp_millis;
  148. long t_high = 0;
  149. long t_low = 0;
  150. long bias, d;
  151. float Ku, Tu;
  152. float Kp, Ki, Kd;
  153. float max = 0, min = 10000;
  154. if ((extruder > EXTRUDERS)
  155. #if (TEMP_BED_PIN <= -1)
  156. ||(extruder < 0)
  157. #endif
  158. ){
  159. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  160. return;
  161. }
  162. SERIAL_ECHOLN("PID Autotune start");
  163. disable_heater(); // switch off all heaters.
  164. if (extruder<0)
  165. {
  166. soft_pwm_bed = (MAX_BED_POWER)/2;
  167. bias = d = (MAX_BED_POWER)/2;
  168. }
  169. else
  170. {
  171. soft_pwm[extruder] = (PID_MAX)/2;
  172. bias = d = (PID_MAX)/2;
  173. }
  174. for(;;) {
  175. if(temp_meas_ready == true) { // temp sample ready
  176. updateTemperaturesFromRawValues();
  177. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  178. max=max(max,input);
  179. min=min(min,input);
  180. if(heating == true && input > temp) {
  181. if(millis() - t2 > 5000) {
  182. heating=false;
  183. if (extruder<0)
  184. soft_pwm_bed = (bias - d) >> 1;
  185. else
  186. soft_pwm[extruder] = (bias - d) >> 1;
  187. t1=millis();
  188. t_high=t1 - t2;
  189. max=temp;
  190. }
  191. }
  192. if(heating == false && input < temp) {
  193. if(millis() - t1 > 5000) {
  194. heating=true;
  195. t2=millis();
  196. t_low=t2 - t1;
  197. if(cycles > 0) {
  198. bias += (d*(t_high - t_low))/(t_low + t_high);
  199. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  200. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  201. else d = bias;
  202. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  203. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  204. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  205. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  206. if(cycles > 2) {
  207. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  208. Tu = ((float)(t_low + t_high)/1000.0);
  209. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  210. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  211. Kp = 0.6*Ku;
  212. Ki = 2*Kp/Tu;
  213. Kd = Kp*Tu/8;
  214. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  215. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  216. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  217. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  218. /*
  219. Kp = 0.33*Ku;
  220. Ki = Kp/Tu;
  221. Kd = Kp*Tu/3;
  222. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  223. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  224. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  225. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  226. Kp = 0.2*Ku;
  227. Ki = 2*Kp/Tu;
  228. Kd = Kp*Tu/3;
  229. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  230. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  231. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  232. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  233. */
  234. }
  235. }
  236. if (extruder<0)
  237. soft_pwm_bed = (bias + d) >> 1;
  238. else
  239. soft_pwm[extruder] = (bias + d) >> 1;
  240. cycles++;
  241. min=temp;
  242. }
  243. }
  244. }
  245. if(input > (temp + 20)) {
  246. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  247. return;
  248. }
  249. if(millis() - temp_millis > 2000) {
  250. int p;
  251. if (extruder<0){
  252. p=soft_pwm_bed;
  253. SERIAL_PROTOCOLPGM("ok B:");
  254. }else{
  255. p=soft_pwm[extruder];
  256. SERIAL_PROTOCOLPGM("ok T:");
  257. }
  258. SERIAL_PROTOCOL(input);
  259. SERIAL_PROTOCOLPGM(" @:");
  260. SERIAL_PROTOCOLLN(p);
  261. temp_millis = millis();
  262. }
  263. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  264. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  265. return;
  266. }
  267. if(cycles > ncycles) {
  268. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  269. return;
  270. }
  271. lcd_update();
  272. }
  273. }
  274. void updatePID()
  275. {
  276. #ifdef PIDTEMP
  277. for(int e = 0; e < EXTRUDERS; e++) {
  278. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  279. }
  280. #endif
  281. #ifdef PIDTEMPBED
  282. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  283. #endif
  284. }
  285. int getHeaterPower(int heater) {
  286. if (heater<0)
  287. return soft_pwm_bed;
  288. return soft_pwm[heater];
  289. }
  290. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  291. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  292. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  293. #if defined(FAN_PIN) && FAN_PIN > -1
  294. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  295. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  296. #endif
  297. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  298. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  299. #endif
  300. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  301. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  302. #endif
  303. #endif
  304. void setExtruderAutoFanState(int pin, bool state)
  305. {
  306. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  307. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  308. pinMode(pin, OUTPUT);
  309. digitalWrite(pin, newFanSpeed);
  310. analogWrite(pin, newFanSpeed);
  311. }
  312. void checkExtruderAutoFans()
  313. {
  314. uint8_t fanState = 0;
  315. // which fan pins need to be turned on?
  316. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  317. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  318. fanState |= 1;
  319. #endif
  320. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  321. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  322. {
  323. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  324. fanState |= 1;
  325. else
  326. fanState |= 2;
  327. }
  328. #endif
  329. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  330. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  331. {
  332. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  333. fanState |= 1;
  334. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  335. fanState |= 2;
  336. else
  337. fanState |= 4;
  338. }
  339. #endif
  340. // update extruder auto fan states
  341. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  342. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  343. #endif
  344. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  345. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  346. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  347. #endif
  348. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  349. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  350. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  351. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  352. #endif
  353. }
  354. #endif // any extruder auto fan pins set
  355. void manage_heater()
  356. {
  357. float pid_input;
  358. float pid_output;
  359. if(temp_meas_ready != true) //better readability
  360. return;
  361. updateTemperaturesFromRawValues();
  362. for(int e = 0; e < EXTRUDERS; e++)
  363. {
  364. #ifdef PIDTEMP
  365. pid_input = current_temperature[e];
  366. #ifndef PID_OPENLOOP
  367. pid_error[e] = target_temperature[e] - pid_input;
  368. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  369. pid_output = BANG_MAX;
  370. pid_reset[e] = true;
  371. }
  372. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  373. pid_output = 0;
  374. pid_reset[e] = true;
  375. }
  376. else {
  377. if(pid_reset[e] == true) {
  378. temp_iState[e] = 0.0;
  379. pid_reset[e] = false;
  380. }
  381. pTerm[e] = Kp * pid_error[e];
  382. temp_iState[e] += pid_error[e];
  383. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  384. iTerm[e] = Ki * temp_iState[e];
  385. //K1 defined in Configuration.h in the PID settings
  386. #define K2 (1.0-K1)
  387. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  388. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  389. }
  390. temp_dState[e] = pid_input;
  391. #else
  392. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  393. #endif //PID_OPENLOOP
  394. #ifdef PID_DEBUG
  395. SERIAL_ECHO_START;
  396. SERIAL_ECHO(" PID_DEBUG ");
  397. SERIAL_ECHO(e);
  398. SERIAL_ECHO(": Input ");
  399. SERIAL_ECHO(pid_input);
  400. SERIAL_ECHO(" Output ");
  401. SERIAL_ECHO(pid_output);
  402. SERIAL_ECHO(" pTerm ");
  403. SERIAL_ECHO(pTerm[e]);
  404. SERIAL_ECHO(" iTerm ");
  405. SERIAL_ECHO(iTerm[e]);
  406. SERIAL_ECHO(" dTerm ");
  407. SERIAL_ECHOLN(dTerm[e]);
  408. #endif //PID_DEBUG
  409. #else /* PID off */
  410. pid_output = 0;
  411. if(current_temperature[e] < target_temperature[e]) {
  412. pid_output = PID_MAX;
  413. }
  414. #endif
  415. // Check if temperature is within the correct range
  416. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  417. {
  418. soft_pwm[e] = (int)pid_output >> 1;
  419. }
  420. else {
  421. soft_pwm[e] = 0;
  422. }
  423. #ifdef WATCH_TEMP_PERIOD
  424. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  425. {
  426. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  427. {
  428. setTargetHotend(0, e);
  429. LCD_MESSAGEPGM("Heating failed");
  430. SERIAL_ECHO_START;
  431. SERIAL_ECHOLN("Heating failed");
  432. }else{
  433. watchmillis[e] = 0;
  434. }
  435. }
  436. #endif
  437. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  438. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  439. disable_heater();
  440. if(IsStopped() == false) {
  441. SERIAL_ERROR_START;
  442. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  443. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  444. }
  445. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  446. Stop();
  447. #endif
  448. }
  449. #endif
  450. } // End extruder for loop
  451. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  452. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  453. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  454. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  455. {
  456. checkExtruderAutoFans();
  457. extruder_autofan_last_check = millis();
  458. }
  459. #endif
  460. #ifndef PIDTEMPBED
  461. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  462. return;
  463. previous_millis_bed_heater = millis();
  464. #endif
  465. #if TEMP_SENSOR_BED != 0
  466. #ifdef PIDTEMPBED
  467. pid_input = current_temperature_bed;
  468. #ifndef PID_OPENLOOP
  469. pid_error_bed = target_temperature_bed - pid_input;
  470. pTerm_bed = bedKp * pid_error_bed;
  471. temp_iState_bed += pid_error_bed;
  472. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  473. iTerm_bed = bedKi * temp_iState_bed;
  474. //K1 defined in Configuration.h in the PID settings
  475. #define K2 (1.0-K1)
  476. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  477. temp_dState_bed = pid_input;
  478. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  479. #else
  480. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  481. #endif //PID_OPENLOOP
  482. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  483. {
  484. soft_pwm_bed = (int)pid_output >> 1;
  485. }
  486. else {
  487. soft_pwm_bed = 0;
  488. }
  489. #elif !defined(BED_LIMIT_SWITCHING)
  490. // Check if temperature is within the correct range
  491. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  492. {
  493. if(current_temperature_bed >= target_temperature_bed)
  494. {
  495. soft_pwm_bed = 0;
  496. }
  497. else
  498. {
  499. soft_pwm_bed = MAX_BED_POWER>>1;
  500. }
  501. }
  502. else
  503. {
  504. soft_pwm_bed = 0;
  505. WRITE(HEATER_BED_PIN,LOW);
  506. }
  507. #else //#ifdef BED_LIMIT_SWITCHING
  508. // Check if temperature is within the correct band
  509. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  510. {
  511. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  512. {
  513. soft_pwm_bed = 0;
  514. }
  515. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  516. {
  517. soft_pwm_bed = MAX_BED_POWER>>1;
  518. }
  519. }
  520. else
  521. {
  522. soft_pwm_bed = 0;
  523. WRITE(HEATER_BED_PIN,LOW);
  524. }
  525. #endif
  526. #endif
  527. }
  528. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  529. // Derived from RepRap FiveD extruder::getTemperature()
  530. // For hot end temperature measurement.
  531. static float analog2temp(int raw, uint8_t e) {
  532. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  533. if(e > EXTRUDERS)
  534. #else
  535. if(e >= EXTRUDERS)
  536. #endif
  537. {
  538. SERIAL_ERROR_START;
  539. SERIAL_ERROR((int)e);
  540. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  541. kill();
  542. }
  543. #ifdef HEATER_0_USES_MAX6675
  544. if (e == 0)
  545. {
  546. return 0.25 * raw;
  547. }
  548. #endif
  549. if(heater_ttbl_map[e] != NULL)
  550. {
  551. float celsius = 0;
  552. uint8_t i;
  553. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  554. for (i=1; i<heater_ttbllen_map[e]; i++)
  555. {
  556. if (PGM_RD_W((*tt)[i][0]) > raw)
  557. {
  558. celsius = PGM_RD_W((*tt)[i-1][1]) +
  559. (raw - PGM_RD_W((*tt)[i-1][0])) *
  560. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  561. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  562. break;
  563. }
  564. }
  565. // Overflow: Set to last value in the table
  566. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  567. return celsius;
  568. }
  569. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  570. }
  571. // Derived from RepRap FiveD extruder::getTemperature()
  572. // For bed temperature measurement.
  573. static float analog2tempBed(int raw) {
  574. #ifdef BED_USES_THERMISTOR
  575. float celsius = 0;
  576. byte i;
  577. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  578. {
  579. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  580. {
  581. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  582. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  583. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  584. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  585. break;
  586. }
  587. }
  588. // Overflow: Set to last value in the table
  589. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  590. return celsius;
  591. #elif defined BED_USES_AD595
  592. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  593. #else
  594. return 0;
  595. #endif
  596. }
  597. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  598. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  599. static void updateTemperaturesFromRawValues()
  600. {
  601. for(uint8_t e=0;e<EXTRUDERS;e++)
  602. {
  603. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  604. }
  605. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  606. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  607. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  608. #endif
  609. //Reset the watchdog after we know we have a temperature measurement.
  610. watchdog_reset();
  611. CRITICAL_SECTION_START;
  612. temp_meas_ready = false;
  613. CRITICAL_SECTION_END;
  614. }
  615. void tp_init()
  616. {
  617. #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  618. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  619. MCUCR=(1<<JTD);
  620. MCUCR=(1<<JTD);
  621. #endif
  622. // Finish init of mult extruder arrays
  623. for(int e = 0; e < EXTRUDERS; e++) {
  624. // populate with the first value
  625. maxttemp[e] = maxttemp[0];
  626. #ifdef PIDTEMP
  627. temp_iState_min[e] = 0.0;
  628. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  629. #endif //PIDTEMP
  630. #ifdef PIDTEMPBED
  631. temp_iState_min_bed = 0.0;
  632. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  633. #endif //PIDTEMPBED
  634. }
  635. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  636. SET_OUTPUT(HEATER_0_PIN);
  637. #endif
  638. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  639. SET_OUTPUT(HEATER_1_PIN);
  640. #endif
  641. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  642. SET_OUTPUT(HEATER_2_PIN);
  643. #endif
  644. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  645. SET_OUTPUT(HEATER_BED_PIN);
  646. #endif
  647. #if defined(FAN_PIN) && (FAN_PIN > -1)
  648. SET_OUTPUT(FAN_PIN);
  649. #ifdef FAST_PWM_FAN
  650. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  651. #endif
  652. #ifdef FAN_SOFT_PWM
  653. soft_pwm_fan = fanSpeedSoftPwm / 2;
  654. #endif
  655. #endif
  656. #ifdef HEATER_0_USES_MAX6675
  657. #ifndef SDSUPPORT
  658. SET_OUTPUT(MAX_SCK_PIN);
  659. WRITE(MAX_SCK_PIN,0);
  660. SET_OUTPUT(MAX_MOSI_PIN);
  661. WRITE(MAX_MOSI_PIN,1);
  662. SET_INPUT(MAX_MISO_PIN);
  663. WRITE(MAX_MISO_PIN,1);
  664. #endif
  665. SET_OUTPUT(MAX6675_SS);
  666. WRITE(MAX6675_SS,1);
  667. #endif
  668. // Set analog inputs
  669. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  670. DIDR0 = 0;
  671. #ifdef DIDR2
  672. DIDR2 = 0;
  673. #endif
  674. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  675. #if TEMP_0_PIN < 8
  676. DIDR0 |= 1 << TEMP_0_PIN;
  677. #else
  678. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  679. #endif
  680. #endif
  681. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  682. #if TEMP_1_PIN < 8
  683. DIDR0 |= 1<<TEMP_1_PIN;
  684. #else
  685. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  686. #endif
  687. #endif
  688. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  689. #if TEMP_2_PIN < 8
  690. DIDR0 |= 1 << TEMP_2_PIN;
  691. #else
  692. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  693. #endif
  694. #endif
  695. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  696. #if TEMP_BED_PIN < 8
  697. DIDR0 |= 1<<TEMP_BED_PIN;
  698. #else
  699. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  700. #endif
  701. #endif
  702. // Use timer0 for temperature measurement
  703. // Interleave temperature interrupt with millies interrupt
  704. OCR0B = 128;
  705. TIMSK0 |= (1<<OCIE0B);
  706. // Wait for temperature measurement to settle
  707. delay(250);
  708. #ifdef HEATER_0_MINTEMP
  709. minttemp[0] = HEATER_0_MINTEMP;
  710. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  711. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  712. minttemp_raw[0] += OVERSAMPLENR;
  713. #else
  714. minttemp_raw[0] -= OVERSAMPLENR;
  715. #endif
  716. }
  717. #endif //MINTEMP
  718. #ifdef HEATER_0_MAXTEMP
  719. maxttemp[0] = HEATER_0_MAXTEMP;
  720. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  721. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  722. maxttemp_raw[0] -= OVERSAMPLENR;
  723. #else
  724. maxttemp_raw[0] += OVERSAMPLENR;
  725. #endif
  726. }
  727. #endif //MAXTEMP
  728. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  729. minttemp[1] = HEATER_1_MINTEMP;
  730. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  731. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  732. minttemp_raw[1] += OVERSAMPLENR;
  733. #else
  734. minttemp_raw[1] -= OVERSAMPLENR;
  735. #endif
  736. }
  737. #endif // MINTEMP 1
  738. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  739. maxttemp[1] = HEATER_1_MAXTEMP;
  740. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  741. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  742. maxttemp_raw[1] -= OVERSAMPLENR;
  743. #else
  744. maxttemp_raw[1] += OVERSAMPLENR;
  745. #endif
  746. }
  747. #endif //MAXTEMP 1
  748. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  749. minttemp[2] = HEATER_2_MINTEMP;
  750. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  751. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  752. minttemp_raw[2] += OVERSAMPLENR;
  753. #else
  754. minttemp_raw[2] -= OVERSAMPLENR;
  755. #endif
  756. }
  757. #endif //MINTEMP 2
  758. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  759. maxttemp[2] = HEATER_2_MAXTEMP;
  760. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  761. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  762. maxttemp_raw[2] -= OVERSAMPLENR;
  763. #else
  764. maxttemp_raw[2] += OVERSAMPLENR;
  765. #endif
  766. }
  767. #endif //MAXTEMP 2
  768. #ifdef BED_MINTEMP
  769. /* No bed MINTEMP error implemented?!? */ /*
  770. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  771. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  772. bed_minttemp_raw += OVERSAMPLENR;
  773. #else
  774. bed_minttemp_raw -= OVERSAMPLENR;
  775. #endif
  776. }
  777. */
  778. #endif //BED_MINTEMP
  779. #ifdef BED_MAXTEMP
  780. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  781. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  782. bed_maxttemp_raw -= OVERSAMPLENR;
  783. #else
  784. bed_maxttemp_raw += OVERSAMPLENR;
  785. #endif
  786. }
  787. #endif //BED_MAXTEMP
  788. }
  789. void setWatch()
  790. {
  791. #ifdef WATCH_TEMP_PERIOD
  792. for (int e = 0; e < EXTRUDERS; e++)
  793. {
  794. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  795. {
  796. watch_start_temp[e] = degHotend(e);
  797. watchmillis[e] = millis();
  798. }
  799. }
  800. #endif
  801. }
  802. void disable_heater()
  803. {
  804. for(int i=0;i<EXTRUDERS;i++)
  805. setTargetHotend(0,i);
  806. setTargetBed(0);
  807. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  808. target_temperature[0]=0;
  809. soft_pwm[0]=0;
  810. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  811. WRITE(HEATER_0_PIN,LOW);
  812. #endif
  813. #endif
  814. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1
  815. target_temperature[1]=0;
  816. soft_pwm[1]=0;
  817. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  818. WRITE(HEATER_1_PIN,LOW);
  819. #endif
  820. #endif
  821. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1
  822. target_temperature[2]=0;
  823. soft_pwm[2]=0;
  824. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  825. WRITE(HEATER_2_PIN,LOW);
  826. #endif
  827. #endif
  828. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  829. target_temperature_bed=0;
  830. soft_pwm_bed=0;
  831. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  832. WRITE(HEATER_BED_PIN,LOW);
  833. #endif
  834. #endif
  835. }
  836. void max_temp_error(uint8_t e) {
  837. disable_heater();
  838. if(IsStopped() == false) {
  839. SERIAL_ERROR_START;
  840. SERIAL_ERRORLN((int)e);
  841. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  842. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  843. }
  844. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  845. Stop();
  846. #endif
  847. }
  848. void min_temp_error(uint8_t e) {
  849. disable_heater();
  850. if(IsStopped() == false) {
  851. SERIAL_ERROR_START;
  852. SERIAL_ERRORLN((int)e);
  853. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  854. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  855. }
  856. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  857. Stop();
  858. #endif
  859. }
  860. void bed_max_temp_error(void) {
  861. #if HEATER_BED_PIN > -1
  862. WRITE(HEATER_BED_PIN, 0);
  863. #endif
  864. if(IsStopped() == false) {
  865. SERIAL_ERROR_START;
  866. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  867. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  868. }
  869. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  870. Stop();
  871. #endif
  872. }
  873. #ifdef HEATER_0_USES_MAX6675
  874. #define MAX6675_HEAT_INTERVAL 250
  875. long max6675_previous_millis = -HEAT_INTERVAL;
  876. int max6675_temp = 2000;
  877. int read_max6675()
  878. {
  879. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  880. return max6675_temp;
  881. max6675_previous_millis = millis();
  882. max6675_temp = 0;
  883. #ifdef PRR
  884. PRR &= ~(1<<PRSPI);
  885. #elif defined PRR0
  886. PRR0 &= ~(1<<PRSPI);
  887. #endif
  888. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  889. // enable TT_MAX6675
  890. WRITE(MAX6675_SS, 0);
  891. // ensure 100ns delay - a bit extra is fine
  892. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  893. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  894. // read MSB
  895. SPDR = 0;
  896. for (;(SPSR & (1<<SPIF)) == 0;);
  897. max6675_temp = SPDR;
  898. max6675_temp <<= 8;
  899. // read LSB
  900. SPDR = 0;
  901. for (;(SPSR & (1<<SPIF)) == 0;);
  902. max6675_temp |= SPDR;
  903. // disable TT_MAX6675
  904. WRITE(MAX6675_SS, 1);
  905. if (max6675_temp & 4)
  906. {
  907. // thermocouple open
  908. max6675_temp = 2000;
  909. }
  910. else
  911. {
  912. max6675_temp = max6675_temp >> 3;
  913. }
  914. return max6675_temp;
  915. }
  916. #endif
  917. // Timer 0 is shared with millies
  918. ISR(TIMER0_COMPB_vect)
  919. {
  920. //these variables are only accesible from the ISR, but static, so they don't lose their value
  921. static unsigned char temp_count = 0;
  922. static unsigned long raw_temp_0_value = 0;
  923. static unsigned long raw_temp_1_value = 0;
  924. static unsigned long raw_temp_2_value = 0;
  925. static unsigned long raw_temp_bed_value = 0;
  926. static unsigned char temp_state = 8;
  927. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  928. static unsigned char soft_pwm_0;
  929. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  930. static unsigned char soft_pwm_1;
  931. #endif
  932. #if EXTRUDERS > 2
  933. static unsigned char soft_pwm_2;
  934. #endif
  935. #if HEATER_BED_PIN > -1
  936. static unsigned char soft_pwm_b;
  937. #endif
  938. if(pwm_count == 0){
  939. soft_pwm_0 = soft_pwm[0];
  940. if(soft_pwm_0 > 0) {
  941. WRITE(HEATER_0_PIN,1);
  942. #ifdef HEATERS_PARALLEL
  943. WRITE(HEATER_1_PIN,1);
  944. #endif
  945. } else WRITE(HEATER_0_PIN,0);
  946. #if EXTRUDERS > 1
  947. soft_pwm_1 = soft_pwm[1];
  948. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  949. #endif
  950. #if EXTRUDERS > 2
  951. soft_pwm_2 = soft_pwm[2];
  952. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  953. #endif
  954. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  955. soft_pwm_b = soft_pwm_bed;
  956. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  957. #endif
  958. #ifdef FAN_SOFT_PWM
  959. soft_pwm_fan = fanSpeedSoftPwm / 2;
  960. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  961. #endif
  962. }
  963. if(soft_pwm_0 < pwm_count) {
  964. WRITE(HEATER_0_PIN,0);
  965. #ifdef HEATERS_PARALLEL
  966. WRITE(HEATER_1_PIN,0);
  967. #endif
  968. }
  969. #if EXTRUDERS > 1
  970. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  971. #endif
  972. #if EXTRUDERS > 2
  973. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  974. #endif
  975. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  976. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  977. #endif
  978. #ifdef FAN_SOFT_PWM
  979. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  980. #endif
  981. pwm_count += (1 << SOFT_PWM_SCALE);
  982. pwm_count &= 0x7f;
  983. switch(temp_state) {
  984. case 0: // Prepare TEMP_0
  985. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  986. #if TEMP_0_PIN > 7
  987. ADCSRB = 1<<MUX5;
  988. #else
  989. ADCSRB = 0;
  990. #endif
  991. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  992. ADCSRA |= 1<<ADSC; // Start conversion
  993. #endif
  994. lcd_buttons_update();
  995. temp_state = 1;
  996. break;
  997. case 1: // Measure TEMP_0
  998. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  999. raw_temp_0_value += ADC;
  1000. #endif
  1001. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1002. raw_temp_0_value = read_max6675();
  1003. #endif
  1004. temp_state = 2;
  1005. break;
  1006. case 2: // Prepare TEMP_BED
  1007. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1008. #if TEMP_BED_PIN > 7
  1009. ADCSRB = 1<<MUX5;
  1010. #else
  1011. ADCSRB = 0;
  1012. #endif
  1013. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1014. ADCSRA |= 1<<ADSC; // Start conversion
  1015. #endif
  1016. lcd_buttons_update();
  1017. temp_state = 3;
  1018. break;
  1019. case 3: // Measure TEMP_BED
  1020. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1021. raw_temp_bed_value += ADC;
  1022. #endif
  1023. temp_state = 4;
  1024. break;
  1025. case 4: // Prepare TEMP_1
  1026. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1027. #if TEMP_1_PIN > 7
  1028. ADCSRB = 1<<MUX5;
  1029. #else
  1030. ADCSRB = 0;
  1031. #endif
  1032. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1033. ADCSRA |= 1<<ADSC; // Start conversion
  1034. #endif
  1035. lcd_buttons_update();
  1036. temp_state = 5;
  1037. break;
  1038. case 5: // Measure TEMP_1
  1039. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1040. raw_temp_1_value += ADC;
  1041. #endif
  1042. temp_state = 6;
  1043. break;
  1044. case 6: // Prepare TEMP_2
  1045. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1046. #if TEMP_2_PIN > 7
  1047. ADCSRB = 1<<MUX5;
  1048. #else
  1049. ADCSRB = 0;
  1050. #endif
  1051. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1052. ADCSRA |= 1<<ADSC; // Start conversion
  1053. #endif
  1054. lcd_buttons_update();
  1055. temp_state = 7;
  1056. break;
  1057. case 7: // Measure TEMP_2
  1058. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1059. raw_temp_2_value += ADC;
  1060. #endif
  1061. temp_state = 0;
  1062. temp_count++;
  1063. break;
  1064. case 8: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1065. temp_state = 0;
  1066. break;
  1067. // default:
  1068. // SERIAL_ERROR_START;
  1069. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1070. // break;
  1071. }
  1072. if(temp_count >= OVERSAMPLENR) // 8 * 16 * 1/(16000000/64/256) = 131ms.
  1073. {
  1074. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1075. {
  1076. current_temperature_raw[0] = raw_temp_0_value;
  1077. #if EXTRUDERS > 1
  1078. current_temperature_raw[1] = raw_temp_1_value;
  1079. #endif
  1080. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1081. redundant_temperature_raw = raw_temp_1_value;
  1082. #endif
  1083. #if EXTRUDERS > 2
  1084. current_temperature_raw[2] = raw_temp_2_value;
  1085. #endif
  1086. current_temperature_bed_raw = raw_temp_bed_value;
  1087. }
  1088. temp_meas_ready = true;
  1089. temp_count = 0;
  1090. raw_temp_0_value = 0;
  1091. raw_temp_1_value = 0;
  1092. raw_temp_2_value = 0;
  1093. raw_temp_bed_value = 0;
  1094. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1095. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1096. #else
  1097. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1098. #endif
  1099. max_temp_error(0);
  1100. }
  1101. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1102. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1103. #else
  1104. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1105. #endif
  1106. min_temp_error(0);
  1107. }
  1108. #if EXTRUDERS > 1
  1109. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1110. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1111. #else
  1112. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1113. #endif
  1114. max_temp_error(1);
  1115. }
  1116. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1117. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1118. #else
  1119. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1120. #endif
  1121. min_temp_error(1);
  1122. }
  1123. #endif
  1124. #if EXTRUDERS > 2
  1125. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1126. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1127. #else
  1128. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1129. #endif
  1130. max_temp_error(2);
  1131. }
  1132. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1133. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1134. #else
  1135. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1136. #endif
  1137. min_temp_error(2);
  1138. }
  1139. #endif
  1140. /* No bed MINTEMP error? */
  1141. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1142. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1143. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1144. #else
  1145. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1146. #endif
  1147. target_temperature_bed = 0;
  1148. bed_max_temp_error();
  1149. }
  1150. #endif
  1151. }
  1152. #ifdef BABYSTEPPING
  1153. for(uint8_t axis=0;axis<3;axis++)
  1154. {
  1155. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1156. if(curTodo>0)
  1157. {
  1158. babystep(axis,/*fwd*/true);
  1159. babystepsTodo[axis]--; //less to do next time
  1160. }
  1161. else
  1162. if(curTodo<0)
  1163. {
  1164. babystep(axis,/*fwd*/false);
  1165. babystepsTodo[axis]++; //less to do next time
  1166. }
  1167. }
  1168. #endif //BABYSTEPPING
  1169. }
  1170. #ifdef PIDTEMP
  1171. // Apply the scale factors to the PID values
  1172. float scalePID_i(float i)
  1173. {
  1174. return i*PID_dT;
  1175. }
  1176. float unscalePID_i(float i)
  1177. {
  1178. return i/PID_dT;
  1179. }
  1180. float scalePID_d(float d)
  1181. {
  1182. return d/PID_dT;
  1183. }
  1184. float unscalePID_d(float d)
  1185. {
  1186. return d*PID_dT;
  1187. }
  1188. #endif //PIDTEMP