CNCCookbook’s G-Code Tutorial

**Introduction**

G-Code has some powerful operations that allow you to transform coordinates and mold them to your needs. This chapter in the G-Code Tutorial is all about introducing what kinds of things can be done to g-code coordinates and how they fit together. Future chapters will provide details on each specific type of operation. For a basic introduction to g-code coordinates, refer to our intro chapter on the g-code coordinate system.

**The 5 Step G-Code Coordinate Pipeline**

Let’s start our overview with a graphical diagram of the 5-steps involved in transforming a coordinate from the number you type into your g-code program to the actual machine coordinates that will be used:

The 5 Step G-Code Coordinate Pipeline…

I call it a *Coordinate Pipeline* because you can think of each box as representing a calculation that is done on the coordinate from your part program to get to the machine coordinate that will actually be used. By dividing and conquering the pipeline into it’s building blocks, you’ll get an idea of what’s possible. Let’s go through each step to understand what it does.

**Step 1: Unit Conversion**

The first calculation involves converting from the coordinates used in your part program (as defined by G20 and G21) to the units your machine uses internally (typically setup with a parameter in your controller). I’ve portrayed a conversion to Metric in the diagram, but this could as easily be a conversion to Imperial (inches) if that’s what your controller is internally setup for. The point is that once we’re past this stage, G20 and G21 are no longer being considered.

**Step 2: Conversion from Relative or Polar to Absolute Coordinates**

Next up is conversion from either relative or polar coordinates to absolute coordinates. Relative coordinates (whether you’re in relative or absolute mode is defined by G90/G91) are easy: just add the values given on the current line to the current position to get the new absolute coordinates. Polar coordinates are a little tougher. It takes a little trigonometry to convert the polar coordinate angle and distance to a cartesian coordinate.

Once we’re done with Step 2, we now have absolute coordinates.

**Step 3: Offsets: G52 (Local), G54 (Work), and G92 (Workpiece Coordinate Setting)**

Step 3 is all about shifting the coordinate by some *offset*. This is an extremely common operation, and there are many kinds of offsets available:

– Work Offsets define different places a part zero may be. They’re typically handled via G54 and its derivatives, though there are other possibilities.

– Local Offsets are defined by G52. They make it easily to temporarily move the zero to a new place. For example, the center of a bolt circle while you’re busy drilling it so you don’t have to offset each hole from part zero.

– Workpiece Coordinate setting is another facility for setting offsets.

All of those offsets can be used in concert and often are. For example, let’s say you have two vises on your milling table and you’ve set G54 to be the lower left corner of the lefthand vise and G55 to be the lower left corner of the righthand vise. You can machine an identical part in each vise simply by switching back and forth between G54 and G55. Now let’s say each part has a bolt circle. G52 can be used to offset to the center of the bolt circle, and you can then apply polar coordinates to the bolt locations, and all that works for both G54 and G55. See our chapter on Work Offsets for lots more detail on this important function.

There are still more offsets out there to know about, and I didn’t put them on the diagram just because it was already getting crowded. Perhaps the most important are the Tool Offsets. There are length and width offsets for tools as well as wear offsets to permit fine adjustment.

By the end of Step 3 the controller has added in every concievable offset and it’s now time to move to more grandiose transformations.

**Step 4: Scaling and Mirroring: G51**

Speaking of grandiose, how about being able to scale your part to a different size? You could make it much more grandiose by using G51 to scale it to 10x the size it was originally programmed in. You can also mirror the part in many g-code dialects by applying a minus sign to G51 or sometimes by using a different g-code.

Aside from making things much different in scale, there is value in making things slightly different in scale. Let’s say your machine is equipped with an accurate digital touch probe that can measure things very accurately. Let’s further assume you need to make a bore that is extremely accurate and that your part is held in a fixture that has a bore right on the fixture that you have painstakingly made accurate to extreme tolerances. Lastly, let’s say that bore has to be 2.000 inches, plus 0.0001 and minus 0.0000. That’s pretty tight!

Now here is a neat trick you can perform with the probe:

– Measure the bore with the probe. You know it measures within your tolerances to 2 inches at the normal inspection temperatures your customer will use. Let’s say the probe reports a measurement of 1.9993. The hole is allowed to be a tenth larger, but cannot be smaller than 2 inches. So you’re off by 0.0007″ and need the hole to be larger.

– So, your g-code performs a little calculation. 0.0007″ / 2.0000″ is 0.00035. Now you know the correction factor that’s needed.

– When you make your finish pass, use G51 to scale the finish pass to 1.00035x the coordinates that are in the g-code program.

Your part should come out extremely close!

That’s the technique used in Renishaw’s RAMTIC manufacturing, and it’s a very powerful way to increase the accuracy of your machining operations.

For full details on the powerful G51 code, see our chapter on G51 Scaling programming.

With Step 4 complete, we’re almost ready with a machine coordinate. All that is left is possible rotation.

**Step 5: Rotation**: **G68**

The last step in the Coordinate Pipeline is *rotation*. With G68, you can rotate the coordinates an arbitrary number of degrees about an arbitrary center.

Whoa! Sounds cool, but what the heck is this good for?

A simple example might be machining identical features that are aligned in a circle. Perhaps your fixture keeps parts in a circle, or perhaps your part has identical pockets arranged around a center.

In keeping with our RAMTIC example for scaling, let’s consider another clever use of rotation. Every machinist has trammed a vise or fixture so it is precisely aligned with the machine’s travel. It doesn’t take long to sweep the indicator and tap things into alignment, but what if you didn’t have to do it at all? Maybe real machinists are scarce around your shop and you want your operators to be able to just stick a part in the vise and press the green button all day long.

Turns out we can write g-code that will sweep a probe along the surface that’s supposed to be aligned–let’s say it’s the rear fixed vise jaw. Given that information we can tell what angle the vise jaw sits at relative to the motion of the axis.

You see where this is going, right?

Yes! We can use G68 coordinate rotation to take that angle back out and make it zero without ever having to touch the vise.

**Conclusion**

Coordinates are the basic currency of g-code, so it should come as no surprise that we have all sorts of tricks available for transforming coordinates. This has been an overview of what’s possible and we’ve been sketchy about the details on purpose. We wanted you to have a framework to fit those details into first. Later chapters in our g-code tutorial go through each type of coordinate transformation and explain the details.

One more thing: you can use our G-Code Editor Software to experiment with these transformations yourself and see how they work. It implements a Coordinate Pipeline just like the one described here in its g-code simulator.

**Exercises**

1. Come up with some ideas of your own for how you’d use coordinate transformation to make your machining work faster, easier, and more efficient.

2. Think about a system of work offsets for your use. Consider whether you’d want a work offset for each part or one for each fixture or vise? Maybe both?

## Like what you read on CNCCookbook?

**Join 100,000+ CNC'ers!** Get our latest blog posts delivered straight to your email inbox once a week for free. Plus, we’ll give you access to some great CNC reference materials including:

- Our Big List of over 200 CNC Tips and Techniques
- Our Free GCode Programming Basics Course
- And more!

Just enter your name and email address below:

*100% Privacy: We will never Spam you!*