temperature.cpp 67 KB

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