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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. #include "Sd2PinMap.h"
  28. //===========================================================================
  29. //=============================public variables============================
  30. //===========================================================================
  31. int target_temperature[EXTRUDERS] = { 0 };
  32. int target_temperature_bed = 0;
  33. int current_temperature_raw[EXTRUDERS] = { 0 };
  34. float current_temperature[EXTRUDERS] = { 0.0 };
  35. int current_temperature_bed_raw = 0;
  36. float current_temperature_bed = 0.0;
  37. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  38. int redundant_temperature_raw = 0;
  39. float redundant_temperature = 0.0;
  40. #endif
  41. #ifdef PIDTEMPBED
  42. float bedKp=DEFAULT_bedKp;
  43. float bedKi=(DEFAULT_bedKi*PID_dT);
  44. float bedKd=(DEFAULT_bedKd/PID_dT);
  45. #endif //PIDTEMPBED
  46. #ifdef FAN_SOFT_PWM
  47. unsigned char fanSpeedSoftPwm;
  48. #endif
  49. unsigned char soft_pwm_bed;
  50. #ifdef BABYSTEPPING
  51. volatile int babystepsTodo[3]={0,0,0};
  52. #endif
  53. #ifdef FILAMENT_SENSOR
  54. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  55. #endif
  56. //===========================================================================
  57. //=============================private variables============================
  58. //===========================================================================
  59. static volatile bool temp_meas_ready = false;
  60. #ifdef PIDTEMP
  61. //static cannot be external:
  62. static float temp_iState[EXTRUDERS] = { 0 };
  63. static float temp_dState[EXTRUDERS] = { 0 };
  64. static float pTerm[EXTRUDERS];
  65. static float iTerm[EXTRUDERS];
  66. static float dTerm[EXTRUDERS];
  67. //int output;
  68. static float pid_error[EXTRUDERS];
  69. static float temp_iState_min[EXTRUDERS];
  70. static float temp_iState_max[EXTRUDERS];
  71. // static float pid_input[EXTRUDERS];
  72. // static float pid_output[EXTRUDERS];
  73. static bool pid_reset[EXTRUDERS];
  74. #endif //PIDTEMP
  75. #ifdef PIDTEMPBED
  76. //static cannot be external:
  77. static float temp_iState_bed = { 0 };
  78. static float temp_dState_bed = { 0 };
  79. static float pTerm_bed;
  80. static float iTerm_bed;
  81. static float dTerm_bed;
  82. //int output;
  83. static float pid_error_bed;
  84. static float temp_iState_min_bed;
  85. static float temp_iState_max_bed;
  86. #else //PIDTEMPBED
  87. static unsigned long previous_millis_bed_heater;
  88. #endif //PIDTEMPBED
  89. static unsigned char soft_pwm[EXTRUDERS];
  90. #ifdef FAN_SOFT_PWM
  91. static unsigned char soft_pwm_fan;
  92. #endif
  93. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  94. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  95. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  96. static unsigned long extruder_autofan_last_check;
  97. #endif
  98. #if EXTRUDERS > 3
  99. # error Unsupported number of extruders
  100. #elif EXTRUDERS > 2
  101. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  102. #elif EXTRUDERS > 1
  103. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  104. #else
  105. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  106. #endif
  107. #ifdef PIDTEMP
  108. #ifdef PID_PARAMS_PER_EXTRUDER
  109. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp);
  110. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT);
  111. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT);
  112. #ifdef PID_ADD_EXTRUSION_RATE
  113. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc);
  114. #endif // PID_ADD_EXTRUSION_RATE
  115. #else //PID_PARAMS_PER_EXTRUDER
  116. float Kp = DEFAULT_Kp;
  117. float Ki = DEFAULT_Ki * PID_dT;
  118. float Kd = DEFAULT_Kd / PID_dT;
  119. #ifdef PID_ADD_EXTRUSION_RATE
  120. float Kc = DEFAULT_Kc;
  121. #endif // PID_ADD_EXTRUSION_RATE
  122. #endif // PID_PARAMS_PER_EXTRUDER
  123. #endif //PIDTEMP
  124. // Init min and max temp with extreme values to prevent false errors during startup
  125. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  126. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  127. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  128. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  129. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  130. #ifdef BED_MAXTEMP
  131. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  132. #endif
  133. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  134. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  135. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  136. #else
  137. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  138. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  139. #endif
  140. static float analog2temp(int raw, uint8_t e);
  141. static float analog2tempBed(int raw);
  142. static void updateTemperaturesFromRawValues();
  143. #ifdef WATCH_TEMP_PERIOD
  144. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  145. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  146. #endif //WATCH_TEMP_PERIOD
  147. #ifndef SOFT_PWM_SCALE
  148. #define SOFT_PWM_SCALE 0
  149. #endif
  150. #ifdef FILAMENT_SENSOR
  151. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  152. #endif
  153. #ifdef HEATER_0_USES_MAX6675
  154. static int read_max6675();
  155. #endif
  156. //===========================================================================
  157. //============================= functions ============================
  158. //===========================================================================
  159. void PID_autotune(float temp, int extruder, int ncycles)
  160. {
  161. float input = 0.0;
  162. int cycles=0;
  163. bool heating = true;
  164. unsigned long temp_millis = millis();
  165. unsigned long t1=temp_millis;
  166. unsigned long t2=temp_millis;
  167. long t_high = 0;
  168. long t_low = 0;
  169. long bias, d;
  170. float Ku, Tu;
  171. float Kp, Ki, Kd;
  172. float max = 0, min = 10000;
  173. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  174. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  175. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  176. unsigned long extruder_autofan_last_check = millis();
  177. #endif
  178. if ((extruder >= EXTRUDERS)
  179. #if (TEMP_BED_PIN <= -1)
  180. ||(extruder < 0)
  181. #endif
  182. ){
  183. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  184. return;
  185. }
  186. SERIAL_ECHOLN("PID Autotune start");
  187. disable_heater(); // switch off all heaters.
  188. if (extruder<0)
  189. {
  190. soft_pwm_bed = (MAX_BED_POWER)/2;
  191. bias = d = (MAX_BED_POWER)/2;
  192. }
  193. else
  194. {
  195. soft_pwm[extruder] = (PID_MAX)/2;
  196. bias = d = (PID_MAX)/2;
  197. }
  198. for(;;) {
  199. if(temp_meas_ready == true) { // temp sample ready
  200. updateTemperaturesFromRawValues();
  201. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  202. max=max(max,input);
  203. min=min(min,input);
  204. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  205. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  206. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  207. if(millis() - extruder_autofan_last_check > 2500) {
  208. checkExtruderAutoFans();
  209. extruder_autofan_last_check = millis();
  210. }
  211. #endif
  212. if(heating == true && input > temp) {
  213. if(millis() - t2 > 5000) {
  214. heating=false;
  215. if (extruder<0)
  216. soft_pwm_bed = (bias - d) >> 1;
  217. else
  218. soft_pwm[extruder] = (bias - d) >> 1;
  219. t1=millis();
  220. t_high=t1 - t2;
  221. max=temp;
  222. }
  223. }
  224. if(heating == false && input < temp) {
  225. if(millis() - t1 > 5000) {
  226. heating=true;
  227. t2=millis();
  228. t_low=t2 - t1;
  229. if(cycles > 0) {
  230. bias += (d*(t_high - t_low))/(t_low + t_high);
  231. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  232. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  233. else d = bias;
  234. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  235. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  236. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  237. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  238. if(cycles > 2) {
  239. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  240. Tu = ((float)(t_low + t_high)/1000.0);
  241. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  242. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  243. Kp = 0.6*Ku;
  244. Ki = 2*Kp/Tu;
  245. Kd = Kp*Tu/8;
  246. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  247. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  248. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  249. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  250. /*
  251. Kp = 0.33*Ku;
  252. Ki = Kp/Tu;
  253. Kd = Kp*Tu/3;
  254. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  255. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  256. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  257. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  258. Kp = 0.2*Ku;
  259. Ki = 2*Kp/Tu;
  260. Kd = Kp*Tu/3;
  261. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  262. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  263. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  264. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  265. */
  266. }
  267. }
  268. if (extruder<0)
  269. soft_pwm_bed = (bias + d) >> 1;
  270. else
  271. soft_pwm[extruder] = (bias + d) >> 1;
  272. cycles++;
  273. min=temp;
  274. }
  275. }
  276. }
  277. if(input > (temp + 20)) {
  278. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  279. return;
  280. }
  281. if(millis() - temp_millis > 2000) {
  282. int p;
  283. if (extruder<0){
  284. p=soft_pwm_bed;
  285. SERIAL_PROTOCOLPGM("ok B:");
  286. }else{
  287. p=soft_pwm[extruder];
  288. SERIAL_PROTOCOLPGM("ok T:");
  289. }
  290. SERIAL_PROTOCOL(input);
  291. SERIAL_PROTOCOLPGM(" @:");
  292. SERIAL_PROTOCOLLN(p);
  293. temp_millis = millis();
  294. }
  295. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  296. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  297. return;
  298. }
  299. if(cycles > ncycles) {
  300. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  301. return;
  302. }
  303. lcd_update();
  304. }
  305. }
  306. void updatePID()
  307. {
  308. #ifdef PIDTEMP
  309. for(int e = 0; e < EXTRUDERS; e++) {
  310. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  311. }
  312. #endif
  313. #ifdef PIDTEMPBED
  314. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  315. #endif
  316. }
  317. int getHeaterPower(int heater) {
  318. if (heater<0)
  319. return soft_pwm_bed;
  320. return soft_pwm[heater];
  321. }
  322. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  323. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  324. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  325. #if defined(FAN_PIN) && FAN_PIN > -1
  326. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  327. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  328. #endif
  329. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  330. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  331. #endif
  332. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  333. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  334. #endif
  335. #endif
  336. void setExtruderAutoFanState(int pin, bool state)
  337. {
  338. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  339. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  340. pinMode(pin, OUTPUT);
  341. digitalWrite(pin, newFanSpeed);
  342. analogWrite(pin, newFanSpeed);
  343. }
  344. void checkExtruderAutoFans()
  345. {
  346. uint8_t fanState = 0;
  347. // which fan pins need to be turned on?
  348. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  349. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  350. fanState |= 1;
  351. #endif
  352. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  353. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  354. {
  355. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  356. fanState |= 1;
  357. else
  358. fanState |= 2;
  359. }
  360. #endif
  361. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  362. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  363. {
  364. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  365. fanState |= 1;
  366. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  367. fanState |= 2;
  368. else
  369. fanState |= 4;
  370. }
  371. #endif
  372. // update extruder auto fan states
  373. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  374. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  375. #endif
  376. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  377. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  378. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  379. #endif
  380. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  381. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  382. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  383. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  384. #endif
  385. }
  386. #endif // any extruder auto fan pins set
  387. void manage_heater()
  388. {
  389. float pid_input;
  390. float pid_output;
  391. if(temp_meas_ready != true) //better readability
  392. return;
  393. updateTemperaturesFromRawValues();
  394. #ifdef HEATER_0_USES_MAX6675
  395. if (current_temperature[0] > 1023 || current_temperature[0] > HEATER_0_MAXTEMP) {
  396. max_temp_error(0);
  397. }
  398. if (current_temperature[0] == 0 || current_temperature[0] < HEATER_0_MINTEMP) {
  399. min_temp_error(0);
  400. }
  401. #endif //HEATER_0_USES_MAX6675
  402. for(int e = 0; e < EXTRUDERS; e++)
  403. {
  404. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  405. 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);
  406. #endif
  407. #ifdef PIDTEMP
  408. pid_input = current_temperature[e];
  409. #ifndef PID_OPENLOOP
  410. pid_error[e] = target_temperature[e] - pid_input;
  411. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  412. pid_output = BANG_MAX;
  413. pid_reset[e] = true;
  414. }
  415. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  416. pid_output = 0;
  417. pid_reset[e] = true;
  418. }
  419. else {
  420. if(pid_reset[e] == true) {
  421. temp_iState[e] = 0.0;
  422. pid_reset[e] = false;
  423. }
  424. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  425. temp_iState[e] += pid_error[e];
  426. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  427. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  428. //K1 defined in Configuration.h in the PID settings
  429. #define K2 (1.0-K1)
  430. dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  431. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  432. if (pid_output > PID_MAX) {
  433. if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  434. pid_output=PID_MAX;
  435. } else if (pid_output < 0){
  436. if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  437. pid_output=0;
  438. }
  439. }
  440. temp_dState[e] = pid_input;
  441. #else
  442. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  443. #endif //PID_OPENLOOP
  444. #ifdef PID_DEBUG
  445. SERIAL_ECHO_START;
  446. SERIAL_ECHO(" PID_DEBUG ");
  447. SERIAL_ECHO(e);
  448. SERIAL_ECHO(": Input ");
  449. SERIAL_ECHO(pid_input);
  450. SERIAL_ECHO(" Output ");
  451. SERIAL_ECHO(pid_output);
  452. SERIAL_ECHO(" pTerm ");
  453. SERIAL_ECHO(pTerm[e]);
  454. SERIAL_ECHO(" iTerm ");
  455. SERIAL_ECHO(iTerm[e]);
  456. SERIAL_ECHO(" dTerm ");
  457. SERIAL_ECHOLN(dTerm[e]);
  458. #endif //PID_DEBUG
  459. #else /* PID off */
  460. pid_output = 0;
  461. if(current_temperature[e] < target_temperature[e]) {
  462. pid_output = PID_MAX;
  463. }
  464. #endif
  465. // Check if temperature is within the correct range
  466. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  467. {
  468. soft_pwm[e] = (int)pid_output >> 1;
  469. }
  470. else {
  471. soft_pwm[e] = 0;
  472. }
  473. #ifdef WATCH_TEMP_PERIOD
  474. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  475. {
  476. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  477. {
  478. setTargetHotend(0, e);
  479. LCD_MESSAGEPGM("Heating failed");
  480. SERIAL_ECHO_START;
  481. SERIAL_ECHOLN("Heating failed");
  482. }else{
  483. watchmillis[e] = 0;
  484. }
  485. }
  486. #endif
  487. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  488. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  489. disable_heater();
  490. if(IsStopped() == false) {
  491. SERIAL_ERROR_START;
  492. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  493. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  494. }
  495. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  496. Stop();
  497. #endif
  498. }
  499. #endif
  500. } // End extruder for loop
  501. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  502. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  503. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  504. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  505. {
  506. checkExtruderAutoFans();
  507. extruder_autofan_last_check = millis();
  508. }
  509. #endif
  510. #ifndef PIDTEMPBED
  511. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  512. return;
  513. previous_millis_bed_heater = millis();
  514. #endif
  515. #if TEMP_SENSOR_BED != 0
  516. #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
  517. 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);
  518. #endif
  519. #ifdef PIDTEMPBED
  520. pid_input = current_temperature_bed;
  521. #ifndef PID_OPENLOOP
  522. pid_error_bed = target_temperature_bed - pid_input;
  523. pTerm_bed = bedKp * pid_error_bed;
  524. temp_iState_bed += pid_error_bed;
  525. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  526. iTerm_bed = bedKi * temp_iState_bed;
  527. //K1 defined in Configuration.h in the PID settings
  528. #define K2 (1.0-K1)
  529. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  530. temp_dState_bed = pid_input;
  531. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  532. if (pid_output > MAX_BED_POWER) {
  533. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  534. pid_output=MAX_BED_POWER;
  535. } else if (pid_output < 0){
  536. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  537. pid_output=0;
  538. }
  539. #else
  540. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  541. #endif //PID_OPENLOOP
  542. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  543. {
  544. soft_pwm_bed = (int)pid_output >> 1;
  545. }
  546. else {
  547. soft_pwm_bed = 0;
  548. }
  549. #elif !defined(BED_LIMIT_SWITCHING)
  550. // Check if temperature is within the correct range
  551. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  552. {
  553. if(current_temperature_bed >= target_temperature_bed)
  554. {
  555. soft_pwm_bed = 0;
  556. }
  557. else
  558. {
  559. soft_pwm_bed = MAX_BED_POWER>>1;
  560. }
  561. }
  562. else
  563. {
  564. soft_pwm_bed = 0;
  565. WRITE(HEATER_BED_PIN,LOW);
  566. }
  567. #else //#ifdef BED_LIMIT_SWITCHING
  568. // Check if temperature is within the correct band
  569. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  570. {
  571. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  572. {
  573. soft_pwm_bed = 0;
  574. }
  575. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  576. {
  577. soft_pwm_bed = MAX_BED_POWER>>1;
  578. }
  579. }
  580. else
  581. {
  582. soft_pwm_bed = 0;
  583. WRITE(HEATER_BED_PIN,LOW);
  584. }
  585. #endif
  586. #endif
  587. //code for controlling the extruder rate based on the width sensor
  588. #ifdef FILAMENT_SENSOR
  589. if(filament_sensor)
  590. {
  591. meas_shift_index=delay_index1-meas_delay_cm;
  592. if(meas_shift_index<0)
  593. meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
  594. //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
  595. //then square it to get an area
  596. if(meas_shift_index<0)
  597. meas_shift_index=0;
  598. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  599. meas_shift_index=MAX_MEASUREMENT_DELAY;
  600. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  601. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  602. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  603. }
  604. #endif
  605. }
  606. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  607. // Derived from RepRap FiveD extruder::getTemperature()
  608. // For hot end temperature measurement.
  609. static float analog2temp(int raw, uint8_t e) {
  610. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  611. if(e > EXTRUDERS)
  612. #else
  613. if(e >= EXTRUDERS)
  614. #endif
  615. {
  616. SERIAL_ERROR_START;
  617. SERIAL_ERROR((int)e);
  618. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  619. kill();
  620. return 0.0;
  621. }
  622. #ifdef HEATER_0_USES_MAX6675
  623. if (e == 0)
  624. {
  625. return 0.25 * raw;
  626. }
  627. #endif
  628. if(heater_ttbl_map[e] != NULL)
  629. {
  630. float celsius = 0;
  631. uint8_t i;
  632. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  633. for (i=1; i<heater_ttbllen_map[e]; i++)
  634. {
  635. if (PGM_RD_W((*tt)[i][0]) > raw)
  636. {
  637. celsius = PGM_RD_W((*tt)[i-1][1]) +
  638. (raw - PGM_RD_W((*tt)[i-1][0])) *
  639. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  640. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  641. break;
  642. }
  643. }
  644. // Overflow: Set to last value in the table
  645. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  646. return celsius;
  647. }
  648. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  649. }
  650. // Derived from RepRap FiveD extruder::getTemperature()
  651. // For bed temperature measurement.
  652. static float analog2tempBed(int raw) {
  653. #ifdef BED_USES_THERMISTOR
  654. float celsius = 0;
  655. byte i;
  656. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  657. {
  658. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  659. {
  660. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  661. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  662. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  663. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  664. break;
  665. }
  666. }
  667. // Overflow: Set to last value in the table
  668. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  669. return celsius;
  670. #elif defined BED_USES_AD595
  671. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  672. #else
  673. return 0;
  674. #endif
  675. }
  676. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  677. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  678. static void updateTemperaturesFromRawValues()
  679. {
  680. #ifdef HEATER_0_USES_MAX6675
  681. current_temperature_raw[0] = read_max6675();
  682. #endif
  683. for(uint8_t e=0;e<EXTRUDERS;e++)
  684. {
  685. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  686. }
  687. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  688. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  689. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  690. #endif
  691. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  692. filament_width_meas = analog2widthFil();
  693. #endif
  694. //Reset the watchdog after we know we have a temperature measurement.
  695. watchdog_reset();
  696. CRITICAL_SECTION_START;
  697. temp_meas_ready = false;
  698. CRITICAL_SECTION_END;
  699. }
  700. // For converting raw Filament Width to milimeters
  701. #ifdef FILAMENT_SENSOR
  702. float analog2widthFil() {
  703. return current_raw_filwidth/16383.0*5.0;
  704. //return current_raw_filwidth;
  705. }
  706. // For converting raw Filament Width to a ratio
  707. int widthFil_to_size_ratio() {
  708. float temp;
  709. temp=filament_width_meas;
  710. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  711. temp=filament_width_nominal; //assume sensor cut out
  712. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  713. temp= MEASURED_UPPER_LIMIT;
  714. return(filament_width_nominal/temp*100);
  715. }
  716. #endif
  717. void tp_init()
  718. {
  719. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  720. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  721. MCUCR=(1<<JTD);
  722. MCUCR=(1<<JTD);
  723. #endif
  724. // Finish init of mult extruder arrays
  725. for(int e = 0; e < EXTRUDERS; e++) {
  726. // populate with the first value
  727. maxttemp[e] = maxttemp[0];
  728. #ifdef PIDTEMP
  729. temp_iState_min[e] = 0.0;
  730. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  731. #endif //PIDTEMP
  732. #ifdef PIDTEMPBED
  733. temp_iState_min_bed = 0.0;
  734. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  735. #endif //PIDTEMPBED
  736. }
  737. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  738. SET_OUTPUT(HEATER_0_PIN);
  739. #endif
  740. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  741. SET_OUTPUT(HEATER_1_PIN);
  742. #endif
  743. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  744. SET_OUTPUT(HEATER_2_PIN);
  745. #endif
  746. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  747. SET_OUTPUT(HEATER_BED_PIN);
  748. #endif
  749. #if defined(FAN_PIN) && (FAN_PIN > -1)
  750. SET_OUTPUT(FAN_PIN);
  751. #ifdef FAST_PWM_FAN
  752. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  753. #endif
  754. #ifdef FAN_SOFT_PWM
  755. soft_pwm_fan = fanSpeedSoftPwm / 2;
  756. #endif
  757. #endif
  758. #ifdef HEATER_0_USES_MAX6675
  759. #ifndef SDSUPPORT
  760. SET_OUTPUT(SCK_PIN);
  761. WRITE(SCK_PIN,0);
  762. SET_OUTPUT(MOSI_PIN);
  763. WRITE(MOSI_PIN,1);
  764. SET_INPUT(MISO_PIN);
  765. WRITE(MISO_PIN,1);
  766. #else
  767. pinMode(SS_PIN, OUTPUT);
  768. digitalWrite(SS_PIN, HIGH);
  769. #endif
  770. SET_OUTPUT(MAX6675_SS);
  771. WRITE(MAX6675_SS,1);
  772. #endif //HEATER_0_USES_MAX6675
  773. // Set analog inputs
  774. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  775. DIDR0 = 0;
  776. #ifdef DIDR2
  777. DIDR2 = 0;
  778. #endif
  779. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  780. #if TEMP_0_PIN < 8
  781. DIDR0 |= 1 << TEMP_0_PIN;
  782. #else
  783. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  784. #endif
  785. #endif
  786. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  787. #if TEMP_1_PIN < 8
  788. DIDR0 |= 1<<TEMP_1_PIN;
  789. #else
  790. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  791. #endif
  792. #endif
  793. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  794. #if TEMP_2_PIN < 8
  795. DIDR0 |= 1 << TEMP_2_PIN;
  796. #else
  797. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  798. #endif
  799. #endif
  800. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  801. #if TEMP_BED_PIN < 8
  802. DIDR0 |= 1<<TEMP_BED_PIN;
  803. #else
  804. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  805. #endif
  806. #endif
  807. //Added for Filament Sensor
  808. #ifdef FILAMENT_SENSOR
  809. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  810. #if FILWIDTH_PIN < 8
  811. DIDR0 |= 1<<FILWIDTH_PIN;
  812. #else
  813. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  814. #endif
  815. #endif
  816. #endif
  817. // Use timer0 for temperature measurement
  818. // Interleave temperature interrupt with millies interrupt
  819. OCR0B = 128;
  820. TIMSK0 |= (1<<OCIE0B);
  821. // Wait for temperature measurement to settle
  822. delay(250);
  823. #ifdef HEATER_0_MINTEMP
  824. minttemp[0] = HEATER_0_MINTEMP;
  825. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  826. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  827. minttemp_raw[0] += OVERSAMPLENR;
  828. #else
  829. minttemp_raw[0] -= OVERSAMPLENR;
  830. #endif
  831. }
  832. #endif //MINTEMP
  833. #ifdef HEATER_0_MAXTEMP
  834. maxttemp[0] = HEATER_0_MAXTEMP;
  835. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  836. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  837. maxttemp_raw[0] -= OVERSAMPLENR;
  838. #else
  839. maxttemp_raw[0] += OVERSAMPLENR;
  840. #endif
  841. }
  842. #endif //MAXTEMP
  843. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  844. minttemp[1] = HEATER_1_MINTEMP;
  845. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  846. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  847. minttemp_raw[1] += OVERSAMPLENR;
  848. #else
  849. minttemp_raw[1] -= OVERSAMPLENR;
  850. #endif
  851. }
  852. #endif // MINTEMP 1
  853. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  854. maxttemp[1] = HEATER_1_MAXTEMP;
  855. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  856. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  857. maxttemp_raw[1] -= OVERSAMPLENR;
  858. #else
  859. maxttemp_raw[1] += OVERSAMPLENR;
  860. #endif
  861. }
  862. #endif //MAXTEMP 1
  863. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  864. minttemp[2] = HEATER_2_MINTEMP;
  865. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  866. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  867. minttemp_raw[2] += OVERSAMPLENR;
  868. #else
  869. minttemp_raw[2] -= OVERSAMPLENR;
  870. #endif
  871. }
  872. #endif //MINTEMP 2
  873. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  874. maxttemp[2] = HEATER_2_MAXTEMP;
  875. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  876. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  877. maxttemp_raw[2] -= OVERSAMPLENR;
  878. #else
  879. maxttemp_raw[2] += OVERSAMPLENR;
  880. #endif
  881. }
  882. #endif //MAXTEMP 2
  883. #ifdef BED_MINTEMP
  884. /* No bed MINTEMP error implemented?!? */ /*
  885. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  886. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  887. bed_minttemp_raw += OVERSAMPLENR;
  888. #else
  889. bed_minttemp_raw -= OVERSAMPLENR;
  890. #endif
  891. }
  892. */
  893. #endif //BED_MINTEMP
  894. #ifdef BED_MAXTEMP
  895. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  896. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  897. bed_maxttemp_raw -= OVERSAMPLENR;
  898. #else
  899. bed_maxttemp_raw += OVERSAMPLENR;
  900. #endif
  901. }
  902. #endif //BED_MAXTEMP
  903. }
  904. void setWatch()
  905. {
  906. #ifdef WATCH_TEMP_PERIOD
  907. for (int e = 0; e < EXTRUDERS; e++)
  908. {
  909. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  910. {
  911. watch_start_temp[e] = degHotend(e);
  912. watchmillis[e] = millis();
  913. }
  914. }
  915. #endif
  916. }
  917. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  918. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  919. {
  920. /*
  921. SERIAL_ECHO_START;
  922. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  923. SERIAL_ECHO(heater_id);
  924. SERIAL_ECHO(" ; State:");
  925. SERIAL_ECHO(*state);
  926. SERIAL_ECHO(" ; Timer:");
  927. SERIAL_ECHO(*timer);
  928. SERIAL_ECHO(" ; Temperature:");
  929. SERIAL_ECHO(temperature);
  930. SERIAL_ECHO(" ; Target Temp:");
  931. SERIAL_ECHO(target_temperature);
  932. SERIAL_ECHOLN("");
  933. */
  934. if ((target_temperature == 0) || thermal_runaway)
  935. {
  936. *state = 0;
  937. *timer = 0;
  938. return;
  939. }
  940. switch (*state)
  941. {
  942. case 0: // "Heater Inactive" state
  943. if (target_temperature > 0) *state = 1;
  944. break;
  945. case 1: // "First Heating" state
  946. if (temperature >= target_temperature) *state = 2;
  947. break;
  948. case 2: // "Temperature Stable" state
  949. if (temperature >= (target_temperature - hysteresis_degc))
  950. {
  951. *timer = millis();
  952. }
  953. else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000)
  954. {
  955. SERIAL_ERROR_START;
  956. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  957. SERIAL_ERRORLN((int)heater_id);
  958. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  959. thermal_runaway = true;
  960. while(1)
  961. {
  962. disable_heater();
  963. disable_x();
  964. disable_y();
  965. disable_z();
  966. disable_e0();
  967. disable_e1();
  968. disable_e2();
  969. manage_heater();
  970. lcd_update();
  971. }
  972. }
  973. break;
  974. }
  975. }
  976. #endif
  977. void disable_heater()
  978. {
  979. for(int i=0;i<EXTRUDERS;i++)
  980. setTargetHotend(0,i);
  981. setTargetBed(0);
  982. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  983. target_temperature[0]=0;
  984. soft_pwm[0]=0;
  985. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  986. WRITE(HEATER_0_PIN,LOW);
  987. #endif
  988. #endif
  989. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  990. target_temperature[1]=0;
  991. soft_pwm[1]=0;
  992. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  993. WRITE(HEATER_1_PIN,LOW);
  994. #endif
  995. #endif
  996. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  997. target_temperature[2]=0;
  998. soft_pwm[2]=0;
  999. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1000. WRITE(HEATER_2_PIN,LOW);
  1001. #endif
  1002. #endif
  1003. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1004. target_temperature_bed=0;
  1005. soft_pwm_bed=0;
  1006. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1007. WRITE(HEATER_BED_PIN,LOW);
  1008. #endif
  1009. #endif
  1010. }
  1011. void max_temp_error(uint8_t e) {
  1012. disable_heater();
  1013. if(IsStopped() == false) {
  1014. SERIAL_ERROR_START;
  1015. SERIAL_ERRORLN((int)e);
  1016. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1017. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1018. }
  1019. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1020. Stop();
  1021. #endif
  1022. }
  1023. void min_temp_error(uint8_t e) {
  1024. disable_heater();
  1025. if(IsStopped() == false) {
  1026. SERIAL_ERROR_START;
  1027. SERIAL_ERRORLN((int)e);
  1028. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1029. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1030. }
  1031. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1032. Stop();
  1033. #endif
  1034. }
  1035. void bed_max_temp_error(void) {
  1036. #if HEATER_BED_PIN > -1
  1037. WRITE(HEATER_BED_PIN, 0);
  1038. #endif
  1039. if(IsStopped() == false) {
  1040. SERIAL_ERROR_START;
  1041. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  1042. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1043. }
  1044. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1045. Stop();
  1046. #endif
  1047. }
  1048. #ifdef HEATER_0_USES_MAX6675
  1049. #define MAX6675_HEAT_INTERVAL 250
  1050. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1051. int max6675_temp = 2000;
  1052. static int read_max6675()
  1053. {
  1054. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1055. return max6675_temp;
  1056. max6675_previous_millis = millis();
  1057. max6675_temp = 0;
  1058. #ifdef PRR
  1059. PRR &= ~(1<<PRSPI);
  1060. #elif defined(PRR0)
  1061. PRR0 &= ~(1<<PRSPI);
  1062. #endif
  1063. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1064. // enable TT_MAX6675
  1065. WRITE(MAX6675_SS, 0);
  1066. // ensure 100ns delay - a bit extra is fine
  1067. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1068. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1069. // read MSB
  1070. SPDR = 0;
  1071. for (;(SPSR & (1<<SPIF)) == 0;);
  1072. max6675_temp = SPDR;
  1073. max6675_temp <<= 8;
  1074. // read LSB
  1075. SPDR = 0;
  1076. for (;(SPSR & (1<<SPIF)) == 0;);
  1077. max6675_temp |= SPDR;
  1078. // disable TT_MAX6675
  1079. WRITE(MAX6675_SS, 1);
  1080. if (max6675_temp & 4)
  1081. {
  1082. // thermocouple open
  1083. max6675_temp = 4000;
  1084. }
  1085. else
  1086. {
  1087. max6675_temp = max6675_temp >> 3;
  1088. }
  1089. return max6675_temp;
  1090. }
  1091. #endif //HEATER_0_USES_MAX6675
  1092. // Timer 0 is shared with millies
  1093. ISR(TIMER0_COMPB_vect)
  1094. {
  1095. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1096. static unsigned char temp_count = 0;
  1097. static unsigned long raw_temp_0_value = 0;
  1098. static unsigned long raw_temp_1_value = 0;
  1099. static unsigned long raw_temp_2_value = 0;
  1100. static unsigned long raw_temp_bed_value = 0;
  1101. static unsigned char temp_state = 10;
  1102. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1103. static unsigned char soft_pwm_0;
  1104. #ifdef SLOW_PWM_HEATERS
  1105. static unsigned char slow_pwm_count = 0;
  1106. static unsigned char state_heater_0 = 0;
  1107. static unsigned char state_timer_heater_0 = 0;
  1108. #endif
  1109. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1110. static unsigned char soft_pwm_1;
  1111. #ifdef SLOW_PWM_HEATERS
  1112. static unsigned char state_heater_1 = 0;
  1113. static unsigned char state_timer_heater_1 = 0;
  1114. #endif
  1115. #endif
  1116. #if EXTRUDERS > 2
  1117. static unsigned char soft_pwm_2;
  1118. #ifdef SLOW_PWM_HEATERS
  1119. static unsigned char state_heater_2 = 0;
  1120. static unsigned char state_timer_heater_2 = 0;
  1121. #endif
  1122. #endif
  1123. #if HEATER_BED_PIN > -1
  1124. static unsigned char soft_pwm_b;
  1125. #ifdef SLOW_PWM_HEATERS
  1126. static unsigned char state_heater_b = 0;
  1127. static unsigned char state_timer_heater_b = 0;
  1128. #endif
  1129. #endif
  1130. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1131. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1132. #endif
  1133. #ifndef SLOW_PWM_HEATERS
  1134. /*
  1135. * standard PWM modulation
  1136. */
  1137. if(pwm_count == 0){
  1138. soft_pwm_0 = soft_pwm[0];
  1139. if(soft_pwm_0 > 0) {
  1140. WRITE(HEATER_0_PIN,1);
  1141. #ifdef HEATERS_PARALLEL
  1142. WRITE(HEATER_1_PIN,1);
  1143. #endif
  1144. } else WRITE(HEATER_0_PIN,0);
  1145. #if EXTRUDERS > 1
  1146. soft_pwm_1 = soft_pwm[1];
  1147. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1148. #endif
  1149. #if EXTRUDERS > 2
  1150. soft_pwm_2 = soft_pwm[2];
  1151. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1152. #endif
  1153. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1154. soft_pwm_b = soft_pwm_bed;
  1155. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1156. #endif
  1157. #ifdef FAN_SOFT_PWM
  1158. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1159. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1160. #endif
  1161. }
  1162. if(soft_pwm_0 < pwm_count) {
  1163. WRITE(HEATER_0_PIN,0);
  1164. #ifdef HEATERS_PARALLEL
  1165. WRITE(HEATER_1_PIN,0);
  1166. #endif
  1167. }
  1168. #if EXTRUDERS > 1
  1169. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1170. #endif
  1171. #if EXTRUDERS > 2
  1172. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1173. #endif
  1174. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1175. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1176. #endif
  1177. #ifdef FAN_SOFT_PWM
  1178. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1179. #endif
  1180. pwm_count += (1 << SOFT_PWM_SCALE);
  1181. pwm_count &= 0x7f;
  1182. #else //ifndef SLOW_PWM_HEATERS
  1183. /*
  1184. * SLOW PWM HEATERS
  1185. *
  1186. * for heaters drived by relay
  1187. */
  1188. #ifndef MIN_STATE_TIME
  1189. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1190. #endif
  1191. if (slow_pwm_count == 0) {
  1192. // EXTRUDER 0
  1193. soft_pwm_0 = soft_pwm[0];
  1194. if (soft_pwm_0 > 0) {
  1195. // turn ON heather only if the minimum time is up
  1196. if (state_timer_heater_0 == 0) {
  1197. // if change state set timer
  1198. if (state_heater_0 == 0) {
  1199. state_timer_heater_0 = MIN_STATE_TIME;
  1200. }
  1201. state_heater_0 = 1;
  1202. WRITE(HEATER_0_PIN, 1);
  1203. #ifdef HEATERS_PARALLEL
  1204. WRITE(HEATER_1_PIN, 1);
  1205. #endif
  1206. }
  1207. } else {
  1208. // turn OFF heather only if the minimum time is up
  1209. if (state_timer_heater_0 == 0) {
  1210. // if change state set timer
  1211. if (state_heater_0 == 1) {
  1212. state_timer_heater_0 = MIN_STATE_TIME;
  1213. }
  1214. state_heater_0 = 0;
  1215. WRITE(HEATER_0_PIN, 0);
  1216. #ifdef HEATERS_PARALLEL
  1217. WRITE(HEATER_1_PIN, 0);
  1218. #endif
  1219. }
  1220. }
  1221. #if EXTRUDERS > 1
  1222. // EXTRUDER 1
  1223. soft_pwm_1 = soft_pwm[1];
  1224. if (soft_pwm_1 > 0) {
  1225. // turn ON heather only if the minimum time is up
  1226. if (state_timer_heater_1 == 0) {
  1227. // if change state set timer
  1228. if (state_heater_1 == 0) {
  1229. state_timer_heater_1 = MIN_STATE_TIME;
  1230. }
  1231. state_heater_1 = 1;
  1232. WRITE(HEATER_1_PIN, 1);
  1233. }
  1234. } else {
  1235. // turn OFF heather only if the minimum time is up
  1236. if (state_timer_heater_1 == 0) {
  1237. // if change state set timer
  1238. if (state_heater_1 == 1) {
  1239. state_timer_heater_1 = MIN_STATE_TIME;
  1240. }
  1241. state_heater_1 = 0;
  1242. WRITE(HEATER_1_PIN, 0);
  1243. }
  1244. }
  1245. #endif
  1246. #if EXTRUDERS > 2
  1247. // EXTRUDER 2
  1248. soft_pwm_2 = soft_pwm[2];
  1249. if (soft_pwm_2 > 0) {
  1250. // turn ON heather only if the minimum time is up
  1251. if (state_timer_heater_2 == 0) {
  1252. // if change state set timer
  1253. if (state_heater_2 == 0) {
  1254. state_timer_heater_2 = MIN_STATE_TIME;
  1255. }
  1256. state_heater_2 = 1;
  1257. WRITE(HEATER_2_PIN, 1);
  1258. }
  1259. } else {
  1260. // turn OFF heather only if the minimum time is up
  1261. if (state_timer_heater_2 == 0) {
  1262. // if change state set timer
  1263. if (state_heater_2 == 1) {
  1264. state_timer_heater_2 = MIN_STATE_TIME;
  1265. }
  1266. state_heater_2 = 0;
  1267. WRITE(HEATER_2_PIN, 0);
  1268. }
  1269. }
  1270. #endif
  1271. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1272. // BED
  1273. soft_pwm_b = soft_pwm_bed;
  1274. if (soft_pwm_b > 0) {
  1275. // turn ON heather only if the minimum time is up
  1276. if (state_timer_heater_b == 0) {
  1277. // if change state set timer
  1278. if (state_heater_b == 0) {
  1279. state_timer_heater_b = MIN_STATE_TIME;
  1280. }
  1281. state_heater_b = 1;
  1282. WRITE(HEATER_BED_PIN, 1);
  1283. }
  1284. } else {
  1285. // turn OFF heather only if the minimum time is up
  1286. if (state_timer_heater_b == 0) {
  1287. // if change state set timer
  1288. if (state_heater_b == 1) {
  1289. state_timer_heater_b = MIN_STATE_TIME;
  1290. }
  1291. state_heater_b = 0;
  1292. WRITE(HEATER_BED_PIN, 0);
  1293. }
  1294. }
  1295. #endif
  1296. } // if (slow_pwm_count == 0)
  1297. // EXTRUDER 0
  1298. if (soft_pwm_0 < slow_pwm_count) {
  1299. // turn OFF heather only if the minimum time is up
  1300. if (state_timer_heater_0 == 0) {
  1301. // if change state set timer
  1302. if (state_heater_0 == 1) {
  1303. state_timer_heater_0 = MIN_STATE_TIME;
  1304. }
  1305. state_heater_0 = 0;
  1306. WRITE(HEATER_0_PIN, 0);
  1307. #ifdef HEATERS_PARALLEL
  1308. WRITE(HEATER_1_PIN, 0);
  1309. #endif
  1310. }
  1311. }
  1312. #if EXTRUDERS > 1
  1313. // EXTRUDER 1
  1314. if (soft_pwm_1 < slow_pwm_count) {
  1315. // turn OFF heather only if the minimum time is up
  1316. if (state_timer_heater_1 == 0) {
  1317. // if change state set timer
  1318. if (state_heater_1 == 1) {
  1319. state_timer_heater_1 = MIN_STATE_TIME;
  1320. }
  1321. state_heater_1 = 0;
  1322. WRITE(HEATER_1_PIN, 0);
  1323. }
  1324. }
  1325. #endif
  1326. #if EXTRUDERS > 2
  1327. // EXTRUDER 2
  1328. if (soft_pwm_2 < slow_pwm_count) {
  1329. // turn OFF heather only if the minimum time is up
  1330. if (state_timer_heater_2 == 0) {
  1331. // if change state set timer
  1332. if (state_heater_2 == 1) {
  1333. state_timer_heater_2 = MIN_STATE_TIME;
  1334. }
  1335. state_heater_2 = 0;
  1336. WRITE(HEATER_2_PIN, 0);
  1337. }
  1338. }
  1339. #endif
  1340. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1341. // BED
  1342. if (soft_pwm_b < slow_pwm_count) {
  1343. // turn OFF heather only if the minimum time is up
  1344. if (state_timer_heater_b == 0) {
  1345. // if change state set timer
  1346. if (state_heater_b == 1) {
  1347. state_timer_heater_b = MIN_STATE_TIME;
  1348. }
  1349. state_heater_b = 0;
  1350. WRITE(HEATER_BED_PIN, 0);
  1351. }
  1352. }
  1353. #endif
  1354. #ifdef FAN_SOFT_PWM
  1355. if (pwm_count == 0){
  1356. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1357. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1358. }
  1359. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1360. #endif
  1361. pwm_count += (1 << SOFT_PWM_SCALE);
  1362. pwm_count &= 0x7f;
  1363. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1364. if ((pwm_count % 64) == 0) {
  1365. slow_pwm_count++;
  1366. slow_pwm_count &= 0x7f;
  1367. // Extruder 0
  1368. if (state_timer_heater_0 > 0) {
  1369. state_timer_heater_0--;
  1370. }
  1371. #if EXTRUDERS > 1
  1372. // Extruder 1
  1373. if (state_timer_heater_1 > 0)
  1374. state_timer_heater_1--;
  1375. #endif
  1376. #if EXTRUDERS > 2
  1377. // Extruder 2
  1378. if (state_timer_heater_2 > 0)
  1379. state_timer_heater_2--;
  1380. #endif
  1381. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1382. // Bed
  1383. if (state_timer_heater_b > 0)
  1384. state_timer_heater_b--;
  1385. #endif
  1386. } //if ((pwm_count % 64) == 0) {
  1387. #endif //ifndef SLOW_PWM_HEATERS
  1388. switch(temp_state) {
  1389. case 0: // Prepare TEMP_0
  1390. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1391. #if TEMP_0_PIN > 7
  1392. ADCSRB = 1<<MUX5;
  1393. #else
  1394. ADCSRB = 0;
  1395. #endif
  1396. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1397. ADCSRA |= 1<<ADSC; // Start conversion
  1398. #endif
  1399. lcd_buttons_update();
  1400. temp_state = 1;
  1401. break;
  1402. case 1: // Measure TEMP_0
  1403. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1404. raw_temp_0_value += ADC;
  1405. #endif
  1406. temp_state = 2;
  1407. break;
  1408. case 2: // Prepare TEMP_BED
  1409. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1410. #if TEMP_BED_PIN > 7
  1411. ADCSRB = 1<<MUX5;
  1412. #else
  1413. ADCSRB = 0;
  1414. #endif
  1415. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1416. ADCSRA |= 1<<ADSC; // Start conversion
  1417. #endif
  1418. lcd_buttons_update();
  1419. temp_state = 3;
  1420. break;
  1421. case 3: // Measure TEMP_BED
  1422. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1423. raw_temp_bed_value += ADC;
  1424. #endif
  1425. temp_state = 4;
  1426. break;
  1427. case 4: // Prepare TEMP_1
  1428. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1429. #if TEMP_1_PIN > 7
  1430. ADCSRB = 1<<MUX5;
  1431. #else
  1432. ADCSRB = 0;
  1433. #endif
  1434. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1435. ADCSRA |= 1<<ADSC; // Start conversion
  1436. #endif
  1437. lcd_buttons_update();
  1438. temp_state = 5;
  1439. break;
  1440. case 5: // Measure TEMP_1
  1441. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1442. raw_temp_1_value += ADC;
  1443. #endif
  1444. temp_state = 6;
  1445. break;
  1446. case 6: // Prepare TEMP_2
  1447. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1448. #if TEMP_2_PIN > 7
  1449. ADCSRB = 1<<MUX5;
  1450. #else
  1451. ADCSRB = 0;
  1452. #endif
  1453. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1454. ADCSRA |= 1<<ADSC; // Start conversion
  1455. #endif
  1456. lcd_buttons_update();
  1457. temp_state = 7;
  1458. break;
  1459. case 7: // Measure TEMP_2
  1460. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1461. raw_temp_2_value += ADC;
  1462. #endif
  1463. temp_state = 8;//change so that Filament Width is also measured
  1464. break;
  1465. case 8: //Prepare FILWIDTH
  1466. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1467. #if FILWIDTH_PIN>7
  1468. ADCSRB = 1<<MUX5;
  1469. #else
  1470. ADCSRB = 0;
  1471. #endif
  1472. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1473. ADCSRA |= 1<<ADSC; // Start conversion
  1474. #endif
  1475. lcd_buttons_update();
  1476. temp_state = 9;
  1477. break;
  1478. case 9: //Measure FILWIDTH
  1479. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1480. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1481. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1482. {
  1483. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1484. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1485. }
  1486. #endif
  1487. temp_state = 0;
  1488. temp_count++;
  1489. break;
  1490. case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1491. temp_state = 0;
  1492. break;
  1493. // default:
  1494. // SERIAL_ERROR_START;
  1495. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1496. // break;
  1497. }
  1498. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1499. {
  1500. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1501. {
  1502. #ifndef HEATER_0_USES_MAX6675
  1503. current_temperature_raw[0] = raw_temp_0_value;
  1504. #endif
  1505. #if EXTRUDERS > 1
  1506. current_temperature_raw[1] = raw_temp_1_value;
  1507. #endif
  1508. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1509. redundant_temperature_raw = raw_temp_1_value;
  1510. #endif
  1511. #if EXTRUDERS > 2
  1512. current_temperature_raw[2] = raw_temp_2_value;
  1513. #endif
  1514. current_temperature_bed_raw = raw_temp_bed_value;
  1515. }
  1516. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1517. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1518. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1519. #endif
  1520. temp_meas_ready = true;
  1521. temp_count = 0;
  1522. raw_temp_0_value = 0;
  1523. raw_temp_1_value = 0;
  1524. raw_temp_2_value = 0;
  1525. raw_temp_bed_value = 0;
  1526. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1527. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1528. #else
  1529. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1530. #endif
  1531. #ifndef HEATER_0_USES_MAX6675
  1532. max_temp_error(0);
  1533. #endif
  1534. }
  1535. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1536. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1537. #else
  1538. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1539. #endif
  1540. #ifndef HEATER_0_USES_MAX6675
  1541. min_temp_error(0);
  1542. #endif
  1543. }
  1544. #if EXTRUDERS > 1
  1545. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1546. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1547. #else
  1548. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1549. #endif
  1550. max_temp_error(1);
  1551. }
  1552. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1553. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1554. #else
  1555. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1556. #endif
  1557. min_temp_error(1);
  1558. }
  1559. #endif
  1560. #if EXTRUDERS > 2
  1561. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1562. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1563. #else
  1564. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1565. #endif
  1566. max_temp_error(2);
  1567. }
  1568. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1569. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1570. #else
  1571. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1572. #endif
  1573. min_temp_error(2);
  1574. }
  1575. #endif
  1576. /* No bed MINTEMP error? */
  1577. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1578. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1579. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1580. #else
  1581. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1582. #endif
  1583. target_temperature_bed = 0;
  1584. bed_max_temp_error();
  1585. }
  1586. #endif
  1587. }
  1588. #ifdef BABYSTEPPING
  1589. for(uint8_t axis=0;axis<3;axis++)
  1590. {
  1591. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1592. if(curTodo>0)
  1593. {
  1594. babystep(axis,/*fwd*/true);
  1595. babystepsTodo[axis]--; //less to do next time
  1596. }
  1597. else
  1598. if(curTodo<0)
  1599. {
  1600. babystep(axis,/*fwd*/false);
  1601. babystepsTodo[axis]++; //less to do next time
  1602. }
  1603. }
  1604. #endif //BABYSTEPPING
  1605. }
  1606. #ifdef PIDTEMP
  1607. // Apply the scale factors to the PID values
  1608. float scalePID_i(float i)
  1609. {
  1610. return i*PID_dT;
  1611. }
  1612. float unscalePID_i(float i)
  1613. {
  1614. return i/PID_dT;
  1615. }
  1616. float scalePID_d(float d)
  1617. {
  1618. return d/PID_dT;
  1619. }
  1620. float unscalePID_d(float d)
  1621. {
  1622. return d*PID_dT;
  1623. }
  1624. #endif //PIDTEMP