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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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 THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  365. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_RUNAWAY_PROTECTION_PERIOD, THERMAL_RUNAWAY_PROTECTION_HYSTERESIS);
  366. #endif
  367. #ifdef PIDTEMP
  368. pid_input = current_temperature[e];
  369. #ifndef PID_OPENLOOP
  370. pid_error[e] = target_temperature[e] - pid_input;
  371. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  372. pid_output = BANG_MAX;
  373. pid_reset[e] = true;
  374. }
  375. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  376. pid_output = 0;
  377. pid_reset[e] = true;
  378. }
  379. else {
  380. if(pid_reset[e] == true) {
  381. temp_iState[e] = 0.0;
  382. pid_reset[e] = false;
  383. }
  384. pTerm[e] = Kp * pid_error[e];
  385. temp_iState[e] += pid_error[e];
  386. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  387. iTerm[e] = Ki * temp_iState[e];
  388. //K1 defined in Configuration.h in the PID settings
  389. #define K2 (1.0-K1)
  390. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  391. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  392. }
  393. temp_dState[e] = pid_input;
  394. #else
  395. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  396. #endif //PID_OPENLOOP
  397. #ifdef PID_DEBUG
  398. SERIAL_ECHO_START;
  399. SERIAL_ECHO(" PID_DEBUG ");
  400. SERIAL_ECHO(e);
  401. SERIAL_ECHO(": Input ");
  402. SERIAL_ECHO(pid_input);
  403. SERIAL_ECHO(" Output ");
  404. SERIAL_ECHO(pid_output);
  405. SERIAL_ECHO(" pTerm ");
  406. SERIAL_ECHO(pTerm[e]);
  407. SERIAL_ECHO(" iTerm ");
  408. SERIAL_ECHO(iTerm[e]);
  409. SERIAL_ECHO(" dTerm ");
  410. SERIAL_ECHOLN(dTerm[e]);
  411. #endif //PID_DEBUG
  412. #else /* PID off */
  413. pid_output = 0;
  414. if(current_temperature[e] < target_temperature[e]) {
  415. pid_output = PID_MAX;
  416. }
  417. #endif
  418. // Check if temperature is within the correct range
  419. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  420. {
  421. soft_pwm[e] = (int)pid_output >> 1;
  422. }
  423. else {
  424. soft_pwm[e] = 0;
  425. }
  426. #ifdef WATCH_TEMP_PERIOD
  427. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  428. {
  429. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  430. {
  431. setTargetHotend(0, e);
  432. LCD_MESSAGEPGM("Heating failed");
  433. SERIAL_ECHO_START;
  434. SERIAL_ECHOLN("Heating failed");
  435. }else{
  436. watchmillis[e] = 0;
  437. }
  438. }
  439. #endif
  440. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  441. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  442. disable_heater();
  443. if(IsStopped() == false) {
  444. SERIAL_ERROR_START;
  445. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  446. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  447. }
  448. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  449. Stop();
  450. #endif
  451. }
  452. #endif
  453. } // End extruder for loop
  454. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  455. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  456. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  457. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  458. {
  459. checkExtruderAutoFans();
  460. extruder_autofan_last_check = millis();
  461. }
  462. #endif
  463. #ifndef PIDTEMPBED
  464. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  465. return;
  466. previous_millis_bed_heater = millis();
  467. #endif
  468. #if TEMP_SENSOR_BED != 0
  469. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  470. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
  471. #endif
  472. #ifdef PIDTEMPBED
  473. pid_input = current_temperature_bed;
  474. #ifndef PID_OPENLOOP
  475. pid_error_bed = target_temperature_bed - pid_input;
  476. pTerm_bed = bedKp * pid_error_bed;
  477. temp_iState_bed += pid_error_bed;
  478. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  479. iTerm_bed = bedKi * temp_iState_bed;
  480. //K1 defined in Configuration.h in the PID settings
  481. #define K2 (1.0-K1)
  482. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  483. temp_dState_bed = pid_input;
  484. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  485. #else
  486. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  487. #endif //PID_OPENLOOP
  488. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  489. {
  490. soft_pwm_bed = (int)pid_output >> 1;
  491. }
  492. else {
  493. soft_pwm_bed = 0;
  494. }
  495. #elif !defined(BED_LIMIT_SWITCHING)
  496. // Check if temperature is within the correct range
  497. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  498. {
  499. if(current_temperature_bed >= target_temperature_bed)
  500. {
  501. soft_pwm_bed = 0;
  502. }
  503. else
  504. {
  505. soft_pwm_bed = MAX_BED_POWER>>1;
  506. }
  507. }
  508. else
  509. {
  510. soft_pwm_bed = 0;
  511. WRITE(HEATER_BED_PIN,LOW);
  512. }
  513. #else //#ifdef BED_LIMIT_SWITCHING
  514. // Check if temperature is within the correct band
  515. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  516. {
  517. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  518. {
  519. soft_pwm_bed = 0;
  520. }
  521. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  522. {
  523. soft_pwm_bed = MAX_BED_POWER>>1;
  524. }
  525. }
  526. else
  527. {
  528. soft_pwm_bed = 0;
  529. WRITE(HEATER_BED_PIN,LOW);
  530. }
  531. #endif
  532. #endif
  533. }
  534. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  535. // Derived from RepRap FiveD extruder::getTemperature()
  536. // For hot end temperature measurement.
  537. static float analog2temp(int raw, uint8_t e) {
  538. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  539. if(e > EXTRUDERS)
  540. #else
  541. if(e >= EXTRUDERS)
  542. #endif
  543. {
  544. SERIAL_ERROR_START;
  545. SERIAL_ERROR((int)e);
  546. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  547. kill();
  548. return 0.0;
  549. }
  550. #ifdef HEATER_0_USES_MAX6675
  551. if (e == 0)
  552. {
  553. return 0.25 * raw;
  554. }
  555. #endif
  556. if(heater_ttbl_map[e] != NULL)
  557. {
  558. float celsius = 0;
  559. uint8_t i;
  560. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  561. for (i=1; i<heater_ttbllen_map[e]; i++)
  562. {
  563. if (PGM_RD_W((*tt)[i][0]) > raw)
  564. {
  565. celsius = PGM_RD_W((*tt)[i-1][1]) +
  566. (raw - PGM_RD_W((*tt)[i-1][0])) *
  567. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  568. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  569. break;
  570. }
  571. }
  572. // Overflow: Set to last value in the table
  573. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  574. return celsius;
  575. }
  576. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  577. }
  578. // Derived from RepRap FiveD extruder::getTemperature()
  579. // For bed temperature measurement.
  580. static float analog2tempBed(int raw) {
  581. #ifdef BED_USES_THERMISTOR
  582. float celsius = 0;
  583. byte i;
  584. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  585. {
  586. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  587. {
  588. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  589. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  590. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  591. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  592. break;
  593. }
  594. }
  595. // Overflow: Set to last value in the table
  596. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  597. return celsius;
  598. #elif defined BED_USES_AD595
  599. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  600. #else
  601. return 0;
  602. #endif
  603. }
  604. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  605. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  606. static void updateTemperaturesFromRawValues()
  607. {
  608. for(uint8_t e=0;e<EXTRUDERS;e++)
  609. {
  610. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  611. }
  612. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  613. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  614. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  615. #endif
  616. //Reset the watchdog after we know we have a temperature measurement.
  617. watchdog_reset();
  618. CRITICAL_SECTION_START;
  619. temp_meas_ready = false;
  620. CRITICAL_SECTION_END;
  621. }
  622. void tp_init()
  623. {
  624. #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  625. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  626. MCUCR=(1<<JTD);
  627. MCUCR=(1<<JTD);
  628. #endif
  629. // Finish init of mult extruder arrays
  630. for(int e = 0; e < EXTRUDERS; e++) {
  631. // populate with the first value
  632. maxttemp[e] = maxttemp[0];
  633. #ifdef PIDTEMP
  634. temp_iState_min[e] = 0.0;
  635. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  636. #endif //PIDTEMP
  637. #ifdef PIDTEMPBED
  638. temp_iState_min_bed = 0.0;
  639. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  640. #endif //PIDTEMPBED
  641. }
  642. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  643. SET_OUTPUT(HEATER_0_PIN);
  644. #endif
  645. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  646. SET_OUTPUT(HEATER_1_PIN);
  647. #endif
  648. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  649. SET_OUTPUT(HEATER_2_PIN);
  650. #endif
  651. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  652. SET_OUTPUT(HEATER_BED_PIN);
  653. #endif
  654. #if defined(FAN_PIN) && (FAN_PIN > -1)
  655. SET_OUTPUT(FAN_PIN);
  656. #ifdef FAST_PWM_FAN
  657. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  658. #endif
  659. #ifdef FAN_SOFT_PWM
  660. soft_pwm_fan = fanSpeedSoftPwm / 2;
  661. #endif
  662. #endif
  663. #ifdef HEATER_0_USES_MAX6675
  664. #ifndef SDSUPPORT
  665. SET_OUTPUT(MAX_SCK_PIN);
  666. WRITE(MAX_SCK_PIN,0);
  667. SET_OUTPUT(MAX_MOSI_PIN);
  668. WRITE(MAX_MOSI_PIN,1);
  669. SET_INPUT(MAX_MISO_PIN);
  670. WRITE(MAX_MISO_PIN,1);
  671. #endif
  672. SET_OUTPUT(MAX6675_SS);
  673. WRITE(MAX6675_SS,1);
  674. #endif
  675. // Set analog inputs
  676. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  677. DIDR0 = 0;
  678. #ifdef DIDR2
  679. DIDR2 = 0;
  680. #endif
  681. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  682. #if TEMP_0_PIN < 8
  683. DIDR0 |= 1 << TEMP_0_PIN;
  684. #else
  685. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  686. #endif
  687. #endif
  688. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  689. #if TEMP_1_PIN < 8
  690. DIDR0 |= 1<<TEMP_1_PIN;
  691. #else
  692. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  693. #endif
  694. #endif
  695. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  696. #if TEMP_2_PIN < 8
  697. DIDR0 |= 1 << TEMP_2_PIN;
  698. #else
  699. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  700. #endif
  701. #endif
  702. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  703. #if TEMP_BED_PIN < 8
  704. DIDR0 |= 1<<TEMP_BED_PIN;
  705. #else
  706. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  707. #endif
  708. #endif
  709. // Use timer0 for temperature measurement
  710. // Interleave temperature interrupt with millies interrupt
  711. OCR0B = 128;
  712. TIMSK0 |= (1<<OCIE0B);
  713. // Wait for temperature measurement to settle
  714. delay(250);
  715. #ifdef HEATER_0_MINTEMP
  716. minttemp[0] = HEATER_0_MINTEMP;
  717. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  718. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  719. minttemp_raw[0] += OVERSAMPLENR;
  720. #else
  721. minttemp_raw[0] -= OVERSAMPLENR;
  722. #endif
  723. }
  724. #endif //MINTEMP
  725. #ifdef HEATER_0_MAXTEMP
  726. maxttemp[0] = HEATER_0_MAXTEMP;
  727. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  728. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  729. maxttemp_raw[0] -= OVERSAMPLENR;
  730. #else
  731. maxttemp_raw[0] += OVERSAMPLENR;
  732. #endif
  733. }
  734. #endif //MAXTEMP
  735. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  736. minttemp[1] = HEATER_1_MINTEMP;
  737. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  738. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  739. minttemp_raw[1] += OVERSAMPLENR;
  740. #else
  741. minttemp_raw[1] -= OVERSAMPLENR;
  742. #endif
  743. }
  744. #endif // MINTEMP 1
  745. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  746. maxttemp[1] = HEATER_1_MAXTEMP;
  747. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  748. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  749. maxttemp_raw[1] -= OVERSAMPLENR;
  750. #else
  751. maxttemp_raw[1] += OVERSAMPLENR;
  752. #endif
  753. }
  754. #endif //MAXTEMP 1
  755. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  756. minttemp[2] = HEATER_2_MINTEMP;
  757. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  758. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  759. minttemp_raw[2] += OVERSAMPLENR;
  760. #else
  761. minttemp_raw[2] -= OVERSAMPLENR;
  762. #endif
  763. }
  764. #endif //MINTEMP 2
  765. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  766. maxttemp[2] = HEATER_2_MAXTEMP;
  767. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  768. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  769. maxttemp_raw[2] -= OVERSAMPLENR;
  770. #else
  771. maxttemp_raw[2] += OVERSAMPLENR;
  772. #endif
  773. }
  774. #endif //MAXTEMP 2
  775. #ifdef BED_MINTEMP
  776. /* No bed MINTEMP error implemented?!? */ /*
  777. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  778. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  779. bed_minttemp_raw += OVERSAMPLENR;
  780. #else
  781. bed_minttemp_raw -= OVERSAMPLENR;
  782. #endif
  783. }
  784. */
  785. #endif //BED_MINTEMP
  786. #ifdef BED_MAXTEMP
  787. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  788. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  789. bed_maxttemp_raw -= OVERSAMPLENR;
  790. #else
  791. bed_maxttemp_raw += OVERSAMPLENR;
  792. #endif
  793. }
  794. #endif //BED_MAXTEMP
  795. }
  796. void setWatch()
  797. {
  798. #ifdef WATCH_TEMP_PERIOD
  799. for (int e = 0; e < EXTRUDERS; e++)
  800. {
  801. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  802. {
  803. watch_start_temp[e] = degHotend(e);
  804. watchmillis[e] = millis();
  805. }
  806. }
  807. #endif
  808. }
  809. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  810. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  811. {
  812. /*
  813. SERIAL_ECHO_START;
  814. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  815. SERIAL_ECHO(heater_id);
  816. SERIAL_ECHO(" ; State:");
  817. SERIAL_ECHO(*state);
  818. SERIAL_ECHO(" ; Timer:");
  819. SERIAL_ECHO(*timer);
  820. SERIAL_ECHO(" ; Temperature:");
  821. SERIAL_ECHO(temperature);
  822. SERIAL_ECHO(" ; Target Temp:");
  823. SERIAL_ECHO(target_temperature);
  824. SERIAL_ECHOLN("");
  825. */
  826. if ((target_temperature == 0) || thermal_runaway)
  827. {
  828. *state = 0;
  829. *timer = 0;
  830. return;
  831. }
  832. switch (*state)
  833. {
  834. case 0: // "Heater Inactive" state
  835. if (target_temperature > 0) *state = 1;
  836. break;
  837. case 1: // "First Heating" state
  838. if (temperature >= target_temperature) *state = 2;
  839. break;
  840. case 2: // "Temperature Stable" state
  841. if (temperature >= (target_temperature - hysteresis_degc))
  842. {
  843. *timer = millis();
  844. }
  845. else if ( (millis() - *timer) > period_seconds*1000)
  846. {
  847. SERIAL_ERROR_START;
  848. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  849. SERIAL_ERRORLN((int)heater_id);
  850. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  851. thermal_runaway = true;
  852. while(1)
  853. {
  854. disable_heater();
  855. disable_x();
  856. disable_y();
  857. disable_z();
  858. disable_e0();
  859. disable_e1();
  860. disable_e2();
  861. manage_heater();
  862. lcd_update();
  863. }
  864. }
  865. break;
  866. }
  867. }
  868. #endif
  869. void disable_heater()
  870. {
  871. for(int i=0;i<EXTRUDERS;i++)
  872. setTargetHotend(0,i);
  873. setTargetBed(0);
  874. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  875. target_temperature[0]=0;
  876. soft_pwm[0]=0;
  877. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  878. WRITE(HEATER_0_PIN,LOW);
  879. #endif
  880. #endif
  881. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  882. target_temperature[1]=0;
  883. soft_pwm[1]=0;
  884. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  885. WRITE(HEATER_1_PIN,LOW);
  886. #endif
  887. #endif
  888. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  889. target_temperature[2]=0;
  890. soft_pwm[2]=0;
  891. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  892. WRITE(HEATER_2_PIN,LOW);
  893. #endif
  894. #endif
  895. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  896. target_temperature_bed=0;
  897. soft_pwm_bed=0;
  898. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  899. WRITE(HEATER_BED_PIN,LOW);
  900. #endif
  901. #endif
  902. }
  903. void max_temp_error(uint8_t e) {
  904. disable_heater();
  905. if(IsStopped() == false) {
  906. SERIAL_ERROR_START;
  907. SERIAL_ERRORLN((int)e);
  908. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  909. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  910. }
  911. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  912. Stop();
  913. #endif
  914. }
  915. void min_temp_error(uint8_t e) {
  916. disable_heater();
  917. if(IsStopped() == false) {
  918. SERIAL_ERROR_START;
  919. SERIAL_ERRORLN((int)e);
  920. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  921. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  922. }
  923. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  924. Stop();
  925. #endif
  926. }
  927. void bed_max_temp_error(void) {
  928. #if HEATER_BED_PIN > -1
  929. WRITE(HEATER_BED_PIN, 0);
  930. #endif
  931. if(IsStopped() == false) {
  932. SERIAL_ERROR_START;
  933. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  934. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  935. }
  936. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  937. Stop();
  938. #endif
  939. }
  940. #ifdef HEATER_0_USES_MAX6675
  941. #define MAX6675_HEAT_INTERVAL 250
  942. long max6675_previous_millis = -HEAT_INTERVAL;
  943. int max6675_temp = 2000;
  944. int read_max6675()
  945. {
  946. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  947. return max6675_temp;
  948. max6675_previous_millis = millis();
  949. max6675_temp = 0;
  950. #ifdef PRR
  951. PRR &= ~(1<<PRSPI);
  952. #elif defined PRR0
  953. PRR0 &= ~(1<<PRSPI);
  954. #endif
  955. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  956. // enable TT_MAX6675
  957. WRITE(MAX6675_SS, 0);
  958. // ensure 100ns delay - a bit extra is fine
  959. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  960. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  961. // read MSB
  962. SPDR = 0;
  963. for (;(SPSR & (1<<SPIF)) == 0;);
  964. max6675_temp = SPDR;
  965. max6675_temp <<= 8;
  966. // read LSB
  967. SPDR = 0;
  968. for (;(SPSR & (1<<SPIF)) == 0;);
  969. max6675_temp |= SPDR;
  970. // disable TT_MAX6675
  971. WRITE(MAX6675_SS, 1);
  972. if (max6675_temp & 4)
  973. {
  974. // thermocouple open
  975. max6675_temp = 2000;
  976. }
  977. else
  978. {
  979. max6675_temp = max6675_temp >> 3;
  980. }
  981. return max6675_temp;
  982. }
  983. #endif
  984. // Timer 0 is shared with millies
  985. ISR(TIMER0_COMPB_vect)
  986. {
  987. //these variables are only accesible from the ISR, but static, so they don't lose their value
  988. static unsigned char temp_count = 0;
  989. static unsigned long raw_temp_0_value = 0;
  990. static unsigned long raw_temp_1_value = 0;
  991. static unsigned long raw_temp_2_value = 0;
  992. static unsigned long raw_temp_bed_value = 0;
  993. static unsigned char temp_state = 8;
  994. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  995. static unsigned char soft_pwm_0;
  996. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  997. static unsigned char soft_pwm_1;
  998. #endif
  999. #if EXTRUDERS > 2
  1000. static unsigned char soft_pwm_2;
  1001. #endif
  1002. #if HEATER_BED_PIN > -1
  1003. static unsigned char soft_pwm_b;
  1004. #endif
  1005. if(pwm_count == 0){
  1006. soft_pwm_0 = soft_pwm[0];
  1007. if(soft_pwm_0 > 0) {
  1008. WRITE(HEATER_0_PIN,1);
  1009. #ifdef HEATERS_PARALLEL
  1010. WRITE(HEATER_1_PIN,1);
  1011. #endif
  1012. } else WRITE(HEATER_0_PIN,0);
  1013. #if EXTRUDERS > 1
  1014. soft_pwm_1 = soft_pwm[1];
  1015. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1016. #endif
  1017. #if EXTRUDERS > 2
  1018. soft_pwm_2 = soft_pwm[2];
  1019. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1020. #endif
  1021. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1022. soft_pwm_b = soft_pwm_bed;
  1023. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1024. #endif
  1025. #ifdef FAN_SOFT_PWM
  1026. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1027. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1028. #endif
  1029. }
  1030. if(soft_pwm_0 < pwm_count) {
  1031. WRITE(HEATER_0_PIN,0);
  1032. #ifdef HEATERS_PARALLEL
  1033. WRITE(HEATER_1_PIN,0);
  1034. #endif
  1035. }
  1036. #if EXTRUDERS > 1
  1037. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1038. #endif
  1039. #if EXTRUDERS > 2
  1040. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1041. #endif
  1042. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1043. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1044. #endif
  1045. #ifdef FAN_SOFT_PWM
  1046. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1047. #endif
  1048. pwm_count += (1 << SOFT_PWM_SCALE);
  1049. pwm_count &= 0x7f;
  1050. switch(temp_state) {
  1051. case 0: // Prepare TEMP_0
  1052. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1053. #if TEMP_0_PIN > 7
  1054. ADCSRB = 1<<MUX5;
  1055. #else
  1056. ADCSRB = 0;
  1057. #endif
  1058. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1059. ADCSRA |= 1<<ADSC; // Start conversion
  1060. #endif
  1061. lcd_buttons_update();
  1062. temp_state = 1;
  1063. break;
  1064. case 1: // Measure TEMP_0
  1065. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1066. raw_temp_0_value += ADC;
  1067. #endif
  1068. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1069. raw_temp_0_value = read_max6675();
  1070. #endif
  1071. temp_state = 2;
  1072. break;
  1073. case 2: // Prepare TEMP_BED
  1074. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1075. #if TEMP_BED_PIN > 7
  1076. ADCSRB = 1<<MUX5;
  1077. #else
  1078. ADCSRB = 0;
  1079. #endif
  1080. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1081. ADCSRA |= 1<<ADSC; // Start conversion
  1082. #endif
  1083. lcd_buttons_update();
  1084. temp_state = 3;
  1085. break;
  1086. case 3: // Measure TEMP_BED
  1087. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1088. raw_temp_bed_value += ADC;
  1089. #endif
  1090. temp_state = 4;
  1091. break;
  1092. case 4: // Prepare TEMP_1
  1093. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1094. #if TEMP_1_PIN > 7
  1095. ADCSRB = 1<<MUX5;
  1096. #else
  1097. ADCSRB = 0;
  1098. #endif
  1099. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1100. ADCSRA |= 1<<ADSC; // Start conversion
  1101. #endif
  1102. lcd_buttons_update();
  1103. temp_state = 5;
  1104. break;
  1105. case 5: // Measure TEMP_1
  1106. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1107. raw_temp_1_value += ADC;
  1108. #endif
  1109. temp_state = 6;
  1110. break;
  1111. case 6: // Prepare TEMP_2
  1112. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1113. #if TEMP_2_PIN > 7
  1114. ADCSRB = 1<<MUX5;
  1115. #else
  1116. ADCSRB = 0;
  1117. #endif
  1118. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1119. ADCSRA |= 1<<ADSC; // Start conversion
  1120. #endif
  1121. lcd_buttons_update();
  1122. temp_state = 7;
  1123. break;
  1124. case 7: // Measure TEMP_2
  1125. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1126. raw_temp_2_value += ADC;
  1127. #endif
  1128. temp_state = 0;
  1129. temp_count++;
  1130. break;
  1131. case 8: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1132. temp_state = 0;
  1133. break;
  1134. // default:
  1135. // SERIAL_ERROR_START;
  1136. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1137. // break;
  1138. }
  1139. if(temp_count >= OVERSAMPLENR) // 8 * 16 * 1/(16000000/64/256) = 131ms.
  1140. {
  1141. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1142. {
  1143. current_temperature_raw[0] = raw_temp_0_value;
  1144. #if EXTRUDERS > 1
  1145. current_temperature_raw[1] = raw_temp_1_value;
  1146. #endif
  1147. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1148. redundant_temperature_raw = raw_temp_1_value;
  1149. #endif
  1150. #if EXTRUDERS > 2
  1151. current_temperature_raw[2] = raw_temp_2_value;
  1152. #endif
  1153. current_temperature_bed_raw = raw_temp_bed_value;
  1154. }
  1155. temp_meas_ready = true;
  1156. temp_count = 0;
  1157. raw_temp_0_value = 0;
  1158. raw_temp_1_value = 0;
  1159. raw_temp_2_value = 0;
  1160. raw_temp_bed_value = 0;
  1161. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1162. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1163. #else
  1164. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1165. #endif
  1166. max_temp_error(0);
  1167. }
  1168. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1169. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1170. #else
  1171. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1172. #endif
  1173. min_temp_error(0);
  1174. }
  1175. #if EXTRUDERS > 1
  1176. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1177. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1178. #else
  1179. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1180. #endif
  1181. max_temp_error(1);
  1182. }
  1183. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1184. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1185. #else
  1186. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1187. #endif
  1188. min_temp_error(1);
  1189. }
  1190. #endif
  1191. #if EXTRUDERS > 2
  1192. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1193. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1194. #else
  1195. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1196. #endif
  1197. max_temp_error(2);
  1198. }
  1199. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1200. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1201. #else
  1202. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1203. #endif
  1204. min_temp_error(2);
  1205. }
  1206. #endif
  1207. /* No bed MINTEMP error? */
  1208. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1209. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1210. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1211. #else
  1212. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1213. #endif
  1214. target_temperature_bed = 0;
  1215. bed_max_temp_error();
  1216. }
  1217. #endif
  1218. }
  1219. #ifdef BABYSTEPPING
  1220. for(uint8_t axis=0;axis<3;axis++)
  1221. {
  1222. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1223. if(curTodo>0)
  1224. {
  1225. babystep(axis,/*fwd*/true);
  1226. babystepsTodo[axis]--; //less to do next time
  1227. }
  1228. else
  1229. if(curTodo<0)
  1230. {
  1231. babystep(axis,/*fwd*/false);
  1232. babystepsTodo[axis]++; //less to do next time
  1233. }
  1234. }
  1235. #endif //BABYSTEPPING
  1236. }
  1237. #ifdef PIDTEMP
  1238. // Apply the scale factors to the PID values
  1239. float scalePID_i(float i)
  1240. {
  1241. return i*PID_dT;
  1242. }
  1243. float unscalePID_i(float i)
  1244. {
  1245. return i/PID_dT;
  1246. }
  1247. float scalePID_d(float d)
  1248. {
  1249. return d/PID_dT;
  1250. }
  1251. float unscalePID_d(float d)
  1252. {
  1253. return d*PID_dT;
  1254. }
  1255. #endif //PIDTEMP