Welcome to G-code 101
Understanding G-code is an important machine operator skill for shops looking to increase the output and profitability of their CNC routers. It’s often a shortcut to the more time-consuming task of CAD drawing and CAM programming.
G-code is the language used to send instructions to computer-controlled machines to make something. G-code stands for geometric code and operates in the Cartesian coordinate system (the work envelope of a machine), which is generally defined by X, Y and Z coordinates.
G-code sends step-and-direction instructions that tell the CNC machine’s spindle and bit a given direction and distance to move from the machine’s home position (,0,0,0) when cutting parts. It is essentially the same process a woodworker uses to manually process panels. The difference is telling a machine to do it with explicit commands.
Rather than using a CAD drawing for every object that needs to be cut out of sheets goods, it is much faster to write the process in G-code, plus any changes or corrections can be made right at the CNC machine in minutes instead of hours needed when the program has to start again with a CAD drawing.
Breaking it down
A line of G-code is commonly called a block. A block contains words, which are letters in the alphabet (A to Z). Following the words is a string of numbers called an address. The words and address are the instructions for the CNC machine’s controller.
An example of a block of G-code looks like this:
N## G## X## Y## Z## F## S## T## M##
(N represents the Line Number; G – Machine Motion; X – Horizontal Position; Y – Vertical Position; Z – Depth; F – Feed Rate; S – Spindle Speed; T – Tool Selection; M – Miscellaneous Functions)
A block of G-code and its words have a syntax which is the set of rules, principles and processes that govern the structures of the blocks, words, and addresses, just like any other language. Different controllers have unique dialects or uses of words and the instructions that follow.
There are times that multiples of the same part need to be cut. This can be done with a CAD/CAM program to generate the toolpaths and G-code. But a faster and more profitable method is to write a subprogram, which is part of the main program.
G-codes are available on any CNC machine controller to use subprograms which will make the programming process easier, more efficient and less prone to errors.
And once a main program has been written using subprograms for one purpose, it can be used again. Eventually, a shop will have a library of main and subprograms to choose from.
Functions to know
Subprogram use the functions M97, M98 and M99. The M97 function uses a subprogram that is within the main program (local subprogram), while the M98 function calls a subprogram that is stored in the controller. The M99 function ends the subprogram and transfers the processing back to the main program.
By adding work offset calls (G54-G59) and changing the movement mode from absolute movement mode (G90) to incremental movement mode (G91) throughout the main program and subprograms, the capacity of the M97 and M99 calls for subprograms can be made even more powerful.
G90 instructs the CNC router to go into absolute distance mode, where axis numbers (X, Y, Z) usually represent positions in terms of the currently active coordinate system or work offset. Typically 0,0,0, though there are exceptions.
G91 instructs the CNC router to go into incremental distance mode, where X, Y and Z usually represent incremental moves from the last position of the cutterhead. For example, an instruction such as X5, Y5, tells the spindle to move 5” from its present X- and Y-axis locations.
G54 through G59 and G59 P1-254 are what are known as work offsets. G54 is most often associated with the machine’s usual home position (0,0,0). G58 through G59 can be set to any position within the X-Y-Z work envelope.
This article originally appeared in the November 2019 issue.