temperature.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  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. if (millis() - temp_runaway_timer[_heater_id] > 2000)
  1064. {
  1065. #ifdef TEMP_RUNAWAY_BED_TIMEOUT
  1066. if (_isbed)
  1067. {
  1068. __hysteresis = TEMP_RUNAWAY_BED_HYSTERESIS;
  1069. __timeout = TEMP_RUNAWAY_BED_TIMEOUT;
  1070. }
  1071. #endif
  1072. #ifdef TEMP_RUNAWAY_EXTRUDER_TIMEOUT
  1073. if (!_isbed)
  1074. {
  1075. __hysteresis = TEMP_RUNAWAY_EXTRUDER_HYSTERESIS;
  1076. __timeout = TEMP_RUNAWAY_EXTRUDER_TIMEOUT;
  1077. }
  1078. #endif
  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 ((_current_temperature < _target_temperature) && (temp_runaway_status[_heater_id] == TempRunaway_PREHEAT))
  1101. {
  1102. __preheat_counter[_heater_id]++;
  1103. if (__preheat_counter[_heater_id] > ((_isbed) ? 16 : 8)) // periodicaly check if current temperature changes
  1104. {
  1105. /*SERIAL_ECHOPGM("Heater:");
  1106. MYSERIAL.print(_heater_id);
  1107. SERIAL_ECHOPGM(" T:");
  1108. MYSERIAL.print(_current_temperature);
  1109. SERIAL_ECHOPGM(" Tstart:");
  1110. MYSERIAL.print(__preheat_start[_heater_id]);*/
  1111. if (_current_temperature - __preheat_start[_heater_id] < 2) {
  1112. __preheat_errors[_heater_id]++;
  1113. /*SERIAL_ECHOPGM(" Preheat errors:");
  1114. MYSERIAL.println(__preheat_errors[_heater_id]);*/
  1115. }
  1116. else {
  1117. //SERIAL_ECHOLNPGM("");
  1118. __preheat_errors[_heater_id] = 0;
  1119. }
  1120. if (__preheat_errors[_heater_id] > ((_isbed) ? 2 : 5))
  1121. {
  1122. if (farm_mode) { prusa_statistics(0); }
  1123. temp_runaway_stop(true, _isbed);
  1124. if (farm_mode) { prusa_statistics(91); }
  1125. }
  1126. __preheat_start[_heater_id] = _current_temperature;
  1127. __preheat_counter[_heater_id] = 0;
  1128. }
  1129. }
  1130. if (_current_temperature >= _target_temperature && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  1131. {
  1132. temp_runaway_status[_heater_id] = TempRunaway_ACTIVE;
  1133. temp_runaway_check_active = false;
  1134. }
  1135. if (_output > 0)
  1136. {
  1137. temp_runaway_check_active = true;
  1138. }
  1139. if (temp_runaway_check_active)
  1140. {
  1141. // we are in range
  1142. if ((_current_temperature > (_target_temperature - __hysteresis)) && (_current_temperature < (_target_temperature + __hysteresis)))
  1143. {
  1144. temp_runaway_check_active = false;
  1145. temp_runaway_error_counter[_heater_id] = 0;
  1146. }
  1147. else
  1148. {
  1149. if (temp_runaway_status[_heater_id] > TempRunaway_PREHEAT)
  1150. {
  1151. temp_runaway_error_counter[_heater_id]++;
  1152. if (temp_runaway_error_counter[_heater_id] * 2 > __timeout)
  1153. {
  1154. if (farm_mode) { prusa_statistics(0); }
  1155. temp_runaway_stop(false, _isbed);
  1156. if (farm_mode) { prusa_statistics(90); }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. void temp_runaway_stop(bool isPreheat, bool isBed)
  1164. {
  1165. cancel_heatup = true;
  1166. quickStop();
  1167. if (card.sdprinting)
  1168. {
  1169. card.sdprinting = false;
  1170. card.closefile();
  1171. }
  1172. // Clean the input command queue
  1173. // This is necessary, because in command queue there can be commands which would later set heater or bed temperature.
  1174. cmdqueue_reset();
  1175. disable_heater();
  1176. disable_x();
  1177. disable_y();
  1178. disable_e0();
  1179. disable_e1();
  1180. disable_e2();
  1181. manage_heater();
  1182. lcd_update(0);
  1183. if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE)||(eSoundMode==e_SOUND_MODE_SILENT))
  1184. WRITE(BEEPER, HIGH);
  1185. delayMicroseconds(500);
  1186. WRITE(BEEPER, LOW);
  1187. delayMicroseconds(100);
  1188. if (isPreheat)
  1189. {
  1190. Stop();
  1191. isBed ? LCD_ALERTMESSAGEPGM("BED PREHEAT ERROR") : LCD_ALERTMESSAGEPGM("PREHEAT ERROR");
  1192. SERIAL_ERROR_START;
  1193. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HOTEND)");
  1194. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1195. SET_OUTPUT(FAN_PIN);
  1196. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1197. analogWrite(FAN_PIN, 255);
  1198. fanSpeed = 255;
  1199. delayMicroseconds(2000);
  1200. }
  1201. else
  1202. {
  1203. isBed ? LCD_ALERTMESSAGEPGM("BED THERMAL RUNAWAY") : LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1204. SERIAL_ERROR_START;
  1205. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  1206. }
  1207. }
  1208. #endif
  1209. void disable_heater()
  1210. {
  1211. setAllTargetHotends(0);
  1212. setTargetBed(0);
  1213. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1214. target_temperature[0]=0;
  1215. soft_pwm[0]=0;
  1216. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1217. WRITE(HEATER_0_PIN,LOW);
  1218. #endif
  1219. #endif
  1220. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1221. target_temperature[1]=0;
  1222. soft_pwm[1]=0;
  1223. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1224. WRITE(HEATER_1_PIN,LOW);
  1225. #endif
  1226. #endif
  1227. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1228. target_temperature[2]=0;
  1229. soft_pwm[2]=0;
  1230. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1231. WRITE(HEATER_2_PIN,LOW);
  1232. #endif
  1233. #endif
  1234. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1235. target_temperature_bed=0;
  1236. soft_pwm_bed=0;
  1237. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1238. WRITE(HEATER_BED_PIN,LOW);
  1239. #endif
  1240. #endif
  1241. }
  1242. void max_temp_error(uint8_t e) {
  1243. disable_heater();
  1244. if(IsStopped() == false) {
  1245. SERIAL_ERROR_START;
  1246. SERIAL_ERRORLN((int)e);
  1247. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1248. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1249. }
  1250. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1251. Stop();
  1252. #endif
  1253. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1254. SET_OUTPUT(FAN_PIN);
  1255. SET_OUTPUT(BEEPER);
  1256. WRITE(FAN_PIN, 1);
  1257. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1258. if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE)||(eSoundMode==e_SOUND_MODE_SILENT))
  1259. WRITE(BEEPER, 1);
  1260. // fanSpeed will consumed by the check_axes_activity() routine.
  1261. fanSpeed=255;
  1262. if (farm_mode) { prusa_statistics(93); }
  1263. }
  1264. void min_temp_error(uint8_t e) {
  1265. #ifdef DEBUG_DISABLE_MINTEMP
  1266. return;
  1267. #endif
  1268. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  1269. disable_heater();
  1270. if(IsStopped() == false) {
  1271. SERIAL_ERROR_START;
  1272. SERIAL_ERRORLN((int)e);
  1273. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1274. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1275. }
  1276. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1277. Stop();
  1278. #endif
  1279. if (farm_mode) { prusa_statistics(92); }
  1280. }
  1281. void bed_max_temp_error(void) {
  1282. #if HEATER_BED_PIN > -1
  1283. WRITE(HEATER_BED_PIN, 0);
  1284. #endif
  1285. if(IsStopped() == false) {
  1286. SERIAL_ERROR_START;
  1287. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !");
  1288. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1289. }
  1290. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1291. Stop();
  1292. #endif
  1293. }
  1294. void bed_min_temp_error(void) {
  1295. #ifdef DEBUG_DISABLE_MINTEMP
  1296. return;
  1297. #endif
  1298. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  1299. #if HEATER_BED_PIN > -1
  1300. WRITE(HEATER_BED_PIN, 0);
  1301. #endif
  1302. if(IsStopped() == false) {
  1303. SERIAL_ERROR_START;
  1304. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MINTEMP triggered !");
  1305. LCD_ALERTMESSAGEPGM("Err: MINTEMP BED");
  1306. }
  1307. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1308. Stop();
  1309. #endif
  1310. }
  1311. #ifdef HEATER_0_USES_MAX6675
  1312. #define MAX6675_HEAT_INTERVAL 250
  1313. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1314. int max6675_temp = 2000;
  1315. int read_max6675()
  1316. {
  1317. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1318. return max6675_temp;
  1319. max6675_previous_millis = millis();
  1320. max6675_temp = 0;
  1321. #ifdef PRR
  1322. PRR &= ~(1<<PRSPI);
  1323. #elif defined PRR0
  1324. PRR0 &= ~(1<<PRSPI);
  1325. #endif
  1326. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1327. // enable TT_MAX6675
  1328. WRITE(MAX6675_SS, 0);
  1329. // ensure 100ns delay - a bit extra is fine
  1330. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1331. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1332. // read MSB
  1333. SPDR = 0;
  1334. for (;(SPSR & (1<<SPIF)) == 0;);
  1335. max6675_temp = SPDR;
  1336. max6675_temp <<= 8;
  1337. // read LSB
  1338. SPDR = 0;
  1339. for (;(SPSR & (1<<SPIF)) == 0;);
  1340. max6675_temp |= SPDR;
  1341. // disable TT_MAX6675
  1342. WRITE(MAX6675_SS, 1);
  1343. if (max6675_temp & 4)
  1344. {
  1345. // thermocouple open
  1346. max6675_temp = 2000;
  1347. }
  1348. else
  1349. {
  1350. max6675_temp = max6675_temp >> 3;
  1351. }
  1352. return max6675_temp;
  1353. }
  1354. #endif
  1355. extern "C" {
  1356. void adc_ready(void) //callback from adc when sampling finished
  1357. {
  1358. current_temperature_raw[0] = adc_values[ADC_PIN_IDX(TEMP_0_PIN)]; //heater
  1359. current_temperature_raw_pinda = adc_values[ADC_PIN_IDX(TEMP_PINDA_PIN)];
  1360. current_temperature_bed_raw = adc_values[ADC_PIN_IDX(TEMP_BED_PIN)];
  1361. #ifdef VOLT_PWR_PIN
  1362. current_voltage_raw_pwr = adc_values[ADC_PIN_IDX(VOLT_PWR_PIN)];
  1363. #endif
  1364. #ifdef AMBIENT_THERMISTOR
  1365. current_temperature_raw_ambient = adc_values[ADC_PIN_IDX(TEMP_AMBIENT_PIN)];
  1366. #endif //AMBIENT_THERMISTOR
  1367. #ifdef VOLT_BED_PIN
  1368. current_voltage_raw_bed = adc_values[ADC_PIN_IDX(VOLT_BED_PIN)]; // 6->9
  1369. #endif
  1370. temp_meas_ready = true;
  1371. }
  1372. } // extern "C"
  1373. // Timer 0 is shared with millies
  1374. ISR(TIMER0_COMPB_vect)
  1375. {
  1376. static bool _lock = false;
  1377. if (_lock) return;
  1378. _lock = true;
  1379. asm("sei");
  1380. if (!temp_meas_ready) adc_cycle();
  1381. else
  1382. {
  1383. check_max_temp();
  1384. check_min_temp();
  1385. }
  1386. lcd_buttons_update();
  1387. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1388. static unsigned char soft_pwm_0;
  1389. #ifdef SLOW_PWM_HEATERS
  1390. static unsigned char slow_pwm_count = 0;
  1391. static unsigned char state_heater_0 = 0;
  1392. static unsigned char state_timer_heater_0 = 0;
  1393. #endif
  1394. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1395. static unsigned char soft_pwm_1;
  1396. #ifdef SLOW_PWM_HEATERS
  1397. static unsigned char state_heater_1 = 0;
  1398. static unsigned char state_timer_heater_1 = 0;
  1399. #endif
  1400. #endif
  1401. #if EXTRUDERS > 2
  1402. static unsigned char soft_pwm_2;
  1403. #ifdef SLOW_PWM_HEATERS
  1404. static unsigned char state_heater_2 = 0;
  1405. static unsigned char state_timer_heater_2 = 0;
  1406. #endif
  1407. #endif
  1408. #if HEATER_BED_PIN > -1
  1409. static unsigned char soft_pwm_b;
  1410. #ifdef SLOW_PWM_HEATERS
  1411. static unsigned char state_heater_b = 0;
  1412. static unsigned char state_timer_heater_b = 0;
  1413. #endif
  1414. #endif
  1415. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1416. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1417. #endif
  1418. #ifndef SLOW_PWM_HEATERS
  1419. /*
  1420. * standard PWM modulation
  1421. */
  1422. if (pwm_count == 0)
  1423. {
  1424. soft_pwm_0 = soft_pwm[0];
  1425. if(soft_pwm_0 > 0)
  1426. {
  1427. WRITE(HEATER_0_PIN,1);
  1428. #ifdef HEATERS_PARALLEL
  1429. WRITE(HEATER_1_PIN,1);
  1430. #endif
  1431. } else WRITE(HEATER_0_PIN,0);
  1432. #if EXTRUDERS > 1
  1433. soft_pwm_1 = soft_pwm[1];
  1434. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1435. #endif
  1436. #if EXTRUDERS > 2
  1437. soft_pwm_2 = soft_pwm[2];
  1438. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1439. #endif
  1440. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1441. soft_pwm_b = soft_pwm_bed;
  1442. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1443. #endif
  1444. #ifdef FAN_SOFT_PWM
  1445. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1446. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1447. #endif
  1448. }
  1449. if(soft_pwm_0 < pwm_count)
  1450. {
  1451. WRITE(HEATER_0_PIN,0);
  1452. #ifdef HEATERS_PARALLEL
  1453. WRITE(HEATER_1_PIN,0);
  1454. #endif
  1455. }
  1456. #if EXTRUDERS > 1
  1457. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1458. #endif
  1459. #if EXTRUDERS > 2
  1460. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1461. #endif
  1462. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1463. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1464. #endif
  1465. #ifdef FAN_SOFT_PWM
  1466. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1467. #endif
  1468. pwm_count += (1 << SOFT_PWM_SCALE);
  1469. pwm_count &= 0x7f;
  1470. #else //ifndef SLOW_PWM_HEATERS
  1471. /*
  1472. * SLOW PWM HEATERS
  1473. *
  1474. * for heaters drived by relay
  1475. */
  1476. #ifndef MIN_STATE_TIME
  1477. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1478. #endif
  1479. if (slow_pwm_count == 0) {
  1480. // EXTRUDER 0
  1481. soft_pwm_0 = soft_pwm[0];
  1482. if (soft_pwm_0 > 0) {
  1483. // turn ON heather only if the minimum time is up
  1484. if (state_timer_heater_0 == 0) {
  1485. // if change state set timer
  1486. if (state_heater_0 == 0) {
  1487. state_timer_heater_0 = MIN_STATE_TIME;
  1488. }
  1489. state_heater_0 = 1;
  1490. WRITE(HEATER_0_PIN, 1);
  1491. #ifdef HEATERS_PARALLEL
  1492. WRITE(HEATER_1_PIN, 1);
  1493. #endif
  1494. }
  1495. } else {
  1496. // turn OFF heather only if the minimum time is up
  1497. if (state_timer_heater_0 == 0) {
  1498. // if change state set timer
  1499. if (state_heater_0 == 1) {
  1500. state_timer_heater_0 = MIN_STATE_TIME;
  1501. }
  1502. state_heater_0 = 0;
  1503. WRITE(HEATER_0_PIN, 0);
  1504. #ifdef HEATERS_PARALLEL
  1505. WRITE(HEATER_1_PIN, 0);
  1506. #endif
  1507. }
  1508. }
  1509. #if EXTRUDERS > 1
  1510. // EXTRUDER 1
  1511. soft_pwm_1 = soft_pwm[1];
  1512. if (soft_pwm_1 > 0) {
  1513. // turn ON heather only if the minimum time is up
  1514. if (state_timer_heater_1 == 0) {
  1515. // if change state set timer
  1516. if (state_heater_1 == 0) {
  1517. state_timer_heater_1 = MIN_STATE_TIME;
  1518. }
  1519. state_heater_1 = 1;
  1520. WRITE(HEATER_1_PIN, 1);
  1521. }
  1522. } else {
  1523. // turn OFF heather only if the minimum time is up
  1524. if (state_timer_heater_1 == 0) {
  1525. // if change state set timer
  1526. if (state_heater_1 == 1) {
  1527. state_timer_heater_1 = MIN_STATE_TIME;
  1528. }
  1529. state_heater_1 = 0;
  1530. WRITE(HEATER_1_PIN, 0);
  1531. }
  1532. }
  1533. #endif
  1534. #if EXTRUDERS > 2
  1535. // EXTRUDER 2
  1536. soft_pwm_2 = soft_pwm[2];
  1537. if (soft_pwm_2 > 0) {
  1538. // turn ON heather only if the minimum time is up
  1539. if (state_timer_heater_2 == 0) {
  1540. // if change state set timer
  1541. if (state_heater_2 == 0) {
  1542. state_timer_heater_2 = MIN_STATE_TIME;
  1543. }
  1544. state_heater_2 = 1;
  1545. WRITE(HEATER_2_PIN, 1);
  1546. }
  1547. } else {
  1548. // turn OFF heather only if the minimum time is up
  1549. if (state_timer_heater_2 == 0) {
  1550. // if change state set timer
  1551. if (state_heater_2 == 1) {
  1552. state_timer_heater_2 = MIN_STATE_TIME;
  1553. }
  1554. state_heater_2 = 0;
  1555. WRITE(HEATER_2_PIN, 0);
  1556. }
  1557. }
  1558. #endif
  1559. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1560. // BED
  1561. soft_pwm_b = soft_pwm_bed;
  1562. if (soft_pwm_b > 0) {
  1563. // turn ON heather only if the minimum time is up
  1564. if (state_timer_heater_b == 0) {
  1565. // if change state set timer
  1566. if (state_heater_b == 0) {
  1567. state_timer_heater_b = MIN_STATE_TIME;
  1568. }
  1569. state_heater_b = 1;
  1570. WRITE(HEATER_BED_PIN, 1);
  1571. }
  1572. } else {
  1573. // turn OFF heather only if the minimum time is up
  1574. if (state_timer_heater_b == 0) {
  1575. // if change state set timer
  1576. if (state_heater_b == 1) {
  1577. state_timer_heater_b = MIN_STATE_TIME;
  1578. }
  1579. state_heater_b = 0;
  1580. WRITE(HEATER_BED_PIN, 0);
  1581. }
  1582. }
  1583. #endif
  1584. } // if (slow_pwm_count == 0)
  1585. // EXTRUDER 0
  1586. if (soft_pwm_0 < slow_pwm_count) {
  1587. // turn OFF heather only if the minimum time is up
  1588. if (state_timer_heater_0 == 0) {
  1589. // if change state set timer
  1590. if (state_heater_0 == 1) {
  1591. state_timer_heater_0 = MIN_STATE_TIME;
  1592. }
  1593. state_heater_0 = 0;
  1594. WRITE(HEATER_0_PIN, 0);
  1595. #ifdef HEATERS_PARALLEL
  1596. WRITE(HEATER_1_PIN, 0);
  1597. #endif
  1598. }
  1599. }
  1600. #if EXTRUDERS > 1
  1601. // EXTRUDER 1
  1602. if (soft_pwm_1 < slow_pwm_count) {
  1603. // turn OFF heather only if the minimum time is up
  1604. if (state_timer_heater_1 == 0) {
  1605. // if change state set timer
  1606. if (state_heater_1 == 1) {
  1607. state_timer_heater_1 = MIN_STATE_TIME;
  1608. }
  1609. state_heater_1 = 0;
  1610. WRITE(HEATER_1_PIN, 0);
  1611. }
  1612. }
  1613. #endif
  1614. #if EXTRUDERS > 2
  1615. // EXTRUDER 2
  1616. if (soft_pwm_2 < slow_pwm_count) {
  1617. // turn OFF heather only if the minimum time is up
  1618. if (state_timer_heater_2 == 0) {
  1619. // if change state set timer
  1620. if (state_heater_2 == 1) {
  1621. state_timer_heater_2 = MIN_STATE_TIME;
  1622. }
  1623. state_heater_2 = 0;
  1624. WRITE(HEATER_2_PIN, 0);
  1625. }
  1626. }
  1627. #endif
  1628. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1629. // BED
  1630. if (soft_pwm_b < slow_pwm_count) {
  1631. // turn OFF heather only if the minimum time is up
  1632. if (state_timer_heater_b == 0) {
  1633. // if change state set timer
  1634. if (state_heater_b == 1) {
  1635. state_timer_heater_b = MIN_STATE_TIME;
  1636. }
  1637. state_heater_b = 0;
  1638. WRITE(HEATER_BED_PIN, 0);
  1639. }
  1640. }
  1641. #endif
  1642. #ifdef FAN_SOFT_PWM
  1643. if (pwm_count == 0){
  1644. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1645. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1646. }
  1647. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1648. #endif
  1649. pwm_count += (1 << SOFT_PWM_SCALE);
  1650. pwm_count &= 0x7f;
  1651. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1652. if ((pwm_count % 64) == 0) {
  1653. slow_pwm_count++;
  1654. slow_pwm_count &= 0x7f;
  1655. // Extruder 0
  1656. if (state_timer_heater_0 > 0) {
  1657. state_timer_heater_0--;
  1658. }
  1659. #if EXTRUDERS > 1
  1660. // Extruder 1
  1661. if (state_timer_heater_1 > 0)
  1662. state_timer_heater_1--;
  1663. #endif
  1664. #if EXTRUDERS > 2
  1665. // Extruder 2
  1666. if (state_timer_heater_2 > 0)
  1667. state_timer_heater_2--;
  1668. #endif
  1669. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1670. // Bed
  1671. if (state_timer_heater_b > 0)
  1672. state_timer_heater_b--;
  1673. #endif
  1674. } //if ((pwm_count % 64) == 0) {
  1675. #endif //ifndef SLOW_PWM_HEATERS
  1676. #ifdef BABYSTEPPING
  1677. for(uint8_t axis=0;axis<3;axis++)
  1678. {
  1679. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1680. if(curTodo>0)
  1681. {
  1682. asm("cli");
  1683. babystep(axis,/*fwd*/true);
  1684. babystepsTodo[axis]--; //less to do next time
  1685. asm("sei");
  1686. }
  1687. else
  1688. if(curTodo<0)
  1689. {
  1690. asm("cli");
  1691. babystep(axis,/*fwd*/false);
  1692. babystepsTodo[axis]++; //less to do next time
  1693. asm("sei");
  1694. }
  1695. }
  1696. #endif //BABYSTEPPING
  1697. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1698. check_fans();
  1699. #endif //(defined(TACH_0))
  1700. _lock = false;
  1701. }
  1702. void check_max_temp()
  1703. {
  1704. //heater
  1705. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1706. if (current_temperature_raw[0] <= maxttemp_raw[0]) {
  1707. #else
  1708. if (current_temperature_raw[0] >= maxttemp_raw[0]) {
  1709. #endif
  1710. max_temp_error(0);
  1711. }
  1712. //bed
  1713. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1714. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1715. if (current_temperature_bed_raw <= bed_maxttemp_raw) {
  1716. #else
  1717. if (current_temperature_bed_raw >= bed_maxttemp_raw) {
  1718. #endif
  1719. target_temperature_bed = 0;
  1720. bed_max_temp_error();
  1721. }
  1722. #endif
  1723. }
  1724. void check_min_temp_heater0()
  1725. {
  1726. //heater
  1727. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1728. if (current_temperature_raw[0] >= minttemp_raw[0]) {
  1729. #else
  1730. if (current_temperature_raw[0] <= minttemp_raw[0]) {
  1731. #endif
  1732. min_temp_error(0);
  1733. }
  1734. }
  1735. void check_min_temp_bed()
  1736. {
  1737. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1738. if (current_temperature_bed_raw >= bed_minttemp_raw) {
  1739. #else
  1740. if (current_temperature_bed_raw <= bed_minttemp_raw) {
  1741. #endif
  1742. bed_min_temp_error();
  1743. }
  1744. }
  1745. void check_min_temp()
  1746. {
  1747. #ifdef AMBIENT_THERMISTOR
  1748. static uint8_t heat_cycles = 0;
  1749. if (current_temperature_raw_ambient > OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW)
  1750. {
  1751. if (READ(HEATER_0_PIN) == HIGH)
  1752. {
  1753. // if ((heat_cycles % 10) == 0)
  1754. // printf_P(PSTR("X%d\n"), heat_cycles);
  1755. if (heat_cycles > 50) //reaction time 5-10s
  1756. check_min_temp_heater0();
  1757. else
  1758. heat_cycles++;
  1759. }
  1760. else
  1761. heat_cycles = 0;
  1762. return;
  1763. }
  1764. #endif //AMBIENT_THERMISTOR
  1765. check_min_temp_heater0();
  1766. check_min_temp_bed();
  1767. }
  1768. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1769. void check_fans() {
  1770. if (READ(TACH_0) != fan_state[0]) {
  1771. fan_edge_counter[0] ++;
  1772. fan_state[0] = !fan_state[0];
  1773. }
  1774. //if (READ(TACH_1) != fan_state[1]) {
  1775. // fan_edge_counter[1] ++;
  1776. // fan_state[1] = !fan_state[1];
  1777. //}
  1778. }
  1779. #endif //TACH_0
  1780. #ifdef PIDTEMP
  1781. // Apply the scale factors to the PID values
  1782. float scalePID_i(float i)
  1783. {
  1784. return i*PID_dT;
  1785. }
  1786. float unscalePID_i(float i)
  1787. {
  1788. return i/PID_dT;
  1789. }
  1790. float scalePID_d(float d)
  1791. {
  1792. return d/PID_dT;
  1793. }
  1794. float unscalePID_d(float d)
  1795. {
  1796. return d*PID_dT;
  1797. }
  1798. #endif //PIDTEMP