temperature.cpp 56 KB

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