temperature.cpp 59 KB

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