temperature.cpp 60 KB

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