temperature.cpp 56 KB

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