temperature.cpp 56 KB

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