91ÊÓÆµÍøÕ¾ÎÛ

Published

Improve CNC Programs With User-Defined G, M Codes

CNC operators can program user-defined G and M codes to call custom macros and improve upon or replace existing codes, which can in turn improve production.

Share

Leaders-In background
CNC Machinist Programming
Photo Credit: Getty Images

FANUC custom macro allows the creation of G and M codes of the user’s design. Instead of calling the custom macro in the usual manner (typically using G65 or G66 with the program number), it is possible to set up a CNC so that a G or M code calls the custom macro, and even specify whether it is modal or not.

Parameter changes are required to create user-defined G or M code. If, for example, we set up a user-defined G code numbered G101, and if it is the first available user defined G code in the series, set the parameter to a value of 101. Then, instead of calling the custom macro with G65 P9010, it can be called with G101.

Users can also replace or improve upon current G and M codes. For example, we can modify the way a vertical machining center’s automatic tool changer command (M06) works so that the Z axis moves first to its reference position, orienting the spindle along the way. This will save time and ensure that the Z axis is at the appropriate position before the tool change occurs.

First, determine which is the first available user-defined M code. For a current model FANUC CNC like the 30iB, look at the values of parameters 6071 through 6079. Locate the first one that contains a value of zero. If they are all zeros, no user-defined M codes have been set up.

Second, place a value corresponding to the M-code number in the parameter. In this case, say parameter 6071 is zero. Set this parameter to 6. From now on, when the control reads an M06, it will execute program O9001.

Finally, create and load program O9001 in the CNC. It would look something like this:

  • %
  • O9001
  • G91 G28 Z0 M19
  • M06
  • M99
  • %

If the G or M code corresponding to the user-defined G or M code is placed in the custom macro (like it is with M06 in program O9001), the CNC will perform the normal function of this word — in our case, a tool change.

I was recently helping Walter Boguslaw of when we came across a new (to me) use for user-defined G and M codes. Boguslaw’s company machines components in both fully automatic and manual mode. Operators working during day shift are present throughout the entire cycle but during night shift, production runs unattended.

The issue was with the M01 optional stop command. Operators on the day shift need this command in the program so that they can stop the cycle at key times, should they need to. But during fully automatic operation, if the optional stop switch happens to be left on, the machine will stop at the first M01, and there will be no one to restart the machine. Entire shifts’ worth of production could be lost if someone forgets to turn off the optional stop switch before they leave.

To solve this problem, we created a user-defined M code for M01 — setting parameter 6071 to 1. Now, when the CNC reads an M01, it executes program O9001.

Let’s take this in steps. Consider this program:

  • %
  • O9001
  • M99
  • %

This is not what we want. With this version of program O9001, the CNC would simply ignore all M01 commands — during day and night shifts.

We could use a flag, like permanent common variable #500, to specify whether M01 commands will be ignored. The operator could set #500 to 0 (much like setting an offset) to ignore M01 commands or set it to 1 to execute them:

  • %
  • O9001
  • IF [#500 EQ 0] GOTO 99
  • M01
  • N99M99
  • %

This works well, but someone must still remember to adjust the value of permanent common variable #500 during shift changes. This provides no improvement.

Here is an automated method. Since we know the shift times, we can use the CNC’s internal clock, system variable #3012, to determine which shift is running. Say there are two ten-hour shifts. Day shift begins at 7 a.m. and ends at 5 p.m. Night shift begins at 6 p.m. and ends at 4 a.m. The optional stop command must be ignored between 6 p.m. and 4 a.m.

The format of system variable #3012 is

  • 161705

That is, a 24-hour clock currently displaying 16 hours, 17 minutes, 5 seconds. The current time is approximately 4:17 p.m.

We need to isolate the hour value (16 in our example), since it is between 18 (6 p.m.) and 4 (4 a.m.) that we want the optional stop command to be ignored. This can be done using the FIX function, which rounds down to the next lower integer — and “FIX-ing” the value of #3012 divided by 10,000. For our example, 161705 divided by 10,000 is 16.1705. By applying the FIX function to this value, we get 16 — the hour value.

Consider this final version of the custom macro:

  • %
  • O9001
  • #33=FIX[#3012] / 10000 (determine hour)
  • IF [[#33 GE 18.0] OR [#33 LT 4.0]] GOTO 99
  • M01
  • N99M99
  • %

Related Content

How to Determine the Currently Active Work Offset Number

Determining the currently active work offset number is practical when the program zero point is changing between workpieces in a production run.

Read More
CNC Tech Talks

2 Secondary Coordinate Systems You Should Know

Coordinate systems tell a CNC machine where to position the cutting tool during the program’s execution for any purpose that requires the cutting tool to move.

Read More
CNC Tech Talks

6 Ways to Streamline the Setup Process

The primary goal of a setup reduction program must be to keep setup people working at the machine during the entire setup process.

Read More
CNC Tech Talks

Troubleshooting Differences in Programming Methods, Machine Usage

Regardless of the level of consistency among machines owned by your company, you probably have experienced consistency-related issues. Here are some tips to help solve them.

Read More

Read Next

Automation

AMRs Are Moving Into Manufacturing: Considerations for Implementation

AMRs can provide a flexible, easy-to-use automation platform so long as manufacturers choose a suitable task and prepare their facilities.

Read More
Top Shops

Last Chance! 2025 Top Shops Benchmarking Survey Still Open Through April 30

Don’t miss out! 91ÊÓÆµÍøÕ¾ÎÛ's Top Shops Benchmarking Survey is still open — but not for long. This is your last chance to a receive free, customized benchmarking report that includes actionable feedback across several shopfloor and business metrics. 

Read More
Basics

Machine Shop MBA

  Making Chips and 91ÊÓÆµÍøÕ¾ÎÛ are teaming up for a new podcast series called Machine Shop MBA—designed to help manufacturers measure their success against the industry’s best. Through the lens of the Top Shops benchmarking program, the series explores the KPIs that set high-performing shops apart, from machine utilization and first-pass yield to employee engagement and revenue per employee.  

Read More