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

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