temperature.cpp 53 KB

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