6 months by cncdivi

CNCCookbook’s G-Code Tutorial

The G71 rough turning cycle present on numerous controls is truly beneficial. You designate a profile using g-code, and the G71 cycle takes care of the preliminary stages. However, not all controls offer G71, and earlier iterations of G71 possess certain restrictions.

–  IF <condition> GOTO <line>:  The ability to switch execution to a specified line if the condition is true.

–  GOTO <line>:  The ability to always go to a specified line.

–  Subprograms

–  Macro variables and expressions

The basic idea revolves around the following g-code programming structure:

( Startup code goes here )

( Variables to set the initial conditions )

#101=0.2 (Depth of cut)

N1000 (Loop to move profile in by cut depth each pass)
G52 X#100
IF [#100 LE 0.0] GOTO 1100
( Call profile )
M97 P10
#100=#100-#101
GOTO 1000
N1100 ( Program goes here when done )

( Rest of program below )

M30

( Subprogram for the profile of a pass )

N10

M99

How does it work?

Let’s go over how that basic loop works in plain English:

First, all the “Startup code” is executed.  This is where you get the establish the basic safe startup conditions you want, select the right tool, get the spindle running, and so on.

Next, we use a couple of variables, #100 and #101, to setup the radius of the material (#100) and the depth of cut to make each pass (#101).

We are going to use G52 to shift the coordinates by the material radius each time.  This means our profile can used fixed coordinates and the G52 will just move that profile to the new material radius after each pass.  In this case, we are starting with 1.5″ as the radius.

The line “IF [#100 LE 0.0] GOTO 1100” checks to see if we’ve moved the profile all the way in to a 0 offset.  You can imagine there will be some profiles where you may not want to go to zero, so this is the line you’d need to change.  We could also break it out as a variable for convenience.

What that line says is, “If the radius variable (#100) is less than or equal to (LE) 0.0, make the program go to line N1100.”  N1100 is the line we’ve marked, “Program goes here when done.”

Since we’ve just started, we won’t go to N110 yet.  Instead we fall through to where it says “Call profile”.

Here’s where we use M97 to call the subprogram that defines the profile of a pass.  In that subprogram, you want to put code that exactly follows the profile of the part you want to make.

Once that subprogram finishes, it uses M99 to pop up to line just after the M97 we used to call it.  On that line we have “#100 = #100-#101.”  What we’re doing there is reducing the turning radius (#100) by our depth of cut (#101).  That means the next pass will cut a little more deeply.

Finally, we get to “GOTO 1000” which sends us back up to N1000 to do it again.  That’s why it’s called a “Loop”.

As you can see, this little program will keep calling the profile, using G52 to shift it a little closer to center by the cut depth each time, until it gets to a zero shift.

Here’s a sample backplot of it from G-Wizard Editor:

The result of the simulated G71…

You can see the passes start out pretty far from center and move in each loop.

This code is a rough approximation of what you’ll need.  To put it to work, you’ll need to adapt it to your machine’s G-Code dialect.  There are probably various embellishments you’d like to make to the code as well, perhaps to set a finish pass.  I’m using a pretty aggressive rough cut of 0.200″ so you can clearly see what’s happening in the backplot.  You may want to adjust that as well.  You may also want different N-numbers.  Those are all easy changes to make and experiment with.

But having done all that, and hopefully debugged it with a tool like G-Wizard Editor, you’ll have created a handy routine that you can use for lots of different profiles on your CNC lathe.  There’s lots of other ways to code the loop I described, but that one is really simply, so hopefully that style will work for the largest number of controls.  I used Fanuc syntax there, but remember, your control may use a different syntax.

This approach is simple, but won’t run as fast as a real G71 because we use the same profile for every pass.  That means we have to cut a fair amount of air whereas a good G71 will take the shortest path down the length in a straight line.  Nevertheless, it’ll get your part made if you need a quick substitute for G71.

Be sure to check out our article on Using Mill CAM for Turning if you want some tips on how to program the profiles with your Mill CAM software.

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!