temperature.cpp 61 KB

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