YCM Alliance
Published

Second-Referencing Variables in Custom Macro

This technique allows operators to query a large number of values using only a few CNC commands.

Share

Here is a little-known technique that can help you query a large number of values with just a few custom macro commands. It can be used with any series of consecutively numbered variables, such as offset-related system variables, axis-related system variables, common variables and permanent common variables. Examples of when this technique can be helpful include:

• After using a spindle probe to determine how much raw material is on a surface to be machined. Maybe the probe has touched along a series of points on a rough casting and you wish to determine the high point. With this information, you can determine how many roughing passes to take.

• When querying a group of tool offset registers to determine whether any are inappropriately set. Perhaps the operator forgot to enter a value or the value is outside an acceptable range. 

The trick lies in understanding how custom macro variables can be second-referenced. Consider these commands:

#100 = 3.25 (Set common variable #100 to a value of 3.25)
#33 = 100 (Set local variable #33 to a value of 100)
#32 = #[#33] (Set local variable #32 the value of #100, which is 3.25)

In the third command, the first hashtag character (#) of #[#33] tells the CNC that a variable will be referenced; the #33 inside the brackets tells the CNC which variable will be referenced. Variable #33 is currently set to a value of 100, so the CNC currently evaluates #[#33] as #100.

In custom macro terms, this is called second-referencing a variable, and by itself, it may not seem to have helpful implications. Consider, however, that the value of the second-referencing variable (#33 in our example) can be changed, meaning a different variable will be in play. If a loop is used that steps the second-referencing variable number, a large number of variables can be queried with but a few custom macro commands.

Consider this more realistic example: You have a large casting to machine, but you are unsure of how much raw material must be removed from a surface, and that amount of material may change with each new casting.

Say the material to be removed is on the right side (X plus) of the casting, and this side is 20 inches long. If you use a spindle probe to find the most positive position in the X axis on the right side of the casting, the high point, you will be able to calculate the worst-case condition in terms of the amount of raw material to be removed. With this information, you will be able to determine how many roughing passes to take. You then have the spindle probe perform a series of touches, say 40 of them at 0.5-inch increments. You make the probe store the results of these touches (40 X positions) in common variables #101 through #140. Now you need to determine the high point (largest value). Here is a series of commands that will do so:

(Determine largest X value of #101-#140)
#32 = #101 (Starting largest X position)
#33 = 102 (Current common variable number to test)
N10 WHILE [#33 LE 140] DO 1 (Start of loop, testing through #140)
IF [#[#33] GT #32] THEN #32 = #[#33] (Retain larger value, #[#33] or keep #32)
#33 = #33 + 1 (Step second-referencing value)
END 1 (End of loop; #32 is the largest X position in the group)

These six custom macro commands test 39 common variables. When the loop is finished, local variable #32 will contain the largest X value in the group of probed positions. This value then can be compared to the X position of the finished X surface to determine how much raw material must be removed and how many passes to take.

Second-referencing variables can also be used with tool offsets. System variables from #2001 on, for example, are commonly used with machining center CNCs to provide access to tool-length geometry offsets: #2001 for offset one, #2002 for offset two and so on.

Consider this series of commands that tests offset registers from 1 through 20. If any are set to zero, an alarm is sounded (possibly indicating that the operator forgot to set one of them).

(Test offsets 1-20)
#100 = 2001 (Current offset number to test)
N10 WHILE [#100 LE 2020] DO 1 (Start of loop, testing through offset 20)
IF [#[#100] NE 0] GOTO 11
#3000 = 100 (Offset set to zero)
N11#100 = #100 + 1 (Step second-referencing value)
END 1 (End of loop; if loop ends, all offset registers have a value)

These examples show how second-referencing variables can come in handy whenever you have a series of consecutively numbered custom macro variables to query. 

SW North America, CNC Machines and Automation
YCM Alliance
Campro USA
MMS Made in the USA
World Machine Tool Survey
Paperless Parts
JTEKT
KraussMaffei
More blasting. Less part handling.
QualiChem Metalworking Fluids
715 Series - 5-axis complete machining
VERISURF

Related Content

CNC Tech Talks

Strange But True: Odd Things That Happen With CNCs

These oddities in the way a CNC naturally behaves can help explain some rather unusual situations that may occur during machining.

Read More
CNC Tech Talks

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

Improve CNC Productivity by Addressing Three Production Issues

To boost a CNC machine’s productivity, prioritize improvements to machine setup time and production runtime.

Read More

5 Reasons Why You Should Know How to Write Custom Macros

Custom macros enhance what can be done in G-code programs, giving users the ability to code operations that were previously not possible.

Read More

Read Next

Tooling

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

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 More
Workforce Development

Inside 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 More
SW North America, CNC Machines and Automation