FAQ about G-Code and M-Code


 


Weird behavior with G-Code

 



What is G-Code?

G-Code is the industry standard programming language used for CNC equipment. It is stored in a text file, and run line by line.


Here is a good article by MachMotion about Mach3 and Mach4's use of G-Codes.  Consider it required reading.

Here is the Wikipedia article, about G-Code and M-Code. Not all information in this Wikipedia article is implemented Mach3 or Mach4.


A simple command that you can type into the input line of the MDI screen is "F10 G1 X1"

  • With the F10 command we are manually defining the feedrate to be 10 inches per minute (or mm per minute if your system is set to mm).  A value of 20 would advance the tool twice as fast.  [If this was a rotational axis, the feedrate would be inches per revolution or mm per rev.]
  • G1 will command a linear move, or a straight line move for the current position to the end position.  We are only specifying X1, which would be 1 inch (or mm) on the X axis, and the other axes would maintain their current position.  We could also specify the other axes to have positions also by doing this: "F10 G1 X1.0 Y0.5 Z2.325" The allowable axes are X, Y, Z, A, B and C.

 

MDI



What is M-Code?  (Plus M11/M10 and M62/M63)

M-Code is similar to G-Code, other than it allows you to run a Macro.   Macros allow you to create logic that is specific to your hardware and system.  However, Macros have a delay associated with them that can be around 500ms.  For most milling operations this delay will not be an issue.  However, for laser systems of plasma cutting systems this extra 500 ms of on time can cause burn through, excess cutting, or continued cutting in areas where the laser should be off.    

Here is a good article by MachMotion about Mach3 and Mach4's use of M-Codes.

Here is the Wikipedia article, about G-Code and M-Code. Not all information in this Wikipedia article is implemented Mach3 or Mach4.

There are two special real time digital output control Macros in Mach3 which are M10 and M11.  In Mach4 these two special real time digital output control Macros are M62 and M63.  We commonly use these for laser and THC control for on/off commands without the delay of other macros.  The M10/M11 and M62/M63 Macros will be loaded and then executed at the beginning of the next motion command (so call a G0 or G1 motion command right away after your M10/M11/M62/M63 command).   If the motion command doesn't cause the head to move, there will likely be a 500ms delay instead of a real time ON or OFF.

The commands are as follows, with x referring to digital output 1, 2, 3, 4, 5 or 6.

  • M10Px      Turns Output x OFF with subsequent motion command.  Used in Mach3.  (Logically backwards.)
  • M11Px      Turns Output x ON with subsequent motion command.  Used in Mach3.  (Logically backwards.)
  • M62Px      Turns Output x ON with subsequent motion command.  Used in Mach4.
  • M63Px      Turns Output x OFF with subsequent motion command.  Used in Mach4.

To set this up in Mach3, go to Mach3's Menu -> Plugin Control -> Main Config ESS-v*****

  • Check the box for Output Mode
  • Assign the correct output number
  • Make sure your G-Code includes  M11P3 or M10P3 (the 3 indicates output #3) commands followed by a move command, like this Mach3 Example or this Mach4 Example.

 

M11 and M10 outputs

 



Where can I learn more about General G-Code commands?

A really good course/tutorial is here, by CNC Cookbook.  This is the intro page, and at the bottom is a table of contents.  There is a lot to read and learn!

 



What can I do to prevent mistakes that waste material?

There are multiple things you can do to prevent mistakes that waste material:

  1. Air cut!  Run your machine without a tool, or without material.  You will be 'cutting air' but this will help you become comfortable with your equipment.  Another option it to use a marker instead of a bit, and watch the shape that it traces.
  2. Look at your tool path and make sure it looks correct.  If things don't look right in the tool path, they probably won't look right after cutting.
  3. Understand the setting in your CAD/CAM processor (Computer Aided Design / Manufacturing software).  There are many options and settings that will impact your file creation.
  4. Set up each of your jobs consistently.  That means that the start of EACH AND EVERY G-Code file should include these commands:
    • G20 to use inches (imperial mode) or G21 for mm (metric mode) for length units. 
    • G90 is used for absolute distance mode (coordinates represent absolute positions relative to the origin (0,0,0).    G91 is used for incremental distance mode, or how far you should move from the current position.
    • Home your machine each and every time you start it, and every time after your press the EStop button.  Also home any time you think you may have lost your current position.
    • If you need to pause your machine, press the Feed hold button, and the the Stop button in Mach.  This will preserve your current position.

 


 

 

How can I turn Outputs On and Off with Macros?

Please watch this video, which shows you how to do this in Mach3. Here are the files I used in the video:

 

 

 

 


 


Mach3 script modification for minimum spindle RPM

If you go into the Mach3 VB Script editor, and change your "spindlespeed.m1s" macro file, you can make sure that your Spindle will never run below a minimum RPM that you want it to run at. 

Make sure that you are editing the correct Spindle Speed macro located in "C:\Mach3\Macros\YOUR PROFILE NAME\" since it will only affect that profile.


b1.png



b2.png




If you edit it so it looks like this, it will not let you command a speed less than 6000 RPM.  Use whatever value suits your needs.


rpm = GetRPM()

If rpm < 1 Then

rpm = 0

ElseIf rpm < 6000 Then

rpm = 6000

End If

SetSpinSpeed( rpm )

 

 

I added in the value of less than 1 shutting the spindle off, in case there ever was some rounding error wanting the spindle to turn at 0.0001 or something like that.

 



I keep getting weird (crop) circles instead of arcs

This is likely due to your IJ mode being in Absolute instead of Inc.   Go to Mach3 -> Menu -> Config -> General Config...

IJ Mode

 

 

This will explain the following G-Codes in depth:

G90 is used for absolute distance mode (coordinates represent absolute positions relative to the origin (0,0,0).    G91 is used for incremental distance mode, or how far you should move from the current position.  (BLUE arrow in the picture.) 

G90.1 is for Absolute Arc (IJK) mode.  G91.1 is used for Incremental Arc (IJK) mode.  (GREEN arrow in the picture.)   These are similar, but represent the motion moves differently.  Which one you will be working with depends on how your CAD/CAM processor is setup.

Your G2 CW Arc commands (Clockwise circular/helical interpolation)  and G3 CCW Arc commands (Counterclockwise circular/Helical interpolation) will be generated by your CAM software in regards to the expected modes (G90/G91 and G90.1 and G91.1).


Here is a good article explaining the basics of circular arcs.

 

 

 

Go to top