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.
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 Harold Beck and Sons Inc. 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
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 More3 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 MoreThe Best Point of Reference for Program Zero Assignment Entries
Correctly specified program zero assignment and coordinate position values enable the CNC to determine how far to move the cutting tool during each positioning motion.
Read More4 Reasons to Use Safety Commands
Safety commands help safeguard CNC applications from common programming or operation errors.
Read MoreRead Next
IMTS 2024: Trends & Takeaways From the Modern Machine Shop Editorial Team
The Modern Machine Shop editorial team highlights their takeaways from IMTS 2024 in a video recap.
Read MoreIncreasing 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 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