Combining Functions With User-Defined G & M Codes
CNC programmers gain flexibility when using custom macros to define G and M codes themselves.
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
How this Job Shop Grew Capacity Without Expanding Footprint
This shop relies on digital solutions to grow their manufacturing business. With this approach, W.A. Pfeiffer has achieved seamless end-to-end connectivity, shorter lead times and increased throughput.
Read MoreHow to Mitigate Chatter to Boost Machining Rates
There are usually better solutions to chatter than just reducing the feed rate. Through vibration analysis, the chatter problem can be solved, enabling much higher metal removal rates, better quality and longer tool life.
Read MoreContinuous Improvement and New Functionality Are the Name of the Game
Mastercam 2025 incorporates big advancements and small — all based on customer feedback and the company’s commitment to keeping its signature product best in class.
Read More4 Commonly Misapplied CNC Features
Misapplication of these important CNC features will result in wasted time, wasted or duplicated effort and/or wasted material.
Read MoreRead Next
Increasing Productivity with Digitalization and AI
Job shops are implementing automation and digitalization into workflows to eliminate set up time and increase repeatability in production.
Read MoreInside Machineosaurus: Unique Job Shop with Dinosaur-Named CNC Machines, Four-Day Workweek & High-Precision Machining
Take a tour of Machineosaurus, a Massachusetts machine shop where every CNC machine is named after a dinosaur!
Read MoreThe Future of High Feed Milling in Modern Manufacturing
Achieve higher metal removal rates and enhanced predictability with ISCAR’s advanced high-feed milling tools — optimized for today’s competitive global market.
Read More