Taking Advantage of G-Code Grouping

These preparatory functions make sure your CNC machine does what you want it to do.


Facebook Share Icon LinkedIn Share Icon Twitter Share Icon Share by EMail icon Print Icon

G codes are at the heart of a CNC program. These preparatory functions commonly set modes, telling the machine how to behave in the current command, and often in upcoming commands as well.

Closely related G codes are placed in G code groups. With FANUC controls, these groups are numbered. Group 01, for example, contains motion G codes G00, G01, G02 and G03 (among others). Group 02 contains plane selection G codes G17, G18 and G19. Group 03 contains positioning mode selection G codes G90 and G91. Reference the list of G codes in your FANUC manual to determine which G codes are in each group, as well as the group number for each group.

(By the way, Group 00 contains non-modal [one-shot] G codes, including dwell commands G04 and G09, reference return commands G28 and G30, and the custom macro calling command G65. This group is not of importance to this discussion.)

A series of custom macro system variables in the #4000 series provides access to the currently instated G code numeric value in every group. To determine the appropriate system variable number that provides access to a given group, add 4,000 to the group number. If the currently instated G code in group No. 2 two is G17, for example, the value of #4002 will be 17.0.

You can use #4000 series system variables to track which G code in a group is currently active and use this to your advantage when programming. Consider this example for tracking positioning mode, specified by G90 (absolute) and G91 (incremental) for FANUC-controlled machining centers. Say you want to write an incremental subprogram or custom macro that will be used by others. You’ll need to ensure that the machine is left in the original positioning mode when your program is finished. 

Positioning mode G codes are in group No. 3, so system variable #4003 provides access to which positioning mode is active. If absolute mode is currently active, the value of #4003 will be 90.0. If incremental mode is active, the value of #4003 will be 91.0. You can place this command at the beginning of your subprogram or custom macro:

#101 = #4003 (Store the current positioning mode G code value)

You can then switch to incremental mode (G91) and write the commands for your subprogram or custom macro. When finished, specify this command:

G#101 (Reinstate the original positioning mode)

You can even test the current G-code value and make the machine behave differently based on which G code is instated. Maybe a series of commands must be executed in the absolute mode. Consider the following commands that test current positioning mode. If absolute mode is not currently selected, an alarm will sound.

IF [#4003 NE 91.0] GOTO 5
#3000 = 101 (Wrong position mode)
N5 (Program continues)

You could even provide two sets of motion commands, one written with absolute positioning and the other with incremental positioning, and determine which set of commands will be executed by testing the current value of #4003.

The same techniques could be used with measurement system modes, using system variable #4006 to determine whether the machine is in the inch or metric mode (G20 or G21).

Implications of having access to current G-code settings may be a little difficult to visualize, but just about every G-code group has its own potential applications. Be sure to give this some thought. The best implications tend to be for applications of multi-purpose programs, typically custom macro programs, and especially when they will be used by several people.

Two more examples:
Inch and metric modes (G20 and G21). You have developed a custom macro that can be used in either measurement system. Speed could be specified in either surface feet per minute or surface meters per minute, depending on the chosen measurement system. Your custom macro can determine which measurement system is being used and apply the appropriate formula to calculate spindle speed in rpm.

Workpiece coordinate system offset selection modes (G54-G59). There are several things your program can infer if it knows which coordinate system offset is currently selected. For a rotary table application, for example, you may be trying to optimize positioning movements to clear obstructions between machined surfaces. When a particular side of the workpiece is facing the spindle, you must retract the tool farther than when another side of the workpiece is facing the spindle. By knowing which program zero point is currently active, your custom macro can determine which side is currently facing the spindle and make positioning movements accordingly.