Why Use CAM When a Macro Is All You Need?

Many CNC machines are underutilized simply because programmers never feel they have the time to learn how to use all the features, or are not sure what features are included with the machine. If the machine has a Fanuc, Fanuc copy, or Haas controller, it may very well have a custom macro option. 

Figure 1. Haas CNC lathe controller. (Image courtesy of Conestoga College Institute of Technology and Advanced Learning.)

Custom macros do take a bit of time initially to create. However, once a well-written macro is created, it can save hours of programming and program proving time. The use of a well-written custom macro can also help many CNC users avoid the cost of expensive CAM programming packages.  

So, what is a macro? A macro, or custom macro, is a fancy sub-program that is called or invoked not with an M98 but with a G65. A macro sub-program is not always required. Understanding how macros work can often be enough to let you create a very powerful sub-program.

A true macro will perform the desired function in any material, at any speed/feed and as many times/locations as required, without the program needing to be altered for each individual part. For example, bolt-hole circles are often machined. A bolt-hole circle macro will drill, tap, ream or perform whatever hole-making operation is required in any part, any number of times and to any depth. 

How do you know when a macro could save you time? If you find you are repeatedly writing essentially the same program but for different parts, then a macro could very likely save you time in the long run.

How do I know if my machine has the option to run custom macro programs? Place your machine into manual data input (MDI) mode. Enter the code as shown in Figure 2 (nothing in this program will cause any machine movement).

Figure 2. Macro code used to verify that your machine has the option enabled. (Image courtesy of Conestoga College Institute of Technology and Advanced Learning.)
If you have the option to run custom macros, then no alarm will be generated the first time you run this simple program. If you do not have the option, an alarm will be generated. If you wish to use custom macros, you will need to contact your local reseller for that machine and inquire as to the cost to have them enabled. After this has looped nine times, an alarm message will be generated and displayed in one of several ways, as shown in Figure 3 and below.

1 HOLY MACRO or 3001 HOLY MACRO or 1001 HOLY MACRO.

Figure 3. HOLY MACRO alarm. Image courtesy of Conestoga College Institute of Technology and Advanced Learning.

Variables

Creating and understanding a macro is all about seeing patterns, like most CNC programming. These styles of programs typically use variables and equations (simple or complex) in a repetitive pattern to machine the desired features. Variables are simply a place in the machine memory where a positive or negative number can be stored. There are three main variable types: local variables, common variables and system variables.

Local variables can be used at any time, but are typically reserved for macro programs. Local variables on Fanuc-based machines are #1 through to #33. Like any “word” in a CNC program, there cannot be a space between the “#” and the variable number. Remember, variables are just a place to store a positive or negative number. If you program:

“#1 = 0.125.”

 Then 0.125 will be stored in local variable #1. Nothing more is happening. If you program:

#2 = #1 – 0.025.

Then 0.025 will be subtracted from the value stored in variable #1 and the result will be stored in variable #2. In this case, a value of 0.1 would be stored in variable #2.

When local variables are used, it is important to be aware that once the M99 is executed at the end of the sub-program, or if the power is shut off to the machine, all local variables are cleared and are empty.

The “null” variable is “#0.” It may be used to clear the value stored in any variable. For example, if you program:

#1 = #0.

This does not set the value stored in variable #1 to zero, but clears the contents of the machine memory where the data for variable #1 is kept. 

Common or global variables are not reset to null with M99. These variables are #100 to #149 and #500 to #531. In many cases, the one hundred series variables are cleared when power to the machine is lost.  More common variables are available, but these are the default ones, which all Fanuc-based machines should have. Again, these are simply a location to store a number. Figure 3 shows a value of 1.0 stored in variable #100.
Figure 4. Screen shot showing local variable #100 with a value of 1.0. (Image courtesy of Conestoga College Institute of Technology and Advanced Learning.)

System variables start at #1000 and go up from there. They store everything about the machine and are specific to the controller type. The system variables should never be manipulated by an inexperienced individual. System variables store all of the active G/M codes, machine axis positions and many other system settings and values.

Macro Call

Subprograms are called with the M98 command. A typical subprogram call looks like this:

M98 P9000.

When calling a subprogram, you should not program any other code on the same line after the subprogram call, as it will be ignored. For example, if you program:

G90 Z2.0 M98 P9000.

The machine will move the Z-axis to 2.0 units (inches (G20) or mm (G21)) above the absolute Z zero and then run subprogram 9000. However, this line would be coded as follows:

M98 P9000 G90 Z2.0.

The Z-axis movement would not occur because the subprogram will be executed first. However, macro subprograms are typically called with the G65 code, and other data specific to the program is assigned on the same line. This assigned data is stored in the local variables. There are two types of local variables: Argument Specification 1 and 2. For now, I will only look at Argument Specification 1. Table 1 lists the specification 1 variables.

When looking at the table above, you will notice that the letters G, L, O, N and P are not present. These variables cannot be used in a macro program because they are reserved characters on a Fanuc controller.

How are these letter addresses used? In the macro subprogram call shown below:

G65 P0083 X0 Z-1.1 R0.1 Q0.15 F0.012.

The G65 tells the controller that a macro subprogram is being called, and that program-specific information will be assigned. The “P0083” states the subprogram number. The “X0” will assign a value of “0” to local variable #24, the “Z-1.1” will assign a value of “-1.1” to local variable #26, the “R0.1” will assign a value of “0.1” to local variable #18, the “Q0.15” will assign a value of “0.15” to local variable #17 and the “F0.012” will assign a value of “0.012” to local variable #9. 

In the next article, the actual subprogram, or macro “0083,” will be written and explained. Macro programs can be used to perform very complex machining functions. With each article in this series, new functionality will be introduced.

This is Part 1 of a 3-part series. For more information, read Part 2 here.

About the Author






Fred Fulkerson is a graduate of the Faculty of Education, University of Western Ontario, and of the general machining program at Conestoga College in Ontario. He is a Canadian Red Seal certified general machinist and CNC programmer and a certified Mastercam and SOLIDWORKS instructor.