Mastering VMC G-Code Programming
Program Structure and Coordinates
The Blueprint of a Program
A G-code program is a text file, a set of instructions a machine reads from top to bottom. It always starts and ends with a percent sign (%). Think of it as the cover of a book, telling the machine where the story begins and ends.
%
O1001 (PART-A-VMC)
...
G-code commands go here
...
M30
%
Immediately after the first %, you'll find the program number, also known as the O-address. This is a unique identifier, usually a four or five-digit number preceded by the letter 'O'. For example, O1001. It's good practice to add a comment in parentheses on the same line to describe the program, like (PART-A-VMC).
Every line of G-code is called a block, and each block gives the machine a specific instruction. A block ends with an End-of-Block (EOB) character, which is typically a semicolon (;) that your CNC control adds automatically when you press Enter.
Each block can start with a sequence number, like N10, N20, N30. This is the N-address. While not always required, it makes the code much easier to read and troubleshoot. It's like numbering the steps in a recipe.
The Safety Line
Before you start moving the machine, you need a clean slate. CNC controllers can be modal, meaning a command stays active until it's cancelled or replaced by another command from the same group. If the machine ran a program before yours, it might still be in a state you don't want, like a drilling cycle or an unusual coordinate system.
The first line of commands after the program number should always be a safety block. This line sets the machine to a known, predictable state.
N10 G17 G21 G40 G49 G80 G90
Let's break down that example line:
- G17: Selects the XY plane for circular interpolation.
- G21: Sets the units to millimeters. (Use G20 for inches).
- G40: Cancels cutter compensation.
- G49: Cancels tool length compensation.
- G80: Cancels any active canned cycles (like drilling or tapping).
- G90: Sets the machine to absolute positioning mode.
This single line ensures any lingering commands are cleared, preventing unexpected machine behaviour.
Defining Your Workspace
The machine has its own permanent, unchangeable zero point, called Machine Home. But you almost never machine parts relative to that point. Instead, you define a more convenient zero point on your fixture or the workpiece itself. This is called the Work Home, and it's managed by the Work Coordinate System.
G-code provides several registers, G54 through G59, to store the location of different Work Homes. You tell the machine the distance from Machine Home to your desired Work Home, and you save these values in one of the WCS registers. Then, in your program, you simply activate it.
N20 G54
From this point on, all coordinates in the program are relative to the G54 origin you defined, not the machine's origin. This is incredibly useful, as you can set up multiple jobs on the same machine, each with its own coordinate system (G54 for the first vice, G55 for the second, and so on).
Absolute vs. Incremental
Now that the machine knows its zero point, you can tell it where to move. You have two primary ways to do this: absolute and incremental positioning.
Absolute positioning (G90) is the default and most common mode. Every coordinate you command is interpreted relative to the active Work Home (e.g., G54). If you say G00 X50.0 Y25.0, the tool will move to a point that is 50mm along the X-axis and 25mm along the Y-axis from your defined zero corner.
Incremental positioning (G91) is different. Every coordinate is interpreted as a distance from the tool's current location. If the tool is at X50.0 and you command G91 G00 X10.0, the tool will move 10mm further in the positive X direction, ending up at X60.0 (in absolute terms).
| Mode | G-Code | Description |
|---|---|---|
| Absolute | G90 | All coordinates are from the Work Home (0,0). |
| Incremental | G91 | All coordinates are from the tool's last position. |
Let's see this in action. Assume our Work Home is set and the tool starts at X0 Y0.
Program using G90 (Absolute):
G90
G01 X10.0 Y10.0 F200. (Moves to 10,10)
X20.0 (Moves to 20,10)
Y20.0 (Moves to 20,20)
Program using G91 (Incremental):
G91
G01 X10.0 Y10.0 F200. (Moves by 10,10, arriving at absolute 10,10)
X10.0 (Moves by another 10 in X, arriving at absolute 20,10)
Y10.0 (Moves by another 10 in Y, arriving at absolute 20,20)
Absolute mode is generally safer and easier to read, as you can always tell the tool's exact location by looking at the code. Incremental is useful for repetitive patterns or subprograms where the relative distance between features is more important than their absolute location.
And that's the core structure. You've established a program, set a safe starting state, defined a workspace, and learned how to command movement within it.