temperature.cpp 61 KB

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