Combining Functions With User-Defined G & M Codes
CNC programmers gain flexibility when using custom macros to define G and M codes themselves.
Share




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
Related Content
Five-Axis Machines Speed NASCAR Engine Production
Moving from an aging set of five-axis mills to more advanced machines enabled Hendrick Motorsports to dramatically improve its engine production.
Read MoreSwiss-Type Control Uses CNC Data to Improve Efficiency
Advanced controls for Swiss-type CNC lathes uses machine data to prevent tool collisions, saving setup time and scrap costs.
Read MoreCNC-Related Features of Custom Macro
CNC-related features of custom macro are separated into two topics: system variables and user-defined G and M codes. This column explores both.
Read MoreComputer Programming-Related Features of Custom Macro
Custom macro is an interpreter-based language, meaning that all CNC G code and custom macro commands are executed as the CNC comes across them.
Read MoreRead Next
Setting Up the Building Blocks for a Digital Factory
Woodward Inc. spent over a year developing an API to connect machines to its digital factory. Caron Engineering’s MiConnect has cut most of this process while also granting the shop greater access to machine information.
Read MoreWhy We Ask Machine Shop Leaders to Speak at TASC – The Automated Shop Conference
TASC is our industry’s premier peer-to-peer automation stage where America’s shop leaders refine the art of metalworking and CNC machining. For conference speakers, it's also an opportunity to showcase your skills and gain exposure for your business. Here are five why stepping into the spotlight at TASC could be your smartest move toward elevating your shop.
Read MoreShop Tour Video: You've Never Seen a Manufacturing Facility Like This
In the latest installment of our “View From My Shop” series, explore Marathon Precision’s multi-process approach to manufacturing, where blacksmiths and hand-forged dies meet state-of-the-art CNC machining. Discover how restoring classic muscle cars and building custom art projects creates a dynamic shop culture — and draws top talent to this unique and innovative metalworking facility.
Read More