temperature.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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. //! codes of alert messages for the LCD - it is shorter to compare an uin8_t
  805. //! than raw const char * of the messages themselves.
  806. //! Could be used for MAXTEMP situations too - after reaching MAXTEMP and turning off the heater automagically
  807. //! the heater/bed may cool down and a similar alert message like "MAXTERM fixed..." may be displayed.
  808. enum { LCDALERT_NONE = 0, LCDALERT_HEATERMINTEMP, LCDALERT_BEDMINTEMP, LCDALERT_MINTEMPFIXED, LCDALERT_PLEASERESTART };
  809. //! remember the last alert message sent to the LCD
  810. //! to prevent flicker and improve speed
  811. uint8_t last_alert_sent_to_lcd = LCDALERT_NONE;
  812. //! update the current temperature error message
  813. //! @param type short error abbreviation (PROGMEM)
  814. void temp_update_messagepgm(const char* PROGMEM type)
  815. {
  816. char msg[LCD_WIDTH];
  817. strcpy_P(msg, PSTR("Err: "));
  818. strcat_P(msg, type);
  819. lcd_setalertstatus(msg, LCD_STATUS_CRITICAL);
  820. }
  821. //! signal a temperature error on both the lcd and serial
  822. //! @param type short error abbreviation (PROGMEM)
  823. //! @param e optional extruder index for hotend errors
  824. void temp_error_messagepgm(const char* PROGMEM type, uint8_t e = EXTRUDERS)
  825. {
  826. temp_update_messagepgm(type);
  827. SERIAL_ERROR_START;
  828. if(e != EXTRUDERS) {
  829. SERIAL_ERROR((int)e);
  830. SERIAL_ERRORPGM(": ");
  831. }
  832. SERIAL_ERRORPGM("Heaters switched off. ");
  833. SERIAL_ERRORRPGM(type);
  834. SERIAL_ERRORLNPGM(" triggered!");
  835. }
  836. void max_temp_error(uint8_t e) {
  837. disable_heater();
  838. if(IsStopped() == false) {
  839. temp_error_messagepgm(PSTR("MAXTEMP"), e);
  840. }
  841. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  842. Stop();
  843. #endif
  844. hotendFanSetFullSpeed();
  845. if (farm_mode) { prusa_statistics(93); }
  846. }
  847. void min_temp_error(uint8_t e) {
  848. #ifdef DEBUG_DISABLE_MINTEMP
  849. return;
  850. #endif
  851. disable_heater();
  852. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  853. static const char err[] PROGMEM = "MINTEMP";
  854. if(IsStopped() == false) {
  855. temp_error_messagepgm(err, e);
  856. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  857. } 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)
  858. // we are already stopped due to some error, only update the status message without flickering
  859. temp_update_messagepgm(err);
  860. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  861. }
  862. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  863. // if( last_alert_sent_to_lcd != LCDALERT_HEATERMINTEMP ){
  864. // last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  865. // lcd_print_stop();
  866. // }
  867. Stop();
  868. #endif
  869. if (farm_mode) { prusa_statistics(92); }
  870. }
  871. void bed_max_temp_error(void) {
  872. disable_heater();
  873. if(IsStopped() == false) {
  874. temp_error_messagepgm(PSTR("MAXTEMP BED"));
  875. }
  876. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  877. Stop();
  878. #endif
  879. }
  880. void bed_min_temp_error(void) {
  881. #ifdef DEBUG_DISABLE_MINTEMP
  882. return;
  883. #endif
  884. disable_heater();
  885. static const char err[] PROGMEM = "MINTEMP BED";
  886. if(IsStopped() == false) {
  887. temp_error_messagepgm(err);
  888. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  889. } 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)
  890. // we are already stopped due to some error, only update the status message without flickering
  891. temp_update_messagepgm(err);
  892. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  893. }
  894. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  895. Stop();
  896. #endif
  897. }
  898. #ifdef AMBIENT_THERMISTOR
  899. void ambient_max_temp_error(void) {
  900. disable_heater();
  901. if(IsStopped() == false) {
  902. temp_error_messagepgm(PSTR("MAXTEMP AMB"));
  903. }
  904. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  905. Stop();
  906. #endif
  907. }
  908. void ambient_min_temp_error(void) {
  909. #ifdef DEBUG_DISABLE_MINTEMP
  910. return;
  911. #endif
  912. disable_heater();
  913. if(IsStopped() == false) {
  914. temp_error_messagepgm(PSTR("MINTEMP AMB"));
  915. }
  916. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  917. Stop();
  918. #endif
  919. }
  920. #endif
  921. #ifdef HEATER_0_USES_MAX6675
  922. #define MAX6675_HEAT_INTERVAL 250
  923. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  924. int max6675_temp = 2000;
  925. int read_max6675()
  926. {
  927. if (_millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  928. return max6675_temp;
  929. max6675_previous_millis = _millis();
  930. max6675_temp = 0;
  931. #ifdef PRR
  932. PRR &= ~(1<<PRSPI);
  933. #elif defined PRR0
  934. PRR0 &= ~(1<<PRSPI);
  935. #endif
  936. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  937. // enable TT_MAX6675
  938. WRITE(MAX6675_SS, 0);
  939. // ensure 100ns delay - a bit extra is fine
  940. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  941. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  942. // read MSB
  943. SPDR = 0;
  944. for (;(SPSR & (1<<SPIF)) == 0;);
  945. max6675_temp = SPDR;
  946. max6675_temp <<= 8;
  947. // read LSB
  948. SPDR = 0;
  949. for (;(SPSR & (1<<SPIF)) == 0;);
  950. max6675_temp |= SPDR;
  951. // disable TT_MAX6675
  952. WRITE(MAX6675_SS, 1);
  953. if (max6675_temp & 4)
  954. {
  955. // thermocouple open
  956. max6675_temp = 2000;
  957. }
  958. else
  959. {
  960. max6675_temp = max6675_temp >> 3;
  961. }
  962. return max6675_temp;
  963. }
  964. #endif
  965. #ifdef BABYSTEPPING
  966. FORCE_INLINE static void applyBabysteps() {
  967. for(uint8_t axis=0;axis<3;axis++)
  968. {
  969. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  970. if(curTodo>0)
  971. {
  972. CRITICAL_SECTION_START;
  973. babystep(axis,/*fwd*/true);
  974. babystepsTodo[axis]--; //less to do next time
  975. CRITICAL_SECTION_END;
  976. }
  977. else
  978. if(curTodo<0)
  979. {
  980. CRITICAL_SECTION_START;
  981. babystep(axis,/*fwd*/false);
  982. babystepsTodo[axis]++; //less to do next time
  983. CRITICAL_SECTION_END;
  984. }
  985. }
  986. }
  987. #endif //BABYSTEPPING
  988. FORCE_INLINE static void soft_pwm_core()
  989. {
  990. static uint8_t pwm_count = (1 << SOFT_PWM_SCALE);
  991. static uint8_t soft_pwm_0;
  992. #ifdef SLOW_PWM_HEATERS
  993. static unsigned char slow_pwm_count = 0;
  994. static unsigned char state_heater_0 = 0;
  995. static unsigned char state_timer_heater_0 = 0;
  996. #endif
  997. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  998. static unsigned char soft_pwm_1;
  999. #ifdef SLOW_PWM_HEATERS
  1000. static unsigned char state_heater_1 = 0;
  1001. static unsigned char state_timer_heater_1 = 0;
  1002. #endif
  1003. #endif
  1004. #if EXTRUDERS > 2
  1005. static unsigned char soft_pwm_2;
  1006. #ifdef SLOW_PWM_HEATERS
  1007. static unsigned char state_heater_2 = 0;
  1008. static unsigned char state_timer_heater_2 = 0;
  1009. #endif
  1010. #endif
  1011. #if HEATER_BED_PIN > -1
  1012. // @@DR static unsigned char soft_pwm_b;
  1013. #ifdef SLOW_PWM_HEATERS
  1014. static unsigned char state_heater_b = 0;
  1015. static unsigned char state_timer_heater_b = 0;
  1016. #endif
  1017. #endif
  1018. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1019. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1020. #endif
  1021. #ifndef SLOW_PWM_HEATERS
  1022. /*
  1023. * standard PWM modulation
  1024. */
  1025. if (pwm_count == 0)
  1026. {
  1027. soft_pwm_0 = soft_pwm[0];
  1028. if(soft_pwm_0 > 0)
  1029. {
  1030. WRITE(HEATER_0_PIN,1);
  1031. #ifdef HEATERS_PARALLEL
  1032. WRITE(HEATER_1_PIN,1);
  1033. #endif
  1034. } else WRITE(HEATER_0_PIN,0);
  1035. #if EXTRUDERS > 1
  1036. soft_pwm_1 = soft_pwm[1];
  1037. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1038. #endif
  1039. #if EXTRUDERS > 2
  1040. soft_pwm_2 = soft_pwm[2];
  1041. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1042. #endif
  1043. }
  1044. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1045. #if 0 // @@DR vypnuto pro hw pwm bedu
  1046. // tuhle prasarnu bude potreba poustet ve stanovenych intervalech, jinak nemam moc sanci zareagovat
  1047. // teoreticky by se tato cast uz vubec nemusela poustet
  1048. if ((pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1)) == 0)
  1049. {
  1050. soft_pwm_b = soft_pwm_bed >> (7 - HEATER_BED_SOFT_PWM_BITS);
  1051. # ifndef SYSTEM_TIMER_2
  1052. // tady budu krokovat pomalou frekvenci na automatu - tohle je rizeni spinani a rozepinani
  1053. // jako ridici frekvenci mam 2khz, jako vystupni frekvenci mam 30hz
  1054. // 2kHz jsou ovsem ve slysitelnem pasmu, mozna bude potreba jit s frekvenci nahoru (a tomu taky prizpusobit ostatni veci)
  1055. // Teoreticky bych mohl stahnout OCR0B citac na 6, cimz bych se dostal nekam ke 40khz a tady potom honit PWM rychleji nebo i pomaleji
  1056. // to nicemu nevadi. Soft PWM scale by se 20x zvetsilo (no dobre, 16x), cimz by se to posunulo k puvodnimu 30Hz PWM
  1057. //if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1058. # endif //SYSTEM_TIMER_2
  1059. }
  1060. #endif
  1061. #endif
  1062. #ifdef FAN_SOFT_PWM
  1063. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1064. {
  1065. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1066. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1067. }
  1068. #endif
  1069. if(soft_pwm_0 < pwm_count)
  1070. {
  1071. WRITE(HEATER_0_PIN,0);
  1072. #ifdef HEATERS_PARALLEL
  1073. WRITE(HEATER_1_PIN,0);
  1074. #endif
  1075. }
  1076. #if EXTRUDERS > 1
  1077. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1078. #endif
  1079. #if EXTRUDERS > 2
  1080. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1081. #endif
  1082. #if 0 // @@DR
  1083. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1084. if (soft_pwm_b < (pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1))){
  1085. //WRITE(HEATER_BED_PIN,0);
  1086. }
  1087. //WRITE(HEATER_BED_PIN, pwm_count & 1 );
  1088. #endif
  1089. #endif
  1090. #ifdef FAN_SOFT_PWM
  1091. if (soft_pwm_fan < (pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1))) WRITE(FAN_PIN,0);
  1092. #endif
  1093. pwm_count += (1 << SOFT_PWM_SCALE);
  1094. pwm_count &= 0x7f;
  1095. #else //ifndef SLOW_PWM_HEATERS
  1096. /*
  1097. * SLOW PWM HEATERS
  1098. *
  1099. * for heaters drived by relay
  1100. */
  1101. #ifndef MIN_STATE_TIME
  1102. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1103. #endif
  1104. if (slow_pwm_count == 0) {
  1105. // EXTRUDER 0
  1106. soft_pwm_0 = soft_pwm[0];
  1107. if (soft_pwm_0 > 0) {
  1108. // turn ON heather only if the minimum time is up
  1109. if (state_timer_heater_0 == 0) {
  1110. // if change state set timer
  1111. if (state_heater_0 == 0) {
  1112. state_timer_heater_0 = MIN_STATE_TIME;
  1113. }
  1114. state_heater_0 = 1;
  1115. WRITE(HEATER_0_PIN, 1);
  1116. #ifdef HEATERS_PARALLEL
  1117. WRITE(HEATER_1_PIN, 1);
  1118. #endif
  1119. }
  1120. } else {
  1121. // turn OFF heather only if the minimum time is up
  1122. if (state_timer_heater_0 == 0) {
  1123. // if change state set timer
  1124. if (state_heater_0 == 1) {
  1125. state_timer_heater_0 = MIN_STATE_TIME;
  1126. }
  1127. state_heater_0 = 0;
  1128. WRITE(HEATER_0_PIN, 0);
  1129. #ifdef HEATERS_PARALLEL
  1130. WRITE(HEATER_1_PIN, 0);
  1131. #endif
  1132. }
  1133. }
  1134. #if EXTRUDERS > 1
  1135. // EXTRUDER 1
  1136. soft_pwm_1 = soft_pwm[1];
  1137. if (soft_pwm_1 > 0) {
  1138. // turn ON heather only if the minimum time is up
  1139. if (state_timer_heater_1 == 0) {
  1140. // if change state set timer
  1141. if (state_heater_1 == 0) {
  1142. state_timer_heater_1 = MIN_STATE_TIME;
  1143. }
  1144. state_heater_1 = 1;
  1145. WRITE(HEATER_1_PIN, 1);
  1146. }
  1147. } else {
  1148. // turn OFF heather only if the minimum time is up
  1149. if (state_timer_heater_1 == 0) {
  1150. // if change state set timer
  1151. if (state_heater_1 == 1) {
  1152. state_timer_heater_1 = MIN_STATE_TIME;
  1153. }
  1154. state_heater_1 = 0;
  1155. WRITE(HEATER_1_PIN, 0);
  1156. }
  1157. }
  1158. #endif
  1159. #if EXTRUDERS > 2
  1160. // EXTRUDER 2
  1161. soft_pwm_2 = soft_pwm[2];
  1162. if (soft_pwm_2 > 0) {
  1163. // turn ON heather only if the minimum time is up
  1164. if (state_timer_heater_2 == 0) {
  1165. // if change state set timer
  1166. if (state_heater_2 == 0) {
  1167. state_timer_heater_2 = MIN_STATE_TIME;
  1168. }
  1169. state_heater_2 = 1;
  1170. WRITE(HEATER_2_PIN, 1);
  1171. }
  1172. } else {
  1173. // turn OFF heather only if the minimum time is up
  1174. if (state_timer_heater_2 == 0) {
  1175. // if change state set timer
  1176. if (state_heater_2 == 1) {
  1177. state_timer_heater_2 = MIN_STATE_TIME;
  1178. }
  1179. state_heater_2 = 0;
  1180. WRITE(HEATER_2_PIN, 0);
  1181. }
  1182. }
  1183. #endif
  1184. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1185. // BED
  1186. soft_pwm_b = soft_pwm_bed;
  1187. if (soft_pwm_b > 0) {
  1188. // turn ON heather only if the minimum time is up
  1189. if (state_timer_heater_b == 0) {
  1190. // if change state set timer
  1191. if (state_heater_b == 0) {
  1192. state_timer_heater_b = MIN_STATE_TIME;
  1193. }
  1194. state_heater_b = 1;
  1195. //WRITE(HEATER_BED_PIN, 1);
  1196. }
  1197. } else {
  1198. // turn OFF heather only if the minimum time is up
  1199. if (state_timer_heater_b == 0) {
  1200. // if change state set timer
  1201. if (state_heater_b == 1) {
  1202. state_timer_heater_b = MIN_STATE_TIME;
  1203. }
  1204. state_heater_b = 0;
  1205. WRITE(HEATER_BED_PIN, 0);
  1206. }
  1207. }
  1208. #endif
  1209. } // if (slow_pwm_count == 0)
  1210. // EXTRUDER 0
  1211. if (soft_pwm_0 < slow_pwm_count) {
  1212. // turn OFF heather only if the minimum time is up
  1213. if (state_timer_heater_0 == 0) {
  1214. // if change state set timer
  1215. if (state_heater_0 == 1) {
  1216. state_timer_heater_0 = MIN_STATE_TIME;
  1217. }
  1218. state_heater_0 = 0;
  1219. WRITE(HEATER_0_PIN, 0);
  1220. #ifdef HEATERS_PARALLEL
  1221. WRITE(HEATER_1_PIN, 0);
  1222. #endif
  1223. }
  1224. }
  1225. #if EXTRUDERS > 1
  1226. // EXTRUDER 1
  1227. if (soft_pwm_1 < slow_pwm_count) {
  1228. // turn OFF heather only if the minimum time is up
  1229. if (state_timer_heater_1 == 0) {
  1230. // if change state set timer
  1231. if (state_heater_1 == 1) {
  1232. state_timer_heater_1 = MIN_STATE_TIME;
  1233. }
  1234. state_heater_1 = 0;
  1235. WRITE(HEATER_1_PIN, 0);
  1236. }
  1237. }
  1238. #endif
  1239. #if EXTRUDERS > 2
  1240. // EXTRUDER 2
  1241. if (soft_pwm_2 < slow_pwm_count) {
  1242. // turn OFF heather only if the minimum time is up
  1243. if (state_timer_heater_2 == 0) {
  1244. // if change state set timer
  1245. if (state_heater_2 == 1) {
  1246. state_timer_heater_2 = MIN_STATE_TIME;
  1247. }
  1248. state_heater_2 = 0;
  1249. WRITE(HEATER_2_PIN, 0);
  1250. }
  1251. }
  1252. #endif
  1253. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1254. // BED
  1255. if (soft_pwm_b < slow_pwm_count) {
  1256. // turn OFF heather only if the minimum time is up
  1257. if (state_timer_heater_b == 0) {
  1258. // if change state set timer
  1259. if (state_heater_b == 1) {
  1260. state_timer_heater_b = MIN_STATE_TIME;
  1261. }
  1262. state_heater_b = 0;
  1263. WRITE(HEATER_BED_PIN, 0);
  1264. }
  1265. }
  1266. #endif
  1267. #ifdef FAN_SOFT_PWM
  1268. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1269. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1270. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1271. }
  1272. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1273. #endif
  1274. pwm_count += (1 << SOFT_PWM_SCALE);
  1275. pwm_count &= 0x7f;
  1276. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1277. if ((pwm_count % 64) == 0) {
  1278. slow_pwm_count++;
  1279. slow_pwm_count &= 0x7f;
  1280. // Extruder 0
  1281. if (state_timer_heater_0 > 0) {
  1282. state_timer_heater_0--;
  1283. }
  1284. #if EXTRUDERS > 1
  1285. // Extruder 1
  1286. if (state_timer_heater_1 > 0)
  1287. state_timer_heater_1--;
  1288. #endif
  1289. #if EXTRUDERS > 2
  1290. // Extruder 2
  1291. if (state_timer_heater_2 > 0)
  1292. state_timer_heater_2--;
  1293. #endif
  1294. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1295. // Bed
  1296. if (state_timer_heater_b > 0)
  1297. state_timer_heater_b--;
  1298. #endif
  1299. } //if ((pwm_count % 64) == 0) {
  1300. #endif //ifndef SLOW_PWM_HEATERS
  1301. }
  1302. FORCE_INLINE static void soft_pwm_isr()
  1303. {
  1304. lcd_buttons_update();
  1305. soft_pwm_core();
  1306. #ifdef BABYSTEPPING
  1307. applyBabysteps();
  1308. #endif //BABYSTEPPING
  1309. // Check if a stack overflow happened
  1310. if (!SdFatUtil::test_stack_integrity()) stack_error();
  1311. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1312. readFanTach();
  1313. #endif //(defined(TACH_0))
  1314. }
  1315. // Timer2 (originaly timer0) is shared with millies
  1316. #ifdef SYSTEM_TIMER_2
  1317. ISR(TIMER2_COMPB_vect)
  1318. #else //SYSTEM_TIMER_2
  1319. ISR(TIMER0_COMPB_vect)
  1320. #endif //SYSTEM_TIMER_2
  1321. {
  1322. DISABLE_SOFT_PWM_INTERRUPT();
  1323. sei();
  1324. soft_pwm_isr();
  1325. cli();
  1326. ENABLE_SOFT_PWM_INTERRUPT();
  1327. }
  1328. void check_max_temp()
  1329. {
  1330. //heater
  1331. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1332. if (current_temperature_raw[0] <= maxttemp_raw[0]) {
  1333. #else
  1334. if (current_temperature_raw[0] >= maxttemp_raw[0]) {
  1335. #endif
  1336. max_temp_error(0);
  1337. }
  1338. //bed
  1339. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1340. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1341. if (current_temperature_bed_raw <= bed_maxttemp_raw) {
  1342. #else
  1343. if (current_temperature_bed_raw >= bed_maxttemp_raw) {
  1344. #endif
  1345. bed_max_temp_error();
  1346. }
  1347. #endif
  1348. //ambient
  1349. #if defined(AMBIENT_MAXTEMP) && (TEMP_SENSOR_AMBIENT != 0)
  1350. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1351. if (current_temperature_raw_ambient <= ambient_maxttemp_raw) {
  1352. #else
  1353. if (current_temperature_raw_ambient >= ambient_maxttemp_raw) {
  1354. #endif
  1355. ambient_max_temp_error();
  1356. }
  1357. #endif
  1358. }
  1359. //! number of repeating the same state with consecutive step() calls
  1360. //! used to slow down text switching
  1361. struct alert_automaton_mintemp {
  1362. const char *m2;
  1363. alert_automaton_mintemp(const char *m2):m2(m2){}
  1364. private:
  1365. enum { ALERT_AUTOMATON_SPEED_DIV = 5 };
  1366. enum class States : uint8_t { Init = 0, TempAboveMintemp, ShowPleaseRestart, ShowMintemp };
  1367. States state = States::Init;
  1368. uint8_t repeat = ALERT_AUTOMATON_SPEED_DIV;
  1369. void substep(States next_state){
  1370. if( repeat == 0 ){
  1371. state = next_state; // advance to the next state
  1372. repeat = ALERT_AUTOMATON_SPEED_DIV; // and prepare repeating for it too
  1373. } else {
  1374. --repeat;
  1375. }
  1376. }
  1377. public:
  1378. //! brief state automaton step routine
  1379. //! @param current_temp current hotend/bed temperature (for computing simple hysteresis)
  1380. //! @param mintemp minimal temperature including hysteresis to check current_temp against
  1381. void step(float current_temp, float mintemp){
  1382. static const char m1[] PROGMEM = "Please restart";
  1383. switch(state){
  1384. case States::Init: // initial state - check hysteresis
  1385. if( current_temp > mintemp ){
  1386. state = States::TempAboveMintemp;
  1387. }
  1388. // otherwise keep the Err MINTEMP alert message on the display,
  1389. // i.e. do not transfer to state 1
  1390. break;
  1391. case States::TempAboveMintemp: // the temperature has risen above the hysteresis check
  1392. lcd_setalertstatuspgm(m2);
  1393. substep(States::ShowMintemp);
  1394. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1395. break;
  1396. case States::ShowPleaseRestart: // displaying "Please restart"
  1397. lcd_updatestatuspgm(m1);
  1398. substep(States::ShowMintemp);
  1399. last_alert_sent_to_lcd = LCDALERT_PLEASERESTART;
  1400. break;
  1401. case States::ShowMintemp: // displaying "MINTEMP fixed"
  1402. lcd_updatestatuspgm(m2);
  1403. substep(States::ShowPleaseRestart);
  1404. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1405. break;
  1406. }
  1407. }
  1408. };
  1409. static const char m2hotend[] PROGMEM = "MINTEMP HOTEND fixed";
  1410. static const char m2bed[] PROGMEM = "MINTEMP BED fixed";
  1411. static alert_automaton_mintemp alert_automaton_hotend(m2hotend), alert_automaton_bed(m2bed);
  1412. void check_min_temp_heater0()
  1413. {
  1414. //heater
  1415. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1416. if (current_temperature_raw[0] >= minttemp_raw[0]) {
  1417. #else
  1418. if (current_temperature_raw[0] <= minttemp_raw[0]) {
  1419. #endif
  1420. menu_set_serious_error(SERIOUS_ERR_MINTEMP_HEATER);
  1421. min_temp_error(0);
  1422. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_HEATER) ) {
  1423. // no recovery, just force the user to restart the printer
  1424. // which is a safer variant than just continuing printing
  1425. // The automaton also checks for hysteresis - the temperature must have reached a few degrees above the MINTEMP, before
  1426. // we shall signalize, that MINTEMP has been fixed
  1427. // Code notice: normally the alert_automaton instance would have been placed here
  1428. // as static alert_automaton_mintemp alert_automaton_hotend, but
  1429. // due to stupid compiler that takes 16 more bytes.
  1430. alert_automaton_hotend.step(current_temperature[0], minttemp[0] + TEMP_HYSTERESIS);
  1431. }
  1432. }
  1433. void check_min_temp_bed()
  1434. {
  1435. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1436. if (current_temperature_bed_raw >= bed_minttemp_raw) {
  1437. #else
  1438. if (current_temperature_bed_raw <= bed_minttemp_raw) {
  1439. #endif
  1440. menu_set_serious_error(SERIOUS_ERR_MINTEMP_BED);
  1441. bed_min_temp_error();
  1442. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_BED) ){
  1443. // no recovery, just force the user to restart the printer
  1444. // which is a safer variant than just continuing printing
  1445. alert_automaton_bed.step(current_temperature_bed, BED_MINTEMP + TEMP_HYSTERESIS);
  1446. }
  1447. }
  1448. #ifdef AMBIENT_MINTEMP
  1449. void check_min_temp_ambient()
  1450. {
  1451. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1452. if (current_temperature_raw_ambient >= ambient_minttemp_raw) {
  1453. #else
  1454. if (current_temperature_raw_ambient <= ambient_minttemp_raw) {
  1455. #endif
  1456. ambient_min_temp_error();
  1457. }
  1458. }
  1459. #endif
  1460. void check_min_temp()
  1461. {
  1462. static bool bCheckingOnHeater=false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over heaterMintemp)
  1463. static bool bCheckingOnBed=false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over bedMintemp)
  1464. #ifdef AMBIENT_THERMISTOR
  1465. #ifdef AMBIENT_MINTEMP
  1466. check_min_temp_ambient();
  1467. #endif
  1468. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1469. if(current_temperature_raw_ambient>(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW)) // thermistor is NTC type
  1470. #else
  1471. if(current_temperature_raw_ambient=<(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW))
  1472. #endif
  1473. { // ambient temperature is low
  1474. #endif //AMBIENT_THERMISTOR
  1475. // *** 'common' part of code for MK2.5 & MK3
  1476. // * nozzle checking
  1477. if(target_temperature[active_extruder]>minttemp[active_extruder])
  1478. { // ~ nozzle heating is on
  1479. bCheckingOnHeater=bCheckingOnHeater||(current_temperature[active_extruder]>(minttemp[active_extruder]+TEMP_HYSTERESIS)); // for eventually delay cutting
  1480. if(oTimer4minTempHeater.expired(HEATER_MINTEMP_DELAY)||(!oTimer4minTempHeater.running())||bCheckingOnHeater)
  1481. {
  1482. bCheckingOnHeater=true; // not necessary
  1483. check_min_temp_heater0(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  1484. }
  1485. }
  1486. else { // ~ nozzle heating is off
  1487. oTimer4minTempHeater.start();
  1488. bCheckingOnHeater=false;
  1489. }
  1490. // * bed checking
  1491. if(target_temperature_bed>BED_MINTEMP)
  1492. { // ~ bed heating is on
  1493. bCheckingOnBed=bCheckingOnBed||(current_temperature_bed>(BED_MINTEMP+TEMP_HYSTERESIS)); // for eventually delay cutting
  1494. if(oTimer4minTempBed.expired(BED_MINTEMP_DELAY)||(!oTimer4minTempBed.running())||bCheckingOnBed)
  1495. {
  1496. bCheckingOnBed=true; // not necessary
  1497. check_min_temp_bed(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  1498. }
  1499. }
  1500. else { // ~ bed heating is off
  1501. oTimer4minTempBed.start();
  1502. bCheckingOnBed=false;
  1503. }
  1504. // *** end of 'common' part
  1505. #ifdef AMBIENT_THERMISTOR
  1506. }
  1507. else { // ambient temperature is standard
  1508. check_min_temp_heater0();
  1509. check_min_temp_bed();
  1510. }
  1511. #endif //AMBIENT_THERMISTOR
  1512. }
  1513. #ifdef PIDTEMP
  1514. // Apply the scale factors to the PID values
  1515. float scalePID_i(float i)
  1516. {
  1517. return i*PID_dT;
  1518. }
  1519. float unscalePID_i(float i)
  1520. {
  1521. return i/PID_dT;
  1522. }
  1523. float scalePID_d(float d)
  1524. {
  1525. return d/PID_dT;
  1526. }
  1527. float unscalePID_d(float d)
  1528. {
  1529. return d*PID_dT;
  1530. }
  1531. #endif //PIDTEMP
  1532. #ifdef PINDA_THERMISTOR
  1533. //! @brief PINDA thermistor detected
  1534. //!
  1535. //! @retval true firmware should do temperature compensation and allow calibration
  1536. //! @retval false PINDA thermistor is not detected, disable temperature compensation and calibration
  1537. //! @retval true/false when forced via LCD menu Settings->HW Setup->SuperPINDA
  1538. //!
  1539. bool has_temperature_compensation()
  1540. {
  1541. #ifdef SUPERPINDA_SUPPORT
  1542. #ifdef PINDA_TEMP_COMP
  1543. uint8_t pinda_temp_compensation = eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION);
  1544. if (pinda_temp_compensation == EEPROM_EMPTY_VALUE) //Unkown PINDA temp compenstation, so check it.
  1545. {
  1546. #endif //PINDA_TEMP_COMP
  1547. return (current_temperature_pinda >= PINDA_MINTEMP) ? true : false;
  1548. #ifdef PINDA_TEMP_COMP
  1549. }
  1550. else if (pinda_temp_compensation == 0) return true; //Overwritten via LCD menu SuperPINDA [No]
  1551. else return false; //Overwritten via LCD menu SuperPINDA [YES]
  1552. #endif //PINDA_TEMP_COMP
  1553. #else
  1554. return true;
  1555. #endif
  1556. }
  1557. #endif //PINDA_THERMISTOR
  1558. #define TEMP_MGR_INTV 0.27 // seconds, ~3.7Hz
  1559. #define TIMER5_PRESCALE 256
  1560. #define TIMER5_OCRA_OVF (uint16_t)(TEMP_MGR_INTV / ((long double)TIMER5_PRESCALE / F_CPU))
  1561. #define TEMP_MGR_INTERRUPT_STATE() (TIMSK5 & (1<<OCIE5A))
  1562. #define ENABLE_TEMP_MGR_INTERRUPT() TIMSK5 |= (1<<OCIE5A)
  1563. #define DISABLE_TEMP_MGR_INTERRUPT() TIMSK5 &= ~(1<<OCIE5A)
  1564. // RAII helper class to run a code block with temp_mgr_isr disabled
  1565. class TempMgrGuard
  1566. {
  1567. bool temp_mgr_state;
  1568. public:
  1569. TempMgrGuard() {
  1570. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  1571. temp_mgr_state = TEMP_MGR_INTERRUPT_STATE();
  1572. }
  1573. }
  1574. ~TempMgrGuard() throw() {
  1575. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  1576. if(temp_mgr_state) ENABLE_TEMP_MGR_INTERRUPT();
  1577. }
  1578. }
  1579. };
  1580. void temp_mgr_init()
  1581. {
  1582. // initialize the ADC and start a conversion
  1583. adc_init();
  1584. adc_start_cycle();
  1585. // initialize timer5
  1586. CRITICAL_SECTION_START;
  1587. // CTC
  1588. TCCR5B &= ~(1<<WGM53);
  1589. TCCR5B |= (1<<WGM52);
  1590. TCCR5A &= ~(1<<WGM51);
  1591. TCCR5A &= ~(1<<WGM50);
  1592. // output mode = 00 (disconnected)
  1593. TCCR5A &= ~(3<<COM5A0);
  1594. TCCR5A &= ~(3<<COM5B0);
  1595. // x/256 prescaler
  1596. TCCR5B |= (1<<CS52);
  1597. TCCR5B &= ~(1<<CS51);
  1598. TCCR5B &= ~(1<<CS50);
  1599. // reset counter
  1600. TCNT5 = 0;
  1601. OCR5A = TIMER5_OCRA_OVF;
  1602. // clear pending interrupts, enable COMPA
  1603. TIFR5 |= (1<<OCF5A);
  1604. ENABLE_TEMP_MGR_INTERRUPT();
  1605. CRITICAL_SECTION_END;
  1606. }
  1607. static void pid_heater(uint8_t e, const float current, const int target)
  1608. {
  1609. float pid_input;
  1610. float pid_output;
  1611. #ifdef TEMP_RUNAWAY_EXTRUDER_HYSTERESIS
  1612. temp_runaway_check(e+1, target, current, (int)soft_pwm[e], false);
  1613. #endif
  1614. #ifdef PIDTEMP
  1615. pid_input = current;
  1616. #ifndef PID_OPENLOOP
  1617. if(target == 0) {
  1618. pid_output = 0;
  1619. pid_reset[e] = true;
  1620. } else {
  1621. pid_error[e] = target - pid_input;
  1622. if(pid_reset[e]) {
  1623. iState_sum[e] = 0.0;
  1624. dTerm[e] = 0.0; // 'dState_last[e]' initial setting is not necessary (see end of if-statement)
  1625. pid_reset[e] = false;
  1626. }
  1627. #ifndef PonM
  1628. pTerm[e] = cs.Kp * pid_error[e];
  1629. iState_sum[e] += pid_error[e];
  1630. iState_sum[e] = constrain(iState_sum[e], iState_sum_min[e], iState_sum_max[e]);
  1631. iTerm[e] = cs.Ki * iState_sum[e];
  1632. // PID_K1 defined in Configuration.h in the PID settings
  1633. #define K2 (1.0-PID_K1)
  1634. dTerm[e] = (cs.Kd * (pid_input - dState_last[e]))*K2 + (PID_K1 * dTerm[e]); // e.g. digital filtration of derivative term changes
  1635. 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)
  1636. if (pid_output > PID_MAX) {
  1637. if (pid_error[e] > 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1638. pid_output=PID_MAX;
  1639. } else if (pid_output < 0) {
  1640. if (pid_error[e] < 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1641. pid_output=0;
  1642. }
  1643. #else // PonM ("Proportional on Measurement" method)
  1644. iState_sum[e] += cs.Ki * pid_error[e];
  1645. iState_sum[e] -= cs.Kp * (pid_input - dState_last[e]);
  1646. iState_sum[e] = constrain(iState_sum[e], 0, PID_INTEGRAL_DRIVE_MAX);
  1647. dTerm[e] = cs.Kd * (pid_input - dState_last[e]);
  1648. 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)
  1649. pid_output = constrain(pid_output, 0, PID_MAX);
  1650. #endif // PonM
  1651. }
  1652. dState_last[e] = pid_input;
  1653. #else //PID_OPENLOOP
  1654. pid_output = constrain(target[e], 0, PID_MAX);
  1655. #endif //PID_OPENLOOP
  1656. #ifdef PID_DEBUG
  1657. SERIAL_ECHO_START;
  1658. SERIAL_ECHO(" PID_DEBUG ");
  1659. SERIAL_ECHO(e);
  1660. SERIAL_ECHO(": Input ");
  1661. SERIAL_ECHO(pid_input);
  1662. SERIAL_ECHO(" Output ");
  1663. SERIAL_ECHO(pid_output);
  1664. SERIAL_ECHO(" pTerm ");
  1665. SERIAL_ECHO(pTerm[e]);
  1666. SERIAL_ECHO(" iTerm ");
  1667. SERIAL_ECHO(iTerm[e]);
  1668. SERIAL_ECHO(" dTerm ");
  1669. SERIAL_ECHOLN(-dTerm[e]);
  1670. #endif //PID_DEBUG
  1671. #else /* PID off */
  1672. pid_output = 0;
  1673. if(current[e] < target[e]) {
  1674. pid_output = PID_MAX;
  1675. }
  1676. #endif
  1677. // Check if temperature is within the correct range
  1678. if((current < maxttemp[e]) && (target != 0))
  1679. soft_pwm[e] = (int)pid_output >> 1;
  1680. else
  1681. soft_pwm[e] = 0;
  1682. }
  1683. static void pid_bed(const float current, const int target)
  1684. {
  1685. float pid_input;
  1686. float pid_output;
  1687. #ifdef TEMP_RUNAWAY_BED_HYSTERESIS
  1688. temp_runaway_check(0, target, current, (int)soft_pwm_bed, true);
  1689. #endif
  1690. #ifndef PIDTEMPBED
  1691. if(_millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  1692. return;
  1693. previous_millis_bed_heater = _millis();
  1694. #endif
  1695. #if TEMP_SENSOR_BED != 0
  1696. #ifdef PIDTEMPBED
  1697. pid_input = current;
  1698. #ifndef PID_OPENLOOP
  1699. pid_error_bed = target - pid_input;
  1700. pTerm_bed = cs.bedKp * pid_error_bed;
  1701. temp_iState_bed += pid_error_bed;
  1702. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  1703. iTerm_bed = cs.bedKi * temp_iState_bed;
  1704. //PID_K1 defined in Configuration.h in the PID settings
  1705. #define K2 (1.0-PID_K1)
  1706. dTerm_bed= (cs.bedKd * (pid_input - temp_dState_bed))*K2 + (PID_K1 * dTerm_bed);
  1707. temp_dState_bed = pid_input;
  1708. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  1709. if (pid_output > MAX_BED_POWER) {
  1710. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1711. pid_output=MAX_BED_POWER;
  1712. } else if (pid_output < 0){
  1713. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1714. pid_output=0;
  1715. }
  1716. #else
  1717. pid_output = constrain(target, 0, MAX_BED_POWER);
  1718. #endif //PID_OPENLOOP
  1719. if(current < BED_MAXTEMP)
  1720. {
  1721. soft_pwm_bed = (int)pid_output >> 1;
  1722. timer02_set_pwm0(soft_pwm_bed << 1);
  1723. }
  1724. else
  1725. {
  1726. soft_pwm_bed = 0;
  1727. timer02_set_pwm0(soft_pwm_bed << 1);
  1728. }
  1729. #elif !defined(BED_LIMIT_SWITCHING)
  1730. // Check if temperature is within the correct range
  1731. if(current < BED_MAXTEMP)
  1732. {
  1733. if(current >= target)
  1734. {
  1735. soft_pwm_bed = 0;
  1736. timer02_set_pwm0(soft_pwm_bed << 1);
  1737. }
  1738. else
  1739. {
  1740. soft_pwm_bed = MAX_BED_POWER>>1;
  1741. timer02_set_pwm0(soft_pwm_bed << 1);
  1742. }
  1743. }
  1744. else
  1745. {
  1746. soft_pwm_bed = 0;
  1747. timer02_set_pwm0(soft_pwm_bed << 1);
  1748. WRITE(HEATER_BED_PIN,LOW);
  1749. }
  1750. #else //#ifdef BED_LIMIT_SWITCHING
  1751. // Check if temperature is within the correct band
  1752. if(current < BED_MAXTEMP)
  1753. {
  1754. if(current > target + BED_HYSTERESIS)
  1755. {
  1756. soft_pwm_bed = 0;
  1757. timer02_set_pwm0(soft_pwm_bed << 1);
  1758. }
  1759. else if(current <= target - BED_HYSTERESIS)
  1760. {
  1761. soft_pwm_bed = MAX_BED_POWER>>1;
  1762. timer02_set_pwm0(soft_pwm_bed << 1);
  1763. }
  1764. }
  1765. else
  1766. {
  1767. soft_pwm_bed = 0;
  1768. timer02_set_pwm0(soft_pwm_bed << 1);
  1769. WRITE(HEATER_BED_PIN,LOW);
  1770. }
  1771. #endif //BED_LIMIT_SWITCHING
  1772. if(target==0)
  1773. {
  1774. soft_pwm_bed = 0;
  1775. timer02_set_pwm0(soft_pwm_bed << 1);
  1776. }
  1777. #endif //TEMP_SENSOR_BED
  1778. }
  1779. // ISR-safe temperatures
  1780. static volatile bool adc_values_ready = false;
  1781. float current_temperature_isr[EXTRUDERS];
  1782. int target_temperature_isr[EXTRUDERS];
  1783. float current_temperature_bed_isr;
  1784. int target_temperature_bed_isr;
  1785. #ifdef PINDA_THERMISTOR
  1786. float current_temperature_pinda_isr;
  1787. #endif
  1788. #ifdef AMBIENT_THERMISTOR
  1789. float current_temperature_ambient_isr;
  1790. #endif
  1791. // ISR callback from adc when sampling finished
  1792. void adc_callback()
  1793. {
  1794. current_temperature_raw[0] = adc_values[ADC_PIN_IDX(TEMP_0_PIN)]; //heater
  1795. current_temperature_bed_raw = adc_values[ADC_PIN_IDX(TEMP_BED_PIN)];
  1796. #ifdef PINDA_THERMISTOR
  1797. current_temperature_raw_pinda = adc_values[ADC_PIN_IDX(TEMP_PINDA_PIN)];
  1798. #endif //PINDA_THERMISTOR
  1799. #ifdef AMBIENT_THERMISTOR
  1800. current_temperature_raw_ambient = adc_values[ADC_PIN_IDX(TEMP_AMBIENT_PIN)]; // 5->6
  1801. #endif //AMBIENT_THERMISTOR
  1802. #ifdef VOLT_PWR_PIN
  1803. current_voltage_raw_pwr = adc_values[ADC_PIN_IDX(VOLT_PWR_PIN)];
  1804. #endif
  1805. #ifdef VOLT_BED_PIN
  1806. current_voltage_raw_bed = adc_values[ADC_PIN_IDX(VOLT_BED_PIN)]; // 6->9
  1807. #endif
  1808. #ifdef IR_SENSOR_ANALOG
  1809. current_voltage_raw_IR = adc_values[ADC_PIN_IDX(VOLT_IR_PIN)];
  1810. #endif //IR_SENSOR_ANALOG
  1811. adc_values_ready = true;
  1812. }
  1813. static void setCurrentTemperaturesFromIsr()
  1814. {
  1815. for(uint8_t e=0;e<EXTRUDERS;e++)
  1816. current_temperature[e] = current_temperature_isr[e];
  1817. current_temperature_bed = current_temperature_bed_isr;
  1818. #ifdef PINDA_THERMISTOR
  1819. current_temperature_pinda = current_temperature_pinda_isr;
  1820. #endif
  1821. #ifdef AMBIENT_THERMISTOR
  1822. current_temperature_ambient = current_temperature_ambient_isr;
  1823. #endif
  1824. }
  1825. static void setIsrTargetTemperatures()
  1826. {
  1827. for(uint8_t e=0;e<EXTRUDERS;e++)
  1828. target_temperature_isr[e] = target_temperature[e];
  1829. target_temperature_bed_isr = target_temperature_bed;
  1830. }
  1831. /* Synchronize temperatures:
  1832. - fetch updated values from temp_mgr_isr to current values
  1833. - update target temperatures for temp_mgr_isr regulation
  1834. This function is blocking: check temp_meas_ready before calling! */
  1835. static void updateTemperatures()
  1836. {
  1837. TempMgrGuard temp_mgr_guard;
  1838. setCurrentTemperaturesFromIsr();
  1839. setIsrTargetTemperatures();
  1840. temp_meas_ready = false;
  1841. }
  1842. /* Convert raw values into actual temperatures for temp_mgr. The raw values are created in the ADC
  1843. interrupt context, while this function runs from the temp_mgr isr which is preemptible as
  1844. analog2temp is relatively slow */
  1845. static void setIsrTemperaturesFromRawValues()
  1846. {
  1847. for(uint8_t e=0;e<EXTRUDERS;e++)
  1848. current_temperature_isr[e] = analog2temp(current_temperature_raw[e], e);
  1849. current_temperature_bed_isr = analog2tempBed(current_temperature_bed_raw);
  1850. #ifdef PINDA_THERMISTOR
  1851. current_temperature_pinda_isr = analog2tempBed(current_temperature_raw_pinda);
  1852. #endif
  1853. #ifdef AMBIENT_THERMISTOR
  1854. current_temperature_ambient_isr = analog2tempAmbient(current_temperature_raw_ambient); //thermistor for ambient is NTCG104LH104JT1 (2000)
  1855. #endif
  1856. temp_meas_ready = true;
  1857. }
  1858. static void temp_mgr_pid()
  1859. {
  1860. for(uint8_t e = 0; e < EXTRUDERS; e++)
  1861. pid_heater(e, current_temperature_isr[e], target_temperature_isr[e]);
  1862. pid_bed(current_temperature_bed_isr, target_temperature_bed_isr);
  1863. }
  1864. static void temp_mgr_isr()
  1865. {
  1866. // update *_isr temperatures from raw values for PID regulation
  1867. setIsrTemperaturesFromRawValues();
  1868. // TODO: this is now running inside an isr and cannot directly manipulate the lcd,
  1869. // this needs to disable temperatures and flag the error to be shown in manage_heater!
  1870. check_max_temp();
  1871. check_min_temp();
  1872. // PID regulation
  1873. temp_mgr_pid();
  1874. }
  1875. ISR(TIMER5_COMPA_vect)
  1876. {
  1877. // immediately schedule a new conversion
  1878. if(adc_values_ready != true) return;
  1879. adc_start_cycle();
  1880. adc_values_ready = false;
  1881. // run temperature management with interrupts enabled to reduce latency
  1882. DISABLE_TEMP_MGR_INTERRUPT();
  1883. sei();
  1884. temp_mgr_isr();
  1885. cli();
  1886. ENABLE_TEMP_MGR_INTERRUPT();
  1887. }
  1888. void disable_heater()
  1889. {
  1890. setAllTargetHotends(0);
  1891. setTargetBed(0);
  1892. CRITICAL_SECTION_START;
  1893. // propagate all values down the chain
  1894. setIsrTargetTemperatures();
  1895. temp_mgr_pid();
  1896. // we can't call soft_pwm_core directly to toggle the pins as it would require removing the inline
  1897. // attribute, so disable each pin individually
  1898. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1 && EXTRUDERS > 0
  1899. WRITE(HEATER_0_PIN,LOW);
  1900. #endif
  1901. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1 && EXTRUDERS > 1
  1902. WRITE(HEATER_1_PIN,LOW);
  1903. #endif
  1904. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1 && EXTRUDERS > 2
  1905. WRITE(HEATER_2_PIN,LOW);
  1906. #endif
  1907. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1908. // TODO: this doesn't take immediate effect!
  1909. timer02_set_pwm0(0);
  1910. bedPWMDisabled = 0;
  1911. #endif
  1912. CRITICAL_SECTION_END;
  1913. }