temperature.cpp 61 KB

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