temperature.cpp 64 KB

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