My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 90KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  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 "planner.h"
  26. #include "stepper.h"
  27. #include "temperature.h"
  28. #include "motion_control.h"
  29. #include "cardreader.h"
  30. #include "watchdog.h"
  31. #include "ConfigurationStore.h"
  32. #include "language.h"
  33. #include "pins_arduino.h"
  34. #include "BlinkM.h"
  35. #include "Wire.h"
  36. #if NUM_SERVOS > 0
  37. #include "Servo.h"
  38. #endif
  39. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  40. #include <SPI.h>
  41. #endif
  42. #define VERSION_STRING "1.0.0"
  43. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  44. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  45. //Implemented Codes
  46. //-------------------
  47. // G0 -> G1
  48. // G1 - Coordinated Movement X Y Z E
  49. // G2 - CW ARC
  50. // G3 - CCW ARC
  51. // G4 - Dwell S<seconds> or P<milliseconds>
  52. // G10 - retract filament according to settings of M207
  53. // G11 - retract recover filament according to settings of M208
  54. // G28 - Home all Axis
  55. // G90 - Use Absolute Coordinates
  56. // G91 - Use Relative Coordinates
  57. // G92 - Set current position to cordinates given
  58. // M Codes
  59. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  60. // M1 - Same as M0
  61. // M17 - Enable/Power all stepper motors
  62. // M18 - Disable all stepper motors; same as M84
  63. // M20 - List SD card
  64. // M21 - Init SD card
  65. // M22 - Release SD card
  66. // M23 - Select SD file (M23 filename.g)
  67. // M24 - Start/resume SD print
  68. // M25 - Pause SD print
  69. // M26 - Set SD position in bytes (M26 S12345)
  70. // M27 - Report SD print status
  71. // M28 - Start SD write (M28 filename.g)
  72. // M29 - Stop SD write
  73. // M30 - Delete file from SD (M30 filename.g)
  74. // M31 - Output time since last M109 or SD card start to serial
  75. // M32 - Select file and start SD print (Can be used when printing from SD card)
  76. // M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used.
  77. // M80 - Turn on Power Supply
  78. // M81 - Turn off Power Supply
  79. // M82 - Set E codes absolute (default)
  80. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  81. // M84 - Disable steppers until next move,
  82. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  83. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  84. // M92 - Set axis_steps_per_unit - same syntax as G92
  85. // M104 - Set extruder target temp
  86. // M105 - Read current temp
  87. // M106 - Fan on
  88. // M107 - Fan off
  89. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  90. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  91. // M114 - Output current position to serial port
  92. // M115 - Capabilities string
  93. // M117 - display message
  94. // M119 - Output Endstop status to serial port
  95. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  96. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  97. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  98. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  99. // M140 - Set bed target temp
  100. // M150 - Set BlinkM Colour Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work.
  101. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  102. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  103. // M200 - Set filament diameter
  104. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  105. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  106. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  107. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  108. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  109. // M206 - set additional homeing offset
  110. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  111. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  112. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  113. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  114. // M220 S<factor in percent>- set speed factor override percentage
  115. // M221 S<factor in percent>- set extrude factor override percentage
  116. // M240 - Trigger a camera to take a photograph
  117. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  118. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  119. // M300 - Play beepsound S<frequency Hz> P<duration ms>
  120. // M301 - Set PID parameters P I and D
  121. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  122. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  123. // M304 - Set bed PID parameters P I and D
  124. // M400 - Finish all moves
  125. // M500 - stores paramters in EEPROM
  126. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  127. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  128. // M503 - print the current settings (from memory not from eeprom)
  129. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  130. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  131. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  132. // M907 - Set digital trimpot motor current using axis codes.
  133. // M908 - Control digital trimpot directly.
  134. // M350 - Set microstepping mode.
  135. // M351 - Toggle MS1 MS2 pins directly.
  136. // M928 - Start SD logging (M928 filename.g) - ended by M29
  137. // M999 - Restart after being stopped by error
  138. //Stepper Movement Variables
  139. //===========================================================================
  140. //=============================imported variables============================
  141. //===========================================================================
  142. //===========================================================================
  143. //=============================public variables=============================
  144. //===========================================================================
  145. #ifdef SDSUPPORT
  146. CardReader card;
  147. #endif
  148. float homing_feedrate[] = HOMING_FEEDRATE;
  149. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  150. int feedmultiply=100; //100->1 200->2
  151. int saved_feedmultiply;
  152. int extrudemultiply=100; //100->1 200->2
  153. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  154. float add_homeing[3]={0,0,0};
  155. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  156. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  157. // Extruder offset
  158. #if EXTRUDERS > 1
  159. #ifndef DUAL_X_CARRIAGE
  160. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  161. #else
  162. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  163. #endif
  164. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  165. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  166. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  167. #endif
  168. };
  169. #endif
  170. uint8_t active_extruder = 0;
  171. int fanSpeed=0;
  172. #ifdef SERVO_ENDSTOPS
  173. int servo_endstops[] = SERVO_ENDSTOPS;
  174. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  175. #endif
  176. #ifdef BARICUDA
  177. int ValvePressure=0;
  178. int EtoPPressure=0;
  179. #endif
  180. #ifdef FWRETRACT
  181. bool autoretract_enabled=true;
  182. bool retracted=false;
  183. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  184. float retract_recover_length=0, retract_recover_feedrate=8*60;
  185. #endif
  186. #ifdef ULTIPANEL
  187. bool powersupply = true;
  188. #endif
  189. #ifdef DELTA
  190. float delta[3] = {0.0, 0.0, 0.0};
  191. #endif
  192. //===========================================================================
  193. //=============================private variables=============================
  194. //===========================================================================
  195. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  196. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  197. static float offset[3] = {0.0, 0.0, 0.0};
  198. static bool home_all_axis = true;
  199. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  200. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  201. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  202. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  203. static bool fromsd[BUFSIZE];
  204. static int bufindr = 0;
  205. static int bufindw = 0;
  206. static int buflen = 0;
  207. //static int i = 0;
  208. static char serial_char;
  209. static int serial_count = 0;
  210. static boolean comment_mode = false;
  211. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  212. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  213. //static float tt = 0;
  214. //static float bt = 0;
  215. //Inactivity shutdown variables
  216. static unsigned long previous_millis_cmd = 0;
  217. static unsigned long max_inactive_time = 0;
  218. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  219. unsigned long starttime=0;
  220. unsigned long stoptime=0;
  221. static uint8_t tmp_extruder;
  222. bool Stopped=false;
  223. #if NUM_SERVOS > 0
  224. Servo servos[NUM_SERVOS];
  225. #endif
  226. bool CooldownNoWait = true;
  227. bool target_direction;
  228. //===========================================================================
  229. //=============================ROUTINES=============================
  230. //===========================================================================
  231. void get_arc_coordinates();
  232. bool setTargetedHotend(int code);
  233. void serial_echopair_P(const char *s_P, float v)
  234. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  235. void serial_echopair_P(const char *s_P, double v)
  236. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  237. void serial_echopair_P(const char *s_P, unsigned long v)
  238. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  239. extern "C"{
  240. extern unsigned int __bss_end;
  241. extern unsigned int __heap_start;
  242. extern void *__brkval;
  243. int freeMemory() {
  244. int free_memory;
  245. if((int)__brkval == 0)
  246. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  247. else
  248. free_memory = ((int)&free_memory) - ((int)__brkval);
  249. return free_memory;
  250. }
  251. }
  252. //adds an command to the main command buffer
  253. //thats really done in a non-safe way.
  254. //needs overworking someday
  255. void enquecommand(const char *cmd)
  256. {
  257. if(buflen < BUFSIZE)
  258. {
  259. //this is dangerous if a mixing of serial and this happsens
  260. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  261. SERIAL_ECHO_START;
  262. SERIAL_ECHOPGM("enqueing \"");
  263. SERIAL_ECHO(cmdbuffer[bufindw]);
  264. SERIAL_ECHOLNPGM("\"");
  265. bufindw= (bufindw + 1)%BUFSIZE;
  266. buflen += 1;
  267. }
  268. }
  269. void enquecommand_P(const char *cmd)
  270. {
  271. if(buflen < BUFSIZE)
  272. {
  273. //this is dangerous if a mixing of serial and this happsens
  274. strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
  275. SERIAL_ECHO_START;
  276. SERIAL_ECHOPGM("enqueing \"");
  277. SERIAL_ECHO(cmdbuffer[bufindw]);
  278. SERIAL_ECHOLNPGM("\"");
  279. bufindw= (bufindw + 1)%BUFSIZE;
  280. buflen += 1;
  281. }
  282. }
  283. void setup_killpin()
  284. {
  285. #if defined(KILL_PIN) && KILL_PIN > -1
  286. pinMode(KILL_PIN,INPUT);
  287. WRITE(KILL_PIN,HIGH);
  288. #endif
  289. }
  290. void setup_photpin()
  291. {
  292. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  293. SET_OUTPUT(PHOTOGRAPH_PIN);
  294. WRITE(PHOTOGRAPH_PIN, LOW);
  295. #endif
  296. }
  297. void setup_powerhold()
  298. {
  299. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  300. SET_OUTPUT(SUICIDE_PIN);
  301. WRITE(SUICIDE_PIN, HIGH);
  302. #endif
  303. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  304. SET_OUTPUT(PS_ON_PIN);
  305. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  306. #endif
  307. }
  308. void suicide()
  309. {
  310. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  311. SET_OUTPUT(SUICIDE_PIN);
  312. WRITE(SUICIDE_PIN, LOW);
  313. #endif
  314. }
  315. void servo_init()
  316. {
  317. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  318. servos[0].attach(SERVO0_PIN);
  319. #endif
  320. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  321. servos[1].attach(SERVO1_PIN);
  322. #endif
  323. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  324. servos[2].attach(SERVO2_PIN);
  325. #endif
  326. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  327. servos[3].attach(SERVO3_PIN);
  328. #endif
  329. #if (NUM_SERVOS >= 5)
  330. #error "TODO: enter initalisation code for more servos"
  331. #endif
  332. // Set position of Servo Endstops that are defined
  333. #ifdef SERVO_ENDSTOPS
  334. for(int8_t i = 0; i < 3; i++)
  335. {
  336. if(servo_endstops[i] > -1) {
  337. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  338. }
  339. }
  340. #endif
  341. }
  342. void setup()
  343. {
  344. setup_killpin();
  345. setup_powerhold();
  346. MYSERIAL.begin(BAUDRATE);
  347. SERIAL_PROTOCOLLNPGM("start");
  348. SERIAL_ECHO_START;
  349. // Check startup - does nothing if bootloader sets MCUSR to 0
  350. byte mcu = MCUSR;
  351. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  352. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  353. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  354. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  355. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  356. MCUSR=0;
  357. SERIAL_ECHOPGM(MSG_MARLIN);
  358. SERIAL_ECHOLNPGM(VERSION_STRING);
  359. #ifdef STRING_VERSION_CONFIG_H
  360. #ifdef STRING_CONFIG_H_AUTHOR
  361. SERIAL_ECHO_START;
  362. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  363. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  364. SERIAL_ECHOPGM(MSG_AUTHOR);
  365. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  366. SERIAL_ECHOPGM("Compiled: ");
  367. SERIAL_ECHOLNPGM(__DATE__);
  368. #endif
  369. #endif
  370. SERIAL_ECHO_START;
  371. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  372. SERIAL_ECHO(freeMemory());
  373. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  374. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  375. for(int8_t i = 0; i < BUFSIZE; i++)
  376. {
  377. fromsd[i] = false;
  378. }
  379. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  380. Config_RetrieveSettings();
  381. tp_init(); // Initialize temperature loop
  382. plan_init(); // Initialize planner;
  383. watchdog_init();
  384. st_init(); // Initialize stepper, this enables interrupts!
  385. setup_photpin();
  386. servo_init();
  387. lcd_init();
  388. _delay_ms(1000); // wait 1sec to display the splash screen
  389. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  390. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  391. #endif
  392. }
  393. void loop()
  394. {
  395. if(buflen < (BUFSIZE-1))
  396. get_command();
  397. #ifdef SDSUPPORT
  398. card.checkautostart(false);
  399. #endif
  400. if(buflen)
  401. {
  402. #ifdef SDSUPPORT
  403. if(card.saving)
  404. {
  405. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  406. {
  407. card.write_command(cmdbuffer[bufindr]);
  408. if(card.logging)
  409. {
  410. process_commands();
  411. }
  412. else
  413. {
  414. SERIAL_PROTOCOLLNPGM(MSG_OK);
  415. }
  416. }
  417. else
  418. {
  419. card.closefile();
  420. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  421. }
  422. }
  423. else
  424. {
  425. process_commands();
  426. }
  427. #else
  428. process_commands();
  429. #endif //SDSUPPORT
  430. buflen = (buflen-1);
  431. bufindr = (bufindr + 1)%BUFSIZE;
  432. }
  433. //check heater every n milliseconds
  434. manage_heater();
  435. manage_inactivity();
  436. checkHitEndstops();
  437. lcd_update();
  438. }
  439. void get_command()
  440. {
  441. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  442. serial_char = MYSERIAL.read();
  443. if(serial_char == '\n' ||
  444. serial_char == '\r' ||
  445. (serial_char == ':' && comment_mode == false) ||
  446. serial_count >= (MAX_CMD_SIZE - 1) )
  447. {
  448. if(!serial_count) { //if empty line
  449. comment_mode = false; //for new command
  450. return;
  451. }
  452. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  453. if(!comment_mode){
  454. comment_mode = false; //for new command
  455. fromsd[bufindw] = false;
  456. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  457. {
  458. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  459. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  460. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  461. SERIAL_ERROR_START;
  462. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  463. SERIAL_ERRORLN(gcode_LastN);
  464. //Serial.println(gcode_N);
  465. FlushSerialRequestResend();
  466. serial_count = 0;
  467. return;
  468. }
  469. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  470. {
  471. byte checksum = 0;
  472. byte count = 0;
  473. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  474. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  475. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  476. SERIAL_ERROR_START;
  477. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  478. SERIAL_ERRORLN(gcode_LastN);
  479. FlushSerialRequestResend();
  480. serial_count = 0;
  481. return;
  482. }
  483. //if no errors, continue parsing
  484. }
  485. else
  486. {
  487. SERIAL_ERROR_START;
  488. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  489. SERIAL_ERRORLN(gcode_LastN);
  490. FlushSerialRequestResend();
  491. serial_count = 0;
  492. return;
  493. }
  494. gcode_LastN = gcode_N;
  495. //if no errors, continue parsing
  496. }
  497. else // if we don't receive 'N' but still see '*'
  498. {
  499. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  500. {
  501. SERIAL_ERROR_START;
  502. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  503. SERIAL_ERRORLN(gcode_LastN);
  504. serial_count = 0;
  505. return;
  506. }
  507. }
  508. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  509. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  510. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  511. case 0:
  512. case 1:
  513. case 2:
  514. case 3:
  515. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  516. #ifdef SDSUPPORT
  517. if(card.saving)
  518. break;
  519. #endif //SDSUPPORT
  520. SERIAL_PROTOCOLLNPGM(MSG_OK);
  521. }
  522. else {
  523. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  524. LCD_MESSAGEPGM(MSG_STOPPED);
  525. }
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. bufindw = (bufindw + 1)%BUFSIZE;
  532. buflen += 1;
  533. }
  534. serial_count = 0; //clear buffer
  535. }
  536. else
  537. {
  538. if(serial_char == ';') comment_mode = true;
  539. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  540. }
  541. }
  542. #ifdef SDSUPPORT
  543. if(!card.sdprinting || serial_count!=0){
  544. return;
  545. }
  546. while( !card.eof() && buflen < BUFSIZE) {
  547. int16_t n=card.get();
  548. serial_char = (char)n;
  549. if(serial_char == '\n' ||
  550. serial_char == '\r' ||
  551. (serial_char == ':' && comment_mode == false) ||
  552. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  553. {
  554. if(card.eof()){
  555. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  556. stoptime=millis();
  557. char time[30];
  558. unsigned long t=(stoptime-starttime)/1000;
  559. int hours, minutes;
  560. minutes=(t/60)%60;
  561. hours=t/60/60;
  562. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  563. SERIAL_ECHO_START;
  564. SERIAL_ECHOLN(time);
  565. lcd_setstatus(time);
  566. card.printingHasFinished();
  567. card.checkautostart(true);
  568. }
  569. if(!serial_count)
  570. {
  571. comment_mode = false; //for new command
  572. return; //if empty line
  573. }
  574. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  575. // if(!comment_mode){
  576. fromsd[bufindw] = true;
  577. buflen += 1;
  578. bufindw = (bufindw + 1)%BUFSIZE;
  579. // }
  580. comment_mode = false; //for new command
  581. serial_count = 0; //clear buffer
  582. }
  583. else
  584. {
  585. if(serial_char == ';') comment_mode = true;
  586. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  587. }
  588. }
  589. #endif //SDSUPPORT
  590. }
  591. float code_value()
  592. {
  593. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  594. }
  595. long code_value_long()
  596. {
  597. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  598. }
  599. bool code_seen(char code)
  600. {
  601. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  602. return (strchr_pointer != NULL); //Return True if a character was found
  603. }
  604. #define DEFINE_PGM_READ_ANY(type, reader) \
  605. static inline type pgm_read_any(const type *p) \
  606. { return pgm_read_##reader##_near(p); }
  607. DEFINE_PGM_READ_ANY(float, float);
  608. DEFINE_PGM_READ_ANY(signed char, byte);
  609. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  610. static const PROGMEM type array##_P[3] = \
  611. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  612. static inline type array(int axis) \
  613. { return pgm_read_any(&array##_P[axis]); }
  614. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  615. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  616. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  617. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  618. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  619. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  620. #ifdef DUAL_X_CARRIAGE
  621. #if EXTRUDERS == 1 || defined(COREXY) \
  622. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  623. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  624. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  625. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  626. #endif
  627. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  628. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  629. #endif
  630. #define DXC_FULL_CONTROL_MODE 0
  631. #define DXC_AUTO_PARK_MODE 1
  632. #define DXC_DUPLICATION_MODE 2
  633. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  634. static float x_home_pos(int extruder) {
  635. if (extruder == 0)
  636. return base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  637. else
  638. // In dual carriage mode the extruder offset provides an override of the
  639. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  640. // This allow soft recalibration of the second extruder offset position without firmware reflash
  641. // (through the M218 command).
  642. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  643. }
  644. static int x_home_dir(int extruder) {
  645. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  646. }
  647. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  648. static bool active_extruder_parked = false; // used in mode 1 & 2
  649. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  650. static unsigned long delayed_move_time = 0; // used in mode 1
  651. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  652. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  653. bool extruder_duplication_enabled = false; // used in mode 2
  654. #endif //DUAL_X_CARRIAGE
  655. static void axis_is_at_home(int axis) {
  656. #ifdef DUAL_X_CARRIAGE
  657. if (axis == X_AXIS) {
  658. if (active_extruder != 0) {
  659. current_position[X_AXIS] = x_home_pos(active_extruder);
  660. min_pos[X_AXIS] = X2_MIN_POS;
  661. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  662. return;
  663. }
  664. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  665. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  666. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homeing[X_AXIS];
  667. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homeing[X_AXIS],
  668. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  669. return;
  670. }
  671. }
  672. #endif
  673. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  674. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  675. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  676. }
  677. static void homeaxis(int axis) {
  678. #define HOMEAXIS_DO(LETTER) \
  679. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  680. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  681. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  682. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  683. 0) {
  684. int axis_home_dir = home_dir(axis);
  685. #ifdef DUAL_X_CARRIAGE
  686. if (axis == X_AXIS)
  687. axis_home_dir = x_home_dir(active_extruder);
  688. #endif
  689. // Engage Servo endstop if enabled
  690. #ifdef SERVO_ENDSTOPS
  691. if (SERVO_ENDSTOPS[axis] > -1) {
  692. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  693. }
  694. #endif
  695. current_position[axis] = 0;
  696. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  697. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  698. feedrate = homing_feedrate[axis];
  699. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  700. st_synchronize();
  701. current_position[axis] = 0;
  702. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  703. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  704. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  705. st_synchronize();
  706. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  707. #ifdef DELTA
  708. feedrate = homing_feedrate[axis]/10;
  709. #else
  710. feedrate = homing_feedrate[axis]/2 ;
  711. #endif
  712. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  713. st_synchronize();
  714. axis_is_at_home(axis);
  715. destination[axis] = current_position[axis];
  716. feedrate = 0.0;
  717. endstops_hit_on_purpose();
  718. // Retract Servo endstop if enabled
  719. #ifdef SERVO_ENDSTOPS
  720. if (SERVO_ENDSTOPS[axis] > -1) {
  721. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  722. }
  723. #endif
  724. }
  725. }
  726. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  727. void process_commands()
  728. {
  729. unsigned long codenum; //throw away variable
  730. char *starpos = NULL;
  731. if(code_seen('G'))
  732. {
  733. switch((int)code_value())
  734. {
  735. case 0: // G0 -> G1
  736. case 1: // G1
  737. if(Stopped == false) {
  738. get_coordinates(); // For X Y Z E F
  739. prepare_move();
  740. //ClearToSend();
  741. return;
  742. }
  743. //break;
  744. case 2: // G2 - CW ARC
  745. if(Stopped == false) {
  746. get_arc_coordinates();
  747. prepare_arc_move(true);
  748. return;
  749. }
  750. case 3: // G3 - CCW ARC
  751. if(Stopped == false) {
  752. get_arc_coordinates();
  753. prepare_arc_move(false);
  754. return;
  755. }
  756. case 4: // G4 dwell
  757. LCD_MESSAGEPGM(MSG_DWELL);
  758. codenum = 0;
  759. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  760. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  761. st_synchronize();
  762. codenum += millis(); // keep track of when we started waiting
  763. previous_millis_cmd = millis();
  764. while(millis() < codenum ){
  765. manage_heater();
  766. manage_inactivity();
  767. lcd_update();
  768. }
  769. break;
  770. #ifdef FWRETRACT
  771. case 10: // G10 retract
  772. if(!retracted)
  773. {
  774. destination[X_AXIS]=current_position[X_AXIS];
  775. destination[Y_AXIS]=current_position[Y_AXIS];
  776. destination[Z_AXIS]=current_position[Z_AXIS];
  777. current_position[Z_AXIS]+=-retract_zlift;
  778. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  779. feedrate=retract_feedrate;
  780. retracted=true;
  781. prepare_move();
  782. }
  783. break;
  784. case 11: // G10 retract_recover
  785. if(!retracted)
  786. {
  787. destination[X_AXIS]=current_position[X_AXIS];
  788. destination[Y_AXIS]=current_position[Y_AXIS];
  789. destination[Z_AXIS]=current_position[Z_AXIS];
  790. current_position[Z_AXIS]+=retract_zlift;
  791. current_position[E_AXIS]+=-retract_recover_length;
  792. feedrate=retract_recover_feedrate;
  793. retracted=false;
  794. prepare_move();
  795. }
  796. break;
  797. #endif //FWRETRACT
  798. case 28: //G28 Home all Axis one at a time
  799. saved_feedrate = feedrate;
  800. saved_feedmultiply = feedmultiply;
  801. feedmultiply = 100;
  802. previous_millis_cmd = millis();
  803. enable_endstops(true);
  804. for(int8_t i=0; i < NUM_AXIS; i++) {
  805. destination[i] = current_position[i];
  806. }
  807. feedrate = 0.0;
  808. #ifdef DELTA
  809. // A delta can only safely home all axis at the same time
  810. // all axis have to home at the same time
  811. // Move all carriages up together until the first endstop is hit.
  812. current_position[X_AXIS] = 0;
  813. current_position[Y_AXIS] = 0;
  814. current_position[Z_AXIS] = 0;
  815. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  816. destination[X_AXIS] = 3 * Z_MAX_LENGTH;
  817. destination[Y_AXIS] = 3 * Z_MAX_LENGTH;
  818. destination[Z_AXIS] = 3 * Z_MAX_LENGTH;
  819. feedrate = 1.732 * homing_feedrate[X_AXIS];
  820. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  821. st_synchronize();
  822. endstops_hit_on_purpose();
  823. current_position[X_AXIS] = destination[X_AXIS];
  824. current_position[Y_AXIS] = destination[Y_AXIS];
  825. current_position[Z_AXIS] = destination[Z_AXIS];
  826. // take care of back off and rehome now we are all at the top
  827. HOMEAXIS(X);
  828. HOMEAXIS(Y);
  829. HOMEAXIS(Z);
  830. calculate_delta(current_position);
  831. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  832. #else // NOT DELTA
  833. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  834. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  835. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  836. HOMEAXIS(Z);
  837. }
  838. #endif
  839. #ifdef QUICK_HOME
  840. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  841. {
  842. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  843. #ifndef DUAL_X_CARRIAGE
  844. int x_axis_home_dir = home_dir(X_AXIS);
  845. #else
  846. int x_axis_home_dir = x_home_dir(active_extruder);
  847. extruder_duplication_enabled = false;
  848. #endif
  849. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  850. destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
  851. feedrate = homing_feedrate[X_AXIS];
  852. if(homing_feedrate[Y_AXIS]<feedrate)
  853. feedrate =homing_feedrate[Y_AXIS];
  854. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  855. st_synchronize();
  856. axis_is_at_home(X_AXIS);
  857. axis_is_at_home(Y_AXIS);
  858. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  859. destination[X_AXIS] = current_position[X_AXIS];
  860. destination[Y_AXIS] = current_position[Y_AXIS];
  861. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  862. feedrate = 0.0;
  863. st_synchronize();
  864. endstops_hit_on_purpose();
  865. current_position[X_AXIS] = destination[X_AXIS];
  866. current_position[Y_AXIS] = destination[Y_AXIS];
  867. current_position[Z_AXIS] = destination[Z_AXIS];
  868. }
  869. #endif
  870. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  871. {
  872. #ifdef DUAL_X_CARRIAGE
  873. int tmp_extruder = active_extruder;
  874. extruder_duplication_enabled = false;
  875. active_extruder = !active_extruder;
  876. HOMEAXIS(X);
  877. inactive_extruder_x_pos = current_position[X_AXIS];
  878. active_extruder = tmp_extruder;
  879. HOMEAXIS(X);
  880. // reset state used by the different modes
  881. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  882. delayed_move_time = 0;
  883. active_extruder_parked = true;
  884. #else
  885. HOMEAXIS(X);
  886. #endif
  887. }
  888. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  889. HOMEAXIS(Y);
  890. }
  891. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  892. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  893. HOMEAXIS(Z);
  894. }
  895. #endif
  896. if(code_seen(axis_codes[X_AXIS]))
  897. {
  898. if(code_value_long() != 0) {
  899. current_position[X_AXIS]=code_value()+add_homeing[0];
  900. }
  901. }
  902. if(code_seen(axis_codes[Y_AXIS])) {
  903. if(code_value_long() != 0) {
  904. current_position[Y_AXIS]=code_value()+add_homeing[1];
  905. }
  906. }
  907. if(code_seen(axis_codes[Z_AXIS])) {
  908. if(code_value_long() != 0) {
  909. current_position[Z_AXIS]=code_value()+add_homeing[2];
  910. }
  911. }
  912. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  913. #endif // else DELTA
  914. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  915. enable_endstops(false);
  916. #endif
  917. feedrate = saved_feedrate;
  918. feedmultiply = saved_feedmultiply;
  919. previous_millis_cmd = millis();
  920. endstops_hit_on_purpose();
  921. break;
  922. case 90: // G90
  923. relative_mode = false;
  924. break;
  925. case 91: // G91
  926. relative_mode = true;
  927. break;
  928. case 92: // G92
  929. if(!code_seen(axis_codes[E_AXIS]))
  930. st_synchronize();
  931. for(int8_t i=0; i < NUM_AXIS; i++) {
  932. if(code_seen(axis_codes[i])) {
  933. if(i == E_AXIS) {
  934. current_position[i] = code_value();
  935. plan_set_e_position(current_position[E_AXIS]);
  936. }
  937. else {
  938. current_position[i] = code_value()+add_homeing[i];
  939. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  940. }
  941. }
  942. }
  943. break;
  944. }
  945. }
  946. else if(code_seen('M'))
  947. {
  948. switch( (int)code_value() )
  949. {
  950. #ifdef ULTIPANEL
  951. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  952. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  953. {
  954. LCD_MESSAGEPGM(MSG_USERWAIT);
  955. codenum = 0;
  956. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  957. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  958. st_synchronize();
  959. previous_millis_cmd = millis();
  960. if (codenum > 0){
  961. codenum += millis(); // keep track of when we started waiting
  962. while(millis() < codenum && !lcd_clicked()){
  963. manage_heater();
  964. manage_inactivity();
  965. lcd_update();
  966. }
  967. }else{
  968. while(!lcd_clicked()){
  969. manage_heater();
  970. manage_inactivity();
  971. lcd_update();
  972. }
  973. }
  974. LCD_MESSAGEPGM(MSG_RESUMING);
  975. }
  976. break;
  977. #endif
  978. case 17:
  979. LCD_MESSAGEPGM(MSG_NO_MOVE);
  980. enable_x();
  981. enable_y();
  982. enable_z();
  983. enable_e0();
  984. enable_e1();
  985. enable_e2();
  986. break;
  987. #ifdef SDSUPPORT
  988. case 20: // M20 - list SD card
  989. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  990. card.ls();
  991. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  992. break;
  993. case 21: // M21 - init SD card
  994. card.initsd();
  995. break;
  996. case 22: //M22 - release SD card
  997. card.release();
  998. break;
  999. case 23: //M23 - Select file
  1000. starpos = (strchr(strchr_pointer + 4,'*'));
  1001. if(starpos!=NULL)
  1002. *(starpos-1)='\0';
  1003. card.openFile(strchr_pointer + 4,true);
  1004. break;
  1005. case 24: //M24 - Start SD print
  1006. card.startFileprint();
  1007. starttime=millis();
  1008. break;
  1009. case 25: //M25 - Pause SD print
  1010. card.pauseSDPrint();
  1011. break;
  1012. case 26: //M26 - Set SD index
  1013. if(card.cardOK && code_seen('S')) {
  1014. card.setIndex(code_value_long());
  1015. }
  1016. break;
  1017. case 27: //M27 - Get SD status
  1018. card.getStatus();
  1019. break;
  1020. case 28: //M28 - Start SD write
  1021. starpos = (strchr(strchr_pointer + 4,'*'));
  1022. if(starpos != NULL){
  1023. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1024. strchr_pointer = strchr(npos,' ') + 1;
  1025. *(starpos-1) = '\0';
  1026. }
  1027. card.openFile(strchr_pointer+4,false);
  1028. break;
  1029. case 29: //M29 - Stop SD write
  1030. //processed in write to file routine above
  1031. //card,saving = false;
  1032. break;
  1033. case 30: //M30 <filename> Delete File
  1034. if (card.cardOK){
  1035. card.closefile();
  1036. starpos = (strchr(strchr_pointer + 4,'*'));
  1037. if(starpos != NULL){
  1038. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1039. strchr_pointer = strchr(npos,' ') + 1;
  1040. *(starpos-1) = '\0';
  1041. }
  1042. card.removeFile(strchr_pointer + 4);
  1043. }
  1044. break;
  1045. case 32: //M32 - Select file and start SD print
  1046. if(card.sdprinting) {
  1047. st_synchronize();
  1048. card.closefile();
  1049. card.sdprinting = false;
  1050. }
  1051. starpos = (strchr(strchr_pointer + 4,'*'));
  1052. if(starpos!=NULL)
  1053. *(starpos-1)='\0';
  1054. card.openFile(strchr_pointer + 4,true);
  1055. card.startFileprint();
  1056. starttime=millis();
  1057. break;
  1058. case 928: //M928 - Start SD write
  1059. starpos = (strchr(strchr_pointer + 5,'*'));
  1060. if(starpos != NULL){
  1061. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1062. strchr_pointer = strchr(npos,' ') + 1;
  1063. *(starpos-1) = '\0';
  1064. }
  1065. card.openLogFile(strchr_pointer+5);
  1066. break;
  1067. #endif //SDSUPPORT
  1068. case 31: //M31 take time since the start of the SD print or an M109 command
  1069. {
  1070. stoptime=millis();
  1071. char time[30];
  1072. unsigned long t=(stoptime-starttime)/1000;
  1073. int sec,min;
  1074. min=t/60;
  1075. sec=t%60;
  1076. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  1077. SERIAL_ECHO_START;
  1078. SERIAL_ECHOLN(time);
  1079. lcd_setstatus(time);
  1080. autotempShutdown();
  1081. }
  1082. break;
  1083. case 42: //M42 -Change pin status via gcode
  1084. if (code_seen('S'))
  1085. {
  1086. int pin_status = code_value();
  1087. int pin_number = LED_PIN;
  1088. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  1089. pin_number = code_value();
  1090. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  1091. {
  1092. if (sensitive_pins[i] == pin_number)
  1093. {
  1094. pin_number = -1;
  1095. break;
  1096. }
  1097. }
  1098. #if defined(FAN_PIN) && FAN_PIN > -1
  1099. if (pin_number == FAN_PIN)
  1100. fanSpeed = pin_status;
  1101. #endif
  1102. if (pin_number > -1)
  1103. {
  1104. pinMode(pin_number, OUTPUT);
  1105. digitalWrite(pin_number, pin_status);
  1106. analogWrite(pin_number, pin_status);
  1107. }
  1108. }
  1109. break;
  1110. case 104: // M104
  1111. if(setTargetedHotend(104)){
  1112. break;
  1113. }
  1114. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  1115. #ifdef DUAL_X_CARRIAGE
  1116. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1117. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1118. #endif
  1119. setWatch();
  1120. break;
  1121. case 140: // M140 set bed temp
  1122. if (code_seen('S')) setTargetBed(code_value());
  1123. break;
  1124. case 105 : // M105
  1125. if(setTargetedHotend(105)){
  1126. break;
  1127. }
  1128. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1129. SERIAL_PROTOCOLPGM("ok T:");
  1130. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1131. SERIAL_PROTOCOLPGM(" /");
  1132. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  1133. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1134. SERIAL_PROTOCOLPGM(" B:");
  1135. SERIAL_PROTOCOL_F(degBed(),1);
  1136. SERIAL_PROTOCOLPGM(" /");
  1137. SERIAL_PROTOCOL_F(degTargetBed(),1);
  1138. #endif //TEMP_BED_PIN
  1139. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  1140. SERIAL_PROTOCOLPGM(" T");
  1141. SERIAL_PROTOCOL(cur_extruder);
  1142. SERIAL_PROTOCOLPGM(":");
  1143. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  1144. SERIAL_PROTOCOLPGM(" /");
  1145. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  1146. }
  1147. #else
  1148. SERIAL_ERROR_START;
  1149. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  1150. #endif
  1151. SERIAL_PROTOCOLPGM(" @:");
  1152. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  1153. SERIAL_PROTOCOLPGM(" B@:");
  1154. SERIAL_PROTOCOL(getHeaterPower(-1));
  1155. SERIAL_PROTOCOLLN("");
  1156. return;
  1157. break;
  1158. case 109:
  1159. {// M109 - Wait for extruder heater to reach target.
  1160. if(setTargetedHotend(109)){
  1161. break;
  1162. }
  1163. LCD_MESSAGEPGM(MSG_HEATING);
  1164. #ifdef AUTOTEMP
  1165. autotemp_enabled=false;
  1166. #endif
  1167. if (code_seen('S')) {
  1168. setTargetHotend(code_value(), tmp_extruder);
  1169. #ifdef DUAL_X_CARRIAGE
  1170. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1171. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1172. #endif
  1173. CooldownNoWait = true;
  1174. } else if (code_seen('R')) {
  1175. setTargetHotend(code_value(), tmp_extruder);
  1176. #ifdef DUAL_X_CARRIAGE
  1177. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1178. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1179. #endif
  1180. CooldownNoWait = false;
  1181. }
  1182. #ifdef AUTOTEMP
  1183. if (code_seen('S')) autotemp_min=code_value();
  1184. if (code_seen('B')) autotemp_max=code_value();
  1185. if (code_seen('F'))
  1186. {
  1187. autotemp_factor=code_value();
  1188. autotemp_enabled=true;
  1189. }
  1190. #endif
  1191. setWatch();
  1192. codenum = millis();
  1193. /* See if we are heating up or cooling down */
  1194. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  1195. #ifdef TEMP_RESIDENCY_TIME
  1196. long residencyStart;
  1197. residencyStart = -1;
  1198. /* continue to loop until we have reached the target temp
  1199. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  1200. while((residencyStart == -1) ||
  1201. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  1202. #else
  1203. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  1204. #endif //TEMP_RESIDENCY_TIME
  1205. if( (millis() - codenum) > 1000UL )
  1206. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  1207. SERIAL_PROTOCOLPGM("T:");
  1208. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1209. SERIAL_PROTOCOLPGM(" E:");
  1210. SERIAL_PROTOCOL((int)tmp_extruder);
  1211. #ifdef TEMP_RESIDENCY_TIME
  1212. SERIAL_PROTOCOLPGM(" W:");
  1213. if(residencyStart > -1)
  1214. {
  1215. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  1216. SERIAL_PROTOCOLLN( codenum );
  1217. }
  1218. else
  1219. {
  1220. SERIAL_PROTOCOLLN( "?" );
  1221. }
  1222. #else
  1223. SERIAL_PROTOCOLLN("");
  1224. #endif
  1225. codenum = millis();
  1226. }
  1227. manage_heater();
  1228. manage_inactivity();
  1229. lcd_update();
  1230. #ifdef TEMP_RESIDENCY_TIME
  1231. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  1232. or when current temp falls outside the hysteresis after target temp was reached */
  1233. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  1234. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  1235. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  1236. {
  1237. residencyStart = millis();
  1238. }
  1239. #endif //TEMP_RESIDENCY_TIME
  1240. }
  1241. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  1242. starttime=millis();
  1243. previous_millis_cmd = millis();
  1244. }
  1245. break;
  1246. case 190: // M190 - Wait for bed heater to reach target.
  1247. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1248. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1249. if (code_seen('S')) {
  1250. setTargetBed(code_value());
  1251. CooldownNoWait = true;
  1252. } else if (code_seen('R')) {
  1253. setTargetBed(code_value());
  1254. CooldownNoWait = false;
  1255. }
  1256. codenum = millis();
  1257. target_direction = isHeatingBed(); // true if heating, false if cooling
  1258. while ( target_direction ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  1259. {
  1260. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  1261. {
  1262. float tt=degHotend(active_extruder);
  1263. SERIAL_PROTOCOLPGM("T:");
  1264. SERIAL_PROTOCOL(tt);
  1265. SERIAL_PROTOCOLPGM(" E:");
  1266. SERIAL_PROTOCOL((int)active_extruder);
  1267. SERIAL_PROTOCOLPGM(" B:");
  1268. SERIAL_PROTOCOL_F(degBed(),1);
  1269. SERIAL_PROTOCOLLN("");
  1270. codenum = millis();
  1271. }
  1272. manage_heater();
  1273. manage_inactivity();
  1274. lcd_update();
  1275. }
  1276. LCD_MESSAGEPGM(MSG_BED_DONE);
  1277. previous_millis_cmd = millis();
  1278. #endif
  1279. break;
  1280. #if defined(FAN_PIN) && FAN_PIN > -1
  1281. case 106: //M106 Fan On
  1282. if (code_seen('S')){
  1283. fanSpeed=constrain(code_value(),0,255);
  1284. }
  1285. else {
  1286. fanSpeed=255;
  1287. }
  1288. break;
  1289. case 107: //M107 Fan Off
  1290. fanSpeed = 0;
  1291. break;
  1292. #endif //FAN_PIN
  1293. #ifdef BARICUDA
  1294. // PWM for HEATER_1_PIN
  1295. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1296. case 126: //M126 valve open
  1297. if (code_seen('S')){
  1298. ValvePressure=constrain(code_value(),0,255);
  1299. }
  1300. else {
  1301. ValvePressure=255;
  1302. }
  1303. break;
  1304. case 127: //M127 valve closed
  1305. ValvePressure = 0;
  1306. break;
  1307. #endif //HEATER_1_PIN
  1308. // PWM for HEATER_2_PIN
  1309. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1310. case 128: //M128 valve open
  1311. if (code_seen('S')){
  1312. EtoPPressure=constrain(code_value(),0,255);
  1313. }
  1314. else {
  1315. EtoPPressure=255;
  1316. }
  1317. break;
  1318. case 129: //M129 valve closed
  1319. EtoPPressure = 0;
  1320. break;
  1321. #endif //HEATER_2_PIN
  1322. #endif
  1323. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  1324. case 80: // M80 - Turn on Power Supply
  1325. SET_OUTPUT(PS_ON_PIN); //GND
  1326. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  1327. #ifdef ULTIPANEL
  1328. powersupply = true;
  1329. LCD_MESSAGEPGM(WELCOME_MSG);
  1330. lcd_update();
  1331. #endif
  1332. break;
  1333. #endif
  1334. case 81: // M81 - Turn off Power Supply
  1335. disable_heater();
  1336. st_synchronize();
  1337. disable_e0();
  1338. disable_e1();
  1339. disable_e2();
  1340. finishAndDisableSteppers();
  1341. fanSpeed = 0;
  1342. delay(1000); // Wait a little before to switch off
  1343. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  1344. st_synchronize();
  1345. suicide();
  1346. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  1347. SET_OUTPUT(PS_ON_PIN);
  1348. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  1349. #endif
  1350. #ifdef ULTIPANEL
  1351. powersupply = false;
  1352. LCD_MESSAGEPGM(MACHINE_NAME" "MSG_OFF".");
  1353. lcd_update();
  1354. #endif
  1355. break;
  1356. case 82:
  1357. axis_relative_modes[3] = false;
  1358. break;
  1359. case 83:
  1360. axis_relative_modes[3] = true;
  1361. break;
  1362. case 18: //compatibility
  1363. case 84: // M84
  1364. if(code_seen('S')){
  1365. stepper_inactive_time = code_value() * 1000;
  1366. }
  1367. else
  1368. {
  1369. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1370. if(all_axis)
  1371. {
  1372. st_synchronize();
  1373. disable_e0();
  1374. disable_e1();
  1375. disable_e2();
  1376. finishAndDisableSteppers();
  1377. }
  1378. else
  1379. {
  1380. st_synchronize();
  1381. if(code_seen('X')) disable_x();
  1382. if(code_seen('Y')) disable_y();
  1383. if(code_seen('Z')) disable_z();
  1384. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1385. if(code_seen('E')) {
  1386. disable_e0();
  1387. disable_e1();
  1388. disable_e2();
  1389. }
  1390. #endif
  1391. }
  1392. }
  1393. break;
  1394. case 85: // M85
  1395. code_seen('S');
  1396. max_inactive_time = code_value() * 1000;
  1397. break;
  1398. case 92: // M92
  1399. for(int8_t i=0; i < NUM_AXIS; i++)
  1400. {
  1401. if(code_seen(axis_codes[i]))
  1402. {
  1403. if(i == 3) { // E
  1404. float value = code_value();
  1405. if(value < 20.0) {
  1406. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1407. max_e_jerk *= factor;
  1408. max_feedrate[i] *= factor;
  1409. axis_steps_per_sqr_second[i] *= factor;
  1410. }
  1411. axis_steps_per_unit[i] = value;
  1412. }
  1413. else {
  1414. axis_steps_per_unit[i] = code_value();
  1415. }
  1416. }
  1417. }
  1418. break;
  1419. case 115: // M115
  1420. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  1421. break;
  1422. case 117: // M117 display message
  1423. starpos = (strchr(strchr_pointer + 5,'*'));
  1424. if(starpos!=NULL)
  1425. *(starpos-1)='\0';
  1426. lcd_setstatus(strchr_pointer + 5);
  1427. break;
  1428. case 114: // M114
  1429. SERIAL_PROTOCOLPGM("X:");
  1430. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1431. SERIAL_PROTOCOLPGM("Y:");
  1432. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1433. SERIAL_PROTOCOLPGM("Z:");
  1434. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1435. SERIAL_PROTOCOLPGM("E:");
  1436. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1437. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1438. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1439. SERIAL_PROTOCOLPGM("Y:");
  1440. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1441. SERIAL_PROTOCOLPGM("Z:");
  1442. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1443. SERIAL_PROTOCOLLN("");
  1444. break;
  1445. case 120: // M120
  1446. enable_endstops(false) ;
  1447. break;
  1448. case 121: // M121
  1449. enable_endstops(true) ;
  1450. break;
  1451. case 119: // M119
  1452. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1453. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  1454. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1455. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1456. #endif
  1457. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  1458. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1459. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1460. #endif
  1461. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  1462. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1463. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1464. #endif
  1465. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  1466. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1467. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1468. #endif
  1469. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  1470. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1471. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1472. #endif
  1473. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  1474. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1475. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1476. #endif
  1477. break;
  1478. //TODO: update for all axis, use for loop
  1479. #ifdef BLINKM
  1480. case 150: // M150
  1481. {
  1482. byte red;
  1483. byte grn;
  1484. byte blu;
  1485. if(code_seen('R')) red = code_value();
  1486. if(code_seen('U')) grn = code_value();
  1487. if(code_seen('B')) blu = code_value();
  1488. SendColors(red,grn,blu);
  1489. }
  1490. break;
  1491. #endif //BLINKM
  1492. case 201: // M201
  1493. for(int8_t i=0; i < NUM_AXIS; i++)
  1494. {
  1495. if(code_seen(axis_codes[i]))
  1496. {
  1497. max_acceleration_units_per_sq_second[i] = code_value();
  1498. }
  1499. }
  1500. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  1501. reset_acceleration_rates();
  1502. break;
  1503. #if 0 // Not used for Sprinter/grbl gen6
  1504. case 202: // M202
  1505. for(int8_t i=0; i < NUM_AXIS; i++) {
  1506. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1507. }
  1508. break;
  1509. #endif
  1510. case 203: // M203 max feedrate mm/sec
  1511. for(int8_t i=0; i < NUM_AXIS; i++) {
  1512. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1513. }
  1514. break;
  1515. case 204: // M204 acclereration S normal moves T filmanent only moves
  1516. {
  1517. if(code_seen('S')) acceleration = code_value() ;
  1518. if(code_seen('T')) retract_acceleration = code_value() ;
  1519. }
  1520. break;
  1521. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  1522. {
  1523. if(code_seen('S')) minimumfeedrate = code_value();
  1524. if(code_seen('T')) mintravelfeedrate = code_value();
  1525. if(code_seen('B')) minsegmenttime = code_value() ;
  1526. if(code_seen('X')) max_xy_jerk = code_value() ;
  1527. if(code_seen('Z')) max_z_jerk = code_value() ;
  1528. if(code_seen('E')) max_e_jerk = code_value() ;
  1529. }
  1530. break;
  1531. case 206: // M206 additional homeing offset
  1532. for(int8_t i=0; i < 3; i++)
  1533. {
  1534. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1535. }
  1536. break;
  1537. #ifdef FWRETRACT
  1538. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1539. {
  1540. if(code_seen('S'))
  1541. {
  1542. retract_length = code_value() ;
  1543. }
  1544. if(code_seen('F'))
  1545. {
  1546. retract_feedrate = code_value() ;
  1547. }
  1548. if(code_seen('Z'))
  1549. {
  1550. retract_zlift = code_value() ;
  1551. }
  1552. }break;
  1553. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1554. {
  1555. if(code_seen('S'))
  1556. {
  1557. retract_recover_length = code_value() ;
  1558. }
  1559. if(code_seen('F'))
  1560. {
  1561. retract_recover_feedrate = code_value() ;
  1562. }
  1563. }break;
  1564. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  1565. {
  1566. if(code_seen('S'))
  1567. {
  1568. int t= code_value() ;
  1569. switch(t)
  1570. {
  1571. case 0: autoretract_enabled=false;retracted=false;break;
  1572. case 1: autoretract_enabled=true;retracted=false;break;
  1573. default:
  1574. SERIAL_ECHO_START;
  1575. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1576. SERIAL_ECHO(cmdbuffer[bufindr]);
  1577. SERIAL_ECHOLNPGM("\"");
  1578. }
  1579. }
  1580. }break;
  1581. #endif // FWRETRACT
  1582. #if EXTRUDERS > 1
  1583. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  1584. {
  1585. if(setTargetedHotend(218)){
  1586. break;
  1587. }
  1588. if(code_seen('X'))
  1589. {
  1590. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  1591. }
  1592. if(code_seen('Y'))
  1593. {
  1594. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  1595. }
  1596. #ifdef DUAL_X_CARRIAGE
  1597. if(code_seen('Z'))
  1598. {
  1599. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  1600. }
  1601. #endif
  1602. SERIAL_ECHO_START;
  1603. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1604. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  1605. {
  1606. SERIAL_ECHO(" ");
  1607. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  1608. SERIAL_ECHO(",");
  1609. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  1610. #ifdef DUAL_X_CARRIAGE
  1611. SERIAL_ECHO(",");
  1612. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  1613. #endif
  1614. }
  1615. SERIAL_ECHOLN("");
  1616. }break;
  1617. #endif
  1618. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1619. {
  1620. if(code_seen('S'))
  1621. {
  1622. feedmultiply = code_value() ;
  1623. }
  1624. }
  1625. break;
  1626. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1627. {
  1628. if(code_seen('S'))
  1629. {
  1630. extrudemultiply = code_value() ;
  1631. }
  1632. }
  1633. break;
  1634. #if NUM_SERVOS > 0
  1635. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  1636. {
  1637. int servo_index = -1;
  1638. int servo_position = 0;
  1639. if (code_seen('P'))
  1640. servo_index = code_value();
  1641. if (code_seen('S')) {
  1642. servo_position = code_value();
  1643. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  1644. servos[servo_index].write(servo_position);
  1645. }
  1646. else {
  1647. SERIAL_ECHO_START;
  1648. SERIAL_ECHO("Servo ");
  1649. SERIAL_ECHO(servo_index);
  1650. SERIAL_ECHOLN(" out of range");
  1651. }
  1652. }
  1653. else if (servo_index >= 0) {
  1654. SERIAL_PROTOCOL(MSG_OK);
  1655. SERIAL_PROTOCOL(" Servo ");
  1656. SERIAL_PROTOCOL(servo_index);
  1657. SERIAL_PROTOCOL(": ");
  1658. SERIAL_PROTOCOL(servos[servo_index].read());
  1659. SERIAL_PROTOCOLLN("");
  1660. }
  1661. }
  1662. break;
  1663. #endif // NUM_SERVOS > 0
  1664. #if LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) )
  1665. case 300: // M300
  1666. {
  1667. int beepS = code_seen('S') ? code_value() : 110;
  1668. int beepP = code_seen('P') ? code_value() : 1000;
  1669. if (beepS > 0)
  1670. {
  1671. #if BEEPER > 0
  1672. tone(BEEPER, beepS);
  1673. delay(beepP);
  1674. noTone(BEEPER);
  1675. #elif defined(ULTRALCD)
  1676. lcd_buzz(beepS, beepP);
  1677. #endif
  1678. }
  1679. else
  1680. {
  1681. delay(beepP);
  1682. }
  1683. }
  1684. break;
  1685. #endif // M300
  1686. #ifdef PIDTEMP
  1687. case 301: // M301
  1688. {
  1689. if(code_seen('P')) Kp = code_value();
  1690. if(code_seen('I')) Ki = scalePID_i(code_value());
  1691. if(code_seen('D')) Kd = scalePID_d(code_value());
  1692. #ifdef PID_ADD_EXTRUSION_RATE
  1693. if(code_seen('C')) Kc = code_value();
  1694. #endif
  1695. updatePID();
  1696. SERIAL_PROTOCOL(MSG_OK);
  1697. SERIAL_PROTOCOL(" p:");
  1698. SERIAL_PROTOCOL(Kp);
  1699. SERIAL_PROTOCOL(" i:");
  1700. SERIAL_PROTOCOL(unscalePID_i(Ki));
  1701. SERIAL_PROTOCOL(" d:");
  1702. SERIAL_PROTOCOL(unscalePID_d(Kd));
  1703. #ifdef PID_ADD_EXTRUSION_RATE
  1704. SERIAL_PROTOCOL(" c:");
  1705. //Kc does not have scaling applied above, or in resetting defaults
  1706. SERIAL_PROTOCOL(Kc);
  1707. #endif
  1708. SERIAL_PROTOCOLLN("");
  1709. }
  1710. break;
  1711. #endif //PIDTEMP
  1712. #ifdef PIDTEMPBED
  1713. case 304: // M304
  1714. {
  1715. if(code_seen('P')) bedKp = code_value();
  1716. if(code_seen('I')) bedKi = scalePID_i(code_value());
  1717. if(code_seen('D')) bedKd = scalePID_d(code_value());
  1718. updatePID();
  1719. SERIAL_PROTOCOL(MSG_OK);
  1720. SERIAL_PROTOCOL(" p:");
  1721. SERIAL_PROTOCOL(bedKp);
  1722. SERIAL_PROTOCOL(" i:");
  1723. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  1724. SERIAL_PROTOCOL(" d:");
  1725. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  1726. SERIAL_PROTOCOLLN("");
  1727. }
  1728. break;
  1729. #endif //PIDTEMP
  1730. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1731. {
  1732. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  1733. const uint8_t NUM_PULSES=16;
  1734. const float PULSE_LENGTH=0.01524;
  1735. for(int i=0; i < NUM_PULSES; i++) {
  1736. WRITE(PHOTOGRAPH_PIN, HIGH);
  1737. _delay_ms(PULSE_LENGTH);
  1738. WRITE(PHOTOGRAPH_PIN, LOW);
  1739. _delay_ms(PULSE_LENGTH);
  1740. }
  1741. delay(7.33);
  1742. for(int i=0; i < NUM_PULSES; i++) {
  1743. WRITE(PHOTOGRAPH_PIN, HIGH);
  1744. _delay_ms(PULSE_LENGTH);
  1745. WRITE(PHOTOGRAPH_PIN, LOW);
  1746. _delay_ms(PULSE_LENGTH);
  1747. }
  1748. #endif
  1749. }
  1750. break;
  1751. #ifdef DOGLCD
  1752. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  1753. {
  1754. if (code_seen('C')) {
  1755. lcd_setcontrast( ((int)code_value())&63 );
  1756. }
  1757. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  1758. SERIAL_PROTOCOL(lcd_contrast);
  1759. SERIAL_PROTOCOLLN("");
  1760. }
  1761. break;
  1762. #endif
  1763. #ifdef PREVENT_DANGEROUS_EXTRUDE
  1764. case 302: // allow cold extrudes, or set the minimum extrude temperature
  1765. {
  1766. float temp = .0;
  1767. if (code_seen('S')) temp=code_value();
  1768. set_extrude_min_temp(temp);
  1769. }
  1770. break;
  1771. #endif
  1772. case 303: // M303 PID autotune
  1773. {
  1774. float temp = 150.0;
  1775. int e=0;
  1776. int c=5;
  1777. if (code_seen('E')) e=code_value();
  1778. if (e<0)
  1779. temp=70;
  1780. if (code_seen('S')) temp=code_value();
  1781. if (code_seen('C')) c=code_value();
  1782. PID_autotune(temp, e, c);
  1783. }
  1784. break;
  1785. case 400: // M400 finish all moves
  1786. {
  1787. st_synchronize();
  1788. }
  1789. break;
  1790. case 500: // M500 Store settings in EEPROM
  1791. {
  1792. Config_StoreSettings();
  1793. }
  1794. break;
  1795. case 501: // M501 Read settings from EEPROM
  1796. {
  1797. Config_RetrieveSettings();
  1798. }
  1799. break;
  1800. case 502: // M502 Revert to default settings
  1801. {
  1802. Config_ResetDefault();
  1803. }
  1804. break;
  1805. case 503: // M503 print settings currently in memory
  1806. {
  1807. Config_PrintSettings();
  1808. }
  1809. break;
  1810. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  1811. case 540:
  1812. {
  1813. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  1814. }
  1815. break;
  1816. #endif
  1817. #ifdef FILAMENTCHANGEENABLE
  1818. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  1819. {
  1820. float target[4];
  1821. float lastpos[4];
  1822. target[X_AXIS]=current_position[X_AXIS];
  1823. target[Y_AXIS]=current_position[Y_AXIS];
  1824. target[Z_AXIS]=current_position[Z_AXIS];
  1825. target[E_AXIS]=current_position[E_AXIS];
  1826. lastpos[X_AXIS]=current_position[X_AXIS];
  1827. lastpos[Y_AXIS]=current_position[Y_AXIS];
  1828. lastpos[Z_AXIS]=current_position[Z_AXIS];
  1829. lastpos[E_AXIS]=current_position[E_AXIS];
  1830. //retract by E
  1831. if(code_seen('E'))
  1832. {
  1833. target[E_AXIS]+= code_value();
  1834. }
  1835. else
  1836. {
  1837. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  1838. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  1839. #endif
  1840. }
  1841. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1842. //lift Z
  1843. if(code_seen('Z'))
  1844. {
  1845. target[Z_AXIS]+= code_value();
  1846. }
  1847. else
  1848. {
  1849. #ifdef FILAMENTCHANGE_ZADD
  1850. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  1851. #endif
  1852. }
  1853. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1854. //move xy
  1855. if(code_seen('X'))
  1856. {
  1857. target[X_AXIS]+= code_value();
  1858. }
  1859. else
  1860. {
  1861. #ifdef FILAMENTCHANGE_XPOS
  1862. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  1863. #endif
  1864. }
  1865. if(code_seen('Y'))
  1866. {
  1867. target[Y_AXIS]= code_value();
  1868. }
  1869. else
  1870. {
  1871. #ifdef FILAMENTCHANGE_YPOS
  1872. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  1873. #endif
  1874. }
  1875. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1876. if(code_seen('L'))
  1877. {
  1878. target[E_AXIS]+= code_value();
  1879. }
  1880. else
  1881. {
  1882. #ifdef FILAMENTCHANGE_FINALRETRACT
  1883. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  1884. #endif
  1885. }
  1886. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1887. //finish moves
  1888. st_synchronize();
  1889. //disable extruder steppers so filament can be removed
  1890. disable_e0();
  1891. disable_e1();
  1892. disable_e2();
  1893. delay(100);
  1894. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  1895. uint8_t cnt=0;
  1896. while(!lcd_clicked()){
  1897. cnt++;
  1898. manage_heater();
  1899. manage_inactivity();
  1900. lcd_update();
  1901. if(cnt==0)
  1902. {
  1903. #if BEEPER > 0
  1904. SET_OUTPUT(BEEPER);
  1905. WRITE(BEEPER,HIGH);
  1906. delay(3);
  1907. WRITE(BEEPER,LOW);
  1908. delay(3);
  1909. #else
  1910. lcd_buzz(1000/6,100);
  1911. #endif
  1912. }
  1913. }
  1914. //return to normal
  1915. if(code_seen('L'))
  1916. {
  1917. target[E_AXIS]+= -code_value();
  1918. }
  1919. else
  1920. {
  1921. #ifdef FILAMENTCHANGE_FINALRETRACT
  1922. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  1923. #endif
  1924. }
  1925. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  1926. plan_set_e_position(current_position[E_AXIS]);
  1927. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //should do nothing
  1928. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move xy back
  1929. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move z back
  1930. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], feedrate/60, active_extruder); //final untretract
  1931. }
  1932. break;
  1933. #endif //FILAMENTCHANGEENABLE
  1934. #ifdef DUAL_X_CARRIAGE
  1935. case 605: // Set dual x-carriage movement mode:
  1936. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  1937. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  1938. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  1939. // millimeters x-offset and an optional differential hotend temperature of
  1940. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  1941. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  1942. //
  1943. // Note: the X axis should be homed after changing dual x-carriage mode.
  1944. {
  1945. st_synchronize();
  1946. if (code_seen('S'))
  1947. dual_x_carriage_mode = code_value();
  1948. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  1949. {
  1950. if (code_seen('X'))
  1951. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  1952. if (code_seen('R'))
  1953. duplicate_extruder_temp_offset = code_value();
  1954. SERIAL_ECHO_START;
  1955. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1956. SERIAL_ECHO(" ");
  1957. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  1958. SERIAL_ECHO(",");
  1959. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  1960. SERIAL_ECHO(" ");
  1961. SERIAL_ECHO(duplicate_extruder_x_offset);
  1962. SERIAL_ECHO(",");
  1963. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  1964. }
  1965. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  1966. {
  1967. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  1968. }
  1969. active_extruder_parked = false;
  1970. extruder_duplication_enabled = false;
  1971. delayed_move_time = 0;
  1972. }
  1973. break;
  1974. #endif //DUAL_X_CARRIAGE
  1975. case 907: // M907 Set digital trimpot motor current using axis codes.
  1976. {
  1977. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1978. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  1979. if(code_seen('B')) digipot_current(4,code_value());
  1980. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  1981. #endif
  1982. }
  1983. break;
  1984. case 908: // M908 Control digital trimpot directly.
  1985. {
  1986. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1987. uint8_t channel,current;
  1988. if(code_seen('P')) channel=code_value();
  1989. if(code_seen('S')) current=code_value();
  1990. digitalPotWrite(channel, current);
  1991. #endif
  1992. }
  1993. break;
  1994. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  1995. {
  1996. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1997. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  1998. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  1999. if(code_seen('B')) microstep_mode(4,code_value());
  2000. microstep_readings();
  2001. #endif
  2002. }
  2003. break;
  2004. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  2005. {
  2006. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  2007. if(code_seen('S')) switch((int)code_value())
  2008. {
  2009. case 1:
  2010. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  2011. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  2012. break;
  2013. case 2:
  2014. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  2015. if(code_seen('B')) microstep_ms(4,-1,code_value());
  2016. break;
  2017. }
  2018. microstep_readings();
  2019. #endif
  2020. }
  2021. break;
  2022. case 999: // M999: Restart after being stopped
  2023. Stopped = false;
  2024. lcd_reset_alert_level();
  2025. gcode_LastN = Stopped_gcode_LastN;
  2026. FlushSerialRequestResend();
  2027. break;
  2028. }
  2029. }
  2030. else if(code_seen('T'))
  2031. {
  2032. tmp_extruder = code_value();
  2033. if(tmp_extruder >= EXTRUDERS) {
  2034. SERIAL_ECHO_START;
  2035. SERIAL_ECHO("T");
  2036. SERIAL_ECHO(tmp_extruder);
  2037. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  2038. }
  2039. else {
  2040. boolean make_move = false;
  2041. if(code_seen('F')) {
  2042. make_move = true;
  2043. next_feedrate = code_value();
  2044. if(next_feedrate > 0.0) {
  2045. feedrate = next_feedrate;
  2046. }
  2047. }
  2048. #if EXTRUDERS > 1
  2049. if(tmp_extruder != active_extruder) {
  2050. // Save current position to return to after applying extruder offset
  2051. memcpy(destination, current_position, sizeof(destination));
  2052. #ifdef DUAL_X_CARRIAGE
  2053. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  2054. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  2055. {
  2056. // Park old head: 1) raise 2) move to park position 3) lower
  2057. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2058. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2059. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2060. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  2061. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  2062. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2063. st_synchronize();
  2064. }
  2065. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  2066. current_position[Y_AXIS] = current_position[Y_AXIS] -
  2067. extruder_offset[Y_AXIS][active_extruder] +
  2068. extruder_offset[Y_AXIS][tmp_extruder];
  2069. current_position[Z_AXIS] = current_position[Z_AXIS] -
  2070. extruder_offset[Z_AXIS][active_extruder] +
  2071. extruder_offset[Z_AXIS][tmp_extruder];
  2072. active_extruder = tmp_extruder;
  2073. // This function resets the max/min values - the current position may be overwritten below.
  2074. axis_is_at_home(X_AXIS);
  2075. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  2076. {
  2077. current_position[X_AXIS] = inactive_extruder_x_pos;
  2078. inactive_extruder_x_pos = destination[X_AXIS];
  2079. }
  2080. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  2081. {
  2082. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  2083. if (active_extruder == 0 || active_extruder_parked)
  2084. current_position[X_AXIS] = inactive_extruder_x_pos;
  2085. else
  2086. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  2087. inactive_extruder_x_pos = destination[X_AXIS];
  2088. extruder_duplication_enabled = false;
  2089. }
  2090. else
  2091. {
  2092. // record raised toolhead position for use by unpark
  2093. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  2094. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  2095. active_extruder_parked = true;
  2096. delayed_move_time = 0;
  2097. }
  2098. #else
  2099. // Offset extruder (only by XY)
  2100. int i;
  2101. for(i = 0; i < 2; i++) {
  2102. current_position[i] = current_position[i] -
  2103. extruder_offset[i][active_extruder] +
  2104. extruder_offset[i][tmp_extruder];
  2105. }
  2106. // Set the new active extruder and position
  2107. active_extruder = tmp_extruder;
  2108. #endif //else DUAL_X_CARRIAGE
  2109. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2110. // Move to the old position if 'F' was in the parameters
  2111. if(make_move && Stopped == false) {
  2112. prepare_move();
  2113. }
  2114. }
  2115. #endif
  2116. SERIAL_ECHO_START;
  2117. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  2118. SERIAL_PROTOCOLLN((int)active_extruder);
  2119. }
  2120. }
  2121. else
  2122. {
  2123. SERIAL_ECHO_START;
  2124. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  2125. SERIAL_ECHO(cmdbuffer[bufindr]);
  2126. SERIAL_ECHOLNPGM("\"");
  2127. }
  2128. ClearToSend();
  2129. }
  2130. void FlushSerialRequestResend()
  2131. {
  2132. //char cmdbuffer[bufindr][100]="Resend:";
  2133. MYSERIAL.flush();
  2134. SERIAL_PROTOCOLPGM(MSG_RESEND);
  2135. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  2136. ClearToSend();
  2137. }
  2138. void ClearToSend()
  2139. {
  2140. previous_millis_cmd = millis();
  2141. #ifdef SDSUPPORT
  2142. if(fromsd[bufindr])
  2143. return;
  2144. #endif //SDSUPPORT
  2145. SERIAL_PROTOCOLLNPGM(MSG_OK);
  2146. }
  2147. void get_coordinates()
  2148. {
  2149. bool seen[4]={false,false,false,false};
  2150. for(int8_t i=0; i < NUM_AXIS; i++) {
  2151. if(code_seen(axis_codes[i]))
  2152. {
  2153. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  2154. seen[i]=true;
  2155. }
  2156. else destination[i] = current_position[i]; //Are these else lines really needed?
  2157. }
  2158. if(code_seen('F')) {
  2159. next_feedrate = code_value();
  2160. if(next_feedrate > 0.0) feedrate = next_feedrate;
  2161. }
  2162. #ifdef FWRETRACT
  2163. if(autoretract_enabled)
  2164. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  2165. {
  2166. float echange=destination[E_AXIS]-current_position[E_AXIS];
  2167. if(echange<-MIN_RETRACT) //retract
  2168. {
  2169. if(!retracted)
  2170. {
  2171. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  2172. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  2173. float correctede=-echange-retract_length;
  2174. //to generate the additional steps, not the destination is changed, but inversely the current position
  2175. current_position[E_AXIS]+=-correctede;
  2176. feedrate=retract_feedrate;
  2177. retracted=true;
  2178. }
  2179. }
  2180. else
  2181. if(echange>MIN_RETRACT) //retract_recover
  2182. {
  2183. if(retracted)
  2184. {
  2185. //current_position[Z_AXIS]+=-retract_zlift;
  2186. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  2187. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  2188. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  2189. feedrate=retract_recover_feedrate;
  2190. retracted=false;
  2191. }
  2192. }
  2193. }
  2194. #endif //FWRETRACT
  2195. }
  2196. void get_arc_coordinates()
  2197. {
  2198. #ifdef SF_ARC_FIX
  2199. bool relative_mode_backup = relative_mode;
  2200. relative_mode = true;
  2201. #endif
  2202. get_coordinates();
  2203. #ifdef SF_ARC_FIX
  2204. relative_mode=relative_mode_backup;
  2205. #endif
  2206. if(code_seen('I')) {
  2207. offset[0] = code_value();
  2208. }
  2209. else {
  2210. offset[0] = 0.0;
  2211. }
  2212. if(code_seen('J')) {
  2213. offset[1] = code_value();
  2214. }
  2215. else {
  2216. offset[1] = 0.0;
  2217. }
  2218. }
  2219. void clamp_to_software_endstops(float target[3])
  2220. {
  2221. if (min_software_endstops) {
  2222. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  2223. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  2224. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  2225. }
  2226. if (max_software_endstops) {
  2227. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  2228. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  2229. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  2230. }
  2231. }
  2232. #ifdef DELTA
  2233. void calculate_delta(float cartesian[3])
  2234. {
  2235. delta[X_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2236. - sq(DELTA_TOWER1_X-cartesian[X_AXIS])
  2237. - sq(DELTA_TOWER1_Y-cartesian[Y_AXIS])
  2238. ) + cartesian[Z_AXIS];
  2239. delta[Y_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2240. - sq(DELTA_TOWER2_X-cartesian[X_AXIS])
  2241. - sq(DELTA_TOWER2_Y-cartesian[Y_AXIS])
  2242. ) + cartesian[Z_AXIS];
  2243. delta[Z_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2244. - sq(DELTA_TOWER3_X-cartesian[X_AXIS])
  2245. - sq(DELTA_TOWER3_Y-cartesian[Y_AXIS])
  2246. ) + cartesian[Z_AXIS];
  2247. /*
  2248. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  2249. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  2250. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  2251. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  2252. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  2253. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  2254. */
  2255. }
  2256. #endif
  2257. void prepare_move()
  2258. {
  2259. clamp_to_software_endstops(destination);
  2260. previous_millis_cmd = millis();
  2261. #ifdef DELTA
  2262. float difference[NUM_AXIS];
  2263. for (int8_t i=0; i < NUM_AXIS; i++) {
  2264. difference[i] = destination[i] - current_position[i];
  2265. }
  2266. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  2267. sq(difference[Y_AXIS]) +
  2268. sq(difference[Z_AXIS]));
  2269. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  2270. if (cartesian_mm < 0.000001) { return; }
  2271. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  2272. int steps = max(1, int(DELTA_SEGMENTS_PER_SECOND * seconds));
  2273. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  2274. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  2275. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  2276. for (int s = 1; s <= steps; s++) {
  2277. float fraction = float(s) / float(steps);
  2278. for(int8_t i=0; i < NUM_AXIS; i++) {
  2279. destination[i] = current_position[i] + difference[i] * fraction;
  2280. }
  2281. calculate_delta(destination);
  2282. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  2283. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  2284. active_extruder);
  2285. }
  2286. #else
  2287. #ifdef DUAL_X_CARRIAGE
  2288. if (active_extruder_parked)
  2289. {
  2290. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  2291. {
  2292. // move duplicate extruder into correct duplication position.
  2293. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2294. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  2295. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  2296. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2297. st_synchronize();
  2298. extruder_duplication_enabled = true;
  2299. active_extruder_parked = false;
  2300. }
  2301. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  2302. {
  2303. if (current_position[E_AXIS] == destination[E_AXIS])
  2304. {
  2305. // this is a travel move - skit it but keep track of current position (so that it can later
  2306. // be used as start of first non-travel move)
  2307. if (delayed_move_time != 0xFFFFFFFFUL)
  2308. {
  2309. memcpy(current_position, destination, sizeof(current_position));
  2310. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  2311. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  2312. delayed_move_time = millis();
  2313. return;
  2314. }
  2315. }
  2316. delayed_move_time = 0;
  2317. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  2318. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2319. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  2320. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  2321. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2322. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2323. active_extruder_parked = false;
  2324. }
  2325. }
  2326. #endif //DUAL_X_CARRIAGE
  2327. // Do not use feedmultiply for E or Z only moves
  2328. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  2329. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2330. }
  2331. else {
  2332. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  2333. }
  2334. #endif //else DELTA
  2335. for(int8_t i=0; i < NUM_AXIS; i++) {
  2336. current_position[i] = destination[i];
  2337. }
  2338. }
  2339. void prepare_arc_move(char isclockwise) {
  2340. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  2341. // Trace the arc
  2342. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  2343. // As far as the parser is concerned, the position is now == target. In reality the
  2344. // motion control system might still be processing the action and the real tool position
  2345. // in any intermediate location.
  2346. for(int8_t i=0; i < NUM_AXIS; i++) {
  2347. current_position[i] = destination[i];
  2348. }
  2349. previous_millis_cmd = millis();
  2350. }
  2351. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2352. #if defined(FAN_PIN)
  2353. #if CONTROLLERFAN_PIN == FAN_PIN
  2354. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  2355. #endif
  2356. #endif
  2357. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  2358. unsigned long lastMotorCheck = 0;
  2359. void controllerFan()
  2360. {
  2361. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  2362. {
  2363. lastMotorCheck = millis();
  2364. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  2365. #if EXTRUDERS > 2
  2366. || !READ(E2_ENABLE_PIN)
  2367. #endif
  2368. #if EXTRUDER > 1
  2369. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  2370. || !READ(X2_ENABLE_PIN)
  2371. #endif
  2372. || !READ(E1_ENABLE_PIN)
  2373. #endif
  2374. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  2375. {
  2376. lastMotor = millis(); //... set time to NOW so the fan will turn on
  2377. }
  2378. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  2379. {
  2380. digitalWrite(CONTROLLERFAN_PIN, 0);
  2381. analogWrite(CONTROLLERFAN_PIN, 0);
  2382. }
  2383. else
  2384. {
  2385. // allows digital or PWM fan output to be used (see M42 handling)
  2386. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2387. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2388. }
  2389. }
  2390. }
  2391. #endif
  2392. void manage_inactivity()
  2393. {
  2394. if( (millis() - previous_millis_cmd) > max_inactive_time )
  2395. if(max_inactive_time)
  2396. kill();
  2397. if(stepper_inactive_time) {
  2398. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  2399. {
  2400. if(blocks_queued() == false) {
  2401. disable_x();
  2402. disable_y();
  2403. disable_z();
  2404. disable_e0();
  2405. disable_e1();
  2406. disable_e2();
  2407. }
  2408. }
  2409. }
  2410. #if defined(KILL_PIN) && KILL_PIN > -1
  2411. if( 0 == READ(KILL_PIN) )
  2412. kill();
  2413. #endif
  2414. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2415. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  2416. #endif
  2417. #ifdef EXTRUDER_RUNOUT_PREVENT
  2418. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  2419. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  2420. {
  2421. bool oldstatus=READ(E0_ENABLE_PIN);
  2422. enable_e0();
  2423. float oldepos=current_position[E_AXIS];
  2424. float oldedes=destination[E_AXIS];
  2425. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2426. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  2427. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  2428. current_position[E_AXIS]=oldepos;
  2429. destination[E_AXIS]=oldedes;
  2430. plan_set_e_position(oldepos);
  2431. previous_millis_cmd=millis();
  2432. st_synchronize();
  2433. WRITE(E0_ENABLE_PIN,oldstatus);
  2434. }
  2435. #endif
  2436. #if defined(DUAL_X_CARRIAGE)
  2437. // handle delayed move timeout
  2438. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  2439. {
  2440. // travel moves have been received so enact them
  2441. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  2442. memcpy(destination,current_position,sizeof(destination));
  2443. prepare_move();
  2444. }
  2445. #endif
  2446. check_axes_activity();
  2447. }
  2448. void kill()
  2449. {
  2450. cli(); // Stop interrupts
  2451. disable_heater();
  2452. disable_x();
  2453. disable_y();
  2454. disable_z();
  2455. disable_e0();
  2456. disable_e1();
  2457. disable_e2();
  2458. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2459. pinMode(PS_ON_PIN,INPUT);
  2460. #endif
  2461. SERIAL_ERROR_START;
  2462. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  2463. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  2464. suicide();
  2465. while(1) { /* Intentionally left empty */ } // Wait for reset
  2466. }
  2467. void Stop()
  2468. {
  2469. disable_heater();
  2470. if(Stopped == false) {
  2471. Stopped = true;
  2472. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  2473. SERIAL_ERROR_START;
  2474. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  2475. LCD_MESSAGEPGM(MSG_STOPPED);
  2476. }
  2477. }
  2478. bool IsStopped() { return Stopped; };
  2479. #ifdef FAST_PWM_FAN
  2480. void setPwmFrequency(uint8_t pin, int val)
  2481. {
  2482. val &= 0x07;
  2483. switch(digitalPinToTimer(pin))
  2484. {
  2485. #if defined(TCCR0A)
  2486. case TIMER0A:
  2487. case TIMER0B:
  2488. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  2489. // TCCR0B |= val;
  2490. break;
  2491. #endif
  2492. #if defined(TCCR1A)
  2493. case TIMER1A:
  2494. case TIMER1B:
  2495. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2496. // TCCR1B |= val;
  2497. break;
  2498. #endif
  2499. #if defined(TCCR2)
  2500. case TIMER2:
  2501. case TIMER2:
  2502. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2503. TCCR2 |= val;
  2504. break;
  2505. #endif
  2506. #if defined(TCCR2A)
  2507. case TIMER2A:
  2508. case TIMER2B:
  2509. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  2510. TCCR2B |= val;
  2511. break;
  2512. #endif
  2513. #if defined(TCCR3A)
  2514. case TIMER3A:
  2515. case TIMER3B:
  2516. case TIMER3C:
  2517. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  2518. TCCR3B |= val;
  2519. break;
  2520. #endif
  2521. #if defined(TCCR4A)
  2522. case TIMER4A:
  2523. case TIMER4B:
  2524. case TIMER4C:
  2525. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  2526. TCCR4B |= val;
  2527. break;
  2528. #endif
  2529. #if defined(TCCR5A)
  2530. case TIMER5A:
  2531. case TIMER5B:
  2532. case TIMER5C:
  2533. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  2534. TCCR5B |= val;
  2535. break;
  2536. #endif
  2537. }
  2538. }
  2539. #endif //FAST_PWM_FAN
  2540. bool setTargetedHotend(int code){
  2541. tmp_extruder = active_extruder;
  2542. if(code_seen('T')) {
  2543. tmp_extruder = code_value();
  2544. if(tmp_extruder >= EXTRUDERS) {
  2545. SERIAL_ECHO_START;
  2546. switch(code){
  2547. case 104:
  2548. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  2549. break;
  2550. case 105:
  2551. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  2552. break;
  2553. case 109:
  2554. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  2555. break;
  2556. case 218:
  2557. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  2558. break;
  2559. }
  2560. SERIAL_ECHOLN(tmp_extruder);
  2561. return true;
  2562. }
  2563. }
  2564. return false;
  2565. }