temperature.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  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 "temperature.h"
  24. #include "stepper.h"
  25. #include "ultralcd.h"
  26. #include "menu.h"
  27. #include "sound.h"
  28. #include "fancheck.h"
  29. #include "SdFatUtil.h"
  30. #include <avr/wdt.h>
  31. #include "adc.h"
  32. #include "ConfigurationStore.h"
  33. #include "Timer.h"
  34. #include "Configuration_prusa.h"
  35. #if (ADC_OVRSAMPL != OVERSAMPLENR)
  36. #error "ADC_OVRSAMPL oversampling must match OVERSAMPLENR"
  37. #endif
  38. //===========================================================================
  39. //=============================public variables============================
  40. //===========================================================================
  41. int target_temperature[EXTRUDERS] = { 0 };
  42. int target_temperature_bed = 0;
  43. int current_temperature_raw[EXTRUDERS] = { 0 };
  44. float current_temperature[EXTRUDERS] = { 0.0 };
  45. #ifdef PINDA_THERMISTOR
  46. uint16_t current_temperature_raw_pinda = 0 ; //value with more averaging applied
  47. uint16_t current_temperature_raw_pinda_fast = 0; //value read from adc
  48. float current_temperature_pinda = 0.0;
  49. #endif //PINDA_THERMISTOR
  50. #ifdef AMBIENT_THERMISTOR
  51. int current_temperature_raw_ambient = 0 ;
  52. float current_temperature_ambient = 0.0;
  53. #endif //AMBIENT_THERMISTOR
  54. #ifdef VOLT_PWR_PIN
  55. int current_voltage_raw_pwr = 0;
  56. #endif
  57. #ifdef VOLT_BED_PIN
  58. int current_voltage_raw_bed = 0;
  59. #endif
  60. #ifdef IR_SENSOR_ANALOG
  61. uint16_t current_voltage_raw_IR = 0;
  62. #endif //IR_SENSOR_ANALOG
  63. int current_temperature_bed_raw = 0;
  64. float current_temperature_bed = 0.0;
  65. #ifdef PIDTEMP
  66. float _Kp, _Ki, _Kd;
  67. int pid_cycle, pid_number_of_cycles;
  68. bool pid_tuning_finished = false;
  69. #endif //PIDTEMP
  70. unsigned char soft_pwm_bed;
  71. #ifdef BABYSTEPPING
  72. volatile int babystepsTodo[3]={0,0,0};
  73. #endif
  74. //===========================================================================
  75. //=============================private variables============================
  76. //===========================================================================
  77. static volatile bool temp_meas_ready = false;
  78. #ifdef PIDTEMP
  79. //static cannot be external:
  80. static float iState_sum[EXTRUDERS] = { 0 };
  81. static float dState_last[EXTRUDERS] = { 0 };
  82. static float pTerm[EXTRUDERS];
  83. static float iTerm[EXTRUDERS];
  84. static float dTerm[EXTRUDERS];
  85. //int output;
  86. static float pid_error[EXTRUDERS];
  87. static float iState_sum_min[EXTRUDERS];
  88. static float iState_sum_max[EXTRUDERS];
  89. // static float pid_input[EXTRUDERS];
  90. // static float pid_output[EXTRUDERS];
  91. static bool pid_reset[EXTRUDERS];
  92. #endif //PIDTEMP
  93. #ifdef PIDTEMPBED
  94. //static cannot be external:
  95. static float temp_iState_bed = { 0 };
  96. static float temp_dState_bed = { 0 };
  97. static float pTerm_bed;
  98. static float iTerm_bed;
  99. static float dTerm_bed;
  100. //int output;
  101. static float pid_error_bed;
  102. static float temp_iState_min_bed;
  103. static float temp_iState_max_bed;
  104. #else //PIDTEMPBED
  105. static unsigned long previous_millis_bed_heater;
  106. #endif //PIDTEMPBED
  107. static unsigned char soft_pwm[EXTRUDERS];
  108. #ifdef FAN_SOFT_PWM
  109. unsigned char fanSpeedSoftPwm;
  110. static unsigned char soft_pwm_fan;
  111. #endif
  112. uint8_t fanSpeedBckp = 255;
  113. #if EXTRUDERS > 3
  114. # error Unsupported number of extruders
  115. #elif EXTRUDERS > 2
  116. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  117. #elif EXTRUDERS > 1
  118. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  119. #else
  120. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  121. #endif
  122. static ShortTimer oTimer4minTempHeater,oTimer4minTempBed;
  123. // Init min and max temp with extreme values to prevent false errors during startup
  124. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  125. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  126. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  127. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  128. #ifdef BED_MINTEMP
  129. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  130. #endif
  131. #ifdef BED_MAXTEMP
  132. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  133. #endif
  134. #ifdef AMBIENT_MINTEMP
  135. static int ambient_minttemp_raw = AMBIENT_RAW_LO_TEMP;
  136. #endif
  137. #ifdef AMBIENT_MAXTEMP
  138. static int ambient_maxttemp_raw = AMBIENT_RAW_HI_TEMP;
  139. #endif
  140. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  141. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  142. static float analog2temp(int raw, uint8_t e);
  143. static float analog2tempBed(int raw);
  144. #ifdef AMBIENT_MAXTEMP
  145. static float analog2tempAmbient(int raw);
  146. #endif
  147. static void updateTemperaturesFromRawValues();
  148. enum TempRunawayStates : uint8_t
  149. {
  150. TempRunaway_INACTIVE = 0,
  151. TempRunaway_PREHEAT = 1,
  152. TempRunaway_ACTIVE = 2,
  153. };
  154. #ifndef SOFT_PWM_SCALE
  155. #define SOFT_PWM_SCALE 0
  156. #endif
  157. //===========================================================================
  158. //============================= functions ============================
  159. //===========================================================================
  160. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  161. static uint8_t temp_runaway_status[1 + EXTRUDERS];
  162. static float temp_runaway_target[1 + EXTRUDERS];
  163. static uint32_t temp_runaway_timer[1 + EXTRUDERS];
  164. static uint16_t temp_runaway_error_counter[1 + EXTRUDERS];
  165. static void temp_runaway_check(uint8_t _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed);
  166. static void temp_runaway_stop(bool isPreheat, bool isBed);
  167. #endif
  168. // return "false", if all extruder-heaters are 'off' (ie. "true", if any heater is 'on')
  169. bool checkAllHotends(void)
  170. {
  171. bool result=false;
  172. for(int i=0;i<EXTRUDERS;i++) result=(result||(target_temperature[i]!=0));
  173. return(result);
  174. }
  175. // WARNING: the following function has been marked noinline to avoid a GCC 4.9.2 LTO
  176. // codegen bug causing a stack overwrite issue in process_commands()
  177. void __attribute__((noinline)) 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. unsigned long extruder_autofan_last_check = _millis();
  197. #endif
  198. if ((extruder >= EXTRUDERS)
  199. #if (TEMP_BED_PIN <= -1)
  200. ||(extruder < 0)
  201. #endif
  202. ){
  203. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  204. pid_tuning_finished = true;
  205. pid_cycle = 0;
  206. return;
  207. }
  208. SERIAL_ECHOLN("PID Autotune start");
  209. disable_heater(); // switch off all heaters.
  210. if (extruder<0)
  211. {
  212. soft_pwm_bed = (MAX_BED_POWER)/2;
  213. timer02_set_pwm0(soft_pwm_bed << 1);
  214. bias = d = (MAX_BED_POWER)/2;
  215. target_temperature_bed = (int)temp; // to display the requested target bed temperature properly on the main screen
  216. }
  217. else
  218. {
  219. soft_pwm[extruder] = (PID_MAX)/2;
  220. bias = d = (PID_MAX)/2;
  221. target_temperature[extruder] = (int)temp; // to display the requested target extruder temperature properly on the main screen
  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. if(_millis() - extruder_autofan_last_check > 2500) {
  234. checkExtruderAutoFans();
  235. extruder_autofan_last_check = _millis();
  236. }
  237. #endif
  238. if(heating == true && input > temp) {
  239. if(_millis() - t2 > 5000) {
  240. heating=false;
  241. if (extruder<0)
  242. {
  243. soft_pwm_bed = (bias - d) >> 1;
  244. timer02_set_pwm0(soft_pwm_bed << 1);
  245. }
  246. else
  247. soft_pwm[extruder] = (bias - d) >> 1;
  248. t1=_millis();
  249. t_high=t1 - t2;
  250. max=temp;
  251. }
  252. }
  253. if(heating == false && input < temp) {
  254. if(_millis() - t1 > 5000) {
  255. heating=true;
  256. t2=_millis();
  257. t_low=t2 - t1;
  258. if(pid_cycle > 0) {
  259. bias += (d*(t_high - t_low))/(t_low + t_high);
  260. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  261. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  262. else d = bias;
  263. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  264. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  265. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  266. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  267. if(pid_cycle > 2) {
  268. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  269. Tu = ((float)(t_low + t_high)/1000.0);
  270. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  271. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  272. _Kp = 0.6*Ku;
  273. _Ki = 2*_Kp/Tu;
  274. _Kd = _Kp*Tu/8;
  275. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  276. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  277. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  278. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  279. /*
  280. _Kp = 0.33*Ku;
  281. _Ki = _Kp/Tu;
  282. _Kd = _Kp*Tu/3;
  283. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  284. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  285. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  286. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  287. _Kp = 0.2*Ku;
  288. _Ki = 2*_Kp/Tu;
  289. _Kd = _Kp*Tu/3;
  290. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  291. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  292. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  293. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  294. */
  295. }
  296. }
  297. if (extruder<0)
  298. {
  299. soft_pwm_bed = (bias + d) >> 1;
  300. timer02_set_pwm0(soft_pwm_bed << 1);
  301. }
  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("B:");
  320. }else{
  321. p=soft_pwm[extruder];
  322. SERIAL_PROTOCOLPGM("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 (fabs(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(0);
  363. }
  364. }
  365. void updatePID()
  366. {
  367. #ifdef PIDTEMP
  368. for(uint_least8_t e = 0; e < EXTRUDERS; e++) {
  369. iState_sum_max[e] = PID_INTEGRAL_DRIVE_MAX / cs.Ki;
  370. }
  371. #endif
  372. #ifdef PIDTEMPBED
  373. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / cs.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. // reset PID state after changing target_temperature
  382. void resetPID(uint8_t extruder _UNUSED) {}
  383. void manage_heater()
  384. {
  385. #ifdef WATCHDOG
  386. wdt_reset();
  387. #endif //WATCHDOG
  388. checkFans();
  389. }
  390. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  391. // Derived from RepRap FiveD extruder::getTemperature()
  392. // For hot end temperature measurement.
  393. static float analog2temp(int raw, uint8_t e) {
  394. if(e >= EXTRUDERS)
  395. {
  396. SERIAL_ERROR_START;
  397. SERIAL_ERROR((int)e);
  398. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  399. kill(NULL, 6);
  400. return 0.0;
  401. }
  402. #ifdef HEATER_0_USES_MAX6675
  403. if (e == 0)
  404. {
  405. return 0.25 * raw;
  406. }
  407. #endif
  408. if(heater_ttbl_map[e] != NULL)
  409. {
  410. float celsius = 0;
  411. uint8_t i;
  412. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  413. for (i=1; i<heater_ttbllen_map[e]; i++)
  414. {
  415. if (PGM_RD_W((*tt)[i][0]) > raw)
  416. {
  417. celsius = PGM_RD_W((*tt)[i-1][1]) +
  418. (raw - PGM_RD_W((*tt)[i-1][0])) *
  419. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  420. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  421. break;
  422. }
  423. }
  424. // Overflow: Set to last value in the table
  425. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  426. return celsius;
  427. }
  428. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  429. }
  430. // Derived from RepRap FiveD extruder::getTemperature()
  431. // For bed temperature measurement.
  432. static float analog2tempBed(int raw) {
  433. #ifdef BED_USES_THERMISTOR
  434. float celsius = 0;
  435. byte i;
  436. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  437. {
  438. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  439. {
  440. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  441. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  442. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  443. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  444. break;
  445. }
  446. }
  447. // Overflow: Set to last value in the table
  448. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  449. // temperature offset adjustment
  450. #ifdef BED_OFFSET
  451. float _offset = BED_OFFSET;
  452. float _offset_center = BED_OFFSET_CENTER;
  453. float _offset_start = BED_OFFSET_START;
  454. float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
  455. float _second_koef = (_offset / 2) / (100 - _offset_center);
  456. if (celsius >= _offset_start && celsius <= _offset_center)
  457. {
  458. celsius = celsius + (_first_koef * (celsius - _offset_start));
  459. }
  460. else if (celsius > _offset_center && celsius <= 100)
  461. {
  462. celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + ( _second_koef * ( celsius - ( 100 - _offset_center ) )) ;
  463. }
  464. else if (celsius > 100)
  465. {
  466. celsius = celsius + _offset;
  467. }
  468. #endif
  469. return celsius;
  470. #elif defined BED_USES_AD595
  471. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  472. #else
  473. return 0;
  474. #endif
  475. }
  476. #ifdef AMBIENT_THERMISTOR
  477. static float analog2tempAmbient(int raw)
  478. {
  479. float celsius = 0;
  480. byte i;
  481. for (i=1; i<AMBIENTTEMPTABLE_LEN; i++)
  482. {
  483. if (PGM_RD_W(AMBIENTTEMPTABLE[i][0]) > raw)
  484. {
  485. celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]) +
  486. (raw - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0])) *
  487. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][1]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][1])) /
  488. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][0]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0]));
  489. break;
  490. }
  491. }
  492. // Overflow: Set to last value in the table
  493. if (i == AMBIENTTEMPTABLE_LEN) celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]);
  494. return celsius;
  495. }
  496. #endif //AMBIENT_THERMISTOR
  497. static void setTemperaturesFromRawValues()
  498. {
  499. for(uint8_t e=0;e<EXTRUDERS;e++)
  500. {
  501. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  502. }
  503. #ifdef PINDA_THERMISTOR
  504. current_temperature_raw_pinda = (uint16_t)((uint32_t)current_temperature_raw_pinda * 3 + current_temperature_raw_pinda_fast) >> 2;
  505. current_temperature_pinda = analog2tempBed(current_temperature_raw_pinda);
  506. #endif
  507. #ifdef AMBIENT_THERMISTOR
  508. current_temperature_ambient = analog2tempAmbient(current_temperature_raw_ambient); //thermistor for ambient is NTCG104LH104JT1 (2000)
  509. #endif
  510. #ifdef DEBUG_HEATER_BED_SIM
  511. current_temperature_bed = target_temperature_bed;
  512. #else //DEBUG_HEATER_BED_SIM
  513. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  514. #endif //DEBUG_HEATER_BED_SIM
  515. }
  516. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  517. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  518. static void updateTemperaturesFromRawValues()
  519. {
  520. // restart a new adc conversion
  521. CRITICAL_SECTION_START;
  522. adc_start_cycle();
  523. temp_meas_ready = false;
  524. CRITICAL_SECTION_END;
  525. // update the previous values
  526. setTemperaturesFromRawValues();
  527. }
  528. void soft_pwm_init()
  529. {
  530. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  531. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  532. MCUCR=(1<<JTD);
  533. MCUCR=(1<<JTD);
  534. #endif
  535. // Finish init of mult extruder arrays
  536. for(int e = 0; e < EXTRUDERS; e++) {
  537. // populate with the first value
  538. maxttemp[e] = maxttemp[0];
  539. #ifdef PIDTEMP
  540. iState_sum_min[e] = 0.0;
  541. iState_sum_max[e] = PID_INTEGRAL_DRIVE_MAX / cs.Ki;
  542. #endif //PIDTEMP
  543. #ifdef PIDTEMPBED
  544. temp_iState_min_bed = 0.0;
  545. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / cs.bedKi;
  546. #endif //PIDTEMPBED
  547. }
  548. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  549. SET_OUTPUT(HEATER_0_PIN);
  550. #endif
  551. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  552. SET_OUTPUT(HEATER_1_PIN);
  553. #endif
  554. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  555. SET_OUTPUT(HEATER_2_PIN);
  556. #endif
  557. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  558. SET_OUTPUT(HEATER_BED_PIN);
  559. #endif
  560. #if defined(FAN_PIN) && (FAN_PIN > -1)
  561. SET_OUTPUT(FAN_PIN);
  562. #ifdef FAST_PWM_FAN
  563. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  564. #endif
  565. #ifdef FAN_SOFT_PWM
  566. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  567. #endif
  568. #endif
  569. #ifdef HEATER_0_USES_MAX6675
  570. #ifndef SDSUPPORT
  571. SET_OUTPUT(SCK_PIN);
  572. WRITE(SCK_PIN,0);
  573. SET_OUTPUT(MOSI_PIN);
  574. WRITE(MOSI_PIN,1);
  575. SET_INPUT(MISO_PIN);
  576. WRITE(MISO_PIN,1);
  577. #endif
  578. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  579. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  580. pinMode(SS_PIN, OUTPUT);
  581. digitalWrite(SS_PIN,0);
  582. pinMode(MAX6675_SS, OUTPUT);
  583. digitalWrite(MAX6675_SS,1);
  584. #endif
  585. timer0_init(); //enables the heatbed timer.
  586. // timer2 already enabled earlier in the code
  587. // now enable the COMPB temperature interrupt
  588. OCR2B = 128;
  589. ENABLE_SOFT_PWM_INTERRUPT();
  590. timer4_init(); //for tone and Extruder fan PWM
  591. #ifdef HEATER_0_MINTEMP
  592. minttemp[0] = HEATER_0_MINTEMP;
  593. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  594. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  595. minttemp_raw[0] += OVERSAMPLENR;
  596. #else
  597. minttemp_raw[0] -= OVERSAMPLENR;
  598. #endif
  599. }
  600. #endif //MINTEMP
  601. #ifdef HEATER_0_MAXTEMP
  602. maxttemp[0] = HEATER_0_MAXTEMP;
  603. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  604. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  605. maxttemp_raw[0] -= OVERSAMPLENR;
  606. #else
  607. maxttemp_raw[0] += OVERSAMPLENR;
  608. #endif
  609. }
  610. #endif //MAXTEMP
  611. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  612. minttemp[1] = HEATER_1_MINTEMP;
  613. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  614. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  615. minttemp_raw[1] += OVERSAMPLENR;
  616. #else
  617. minttemp_raw[1] -= OVERSAMPLENR;
  618. #endif
  619. }
  620. #endif // MINTEMP 1
  621. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  622. maxttemp[1] = HEATER_1_MAXTEMP;
  623. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  624. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  625. maxttemp_raw[1] -= OVERSAMPLENR;
  626. #else
  627. maxttemp_raw[1] += OVERSAMPLENR;
  628. #endif
  629. }
  630. #endif //MAXTEMP 1
  631. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  632. minttemp[2] = HEATER_2_MINTEMP;
  633. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  634. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  635. minttemp_raw[2] += OVERSAMPLENR;
  636. #else
  637. minttemp_raw[2] -= OVERSAMPLENR;
  638. #endif
  639. }
  640. #endif //MINTEMP 2
  641. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  642. maxttemp[2] = HEATER_2_MAXTEMP;
  643. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  644. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  645. maxttemp_raw[2] -= OVERSAMPLENR;
  646. #else
  647. maxttemp_raw[2] += OVERSAMPLENR;
  648. #endif
  649. }
  650. #endif //MAXTEMP 2
  651. #ifdef BED_MINTEMP
  652. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  653. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  654. bed_minttemp_raw += OVERSAMPLENR;
  655. #else
  656. bed_minttemp_raw -= OVERSAMPLENR;
  657. #endif
  658. }
  659. #endif //BED_MINTEMP
  660. #ifdef BED_MAXTEMP
  661. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  662. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  663. bed_maxttemp_raw -= OVERSAMPLENR;
  664. #else
  665. bed_maxttemp_raw += OVERSAMPLENR;
  666. #endif
  667. }
  668. #endif //BED_MAXTEMP
  669. #ifdef AMBIENT_MINTEMP
  670. while(analog2tempAmbient(ambient_minttemp_raw) < AMBIENT_MINTEMP) {
  671. #if AMBIENT_RAW_LO_TEMP < AMBIENT_RAW_HI_TEMP
  672. ambient_minttemp_raw += OVERSAMPLENR;
  673. #else
  674. ambient_minttemp_raw -= OVERSAMPLENR;
  675. #endif
  676. }
  677. #endif //AMBIENT_MINTEMP
  678. #ifdef AMBIENT_MAXTEMP
  679. while(analog2tempAmbient(ambient_maxttemp_raw) > AMBIENT_MAXTEMP) {
  680. #if AMBIENT_RAW_LO_TEMP < AMBIENT_RAW_HI_TEMP
  681. ambient_maxttemp_raw -= OVERSAMPLENR;
  682. #else
  683. ambient_maxttemp_raw += OVERSAMPLENR;
  684. #endif
  685. }
  686. #endif //AMBIENT_MAXTEMP
  687. }
  688. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  689. void temp_runaway_check(uint8_t _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed)
  690. {
  691. float __delta;
  692. float __hysteresis = 0;
  693. uint16_t __timeout = 0;
  694. bool temp_runaway_check_active = false;
  695. static float __preheat_start[2] = { 0,0}; //currently just bed and one extruder
  696. static uint8_t __preheat_counter[2] = { 0,0};
  697. static uint8_t __preheat_errors[2] = { 0,0};
  698. if (_millis() - temp_runaway_timer[_heater_id] > 2000)
  699. {
  700. #ifdef TEMP_RUNAWAY_BED_TIMEOUT
  701. if (_isbed)
  702. {
  703. __hysteresis = TEMP_RUNAWAY_BED_HYSTERESIS;
  704. __timeout = TEMP_RUNAWAY_BED_TIMEOUT;
  705. }
  706. #endif
  707. #ifdef TEMP_RUNAWAY_EXTRUDER_TIMEOUT
  708. if (!_isbed)
  709. {
  710. __hysteresis = TEMP_RUNAWAY_EXTRUDER_HYSTERESIS;
  711. __timeout = TEMP_RUNAWAY_EXTRUDER_TIMEOUT;
  712. }
  713. #endif
  714. temp_runaway_timer[_heater_id] = _millis();
  715. if (_output == 0)
  716. {
  717. temp_runaway_check_active = false;
  718. temp_runaway_error_counter[_heater_id] = 0;
  719. }
  720. if (temp_runaway_target[_heater_id] != _target_temperature)
  721. {
  722. if (_target_temperature > 0)
  723. {
  724. temp_runaway_status[_heater_id] = TempRunaway_PREHEAT;
  725. temp_runaway_target[_heater_id] = _target_temperature;
  726. __preheat_start[_heater_id] = _current_temperature;
  727. __preheat_counter[_heater_id] = 0;
  728. }
  729. else
  730. {
  731. temp_runaway_status[_heater_id] = TempRunaway_INACTIVE;
  732. temp_runaway_target[_heater_id] = _target_temperature;
  733. }
  734. }
  735. if ((_current_temperature < _target_temperature) && (temp_runaway_status[_heater_id] == TempRunaway_PREHEAT))
  736. {
  737. __preheat_counter[_heater_id]++;
  738. if (__preheat_counter[_heater_id] > ((_isbed) ? 16 : 8)) // periodicaly check if current temperature changes
  739. {
  740. /*SERIAL_ECHOPGM("Heater:");
  741. MYSERIAL.print(_heater_id);
  742. SERIAL_ECHOPGM(" T:");
  743. MYSERIAL.print(_current_temperature);
  744. SERIAL_ECHOPGM(" Tstart:");
  745. MYSERIAL.print(__preheat_start[_heater_id]);
  746. SERIAL_ECHOPGM(" delta:");
  747. MYSERIAL.print(_current_temperature-__preheat_start[_heater_id]);*/
  748. //-// if (_current_temperature - __preheat_start[_heater_id] < 2) {
  749. //-// if (_current_temperature - __preheat_start[_heater_id] < ((_isbed && (_current_temperature>105.0))?0.6:2.0)) {
  750. __delta=2.0;
  751. if(_isbed)
  752. {
  753. __delta=3.0;
  754. if(_current_temperature>90.0) __delta=2.0;
  755. if(_current_temperature>105.0) __delta=0.6;
  756. }
  757. if (_current_temperature - __preheat_start[_heater_id] < __delta) {
  758. __preheat_errors[_heater_id]++;
  759. /*SERIAL_ECHOPGM(" Preheat errors:");
  760. MYSERIAL.println(__preheat_errors[_heater_id]);*/
  761. }
  762. else {
  763. //SERIAL_ECHOLNPGM("");
  764. __preheat_errors[_heater_id] = 0;
  765. }
  766. if (__preheat_errors[_heater_id] > ((_isbed) ? 3 : 5))
  767. {
  768. if (farm_mode) { prusa_statistics(0); }
  769. temp_runaway_stop(true, _isbed);
  770. if (farm_mode) { prusa_statistics(91); }
  771. }
  772. __preheat_start[_heater_id] = _current_temperature;
  773. __preheat_counter[_heater_id] = 0;
  774. }
  775. }
  776. //-// if (_current_temperature >= _target_temperature && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  777. if ((_current_temperature > (_target_temperature - __hysteresis)) && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  778. {
  779. /*SERIAL_ECHOPGM("Heater:");
  780. MYSERIAL.print(_heater_id);
  781. MYSERIAL.println(" ->tempRunaway");*/
  782. temp_runaway_status[_heater_id] = TempRunaway_ACTIVE;
  783. temp_runaway_check_active = false;
  784. temp_runaway_error_counter[_heater_id] = 0;
  785. }
  786. if (_output > 0)
  787. {
  788. temp_runaway_check_active = true;
  789. }
  790. if (temp_runaway_check_active)
  791. {
  792. // we are in range
  793. if ((_current_temperature > (_target_temperature - __hysteresis)) && (_current_temperature < (_target_temperature + __hysteresis)))
  794. {
  795. temp_runaway_check_active = false;
  796. temp_runaway_error_counter[_heater_id] = 0;
  797. }
  798. else
  799. {
  800. if (temp_runaway_status[_heater_id] > TempRunaway_PREHEAT)
  801. {
  802. temp_runaway_error_counter[_heater_id]++;
  803. if (temp_runaway_error_counter[_heater_id] * 2 > __timeout)
  804. {
  805. if (farm_mode) { prusa_statistics(0); }
  806. temp_runaway_stop(false, _isbed);
  807. if (farm_mode) { prusa_statistics(90); }
  808. }
  809. }
  810. }
  811. }
  812. }
  813. }
  814. void temp_runaway_stop(bool isPreheat, bool isBed)
  815. {
  816. disable_heater();
  817. Sound_MakeCustom(200,0,true);
  818. if (isPreheat)
  819. {
  820. lcd_setalertstatuspgm(isBed? PSTR("BED PREHEAT ERROR") : PSTR("PREHEAT ERROR"), LCD_STATUS_CRITICAL);
  821. SERIAL_ERROR_START;
  822. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY (PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY (PREHEAT HOTEND)");
  823. hotendFanSetFullSpeed();
  824. }
  825. else
  826. {
  827. lcd_setalertstatuspgm(isBed? PSTR("BED THERMAL RUNAWAY") : PSTR("THERMAL RUNAWAY"), LCD_STATUS_CRITICAL);
  828. SERIAL_ERROR_START;
  829. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  830. }
  831. Stop();
  832. }
  833. #endif
  834. void disable_heater()
  835. {
  836. setAllTargetHotends(0);
  837. setTargetBed(0);
  838. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  839. target_temperature[0]=0;
  840. soft_pwm[0]=0;
  841. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  842. WRITE(HEATER_0_PIN,LOW);
  843. #endif
  844. #endif
  845. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  846. target_temperature[1]=0;
  847. soft_pwm[1]=0;
  848. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  849. WRITE(HEATER_1_PIN,LOW);
  850. #endif
  851. #endif
  852. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  853. target_temperature[2]=0;
  854. soft_pwm[2]=0;
  855. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  856. WRITE(HEATER_2_PIN,LOW);
  857. #endif
  858. #endif
  859. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  860. target_temperature_bed=0;
  861. soft_pwm_bed=0;
  862. timer02_set_pwm0(soft_pwm_bed << 1);
  863. bedPWMDisabled = 0;
  864. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  865. //WRITE(HEATER_BED_PIN,LOW);
  866. #endif
  867. #endif
  868. }
  869. //! codes of alert messages for the LCD - it is shorter to compare an uin8_t
  870. //! than raw const char * of the messages themselves.
  871. //! Could be used for MAXTEMP situations too - after reaching MAXTEMP and turning off the heater automagically
  872. //! the heater/bed may cool down and a similar alert message like "MAXTERM fixed..." may be displayed.
  873. enum { LCDALERT_NONE = 0, LCDALERT_HEATERMINTEMP, LCDALERT_BEDMINTEMP, LCDALERT_MINTEMPFIXED, LCDALERT_PLEASERESTART };
  874. //! remember the last alert message sent to the LCD
  875. //! to prevent flicker and improve speed
  876. uint8_t last_alert_sent_to_lcd = LCDALERT_NONE;
  877. //! update the current temperature error message
  878. //! @param type short error abbreviation (PROGMEM)
  879. void temp_update_messagepgm(const char* PROGMEM type)
  880. {
  881. char msg[LCD_WIDTH];
  882. strcpy_P(msg, PSTR("Err: "));
  883. strcat_P(msg, type);
  884. lcd_setalertstatus(msg, LCD_STATUS_CRITICAL);
  885. }
  886. //! signal a temperature error on both the lcd and serial
  887. //! @param type short error abbreviation (PROGMEM)
  888. //! @param e optional extruder index for hotend errors
  889. void temp_error_messagepgm(const char* PROGMEM type, uint8_t e = EXTRUDERS)
  890. {
  891. temp_update_messagepgm(type);
  892. SERIAL_ERROR_START;
  893. if(e != EXTRUDERS) {
  894. SERIAL_ERROR((int)e);
  895. SERIAL_ERRORPGM(": ");
  896. }
  897. SERIAL_ERRORPGM("Heaters switched off. ");
  898. SERIAL_ERRORRPGM(type);
  899. SERIAL_ERRORLNPGM(" triggered!");
  900. }
  901. void max_temp_error(uint8_t e) {
  902. disable_heater();
  903. if(IsStopped() == false) {
  904. temp_error_messagepgm(PSTR("MAXTEMP"), e);
  905. }
  906. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  907. Stop();
  908. #endif
  909. hotendFanSetFullSpeed();
  910. if (farm_mode) { prusa_statistics(93); }
  911. }
  912. void min_temp_error(uint8_t e) {
  913. #ifdef DEBUG_DISABLE_MINTEMP
  914. return;
  915. #endif
  916. disable_heater();
  917. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  918. static const char err[] PROGMEM = "MINTEMP";
  919. if(IsStopped() == false) {
  920. temp_error_messagepgm(err, e);
  921. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  922. } else if( last_alert_sent_to_lcd != LCDALERT_HEATERMINTEMP ){ // only update, if the lcd message is to be changed (i.e. not the same as last time)
  923. // we are already stopped due to some error, only update the status message without flickering
  924. temp_update_messagepgm(err);
  925. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  926. }
  927. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  928. // if( last_alert_sent_to_lcd != LCDALERT_HEATERMINTEMP ){
  929. // last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  930. // lcd_print_stop();
  931. // }
  932. Stop();
  933. #endif
  934. if (farm_mode) { prusa_statistics(92); }
  935. }
  936. void bed_max_temp_error(void) {
  937. disable_heater();
  938. if(IsStopped() == false) {
  939. temp_error_messagepgm(PSTR("MAXTEMP BED"));
  940. }
  941. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  942. Stop();
  943. #endif
  944. }
  945. void bed_min_temp_error(void) {
  946. #ifdef DEBUG_DISABLE_MINTEMP
  947. return;
  948. #endif
  949. disable_heater();
  950. static const char err[] PROGMEM = "MINTEMP BED";
  951. if(IsStopped() == false) {
  952. temp_error_messagepgm(err);
  953. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  954. } else if( last_alert_sent_to_lcd != LCDALERT_BEDMINTEMP ){ // only update, if the lcd message is to be changed (i.e. not the same as last time)
  955. // we are already stopped due to some error, only update the status message without flickering
  956. temp_update_messagepgm(err);
  957. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  958. }
  959. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  960. Stop();
  961. #endif
  962. }
  963. #ifdef AMBIENT_THERMISTOR
  964. void ambient_max_temp_error(void) {
  965. disable_heater();
  966. if(IsStopped() == false) {
  967. temp_error_messagepgm(PSTR("MAXTEMP AMB"));
  968. }
  969. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  970. Stop();
  971. #endif
  972. }
  973. void ambient_min_temp_error(void) {
  974. #ifdef DEBUG_DISABLE_MINTEMP
  975. return;
  976. #endif
  977. disable_heater();
  978. if(IsStopped() == false) {
  979. temp_error_messagepgm(PSTR("MINTEMP AMB"));
  980. }
  981. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  982. Stop();
  983. #endif
  984. }
  985. #endif
  986. #ifdef HEATER_0_USES_MAX6675
  987. #define MAX6675_HEAT_INTERVAL 250
  988. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  989. int max6675_temp = 2000;
  990. int read_max6675()
  991. {
  992. if (_millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  993. return max6675_temp;
  994. max6675_previous_millis = _millis();
  995. max6675_temp = 0;
  996. #ifdef PRR
  997. PRR &= ~(1<<PRSPI);
  998. #elif defined PRR0
  999. PRR0 &= ~(1<<PRSPI);
  1000. #endif
  1001. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1002. // enable TT_MAX6675
  1003. WRITE(MAX6675_SS, 0);
  1004. // ensure 100ns delay - a bit extra is fine
  1005. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1006. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1007. // read MSB
  1008. SPDR = 0;
  1009. for (;(SPSR & (1<<SPIF)) == 0;);
  1010. max6675_temp = SPDR;
  1011. max6675_temp <<= 8;
  1012. // read LSB
  1013. SPDR = 0;
  1014. for (;(SPSR & (1<<SPIF)) == 0;);
  1015. max6675_temp |= SPDR;
  1016. // disable TT_MAX6675
  1017. WRITE(MAX6675_SS, 1);
  1018. if (max6675_temp & 4)
  1019. {
  1020. // thermocouple open
  1021. max6675_temp = 2000;
  1022. }
  1023. else
  1024. {
  1025. max6675_temp = max6675_temp >> 3;
  1026. }
  1027. return max6675_temp;
  1028. }
  1029. #endif
  1030. void adc_ready(void) //callback from adc when sampling finished
  1031. {
  1032. current_temperature_raw[0] = adc_values[ADC_PIN_IDX(TEMP_0_PIN)]; //heater
  1033. #ifdef PINDA_THERMISTOR
  1034. current_temperature_raw_pinda_fast = adc_values[ADC_PIN_IDX(TEMP_PINDA_PIN)];
  1035. #endif //PINDA_THERMISTOR
  1036. current_temperature_bed_raw = adc_values[ADC_PIN_IDX(TEMP_BED_PIN)];
  1037. #ifdef VOLT_PWR_PIN
  1038. current_voltage_raw_pwr = adc_values[ADC_PIN_IDX(VOLT_PWR_PIN)];
  1039. #endif
  1040. #ifdef AMBIENT_THERMISTOR
  1041. current_temperature_raw_ambient = adc_values[ADC_PIN_IDX(TEMP_AMBIENT_PIN)]; // 5->6
  1042. #endif //AMBIENT_THERMISTOR
  1043. #ifdef VOLT_BED_PIN
  1044. current_voltage_raw_bed = adc_values[ADC_PIN_IDX(VOLT_BED_PIN)]; // 6->9
  1045. #endif
  1046. #ifdef IR_SENSOR_ANALOG
  1047. current_voltage_raw_IR = adc_values[ADC_PIN_IDX(VOLT_IR_PIN)];
  1048. #endif //IR_SENSOR_ANALOG
  1049. temp_meas_ready = true;
  1050. }
  1051. #ifdef BABYSTEPPING
  1052. FORCE_INLINE static void applyBabysteps() {
  1053. for(uint8_t axis=0;axis<3;axis++)
  1054. {
  1055. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1056. if(curTodo>0)
  1057. {
  1058. CRITICAL_SECTION_START;
  1059. babystep(axis,/*fwd*/true);
  1060. babystepsTodo[axis]--; //less to do next time
  1061. CRITICAL_SECTION_END;
  1062. }
  1063. else
  1064. if(curTodo<0)
  1065. {
  1066. CRITICAL_SECTION_START;
  1067. babystep(axis,/*fwd*/false);
  1068. babystepsTodo[axis]++; //less to do next time
  1069. CRITICAL_SECTION_END;
  1070. }
  1071. }
  1072. }
  1073. #endif //BABYSTEPPING
  1074. FORCE_INLINE static void soft_pwm_core()
  1075. {
  1076. static uint8_t pwm_count = (1 << SOFT_PWM_SCALE);
  1077. static uint8_t soft_pwm_0;
  1078. #ifdef SLOW_PWM_HEATERS
  1079. static unsigned char slow_pwm_count = 0;
  1080. static unsigned char state_heater_0 = 0;
  1081. static unsigned char state_timer_heater_0 = 0;
  1082. #endif
  1083. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1084. static unsigned char soft_pwm_1;
  1085. #ifdef SLOW_PWM_HEATERS
  1086. static unsigned char state_heater_1 = 0;
  1087. static unsigned char state_timer_heater_1 = 0;
  1088. #endif
  1089. #endif
  1090. #if EXTRUDERS > 2
  1091. static unsigned char soft_pwm_2;
  1092. #ifdef SLOW_PWM_HEATERS
  1093. static unsigned char state_heater_2 = 0;
  1094. static unsigned char state_timer_heater_2 = 0;
  1095. #endif
  1096. #endif
  1097. #if HEATER_BED_PIN > -1
  1098. // @@DR static unsigned char soft_pwm_b;
  1099. #ifdef SLOW_PWM_HEATERS
  1100. static unsigned char state_heater_b = 0;
  1101. static unsigned char state_timer_heater_b = 0;
  1102. #endif
  1103. #endif
  1104. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1105. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1106. #endif
  1107. #ifndef SLOW_PWM_HEATERS
  1108. /*
  1109. * standard PWM modulation
  1110. */
  1111. if (pwm_count == 0)
  1112. {
  1113. soft_pwm_0 = soft_pwm[0];
  1114. if(soft_pwm_0 > 0)
  1115. {
  1116. WRITE(HEATER_0_PIN,1);
  1117. #ifdef HEATERS_PARALLEL
  1118. WRITE(HEATER_1_PIN,1);
  1119. #endif
  1120. } else WRITE(HEATER_0_PIN,0);
  1121. #if EXTRUDERS > 1
  1122. soft_pwm_1 = soft_pwm[1];
  1123. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1124. #endif
  1125. #if EXTRUDERS > 2
  1126. soft_pwm_2 = soft_pwm[2];
  1127. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1128. #endif
  1129. }
  1130. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1131. #if 0 // @@DR vypnuto pro hw pwm bedu
  1132. // tuhle prasarnu bude potreba poustet ve stanovenych intervalech, jinak nemam moc sanci zareagovat
  1133. // teoreticky by se tato cast uz vubec nemusela poustet
  1134. if ((pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1)) == 0)
  1135. {
  1136. soft_pwm_b = soft_pwm_bed >> (7 - HEATER_BED_SOFT_PWM_BITS);
  1137. # ifndef SYSTEM_TIMER_2
  1138. // tady budu krokovat pomalou frekvenci na automatu - tohle je rizeni spinani a rozepinani
  1139. // jako ridici frekvenci mam 2khz, jako vystupni frekvenci mam 30hz
  1140. // 2kHz jsou ovsem ve slysitelnem pasmu, mozna bude potreba jit s frekvenci nahoru (a tomu taky prizpusobit ostatni veci)
  1141. // Teoreticky bych mohl stahnout OCR0B citac na 6, cimz bych se dostal nekam ke 40khz a tady potom honit PWM rychleji nebo i pomaleji
  1142. // to nicemu nevadi. Soft PWM scale by se 20x zvetsilo (no dobre, 16x), cimz by se to posunulo k puvodnimu 30Hz PWM
  1143. //if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1144. # endif //SYSTEM_TIMER_2
  1145. }
  1146. #endif
  1147. #endif
  1148. #ifdef FAN_SOFT_PWM
  1149. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1150. {
  1151. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1152. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1153. }
  1154. #endif
  1155. if(soft_pwm_0 < pwm_count)
  1156. {
  1157. WRITE(HEATER_0_PIN,0);
  1158. #ifdef HEATERS_PARALLEL
  1159. WRITE(HEATER_1_PIN,0);
  1160. #endif
  1161. }
  1162. #if EXTRUDERS > 1
  1163. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1164. #endif
  1165. #if EXTRUDERS > 2
  1166. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1167. #endif
  1168. #if 0 // @@DR
  1169. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1170. if (soft_pwm_b < (pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1))){
  1171. //WRITE(HEATER_BED_PIN,0);
  1172. }
  1173. //WRITE(HEATER_BED_PIN, pwm_count & 1 );
  1174. #endif
  1175. #endif
  1176. #ifdef FAN_SOFT_PWM
  1177. if (soft_pwm_fan < (pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1))) WRITE(FAN_PIN,0);
  1178. #endif
  1179. pwm_count += (1 << SOFT_PWM_SCALE);
  1180. pwm_count &= 0x7f;
  1181. #else //ifndef SLOW_PWM_HEATERS
  1182. /*
  1183. * SLOW PWM HEATERS
  1184. *
  1185. * for heaters drived by relay
  1186. */
  1187. #ifndef MIN_STATE_TIME
  1188. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1189. #endif
  1190. if (slow_pwm_count == 0) {
  1191. // EXTRUDER 0
  1192. soft_pwm_0 = soft_pwm[0];
  1193. if (soft_pwm_0 > 0) {
  1194. // turn ON heather only if the minimum time is up
  1195. if (state_timer_heater_0 == 0) {
  1196. // if change state set timer
  1197. if (state_heater_0 == 0) {
  1198. state_timer_heater_0 = MIN_STATE_TIME;
  1199. }
  1200. state_heater_0 = 1;
  1201. WRITE(HEATER_0_PIN, 1);
  1202. #ifdef HEATERS_PARALLEL
  1203. WRITE(HEATER_1_PIN, 1);
  1204. #endif
  1205. }
  1206. } else {
  1207. // turn OFF heather only if the minimum time is up
  1208. if (state_timer_heater_0 == 0) {
  1209. // if change state set timer
  1210. if (state_heater_0 == 1) {
  1211. state_timer_heater_0 = MIN_STATE_TIME;
  1212. }
  1213. state_heater_0 = 0;
  1214. WRITE(HEATER_0_PIN, 0);
  1215. #ifdef HEATERS_PARALLEL
  1216. WRITE(HEATER_1_PIN, 0);
  1217. #endif
  1218. }
  1219. }
  1220. #if EXTRUDERS > 1
  1221. // EXTRUDER 1
  1222. soft_pwm_1 = soft_pwm[1];
  1223. if (soft_pwm_1 > 0) {
  1224. // turn ON heather only if the minimum time is up
  1225. if (state_timer_heater_1 == 0) {
  1226. // if change state set timer
  1227. if (state_heater_1 == 0) {
  1228. state_timer_heater_1 = MIN_STATE_TIME;
  1229. }
  1230. state_heater_1 = 1;
  1231. WRITE(HEATER_1_PIN, 1);
  1232. }
  1233. } else {
  1234. // turn OFF heather only if the minimum time is up
  1235. if (state_timer_heater_1 == 0) {
  1236. // if change state set timer
  1237. if (state_heater_1 == 1) {
  1238. state_timer_heater_1 = MIN_STATE_TIME;
  1239. }
  1240. state_heater_1 = 0;
  1241. WRITE(HEATER_1_PIN, 0);
  1242. }
  1243. }
  1244. #endif
  1245. #if EXTRUDERS > 2
  1246. // EXTRUDER 2
  1247. soft_pwm_2 = soft_pwm[2];
  1248. if (soft_pwm_2 > 0) {
  1249. // turn ON heather only if the minimum time is up
  1250. if (state_timer_heater_2 == 0) {
  1251. // if change state set timer
  1252. if (state_heater_2 == 0) {
  1253. state_timer_heater_2 = MIN_STATE_TIME;
  1254. }
  1255. state_heater_2 = 1;
  1256. WRITE(HEATER_2_PIN, 1);
  1257. }
  1258. } else {
  1259. // turn OFF heather only if the minimum time is up
  1260. if (state_timer_heater_2 == 0) {
  1261. // if change state set timer
  1262. if (state_heater_2 == 1) {
  1263. state_timer_heater_2 = MIN_STATE_TIME;
  1264. }
  1265. state_heater_2 = 0;
  1266. WRITE(HEATER_2_PIN, 0);
  1267. }
  1268. }
  1269. #endif
  1270. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1271. // BED
  1272. soft_pwm_b = soft_pwm_bed;
  1273. if (soft_pwm_b > 0) {
  1274. // turn ON heather only if the minimum time is up
  1275. if (state_timer_heater_b == 0) {
  1276. // if change state set timer
  1277. if (state_heater_b == 0) {
  1278. state_timer_heater_b = MIN_STATE_TIME;
  1279. }
  1280. state_heater_b = 1;
  1281. //WRITE(HEATER_BED_PIN, 1);
  1282. }
  1283. } else {
  1284. // turn OFF heather only if the minimum time is up
  1285. if (state_timer_heater_b == 0) {
  1286. // if change state set timer
  1287. if (state_heater_b == 1) {
  1288. state_timer_heater_b = MIN_STATE_TIME;
  1289. }
  1290. state_heater_b = 0;
  1291. WRITE(HEATER_BED_PIN, 0);
  1292. }
  1293. }
  1294. #endif
  1295. } // if (slow_pwm_count == 0)
  1296. // EXTRUDER 0
  1297. if (soft_pwm_0 < slow_pwm_count) {
  1298. // turn OFF heather only if the minimum time is up
  1299. if (state_timer_heater_0 == 0) {
  1300. // if change state set timer
  1301. if (state_heater_0 == 1) {
  1302. state_timer_heater_0 = MIN_STATE_TIME;
  1303. }
  1304. state_heater_0 = 0;
  1305. WRITE(HEATER_0_PIN, 0);
  1306. #ifdef HEATERS_PARALLEL
  1307. WRITE(HEATER_1_PIN, 0);
  1308. #endif
  1309. }
  1310. }
  1311. #if EXTRUDERS > 1
  1312. // EXTRUDER 1
  1313. if (soft_pwm_1 < slow_pwm_count) {
  1314. // turn OFF heather only if the minimum time is up
  1315. if (state_timer_heater_1 == 0) {
  1316. // if change state set timer
  1317. if (state_heater_1 == 1) {
  1318. state_timer_heater_1 = MIN_STATE_TIME;
  1319. }
  1320. state_heater_1 = 0;
  1321. WRITE(HEATER_1_PIN, 0);
  1322. }
  1323. }
  1324. #endif
  1325. #if EXTRUDERS > 2
  1326. // EXTRUDER 2
  1327. if (soft_pwm_2 < slow_pwm_count) {
  1328. // turn OFF heather only if the minimum time is up
  1329. if (state_timer_heater_2 == 0) {
  1330. // if change state set timer
  1331. if (state_heater_2 == 1) {
  1332. state_timer_heater_2 = MIN_STATE_TIME;
  1333. }
  1334. state_heater_2 = 0;
  1335. WRITE(HEATER_2_PIN, 0);
  1336. }
  1337. }
  1338. #endif
  1339. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1340. // BED
  1341. if (soft_pwm_b < slow_pwm_count) {
  1342. // turn OFF heather only if the minimum time is up
  1343. if (state_timer_heater_b == 0) {
  1344. // if change state set timer
  1345. if (state_heater_b == 1) {
  1346. state_timer_heater_b = MIN_STATE_TIME;
  1347. }
  1348. state_heater_b = 0;
  1349. WRITE(HEATER_BED_PIN, 0);
  1350. }
  1351. }
  1352. #endif
  1353. #ifdef FAN_SOFT_PWM
  1354. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1355. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1356. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1357. }
  1358. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1359. #endif
  1360. pwm_count += (1 << SOFT_PWM_SCALE);
  1361. pwm_count &= 0x7f;
  1362. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1363. if ((pwm_count % 64) == 0) {
  1364. slow_pwm_count++;
  1365. slow_pwm_count &= 0x7f;
  1366. // Extruder 0
  1367. if (state_timer_heater_0 > 0) {
  1368. state_timer_heater_0--;
  1369. }
  1370. #if EXTRUDERS > 1
  1371. // Extruder 1
  1372. if (state_timer_heater_1 > 0)
  1373. state_timer_heater_1--;
  1374. #endif
  1375. #if EXTRUDERS > 2
  1376. // Extruder 2
  1377. if (state_timer_heater_2 > 0)
  1378. state_timer_heater_2--;
  1379. #endif
  1380. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1381. // Bed
  1382. if (state_timer_heater_b > 0)
  1383. state_timer_heater_b--;
  1384. #endif
  1385. } //if ((pwm_count % 64) == 0) {
  1386. #endif //ifndef SLOW_PWM_HEATERS
  1387. }
  1388. FORCE_INLINE static void soft_pwm_isr()
  1389. {
  1390. lcd_buttons_update();
  1391. soft_pwm_core();
  1392. #ifdef BABYSTEPPING
  1393. applyBabysteps();
  1394. #endif //BABYSTEPPING
  1395. // Check if a stack overflow happened
  1396. if (!SdFatUtil::test_stack_integrity()) stack_error();
  1397. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1398. readFanTach();
  1399. #endif //(defined(TACH_0))
  1400. }
  1401. // Timer2 (originaly timer0) is shared with millies
  1402. #ifdef SYSTEM_TIMER_2
  1403. ISR(TIMER2_COMPB_vect)
  1404. #else //SYSTEM_TIMER_2
  1405. ISR(TIMER0_COMPB_vect)
  1406. #endif //SYSTEM_TIMER_2
  1407. {
  1408. DISABLE_SOFT_PWM_INTERRUPT();
  1409. sei();
  1410. soft_pwm_isr();
  1411. cli();
  1412. ENABLE_SOFT_PWM_INTERRUPT();
  1413. }
  1414. void check_max_temp()
  1415. {
  1416. //heater
  1417. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1418. if (current_temperature_raw[0] <= maxttemp_raw[0]) {
  1419. #else
  1420. if (current_temperature_raw[0] >= maxttemp_raw[0]) {
  1421. #endif
  1422. max_temp_error(0);
  1423. }
  1424. //bed
  1425. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1426. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1427. if (current_temperature_bed_raw <= bed_maxttemp_raw) {
  1428. #else
  1429. if (current_temperature_bed_raw >= bed_maxttemp_raw) {
  1430. #endif
  1431. bed_max_temp_error();
  1432. }
  1433. #endif
  1434. //ambient
  1435. #if defined(AMBIENT_MAXTEMP) && (TEMP_SENSOR_AMBIENT != 0)
  1436. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1437. if (current_temperature_raw_ambient <= ambient_maxttemp_raw) {
  1438. #else
  1439. if (current_temperature_raw_ambient >= ambient_maxttemp_raw) {
  1440. #endif
  1441. ambient_max_temp_error();
  1442. }
  1443. #endif
  1444. }
  1445. //! number of repeating the same state with consecutive step() calls
  1446. //! used to slow down text switching
  1447. struct alert_automaton_mintemp {
  1448. const char *m2;
  1449. alert_automaton_mintemp(const char *m2):m2(m2){}
  1450. private:
  1451. enum { ALERT_AUTOMATON_SPEED_DIV = 5 };
  1452. enum class States : uint8_t { Init = 0, TempAboveMintemp, ShowPleaseRestart, ShowMintemp };
  1453. States state = States::Init;
  1454. uint8_t repeat = ALERT_AUTOMATON_SPEED_DIV;
  1455. void substep(States next_state){
  1456. if( repeat == 0 ){
  1457. state = next_state; // advance to the next state
  1458. repeat = ALERT_AUTOMATON_SPEED_DIV; // and prepare repeating for it too
  1459. } else {
  1460. --repeat;
  1461. }
  1462. }
  1463. public:
  1464. //! brief state automaton step routine
  1465. //! @param current_temp current hotend/bed temperature (for computing simple hysteresis)
  1466. //! @param mintemp minimal temperature including hysteresis to check current_temp against
  1467. void step(float current_temp, float mintemp){
  1468. static const char m1[] PROGMEM = "Please restart";
  1469. switch(state){
  1470. case States::Init: // initial state - check hysteresis
  1471. if( current_temp > mintemp ){
  1472. state = States::TempAboveMintemp;
  1473. }
  1474. // otherwise keep the Err MINTEMP alert message on the display,
  1475. // i.e. do not transfer to state 1
  1476. break;
  1477. case States::TempAboveMintemp: // the temperature has risen above the hysteresis check
  1478. lcd_setalertstatuspgm(m2);
  1479. substep(States::ShowMintemp);
  1480. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1481. break;
  1482. case States::ShowPleaseRestart: // displaying "Please restart"
  1483. lcd_updatestatuspgm(m1);
  1484. substep(States::ShowMintemp);
  1485. last_alert_sent_to_lcd = LCDALERT_PLEASERESTART;
  1486. break;
  1487. case States::ShowMintemp: // displaying "MINTEMP fixed"
  1488. lcd_updatestatuspgm(m2);
  1489. substep(States::ShowPleaseRestart);
  1490. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1491. break;
  1492. }
  1493. }
  1494. };
  1495. static const char m2hotend[] PROGMEM = "MINTEMP HOTEND fixed";
  1496. static const char m2bed[] PROGMEM = "MINTEMP BED fixed";
  1497. static alert_automaton_mintemp alert_automaton_hotend(m2hotend), alert_automaton_bed(m2bed);
  1498. void check_min_temp_heater0()
  1499. {
  1500. //heater
  1501. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1502. if (current_temperature_raw[0] >= minttemp_raw[0]) {
  1503. #else
  1504. if (current_temperature_raw[0] <= minttemp_raw[0]) {
  1505. #endif
  1506. menu_set_serious_error(SERIOUS_ERR_MINTEMP_HEATER);
  1507. min_temp_error(0);
  1508. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_HEATER) ) {
  1509. // no recovery, just force the user to restart the printer
  1510. // which is a safer variant than just continuing printing
  1511. // The automaton also checks for hysteresis - the temperature must have reached a few degrees above the MINTEMP, before
  1512. // we shall signalize, that MINTEMP has been fixed
  1513. // Code notice: normally the alert_automaton instance would have been placed here
  1514. // as static alert_automaton_mintemp alert_automaton_hotend, but
  1515. // due to stupid compiler that takes 16 more bytes.
  1516. alert_automaton_hotend.step(current_temperature[0], minttemp[0] + TEMP_HYSTERESIS);
  1517. }
  1518. }
  1519. void check_min_temp_bed()
  1520. {
  1521. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1522. if (current_temperature_bed_raw >= bed_minttemp_raw) {
  1523. #else
  1524. if (current_temperature_bed_raw <= bed_minttemp_raw) {
  1525. #endif
  1526. menu_set_serious_error(SERIOUS_ERR_MINTEMP_BED);
  1527. bed_min_temp_error();
  1528. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_BED) ){
  1529. // no recovery, just force the user to restart the printer
  1530. // which is a safer variant than just continuing printing
  1531. alert_automaton_bed.step(current_temperature_bed, BED_MINTEMP + TEMP_HYSTERESIS);
  1532. }
  1533. }
  1534. #ifdef AMBIENT_MINTEMP
  1535. void check_min_temp_ambient()
  1536. {
  1537. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1538. if (current_temperature_raw_ambient >= ambient_minttemp_raw) {
  1539. #else
  1540. if (current_temperature_raw_ambient <= ambient_minttemp_raw) {
  1541. #endif
  1542. ambient_min_temp_error();
  1543. }
  1544. }
  1545. #endif
  1546. void check_min_temp()
  1547. {
  1548. static bool bCheckingOnHeater=false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over heaterMintemp)
  1549. static bool bCheckingOnBed=false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over bedMintemp)
  1550. #ifdef AMBIENT_THERMISTOR
  1551. #ifdef AMBIENT_MINTEMP
  1552. check_min_temp_ambient();
  1553. #endif
  1554. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1555. if(current_temperature_raw_ambient>(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW)) // thermistor is NTC type
  1556. #else
  1557. if(current_temperature_raw_ambient=<(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW))
  1558. #endif
  1559. { // ambient temperature is low
  1560. #endif //AMBIENT_THERMISTOR
  1561. // *** 'common' part of code for MK2.5 & MK3
  1562. // * nozzle checking
  1563. if(target_temperature[active_extruder]>minttemp[active_extruder])
  1564. { // ~ nozzle heating is on
  1565. bCheckingOnHeater=bCheckingOnHeater||(current_temperature[active_extruder]>(minttemp[active_extruder]+TEMP_HYSTERESIS)); // for eventually delay cutting
  1566. if(oTimer4minTempHeater.expired(HEATER_MINTEMP_DELAY)||(!oTimer4minTempHeater.running())||bCheckingOnHeater)
  1567. {
  1568. bCheckingOnHeater=true; // not necessary
  1569. check_min_temp_heater0(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  1570. }
  1571. }
  1572. else { // ~ nozzle heating is off
  1573. oTimer4minTempHeater.start();
  1574. bCheckingOnHeater=false;
  1575. }
  1576. // * bed checking
  1577. if(target_temperature_bed>BED_MINTEMP)
  1578. { // ~ bed heating is on
  1579. bCheckingOnBed=bCheckingOnBed||(current_temperature_bed>(BED_MINTEMP+TEMP_HYSTERESIS)); // for eventually delay cutting
  1580. if(oTimer4minTempBed.expired(BED_MINTEMP_DELAY)||(!oTimer4minTempBed.running())||bCheckingOnBed)
  1581. {
  1582. bCheckingOnBed=true; // not necessary
  1583. check_min_temp_bed(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  1584. }
  1585. }
  1586. else { // ~ bed heating is off
  1587. oTimer4minTempBed.start();
  1588. bCheckingOnBed=false;
  1589. }
  1590. // *** end of 'common' part
  1591. #ifdef AMBIENT_THERMISTOR
  1592. }
  1593. else { // ambient temperature is standard
  1594. check_min_temp_heater0();
  1595. check_min_temp_bed();
  1596. }
  1597. #endif //AMBIENT_THERMISTOR
  1598. }
  1599. #ifdef PIDTEMP
  1600. // Apply the scale factors to the PID values
  1601. float scalePID_i(float i)
  1602. {
  1603. return i*PID_dT;
  1604. }
  1605. float unscalePID_i(float i)
  1606. {
  1607. return i/PID_dT;
  1608. }
  1609. float scalePID_d(float d)
  1610. {
  1611. return d/PID_dT;
  1612. }
  1613. float unscalePID_d(float d)
  1614. {
  1615. return d*PID_dT;
  1616. }
  1617. #endif //PIDTEMP
  1618. #ifdef PINDA_THERMISTOR
  1619. //! @brief PINDA thermistor detected
  1620. //!
  1621. //! @retval true firmware should do temperature compensation and allow calibration
  1622. //! @retval false PINDA thermistor is not detected, disable temperature compensation and calibration
  1623. //! @retval true/false when forced via LCD menu Settings->HW Setup->SuperPINDA
  1624. //!
  1625. bool has_temperature_compensation()
  1626. {
  1627. #ifdef SUPERPINDA_SUPPORT
  1628. #ifdef PINDA_TEMP_COMP
  1629. uint8_t pinda_temp_compensation = eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION);
  1630. if (pinda_temp_compensation == EEPROM_EMPTY_VALUE) //Unkown PINDA temp compenstation, so check it.
  1631. {
  1632. #endif //PINDA_TEMP_COMP
  1633. return (current_temperature_pinda >= PINDA_MINTEMP) ? true : false;
  1634. #ifdef PINDA_TEMP_COMP
  1635. }
  1636. else if (pinda_temp_compensation == 0) return true; //Overwritten via LCD menu SuperPINDA [No]
  1637. else return false; //Overwritten via LCD menu SuperPINDA [YES]
  1638. #endif //PINDA_TEMP_COMP
  1639. #else
  1640. return true;
  1641. #endif
  1642. }
  1643. #endif //PINDA_THERMISTOR
  1644. #define TEMP_MGR_INTV 0.27 // seconds, ~3.7Hz
  1645. #define TIMER5_PRESCALE 256
  1646. #define TIMER5_OCRA_OVF (uint16_t)(TEMP_MGR_INTV / ((long double)TIMER5_PRESCALE / F_CPU))
  1647. #define ENABLE_TEMP_MGR_INTERRUPT() TIMSK5 |= (1<<OCIE5A)
  1648. #define DISABLE_TEMP_MGR_INTERRUPT() TIMSK5 &= ~(1<<OCIE5A)
  1649. void temp_mgr_init()
  1650. {
  1651. // initialize the ADC and start a conversion
  1652. adc_init();
  1653. adc_start_cycle();
  1654. // initialize timer5
  1655. CRITICAL_SECTION_START;
  1656. // CTC
  1657. TCCR5B &= ~(1<<WGM53);
  1658. TCCR5B |= (1<<WGM52);
  1659. TCCR5A &= ~(1<<WGM51);
  1660. TCCR5A &= ~(1<<WGM50);
  1661. // output mode = 00 (disconnected)
  1662. TCCR5A &= ~(3<<COM5A0);
  1663. TCCR5A &= ~(3<<COM5B0);
  1664. // x/256 prescaler
  1665. TCCR5B |= (1<<CS52);
  1666. TCCR5B &= ~(1<<CS51);
  1667. TCCR5B &= ~(1<<CS50);
  1668. // reset counter
  1669. TCNT5 = 0;
  1670. OCR5A = TIMER5_OCRA_OVF;
  1671. // clear pending interrupts, enable COMPA
  1672. TIFR5 |= (1<<OCF5A);
  1673. ENABLE_TEMP_MGR_INTERRUPT();
  1674. CRITICAL_SECTION_END;
  1675. }
  1676. static void pid_heater(uint8_t e)
  1677. {
  1678. float pid_input;
  1679. float pid_output;
  1680. #ifdef TEMP_RUNAWAY_EXTRUDER_HYSTERESIS
  1681. temp_runaway_check(e+1, target_temperature[e], current_temperature[e], (int)soft_pwm[e], false);
  1682. #endif
  1683. #ifdef PIDTEMP
  1684. pid_input = current_temperature[e];
  1685. #ifndef PID_OPENLOOP
  1686. if(target_temperature[e] == 0) {
  1687. pid_output = 0;
  1688. pid_reset[e] = true;
  1689. } else {
  1690. pid_error[e] = target_temperature[e] - pid_input;
  1691. if(pid_reset[e]) {
  1692. iState_sum[e] = 0.0;
  1693. dTerm[e] = 0.0; // 'dState_last[e]' initial setting is not necessary (see end of if-statement)
  1694. pid_reset[e] = false;
  1695. }
  1696. #ifndef PonM
  1697. pTerm[e] = cs.Kp * pid_error[e];
  1698. iState_sum[e] += pid_error[e];
  1699. iState_sum[e] = constrain(iState_sum[e], iState_sum_min[e], iState_sum_max[e]);
  1700. iTerm[e] = cs.Ki * iState_sum[e];
  1701. // PID_K1 defined in Configuration.h in the PID settings
  1702. #define K2 (1.0-PID_K1)
  1703. dTerm[e] = (cs.Kd * (pid_input - dState_last[e]))*K2 + (PID_K1 * dTerm[e]); // e.g. digital filtration of derivative term changes
  1704. 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)
  1705. if (pid_output > PID_MAX) {
  1706. if (pid_error[e] > 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1707. pid_output=PID_MAX;
  1708. } else if (pid_output < 0) {
  1709. if (pid_error[e] < 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1710. pid_output=0;
  1711. }
  1712. #else // PonM ("Proportional on Measurement" method)
  1713. iState_sum[e] += cs.Ki * pid_error[e];
  1714. iState_sum[e] -= cs.Kp * (pid_input - dState_last[e]);
  1715. iState_sum[e] = constrain(iState_sum[e], 0, PID_INTEGRAL_DRIVE_MAX);
  1716. dTerm[e] = cs.Kd * (pid_input - dState_last[e]);
  1717. 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)
  1718. pid_output = constrain(pid_output, 0, PID_MAX);
  1719. #endif // PonM
  1720. }
  1721. dState_last[e] = pid_input;
  1722. #else //PID_OPENLOOP
  1723. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  1724. #endif //PID_OPENLOOP
  1725. #ifdef PID_DEBUG
  1726. SERIAL_ECHO_START;
  1727. SERIAL_ECHO(" PID_DEBUG ");
  1728. SERIAL_ECHO(e);
  1729. SERIAL_ECHO(": Input ");
  1730. SERIAL_ECHO(pid_input);
  1731. SERIAL_ECHO(" Output ");
  1732. SERIAL_ECHO(pid_output);
  1733. SERIAL_ECHO(" pTerm ");
  1734. SERIAL_ECHO(pTerm[e]);
  1735. SERIAL_ECHO(" iTerm ");
  1736. SERIAL_ECHO(iTerm[e]);
  1737. SERIAL_ECHO(" dTerm ");
  1738. SERIAL_ECHOLN(-dTerm[e]);
  1739. #endif //PID_DEBUG
  1740. #else /* PID off */
  1741. pid_output = 0;
  1742. if(current_temperature[e] < target_temperature[e]) {
  1743. pid_output = PID_MAX;
  1744. }
  1745. #endif
  1746. // Check if temperature is within the correct range
  1747. if((current_temperature[e] < maxttemp[e]) && (target_temperature[e] != 0))
  1748. soft_pwm[e] = (int)pid_output >> 1;
  1749. else
  1750. soft_pwm[e] = 0;
  1751. }
  1752. static void pid_bed()
  1753. {
  1754. float pid_input;
  1755. float pid_output;
  1756. #ifdef TEMP_RUNAWAY_BED_HYSTERESIS
  1757. temp_runaway_check(0, target_temperature_bed, current_temperature_bed, (int)soft_pwm_bed, true);
  1758. #endif
  1759. #ifndef PIDTEMPBED
  1760. if(_millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  1761. return;
  1762. previous_millis_bed_heater = _millis();
  1763. #endif
  1764. #if TEMP_SENSOR_BED != 0
  1765. #ifdef PIDTEMPBED
  1766. pid_input = current_temperature_bed;
  1767. #ifndef PID_OPENLOOP
  1768. pid_error_bed = target_temperature_bed - pid_input;
  1769. pTerm_bed = cs.bedKp * pid_error_bed;
  1770. temp_iState_bed += pid_error_bed;
  1771. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  1772. iTerm_bed = cs.bedKi * temp_iState_bed;
  1773. //PID_K1 defined in Configuration.h in the PID settings
  1774. #define K2 (1.0-PID_K1)
  1775. dTerm_bed= (cs.bedKd * (pid_input - temp_dState_bed))*K2 + (PID_K1 * dTerm_bed);
  1776. temp_dState_bed = pid_input;
  1777. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  1778. if (pid_output > MAX_BED_POWER) {
  1779. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1780. pid_output=MAX_BED_POWER;
  1781. } else if (pid_output < 0){
  1782. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1783. pid_output=0;
  1784. }
  1785. #else
  1786. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  1787. #endif //PID_OPENLOOP
  1788. if(current_temperature_bed < BED_MAXTEMP)
  1789. {
  1790. soft_pwm_bed = (int)pid_output >> 1;
  1791. timer02_set_pwm0(soft_pwm_bed << 1);
  1792. }
  1793. else
  1794. {
  1795. soft_pwm_bed = 0;
  1796. timer02_set_pwm0(soft_pwm_bed << 1);
  1797. }
  1798. #elif !defined(BED_LIMIT_SWITCHING)
  1799. // Check if temperature is within the correct range
  1800. if(current_temperature_bed < BED_MAXTEMP)
  1801. {
  1802. if(current_temperature_bed >= target_temperature_bed)
  1803. {
  1804. soft_pwm_bed = 0;
  1805. timer02_set_pwm0(soft_pwm_bed << 1);
  1806. }
  1807. else
  1808. {
  1809. soft_pwm_bed = MAX_BED_POWER>>1;
  1810. timer02_set_pwm0(soft_pwm_bed << 1);
  1811. }
  1812. }
  1813. else
  1814. {
  1815. soft_pwm_bed = 0;
  1816. timer02_set_pwm0(soft_pwm_bed << 1);
  1817. WRITE(HEATER_BED_PIN,LOW);
  1818. }
  1819. #else //#ifdef BED_LIMIT_SWITCHING
  1820. // Check if temperature is within the correct band
  1821. if(current_temperature_bed < BED_MAXTEMP)
  1822. {
  1823. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  1824. {
  1825. soft_pwm_bed = 0;
  1826. timer02_set_pwm0(soft_pwm_bed << 1);
  1827. }
  1828. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  1829. {
  1830. soft_pwm_bed = MAX_BED_POWER>>1;
  1831. timer02_set_pwm0(soft_pwm_bed << 1);
  1832. }
  1833. }
  1834. else
  1835. {
  1836. soft_pwm_bed = 0;
  1837. timer02_set_pwm0(soft_pwm_bed << 1);
  1838. WRITE(HEATER_BED_PIN,LOW);
  1839. }
  1840. #endif //BED_LIMIT_SWITCHING
  1841. if(target_temperature_bed==0)
  1842. {
  1843. soft_pwm_bed = 0;
  1844. timer02_set_pwm0(soft_pwm_bed << 1);
  1845. }
  1846. #endif //TEMP_SENSOR_BED
  1847. }
  1848. static void temp_mgr_isr()
  1849. {
  1850. // ADC values need to be converted before checking: converted values are later used in MINTEMP
  1851. setTemperaturesFromRawValues();
  1852. // TODO: this is now running inside an isr and cannot directly manipulate the lcd,
  1853. // this needs to disable temperatures and flag the error to be shown in manage_heaters!
  1854. check_max_temp();
  1855. check_min_temp();
  1856. for(uint8_t e = 0; e < EXTRUDERS; e++)
  1857. pid_heater(e);
  1858. pid_bed();
  1859. }
  1860. ISR(TIMER5_COMPA_vect)
  1861. {
  1862. // immediately schedule a new conversion
  1863. if(temp_meas_ready != true) return;
  1864. adc_start_cycle();
  1865. temp_meas_ready = false;
  1866. // run temperature management with interrupts enabled to reduce latency
  1867. DISABLE_TEMP_MGR_INTERRUPT();
  1868. sei();
  1869. temp_mgr_isr();
  1870. cli();
  1871. ENABLE_TEMP_MGR_INTERRUPT();
  1872. }