Macro Programming Examples

This article is covers macro programming examples. If the reader is new to macro programming. It’s best to read Intro to Macro Programming and Advanced Macro Programming first.

Now that we have a basic understanding of macros and variables under our belts, let’s move on to a more complex macro. This macro will create a helical climb-milling toolpath that will machine a hole. Hole geometry, tool geometry, and machining parameters are all set with variables to generate the toolpath, which is depicted in Figure 1 below.

Figure 1: Helical interpolation toolpath

Helical Interpolation Macro Example.

Here is the code with color coded explanations that follow:

%

O1234

(HELICAL INTERPOLATION MACRO)

( USER VARIABLES -DESIGNED TO BE CHANGED BY THE MACHINE OPERATOR )  

#101 = 1.0 (HOLE DIAMETER)  

#102 = 0.5 (TOOL DIAMETER)  

#103 = 0. (Z START - ABSOLUTE)  

#104 = -1. (Z FINISH - ABSOLUTE)  

#105 = 0.1 (DEPTH OF CUT)  

#106 = 12.5 (FEEDRATE IN/MIN)  

#107 = 2500 (SPINDLE RPM)  

#108 = 1 (TOOL NUMBER)  

#111 = 0. (HOLE CENTER X - ABSOLUTE)  

#112 = 0. (HOLE CENTER Y - ABSOLUTE)  

#121 = 0.050 (MINIMUM INTERPOLATION RADIUS)  

( END OF  USER VARIABLES - DO NOT CHANGE ANYTHING BELOW THIS LINE)  

#120 = [#101/2 - #102/2] (HOLE RADIUS MINUS TOOL RADIUS)  

IF [#120 LT [#121 - 0.00001]] GOTO300 (ALARM IF TOOL DIAMETER TOO LARGE)  

IF [#103 LT #104] GOTO301 (ALARM IF Z START LOWER THAN Z FINISH)  

G28 G91 Z0.

G28 G91 Y0.

G90

T#108 M06

S#107 M03

M08

G0 G90 G54 X#111 Y#112 (RAPID TO HOLE CENTER)

G43 H#108 Z1. (RAPID TO CLEARANCE HEIGHT)  

G01 Z#103 F50.  

G03 X#111 Y[#112 + #120] I0. J[#120/2] F#106 (ARC IN)  

#1 = [#103 - #105]  

WHILE[#1GT[#104 + 0.00001]]DO1   * While loop: the +0.00001 is present to handle any floating-point rounding errors that the calculation may generate. A floating-point rounding error here can cause an extra pass to be executed, adding unnecessary time.

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] Z#1   * Helical pass to [Z] depth (#1) | Helix ③

#1=[#1 - #105]   * Set [Z] depth (#105) for next pass

END1   * End the while loop

 

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] Z#104 (FINAL DEPTH)   * Last helical pass to final [Z] depth (#104) | Helix ③

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] (FLOOR - NO Z)   * Full circle at [Z] final depth (#104) to clean floor | Circle ④

G03 X#111 Y#112 I0. J[-0.5 * #120] (ARC OUT)   * Arc out at [Z] final depth (#104) back to hole center | Arc ⑤

G0 Z1. (RAPID TO CLEARANCE HEIGHT)

M05

M09

G28 G91 Z0.

G28 G91 Y0.

G90

GOTO305 (SKIP OVER ALARMS)

 (ALARMS)

 N300

#3000 = 1. (TOOL DIAMETER TOO LARGE)

M30

N301

#3000 = 1. (Z START LOWER THAN Z FINISH)

M30

 N305

M30

%

Code explanations

The above code is broken into sections with bold text. Explanations in italics and brackets {} follow.

%

O1234

( USER VARIABLES -DESIGNED TO BE CHANGED BY THE MACHINE OPERATOR )  

#101 = 1.0 (HOLE DIAMETER)  

#102 = 0.5 (TOOL DIAMETER)  

#103 = 0. (Z START - ABSOLUTE)  

#104 = -1. (Z FINISH - ABSOLUTE)  

#105 = 0.1 (DEPTH OF CUT)  

#106 = 12.5 (FEEDRATE IN/MIN)  

#107 = 2500 (SPINDLE RPM)  

#108 = 1 (TOOL NUMBER)  

#111 = 0. (HOLE CENTER X - ABSOLUTE)  

#112 = 0. (HOLE CENTER Y - ABSOLUTE)  

#121 = 0.050 (MINIMUM INTERPOLATION RADIUS)  

{The user variables are generally self explanatory with this possible exception of variable #120. This variable is used to set a minimum value for the radius of the helix. It is important to have a minimum radius so that the machine’s motion properly interpolates the circular toolpath. Having an excessively small radius from a tool diameter that is too close to the hole diameter can produce a hole that is not round, and can cause chatter from an excessive tool contact angle, especially if the machine has significant backlash in the axes. See figure 2 below.}

#1 = [#103 - #105]  

{Set variable to first depth for first pass}

Figure 2: Comparison between smaller and larger tool diameters

#120 = [#101/2 - #102/2] (HOLE RADIUS MINUS TOOL RADIUS)  

{This variable is a calculation of our hole’s radius minus our tool’s radius, to simplify the code in our macro’s subsequent calculations.}

IF [#120 LT [#121 - 0.00001]] GOTO300 (ALARM IF TOOL DIAMETER TOO LARGE)  

{This conditional expression will skip to the alarm near the end of our macro to warn the operator if they have entered values that result in our toolpath’s radius being smaller than our specified minimum (#121). The -0.00001 is present to handle any floating-point rounding errors that the (#120) calculation may generate.}

IF [#103 LT #104] GOTO301 (ALARM IF Z START LOWER THAN Z FINISH)  

{This conditional expression will skip to the alarm near the end of our macro to warn the operator if they have entered a [Z] start value (#103) that is below the [Z] finish value (#104).}

G28 G91 Z0.

G28 G91 Y0.

G90

T#108 M06

S#107 M03

M08

G0 G90 G54 X#111 Y#112 (RAPID TO HOLE CENTER)

G43 H#108 Z1. (RAPID TO CLEARANCE HEIGHT)  

{Here we send the tool home, call the tool number, set the speed, turn on the coolant and rapid to positions as commented. Tool length [H] offset (#108) same as tool number}

G01 Z#103 F50.  

{Here we feed the tool to our Position as in Figure 1.}

G03 X#111 Y[#112 + #120] I0. J[#120/2] F#106 (ARC IN)  

{The tool arcs in at [Z] start level (#103) from hole center to toolpath Y+ quadrant. This is position in Figure 1.}

#1 = [#103 - #105]  

{Set variable to first depth for first pass}

WHILE[#1GT[#104 + 0.00001]]DO1  

{While loop: the +0.00001 is present to handle any floating-point rounding errors that the calculation may generate. A floating-point rounding error here can cause an extra pass to be executed, adding unnecessary time.}

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] Z#1  

{Helical pass to [Z] depth (#1). In our example, this cuts a helix down -0.1 repeated with the loop| Helix ③ }

#1=[#1 - #105]  

{Set [Z] depth (#105) for next pass}

END1  

{End the while loop}

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] Z#104 (FINAL DEPTH)  

{Last helical pass to final [Z] depth (#104) | Helix ③ }

G03 X#111 Y[#112 + #120] I0. J[-1 * #120] (FLOOR - NO Z)  

{Full circle at [Z] final depth (#104) to clean floor | Circle ④ }

G03 X#111 Y#112 I0. J[-0.5 * #120] (ARC OUT)  

{Arc out at [Z] final depth (#104) back to hole center | Arc ⑤ }

G0 Z1. (RAPID TO CLEARANCE HEIGHT)

M05

M09

G28 G91 Z0.

G28 G91 Y0.

G90

{Rapid out of the hole, turn off spindle and to machine home}

GOTO305 (SKIP OVER ALARMS)

 (ALARMS)

N300

#3000 = 1. (TOOL DIAMETER TOO LARGE)

M30

N301

#3000 = 1. (Z START LOWER THAN Z FINISH)

M30

N305

M30

%

{Skip over alarms if no conflicts are present and end program}