Add Intelligence to a Tool-Changing Custom Macro

Knowing what is accessible from within a custom macro can help you decide whether you need a more-intelligent tool-change command.


Facebook Share Icon LinkedIn Share Icon Twitter Share Icon Share by EMail icon Print Icon

In past columns, we have discussed how to create user-defined G and M codes. The custom macro section of your control manufacturer’s programming manual will show how this is done. Generally speaking, a parameter is set to specify the M-code number that will call a specific program, such as program number O9001. If the parameter is set to a value of six, for example, the control will execute program O9001 whenever an M06 is commanded. Note that if an M06 is specified in program O9001, the machine will perform the normal function of M06 (tool change). That is, it will not try to call program O9001 again.

We can use this ability to redefine what happens when a tool change is commanded. One helpful technique is to make the machine move to its tool-change position as part of the M06 command, which will shorten programs and eliminate the possibility of a mistake. Assuming the user-defined M-code parameter has been appropriately set to M06 for program O9001, consider this simple custom macro:


G91 G28 Z0 M19 (Move to tool change position, orient spindle)

M06 (Change tools)

M99 (End of custom macro)

For this machine, we’re saying the tool-change position is the machine’s Z-axis zero return position. The first command sends the machine to this position and orients the spindle so the machine will be ready for a tool change when the tool-change position is reached. The M06 in the next command makes the tool change. This command will do the normal (machine’s) function of M06—changing tools. It will not try to call program O9001 again. The M99 command ends the custom macro and returns program execution to the main program.

Though this is a good technique, there is nothing very intelligent about it. However, consider all the things that custom macro B enables you to access: the current value of any CNC word (like T), machine position relative to program zero, machine position relative to the zero return position, tool offset register values, fixture offset register values, and rotary axis position, among others.

Knowing what is accessible from within a custom macro can help you decide whether your tool-change command should be more intelligent. If you never have had concerns in this regard, there may be nothing more to do with your tool-change command. On the other hand, maybe you have had problems with interference, such as tools crashing into fixtures or workpieces during tool-changing commands. Maybe you have had problems with tool-length-compensation offset or fixture offset entry mistakes. Whatever the issue, you can probably build in more intelligence to achieve a needed action.

Consider, for example, a vertical machining center that is using a large fixture for a particular application. From experience, you know that cutting tools must be shorter than 8 inches to clear the fixture. Use the tool changing custom macro to test the tool length compensation offset value (we’re storing the length of each tool in the offset), and determine if the cutting tool will clear the workholding setup before the tool-change command is made.

Our example is for a single-arm tool-changing system, so we can monitor the current value of the T word to determine the tool station number, which is the same as the tool-length compensation offset number for error-trapping purposes. This value is automatically placed in system variable #4120 for our machine whenever a T word is executed. With a double-arm system, we could use #500 series system variables to track the two tools involved with every tool-change-ready position tool and spindle position tool.

O9001 (Tool changing custom macro)

IF [[#[2000+#4120] LT 8.0] GOTO 50

#3000 = 100 (TOOL TOO LONG)

N50 G91 G28 Z0 M19



Again, #4120 contains the current value of the most-recently executed T word. If T12 has been commanded just before this M06, the value in #4120 will be 12. The #2000 series variables give us access to tool-length compensation offsets for this control model. The words #[2000+#4120] will render #2012 if the last specified T word is T12. Also #2010 contains the current value of tool length compensation offset register number 12, which is the length of tool number 12.

If this value is less than 8 inches, everything is fine, and the custom macro will skip the alarm-generating command (#3000). If not, we’ve just saved a crash.

Our example shows just one potential problem you can error-trap by making your tool-changing custom macro more intelligent. Use your own experience to handle the specific problems you have. With a little ingenuity, there is little that cannot be error-trapped.