G20 and G21 G Code: Metric and Imperial Unit Conversion CNC Programming
The first stop on the 5-Step G-Code Coordinate Pipeline is Unit Conversion, and this chapter is all about delving into that subject.
5-Step G-Code Coordinate Pipeline…
The coordinate pipeline shows how coordinates go from the numbers in the g-code program through various transformations to arrive at the numbers the machine actually uses.
G20 / G21 G Codes are Unit Conversion for the Machine, Not Your Part Program
First, it’s important to clear up a possible misconception: G20 and G21 g codes do not convert your part program to a different system of units. You can’t change an Imperial (inches) program to Metric just by sticking a G21 g code on the front of a Metric g-code program. Let’s take it step-by-step and see what is actually happening.
Your controller needs to know what units your program is in because it is responsible for moving the axes of your machine by real amounts. If it simply sees, “X1Y1Z0” it has no idea whether the numbers are inches, millimeters, or furlongs.
So, the role of G20 and G21 g codes is to tell your controller what units the numbers in the program are in. If the program starts with a G20, it will assume the program uses inches for coordinates. If it starts with a G21 g code, it will assume millimeters.
This brings us to an important point:
IT IS ALMOST NEVER A GOOD IDEA TO CHANGE A G20 TO A G21 G CODE OR VICE VERSA WITHOUT CHANGING COORDINATES IN A G-CODE PROGRAM
I put that in all caps for emphasis. If you’re handed a program, and you change a G20 to a G21 G Code or vice versa, you’d better also change the coordinates accordingly or be darned sure that what was there was wrong. Imagine taking a properly written program in millimeters and telling the control to assume the units are inches. That would be like scaling all the dimensions up by 25.4 since 1″ = 25.4 millimeters.
G20 and G21 G Codes simply tell the controller what units the g-code program’s coordinates are in. G20 specifies Imperial (inch) and G21 specifies Metric (mm) units.
Once the controller knows what units you’re giving it, it uses that knowledge to convert the coordinates to the unit system it uses internally. Most controllers can be set up to use either Metric or Imperial internally, and they will also have a default for what units they’re expecting if the part program has no G20 or G21.
Another thought is that it’s a bad idea to include both inch and Metric units (and therefore G20 and G21) in the same program. It’s just going to be way too confusing.
How Do I Convert My GCode from Inches to Metric or Vice Versa?
By now you’re wondering, “If I can’t use G20/G21 to convert my g-code from Metric to Imperial (or vice versa), how can I do unit conversions?”
Well, you could go through everything by hand and convert the units. That’d be a tedious task to do, but certainly doable. A more clever person could at least partially automate the process by using an Excel spreadsheet and some string formulas. The formulas would search out the coordinates, multiply them by an appropriate correction factor, and substitute them back in. Excel is a very handy tool for doing those kinds of things.
There’s a better way though. G-Wizard Editor (our nc viewer software) has a unit conversion facility and will automate just about everything for you. If you think about it, unit conversion is largely a matter of scaling the coordinates. GW Editor has a “Translate” revision that can do offsets, scaling, and rotation. Select it via the Tools Revise menu. When it comes up, pick the “Scale” tab:
Select Tools Revision, the Translate revision, and then the Scale tab…
There are two buttons, “in to mm” and “mm to in” that plug in the correct scaling factor. Pick the one you want, press “Apply”, and GWE will do the rest. After it has finished, make sure there is a G20 or G21 G Code (whichever is appropriate) at the top of the program so everyone (and every controller) knows what units are expected.
Setting Up G-Wizard Editor’s Units
While we’re on the subject of GW Editor, let’s talk briefly about it’s treatment of Metric and Imperial units, because it is pretty similar to many controls. GWE allows you to set units in two ways:
1. Set them on the the first tab of the Setup screen:
This sets up what kind of units will be used by the User Interface of the program. So, for example, the background measurement grid in the backplot area will either be ruled in inches or millimeters. Note that when I say the UI, you have to carefully distinguish whether a prompt is asking you to input a coordinate that is going into the g-code program or not. If it is, there will be no unit conversion done on that coordinate–it’s treated just as a number and directly used with the program without regard to units.
2. Set the units via G20/G21. This tells GWE what the g-code program’s units are, as we have been discussing, and changes how the backplot is scaled versus the background grid. By default, GWE will start in G20 Imperial mode, but you have the ability to override that too. GWE has the ability to execute one line of g-code silently before it starts simulating your g-code. You set it up as a post variable:
Just plug a G21 in there and GWE will assume part programs are Metric, unless it finds a G20 in the program itself. Some of the canned posts we have available are pre-configured to metric in this way, but it’s easy to convert a post by adding a G21 if we don’t already have a metric version of your post available.
3. Set up your Safe Start Code to include either G20 or G21 whenever you use a Conversational CNC Wizard. Just click the “Setup” tab choice on the Wizard Popup and edit the Safe Start Code for lathe or mill:
4. You may also want to edit the Safe Start Code for the Custom snippets button on the Tool Bar:
G21 and G20 G-Code Dialects
Now you know what G20 and G21 do, and also how to convert programs from Imperial to Metric and back. Next, we’re going to take a look at Work Offsets, which make it easy to set up for making multiple parts or quickly accomodating multiple fixtures on your machines.
1. Use GW Editor to convert one of your programs from the units it uses to the alternate system–either convert an Imperial program to Metric or a Metric program to Imperial. Be sure to put the right G20 or G21 at the beginning of the program so the controller knows what units it is getting.