temperature.cpp 55 KB

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