CNC G-Code Help

This page will try and help with the learning of the common CNC programming language called G-Code including some of the functions and some basic routes you can program.
I am using Mach3 CNC controller to debug all of my code but it should be standard across all systems.

Understand your machine

The first things you need to understand, is about the co-ordinates of your specific system, where does the machine home to, where does it consider to be zero, can you reset your system to consider that point the origin.

There are things called fixtures that you can program your software to remember (outside of the code itself) and program to use as a point reference in your code, or if you have another non-zero point reference that you would rather you instead of home. But these are not really needed from what I have seen so far. To use fixture offsets, use the codes G54 to G59, these need to be configured manually in one of the software menus.

The home of the CNC machine can be found by using the function G28 or G30 which will return it to origin.

Axis polarities: what does your machine consider to be the positive polarity of the axis i.e. does it move left, right, up, down when you program a positive increase in say its x axis.

Configuration of Software

There are modes to set up the way your software interprets your commands these include

Units
Select either millimeters or inches with the commands
  • G70 for inches.
  • G71 for millimeters.

Absolute or Incremental Co-ordinates
Select whether to use the origin for the reference co-ordinates or your current position. 
  • G90 for absolute co-ordinates.
  • G91 for incremental co-ordinates.

Path Control Modes
This mode selects whether or not you want the CNC tool to keep its vector velocity when it changes direction i.e. it will round off any harsh changes in the tool direction.
If you want the tool path to be exactly as you programmed it then set the software to exact stop mode which will make sure the armature finishes the specific vector before it moves onto the next one.
  • G61 for exact stop mode.
  • G64 for constant velocity mode.

Axis Scaling
This will add an multiplication factor to your input co-ordinates, used if your axis have differing sensitivity.
  • G51 is the Command.
  • Example G51 X0.1 Y1 Z1 (divides your input commands of the X axis by 10).

Programming for Arcs and Circles
There are setting to optimise the machine for arcs and circle type functions for instance, most of the curve functions operate in 2 axis therefore you need to specify what plane you are working on. keep in mind you can still do helix type movements, it is just that it is impossible to two 3 axis non-linear movement functions.
The plane you want you use can be programmed using:
  • G17 to select the XY plane.
  • G18 to select the XZ plane.
  • G19 to select the YZ plane. 
Polar Co-ordinates
To set your machine to polar co-ordinates use G16 when the machine is sat at this position.
Keep in mind you machine will not move in arcs it just uses an alternative co-ordinate system.
Program G15 to exit polar co-ordinate mode.
Setting IJ mode
IJ type controls are meant for arc and circular type tool vectors and they use the variables I and J to determine where the center point of the arc or circle is. There are two modes analogous to the co-ordinate mode settings G90/G91, the I and J variables also have absolute or incremental modes to set them simply use.
  • G90.1 to select absolute IJ mode
  • G91.1 to select incremental IJ mode 
 

General motion controls


G0/G1
These are your go to commands for basic straight line motion, there isn't much difference in the two apart from G0 uses the traverse rate for its speed and G1 uses the feed rate to control speed.

The syntax could look like: G1 X10 Y15 Z20 F100
You don't have to include all the axis controls as they will be assumed to be zero movement if excluded.

F = Feed-rate
Feed-rate depends on context of use but always relates in someway to the speed of an action.
In the context above it relates to linear speed in units (mm or inches) per minute.

Dwell G4
This is quite a straight forward function it makes the machine wait for a specified amount of time.
The syntax of which is: G4 P100, where P is the time in units which are setup on one of the computer software menus (milliseconds, seconds etc).

Circle G2/G3
The CNC software is capable of circular/helical motion both clockwise (G2) or anti-clockwise (G3) on a specific plane (G17, G18 or G19), the center of which is specified by the IJ co-ordinates and their values depend on if you are in absolute IJ mode or incremental IJ mode.

Subroutines

Subroutines are quite straight forward in G-code they are called using the command M98 followed by a label so the full syntax would be.
  • M98 P01 (01 is an an example number but this could be any number)
Where P represents the subroutine number label, to mark a subroutine you need to use the syntax.
  • O01 to mark the start of the subroutine. (place this code below the end of the main code).
  • Any G-code you want to run in your subroutine.
  • M99 to tell the software to return to the point in the main code where the subroutine was called.
So the 'O' and M99 functions mark the start and end of the subroutine.

Useful 'M' code

  • M0 stops the program until you start it again manually.
  • M30 stops the program, rewinds it to the top of the code and waits to be started manually.
  • M98 marks the start of a subroutine in the main code and marks where to return to when it is finished.
  • M99 marks the end of the subroutine and commands the program to return to the main code.