HCL CAMWorks
Published

CNC-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.

Share

Leaders-In background
An operator at a machine control
Photo Credit: Getty Images

Last month’s column described computer-programming-related features of custom macro. This column address CNC-related features of custom macro, which are separated into two topics: system variables and user-defined G and M codes.

System variables are specified with variables numbered 1,000 and larger. Each type of system variable has its own group of system variable numbers. All provide access to CNC functions that are not available in normal G-code programming. This column goes through some of them, not sequentially, but in order of popularity.

It can be difficult to appreciate the importance of system variables and think of implications as to when they can be helpful. Below are a few simple examples, but they just scratch the surface of what’s possible.

Some system variable numbering varies among various FANUC CNCs (especially related to offset registers), so you must reference custom macro documentation for your CNC to determine which system variables are involved. Also, newer FANUC CNCs provide system variable names as well as numbers. We show the numbers since they work on all FANUC CNCs.

Custom macro enables users to write and read to and from offset registers (ranging in the #2000 and #10000 series). With tool length compensation, for example, #2001 provides access to the value stored in tool length compensation geometry offset register number one. #2002 provides access to offset two — and so on.

Consider these commands:

  • #100=#2001 (place the value currently in offset number one in common variable #100)
  • #2001=#101 (overwrite the value in offset number one with the value of #101)
  • #2001=#2001+#101 (modify the value of in offset number one by the value of #101).

An example that sets offsets 1-50 to zero:

  • .
  • #100=1 (counter)
  • WHILE [#100 LE 50] DO 1
  • #[2000+#100] = 0 (set offset register to zero)
  • #100 = #100 +1 (step counter by one)
  • END 1
  • .

Custom macro also provides access to alarm generation and stop with message (#3000 and #3006). Alarm generation gives users the ability to set error traps. Stop with message is like a program stop command (M00), but a message will be displayed to tell the operator why the program has stopped.

  • #3000=100(OFFSET IS NOT SET)
  • #3006=100(TURN PART AROUND IN CHUCK)

The number to the left of the parentheses (100 in our case) is the alarm or message number. This message will be displayed if the alarm or message command is executed. Consider this command:

  • IF[#2001 LT 3.0] THEN #3000=100(TOOL IS TOO SHORT)

The alarm generation command will only be executed if the value in tool length compensation geometry offset register number one is less than 3.0. If true, the machine will go into alarm state and this message will be displayed on the display screen:

  • MC100 TOOL IS TOO SHORT

Users also can access axis position (ranging in the #5000 series). These system variables are read-only, letting you access the current position of each axis in several ways. Here are three ways for the X-, Y- and Z-axes:

  • #5001: X-axis position relative to program zero
  • #5002: Y-axis position relative to program zero
  • #5003: Z-axis position relative to program zero
  •  
  • #5021: X-axis position relative to the reference (home) position
  • #5022: Y-axis position relative to the reference (home) position
  • #5023: Z-axis position relative to the reference (home) position
  •  
  • #5061: X-axis position after skip signal (used with touch probes)
  • #5062: Y-axis position after skip signal (used with touch probes)
  • #5063: Z-axis position after skip signal (used with touch probes)

Example using an edge finder as a touch probe:

  • .
  • G90 G00 X-0.5 Y0.5 (Move edge finder with 0.5-in of the left side of part)
  • Z-0.2 (move edge finder below top of workpiece)
  • #100=0.1 (Edge finder radius)
  • #3006=100(TOUCH LEFT SIDE OF PART IN X)
  • #101=#5021 + #100 (Surface location in X from reference position)
  • G91 G00 X-0.5 (Move away 0.5-in)
  • .

When the machine stops and the message is displayed at the #3006 command, the operator will switch from automatic to handwheel mode, bring the edge finder flush with the left side of the part, reselect the automatic mode and restart the cycle. The surface location will then be stored in common variable #101. This could be the program zero surface which can now be placed in the X-axis register of the fixture offset.

With user-defined G and M codes, users can create new G and M codes or redefine the way current G- and M-codes work. Doing so involves parameter settings, and the involved parameter numbers vary among FANUC CNC models. The user must reference custom macro documentation for their CNC.

In essence, you will be setting up a kind of cross reference table so that when the CNC comes across the user-defined G or M code in a program, it will execute a predetermined custom macro.

Example that redefines the function of the M06 tool change command:

For a popular FANUC CNC model, parameter number 6071 is used to specify the M-code number that will call program number O9001. We will set this parameter to a value of 6. From then on, whenever the CNC reads an M06, it will execute program O9001. Instead of just making a tool change, change the function of M06 so that it first moves the machine axes to the tool change position and orients the spindle:

  • O9006
  • G91 G28 Z0 M19
  • M06
  • M99

User-defined G codes work much the same way, though a different group of parameters and program numbers is involved. With user-defined G codes, you can include arguments in the calling command. You can also create modal user-defined G codes.

What we have shown is basic. You should be starting to see, however, how CNC-related features might help in your own applications.

HCL CAMWorks
IMTS 2026
Techspex
MMS Top Shops
One-Touch Clamps
Marposs GEM CMS: Advance Machine Protection System
Demo Fulcrum
Paperless Parts machine shop software

Related Content

Sponsored

Continuous 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 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
Sponsored

Swiss-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 More
CNC Tech Talks

4 Commonly Misapplied CNC Features

Misapplication of these important CNC features will result in wasted time, wasted or duplicated effort and/or wasted material.

Read More

Read Next

CNC & Machine Controls

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 More
Top Shops

2025 Top Shops Benchmarking Survey Now Open Through April 30

Modern Machine Shop's Top Shops Benchmarking Survey is now open, offering metalworking and machining operations actionable feedback across several shopfloor and business metrics. 

Read More
Workforce Development

Shop 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
HCL CAMWorks