Digital Readout Kit for Mills, Lathes, & Grinding
Published

Combining Functions With User-Defined G & M Codes

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

Share

A CNC operator
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
Mazak Multi-Tasking: Your Parts Multiplier.
Gravotech
World Machine Tool Survey
Hurco
To any Measurement Question there is an Answer
Techspex
Precision grinding & hard turning custom solutions
Paperless Parts
IMTS 2024
VERISURF
SolidCAM
Koma Precision

Related Content

Understanding G27, G28, G29 and G30

Take a closer look at these reference position commands.

Read More
SPONSORED

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 More

When Handing Down the Family Machine Shop is as Complex as a Swiss-Turned Part

The transition into Swiss-type machining at Deking Screw Products required more than just a shift in production operations. It required a new mindset and a new way of running the family-owned business. Hardest of all, it required that one generation let go, and allow a new one to step in.

Read More

Tips for Designing CNC Programs That Help Operators

The way a G-code program is formatted directly affects the productivity of the CNC people who use them. Design CNC programs that make CNC setup people and operators’ jobs easier.

Read More

Read Next

3 Mistakes That Cause CNC Programs to Fail

Despite enhancements to manufacturing technology, there are still issues today that can cause programs to fail. These failures can cause lost time, scrapped parts, damaged machines and even injured operators.

Read More
Large Part Machining

The Cut Scene: The Finer Details of Large-Format Machining

Small details and features can have an outsized impact on large parts, such as Barbco’s collapsible utility drill head.

Read More
CNC Turnkey Package for Knee Mills and Lathes