temperature.cpp 59 KB

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