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

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