temperature.cpp 63 KB

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