Published

Combining Functions With User-Defined G & M Codes

CNC programmers gain flexibility when using custom macros to define G and M codes themselves.

Founder and President, CNC Concepts Inc.

Share

Leaders-In background
Heidenhain Corporation
Heidenhain Corporation

Heidenhain Corporation

HEIDENHAIN is a world-leading provider of encoders, machine controls, touch probes, digital readouts and metrology solutions—empowering engineer...READ MORE

Featured Content
View More
Featured Videos
View More
All World Machinery Supply
All World Machinery Supply

All World Machinery Supply

All World Machinery Supply is your one-stop shop for machine tool solutions. Machine Tool Spare Parts | Repair Services | Custom Machine Tool Accesso...READ MORE

Featured Content
View More
Featured Videos
View More
QualiChem Inc.

QualiChem Inc.

Featured Content
View More
Featured Videos
View More
SMW Autoblok Corporation
SMW Autoblok Corporation

SMW Autoblok Corporation

Featured Content
View More
Featured Videos
View More
ZEISS Industrial Quality Solutions
ZEISS Industrial Quality Solutions

ZEISS Industrial Quality Solutions

Featured Content
View More
Featured Videos
View More
Photo Credit: Getty Images

Some versions of parametric programming, such as FANUC Custom Macro, enable users to create their own G and M codes, as well as redefine G and M codes that exist within their CNC.

Consider a machining center tool change command (M06), for example. A “standard” M06 simply commands a tool change. With most machines, one or more axes must be in a specific position — commonly the reference position — before a tool change can occur. Users can redefine the function of M06 so that it will first cause axis motion to the tool change position, orienting the spindle along the way, and then make the tool change.

With a FANUC CNC, one must first change a parameter to specify the program that will be called whenever an M06 is executed. The first available user-defined M-code parameter with a FANUC 0iF control is parameter number 6071. By setting this parameter to a value of 6 (the M-code number), the CNC will execute program number O9001 whenever an M06 is executed. Here is an example of how program O9001 could be written for a vertical machining center:

  • O9001 (M06, program related to parameter 6071)
  • G91 G28 Z0 M19 (Move to the Z-axis reference position and orient the spindle)
  • M06 (Make the tool change)
  • M99 (End of Custom Macro)

While user-defined G and M codes can be used for a variety of applications, we limit this presentation to combining functions. The tool change example just shown is one such application. Combining functions will shorten programs, ensure that users don’t forget an important command and possibly help make programs among different machines more compatible.

Another function combining M codes is related spindle and coolant. Some machines have three M codes that turn the spindle and coolant on or off. M13 (spindle forward) and M14 (reverse) are used to turn on the spindle and coolant. M15 turns them both off. If the machine does not have these M codes, they can be recreated. To do so with a FANUC 0iF, set parameters 6072, 6073 and 6074 to values of 13, 14 and 15, respectively. Then load these three programs into the CNC:

  • O9002 (M13, program related to parameter 6072)
  • M03 (Turn on spindle in forward direction)
  • M08 (Turn on coolant)
  • M99 (end of Custom Macro)
  • O9003 (M14, program related to parameter 6073)
  • M04 (Turn on spindle in reverse direction)
  • M08 (Turn on coolant)
  • M99 (end of Custom Macro)
  • O9004 (M15, program related to parameter 6074)
  • M05 (Turn spindle off)
  • M09 (Turn coolant off)
  • M99 (end of Custom Macro)

User-defined G codes can be used for similar purposes. Consider, for example, the series of “safety commands” that many programmers like to include at the beginning of every program. This set of G codes ensures that the states of important G codes are as they were at power up. For a machining center, they commonly include G20 or G21 (measurement system), G90 (absolute mode), G40 (cancel cutter radius compensation), G80 (cancel canned cycles), G94 or G95 (per-minute or per-revolution feedrate) and G64 (normal cutting).

For a FANUC 0iF CNC, the first available user-defined G-code parameter is parameter number 6050, which is related to program number O9010. If the parameter is set to a value of 100, the CNC will call and run program O9010 whenever a G100 is executed. Here is what program 09010 can look like:

  • O9010 (G100, program related to parameter 6050)
  • G20 G90 G40 (Select inch mode, absolute mode and cancel cutter comp)
  • G80 G95 G64 (Cancel canned cycle mode, select per-revolution feedrate mode and normal cutting mode)
  • M99

The last example is related to program structure. The commands related to starting and ending tools are quite redundant. Consider this two-tool turning center program:

  • O0001
  • N005 T0101 (Tool startup structure)
  • N010 G96 S600 M03
  • N015 G00 X1.35 Z0.005 M08
  • N020 G99 G01 X-0.06 F0.012
  • N025 G00 Z0.1
  • N030 X1.25
  • N035 G71 P040 Q075 U0.04 W0.005 F0.012
  • N040 G00 X0.44
  • N045 G01 Z0
  • N050 X0.5 Z-0.03
  • N055 Z-0.5
  • N060 X0.69
  • N065 X0.75 Z-0.53
  • N070 Z-1.0
  • N075 X1.25
  • N080 X8.0 Z5.0 (Tool ending structure)
  • N085 M01
  • N090 T0202 (Tool startup structure)
  • N095 G96 S700 M03
  • N100 G00 X0.6 Z0 M08
  • N105 G01 X-0.06 F0.005
  • N110 G00 Z0.1
  • N115 X1.25
  • N120 G70 P040 Q075 F0.005
  • N125 G00 X8.0 Z5.0 (Tool ending structure)
  • N130 M01
  • N135 M30

Setting parameters 6051 and 6052 to values of 101 and 102 respectively will cause G101 to execute program O9011 and G102 to execute program O9012. The tool startup commands (N005-N015 and N090-N100) and tool ending commands (N080-N085 and N125-N130) could be replaced with these commands:

  • .
  • N005 G101 T1.0 S600.0 X1.35 Z0.005 (Call tool startup Custom Macro)
  • .
  • N080 G102 X8.0 Z5.0 (Call tool ending Custom Macro)
  • .
  • N090 G101 T2.0 S700.0 X0.6 Z0 (Call tool startup Custom Macro
  • .
  • N125 G102 X8.0 Z5.0 (Call tool ending Custom Macro)
  • .

The custom macros could look like this:

  • O9011 (G101, program related to parameter 6051)
  • T[#20+#20*100] (Index turret)
  • G96 S#19 M03 (Start spindle)
  • G00 X#24 Z#26 M08 (Move to position, start coolant)
  • M99
  • O9012 (G102, program related to parameter 6052)
  • G00 X#24 Z#26 (Move to turret index position)
  • M01 (Optional stop)
  • M99
Advertisement HCL CAMWorks
Advertisement CloudNC
Advertisement Norton Saint-Gobain
Advertisement TechSpex 2025
Advertisement Western Gage Corp.
Advertisement starrag

Related Content

CNC Tech Talks

Automatic Sizing Adjustments for High-Volume Lathe Work

Keeping operators from having to manually make sizing adjustments will free them up to do other things for as long as each finishing tool will last, regardless of how many sizing adjustments it requires.

Read More
Basics

Obscure CNC Features That Can Help (or Hurt) You

You cannot begin to take advantage of an available feature if you do not know it exists. Conversely, you will not know how to avoid CNC features that may be detrimental to your process.

Read More
CNC Tech Talks

The Final CNC Tech Talk Column

Mike Lynch signs off on his CNC Tech Talk column after 35 years.

Read More
CNC Tech Talks

6 Variations That Kill Productivity

The act of qualifying CNC programs is largely related to eliminating variations, which can be a daunting task when you consider how many things can change from one time a job is run to the next.

Read More

Read Next

Inspection and Measurement

OEM Tour Video: Lean Manufacturing for Measurement and Metrology

How can a facility that requires manual work for some long-standing parts be made more efficient? Join us as we look inside The L. S. Starrett Company’s headquarters in Athol, Massachusetts, and see how this long-established OEM is updating its processes.

Read More
Top Shops

Registration Opens for 2025 Top Shops Conference

Unlock benchmarking insights, shop secrets, and a behind-the-scenes NASCAR tour, only at this year’s Top Shops Conference.

Read More
Advertisement HCL CAMWorks
Loading