temperature.cpp 61 KB

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