Types and applications of macro programs
The meaning of macro program in CNC system
Macro Program or User Macro in CNC system is a powerful function that allows users to use programming structures such as variables, arithmetic and logical operations, conditional branches (IF...THEN...ELSE), loops (WHILE...DO...END) to write more flexible, more general and parameterizable machining programs.
Differentiate macro programs by how they are written and used
Type A:
Features:
A macro is called using a specific G code (usually G65) or M code, and is followed directly by a macro command word (starting with the letter H) and parameters (variable addresses). Arithmetic and logical operations are implemented using specific H codes.
Advantage:
As an early standard, some old systems or specific functions may only support Class A.
Shortcoming:
The syntax is not intuitive enough, the readability is poor, and the functions are relatively limited (especially the logical control is weak).
The following are the operation instructions of Class A macro programs (different CNC systems have different definitions of operation instructions, the following table is for reference only)
Basic operations | ||
Assignment | G 65 H 01 P #i Q #j | Assign the value of #j to #i |
Addition | G 65 H 02 P #i Q #j R #k | #i = #j + #k |
Subtraction | G 65 H 03 P #i Q #j R #k | #i = #j - #k |
Multiplication | G 65 H 04 P #i Q #j R #k | #i = #j × #k |
Division | G 65 H 05 P #i Q #j R #k | #i = #j / #k |
Logical operations | ||
Logical OR | G 65 H 11 P #i Q #j R #k | #i = #j or #k |
Logical AND | G 65 H 12 P #i Q #j R #k | #i = #j and #k |
Trigonometric operations | ||
Sine function | G 65 H 31 P #i Q #j R #k | #i = #j × sin(#k) |
Cosine function | G 65 H 32 P #i Q #j R #k | #i = #j × cos(#k) |
Tangent function | G 65 H 33 P #i Q #j R #k | #i = #j × tan(#k) |
Inverse tangent | G 65 H 34 P #i Q #j R #k | #i = atan(#j / #k) |
Other operations | ||
Square root | G 65 H 21 P #i Q #j | #i = √#j |
Absolute value | G 65 H 22 P #i Q #j | #i = |#j| |
Transfer Instructions | ||
Unconditional transfer | G 65 H 80 P n | Jump directly to program segment n |
Conditional branching | ||
It is equivalent to turning | G 65 H 81 P n Q #j R #k | When #j = #k, go to program segment n |
Does not mean transfer | G 65 H 82 P n Q #j R #k | When #j ≠ #k, go to program segment n |
If less than | G 65 H 83 P n Q #j R #k | When #j < #k, go to program segment n |
If greater than | G 65 H 84 P n Q #j R #k | When #j > #k, go to program segment n |
Transfer if ≤ | G 65 H 85 P n Q #j R #k | When #j ≤ #k, go to program segment n |
If ≥ | G 65 H 86 P n Q #j R #k | When #j ≥ #k, go to program segment n |
Type B:
Features:
The macro body itself is written like a subroutine ( Oxxxx ), using standard arithmetic operators (+, -, *, /, =), logical operators (EQ, NE, GT, GE, LT, LE, AND, OR, NOT), and flow control statements (IF, GOTO, WHILE).
Advantage:
The syntax is intuitive, readable, powerful (supports complex logic and loops), and the programming style is closer to high-level languages. It is currently the most commonly used and most recommended type of macro program.
Shortcoming:
Requires newer system support
Programming
The following are the operation instructions of Class B macro programs (different CNC systems have different definitions of operation instructions, the following table is for reference only)
Arithmetic operations |
||
addition |
#i=#j+#k |
For example, #5=#10+#32 |
Subtraction |
#i=#j-#k |
For example, #8=#5-#20 |
multiplication |
#i=#j*#k |
For example, #108=#7*#12 |
division |
#i=#j/#k |
For example, #110=#100/#2 |
Function operation |
||
Trigonometric functions |
#i=sin[#j] |
Sine function |
#i=cos[#j] |
Cosine function |
|
#i=tan[#j] |
Tangent function |
|
#i=atan[#j]/[#k] |
Inverse tangent function |
|
Other functions |
#i=sqrt[#j] |
Square root |
#i=abs[#j] |
Take the absolute value |
|
#i=round[#j] |
rounding |
|
#i=fix[#j] |
Round down |
|
#i=fup[#j] |
Round up |
|
Control instructions |
||
Conditional branching |
if[conditional expression] goto n |
If the condition of the conditional expression is met, the corresponding operation of the program number n in the program will be executed instead. The program segment number n can be replaced by a variable or expression; if the condition in the expression is not met, the next program segment will be executed sequentially. |
cycle |
while[conditional expression] do m(m = 1,2,3) end m |
When the conditional expression is satisfied, the program segment do m to end m is repeatedly executed; when the conditional expression is not satisfied, the program jumps to the end m . If the while [conditional expression] part is omitted, the program segment do m to end m will be repeatedly executed. |
Actual program comparison
We use the same tool to process the same trapezoidal thread and compare the differences in the programming of macro programs A and B.
Type A
O1000 (MAIN PROGRAM)G54 G99 G21 G40
T0101 (Trapezoidal thread turning tool)
G97 S500 M03
G00 X100.0 Z10.0 M08
(Set basic parameters)
#100 = 50.0 (large diameter D)
#101 = 5.0 (pitch P)
#102 = 30.0 (length L)
#103 = 0.0 (Z start point)
#104 = 0.1 (finishing allowance)
#105 = 0.2 (cut depth)
#106 = 0.05 (tool width K)
(Calculated tooth height H=0.5P)
G65 H02 P#107 Q#101 R#101 (P+Q→R)
G65 H11 P#107 Q#107 R2.0 (P÷Q→R)
G65 H11 P#107 Q#107 R2.0 (divide by 2 again)
(Calculate the minor diameter d=D-2H)
G65 H02 P#108 Q#107 R#107 (2H)
G65 H02 P#108 Q#108 R#108 (4H)
G65 H03 P#109 Q#100 R#108 (D-2H)
(Calculate the roughing depth)
G65 H03 P#110 Q#107 R#104 (H-residue)
G65 H11 P#111 Q#110 R#105 (Number of rough machining layers)
(Calculate the crest width W1=0.366P)
G65 H04 P#112 Q#101 R366 (P×366)
G65 H11 P#112 Q#112 R1000 (÷1000)
(Calculate the tooth bottom width W2 = W1-0.13)
G65 H03 P#113 Q#112 R13 (W1-0.13)
G65 H11 P#113 Q#113 R100 (÷100)
(Verify tool width)
G65 H84 P1000 Q#106 R#113 (IF K>W2)
(Roughing cycle)
#114 = 1 (counter)
N10 (Cycle start)
(Calculate current cutting depth)
G65 H02 P#115 Q#114 R#114 (2n)
G65 H04 P#115 Q#115 R#105 (n×cutting depth)
(Limits the maximum cutting depth)
G65 H85 P20 Q#115 R#110 (IF>roughing depth)
#115 = #110
N20
(Calculate current X diameter)
G65 H02 P#116 Q#115 R#115 (2×current cutting depth)
G65 H03 P#117 Q#100 R#116 (D-2×cutting depth)
(Calculate Z borrowing amount)
G65 H11 P#118 Q#115 R#107 (cutting depth/H)
G65 H03 P#119 Q100 R#118 (100-percent)
G65 H11 P#119 Q#119 R100 (÷100)
G65 H04 P#120 Q#119 R30 (×0.3)
(Left side cutting)
G65 H02 P#121 Q#103 R#120 (Z starting point + tool borrowing amount)
G00 Z#121
G76 P020030 Q50 R0.05
G76 X#117 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Right side borrowed tool cutting)
G65 H03 P#122 Q#103 R#120 (Z starting point - tool borrowing amount)
G00 Z#122
G76 X#117 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Counter increments)
G65 H02 P#114 Q#114 R1
G65 H86 P10 Q#114 R#111 (loop judgment)
(Right side of finishing tooth profile)
G65 H11 P#123 Q#113 R2 (W2/2)
G65 H03 P#124 Q#123 R2 (Z offset = W2/2-0.02)
G00 Z[#103 + #124]
G76 P020030 Q20 R0.02
G76 X#109 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Left side of finishing tooth profile)
G00 Z[#103 - #124]
G76 X#109 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Finishing tooth bottom)
G00 Z#103
G76 P000030 Q10
G76 X#109 Z[#102] P[#107*1000] Q50 F#101
G00 X100.0 Z50.0
M30
(Tool too wide alarm subroutine)
N1000 #3000=1 (TOOL TOO WIDE)
M99
Type B
O1000 (Main program for trapezoidal thread turning)G54 G99 G21 G40 (Security Settings)
T0101 (Trapezoidal thread turning tool)
G97 S500 M03 (Constant speed)
G00 X100.0 Z10.0 M08 (safe position, coolant on)
(Set basic parameters)
#100 = 50.0 (large diameter D)
#101 = 5.0 (pitch P)
#102 = 30.0 (length L)
#103 = 0.0 (Z start point)
#104 = 0.1 (finishing allowance)
#105 = 0.2 (cut depth)
#106 = 0.05 (tool width K)
(Calculated tooth height H=0.5P)
#107 = 0.5 * #101 (tooth height)
(Calculate the minor diameter d=D-2H)
#109 = #100 - 2 * #107 (trail)
(Calculate the roughing depth)
#110 = #107 - #104 (roughing depth)
#111 = FIX[#110/#105] + 1 (Number of roughing layers)
(Calculate the crest width W1=0.366P)
#112 = 0.366 * #101 (tooth crest width)
(Calculate the tooth bottom width W2 = W1-0.13)
#113 = #112 - 0.13 (tooth base width)
(Verify tool width)
IF [#106 GT #113] THEN #3000=1 (tool over-width alarm)
(Roughing cycle)
#114 = 1 (counter)
WHILE [#114 LE #111] DO1
(Calculate current cutting depth)
#115 = #114 * #105
IF [#115 GT #110] THEN #115 = #110
(Calculate current X diameter)
#117 = #100 - 2 * #115
(Calculate Z borrowing amount)
#118 = #115 / #107 (cut depth percentage)
#119 = 1 - #118 (remaining percentage)
#120 = 0.3 * #119 (borrowing amount)
(Left side cutting)
#121 = #103 + #120 (Z position)
G00 Z#121
G76 P020030 Q50 R0.05
G76 X#117 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Right side borrowed tool cutting)
#122 = #103 - #120
G00 Z#122
G76 X#117 Z[#102] P[#107*1000] Q[#105*1000] F#101
#114 = #114 + 1
END1
(Right side of finishing tooth profile)
#123 = #113 / 2 (half width)
#124 = #123 - 0.02 (Z offset)
G00 Z[#103 + #124]
G76 P020030 Q20 R0.02
G76 X#109 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Left side of finishing tooth profile)
G00 Z[#103 - #124]
G76 X#109 Z[#102] P[#107*1000] Q[#105*1000] F#101
(Finishing the bottom of the tooth)
G00 Z#103
G76 P000030 Q10
G76 X#109 Z[#102] P[#107*1000] Q50 F#101
(Retract)
G00 X100.0 Z50.0
M30
Conclusion
Through the above program comparison, I believe you can more intuitively see the difference between the writing of A-type macro programs and B-type macro programs. Although the A-type macro program was born earlier and lacks simplicity and ease of use, there are reasonable reasons that the A-type and B-type macro programs can help you complete the processing more efficiently in CNC machining.
Among all lathe products of Smartlathe, Smart brain, Syntec and Fanuc all support type A macro program and type B macro program. If you want to know which system's macro program is more suitable for your use needs, please send us a message and our engineers will recommend a more suitable solution for you according to your use needs.