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 79KB

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