temperature.cpp 62 KB

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