Get With The Program: Making the most of macros

Get With The Program: Making the most of macros

Using macros, which are pieces of instruction that provide shortcuts, greatly improves programming productivity, according to the Get With The Program column in the July 2017 issue of Cutting Tool Engineering magazine.

July 17, 2017By Edward Strinden

Once upon a time, G-code programs were simple. The controller read them from the tape, beginning to end, and moved the cutting tool, step-by-step, to make a finished part.

With the advent of "conditional branching" and "variables," the controller could create complex movements using only a few lines of code. These two functions—branching and variables—enable programs to repeat themselves while overwriting their values, making the controller "Turing complete." (Turing refers to English mathematician Alan Turing, who is credited with breaking the German Enigma Code during World War II.) "Turing completeness" describes the set of systems that can simulate each other.

In theory, Turing completeness means that a CNC can do anything any other computer can do. Want to turn a milling machine into an arcade game? It's possible—but your boss will get mad if production stops because you ran out of tokens.



Using macros greatly improves programming productivity.


Macros are pieces of instruction that provide shortcuts. The simplest milling macro, for example, tells the machine to repeat a cutting pattern for multiple depths:

(MILL A PATTERN TO Z-0.699)

N100 VC1=[0] (INITIALIZE VARIABLE)

N110 VC1=[VAR-0.25] (1/4"DOC)

N120 G0X0Y0 (STARTING POSITION)

N130 G1Z=[VC1]

<>

N990 G0Z0.1 (RETRACT)

N1000 IF[VC1 GT -1.0]N110 (LOOP

UNTIL Z=-1.0)

This code sets a variable for the starting DOC (N100). The code then decrements it (N110), feeds the tool to the depth for each step (N130), mills the pattern, then retracts the tool (N990) and checks to see if the program should decrement again (N1000).

Note that the program uses the two functions of Turing completeness: a variable (VC1) and a conditional branch (IF). It also makes excessive use of comments—notes added to the code in parentheses that cause the controller to ignore them. Comments make it easier to understand more complicated programs when you inevitably have to go back to debug them or extend their functionality. They also help in the often-used practice of reusing code, aka copying and pasting.

But rather than copying and pasting, it's better to create subprograms. Subprograms are a way to encapsulate a toolpath in a single block of code and then use that code for multiple programs. Subprograms make your code shorter, cleaner and easier to maintain—when written properly.

As with all code, you should write subprograms defensively. Try to envision as many different situations as possible where you could use the code. Should the code apply to both G90 (absolute coordinates) and G91 (relative coordinates)? Do you need to check any other modal values, such as whether the spindle is running or the current coordinate system? Do you need a clearance zone? Take a few moments to code in as much flexibility as possible and you will save hours of work later.

Following are a few coding best-practices for maximizing flexibility:

  • Write each macro to perform just one essential function. If a single tool performs two operations, write a macro for each operation and execute one after the other. If these two operations are performed on multiple workpieces, write one macro for each operation and a third macro that executes the two operations one after the other. Execute this third macro once for each workpiece.
  • Use local variables instead of global variables. In production code, all variables in calculations should only exist within the context of the subprogram. This keeps them from being overwritten as control passes from one macro to another. If the variables in a macro can be overwritten, there's no guarantee the values will remain sensible.
  • It's wise to have error checking in production code. A few lines at the beginning of a macro can compare the argument variables to expected values, generating an error if the values are unexpected or missing. This step may seem unnecessary now, but you will thank yourself down the road.
  • When possible, code should incorporate cutter compensation for fine adjustment of feature dimensions through the use of the offset values. It also allows you to apply a tool with a different diameter to create the same part features.

Once the toolpaths have been created, the post-processor creates massive linear programs that are read from beginning to end, wasting the variable and conditional-branching capabilities of the CNC. You will often find that modeling a part in CAM software, creating the toolpaths and generating, say, 500 lines of G code takes more time than writing an equivalent 50-line program by hand. Macros can save time and increase productivity.

Glossary terms in this article

  • cutter compensation
    Feature that allows the operator to compensate for tool diameter, length, deflection and radius during a programmed machining cycle.