Apr 15, 2023 Leave a message

Let's Talk About CNC Macro Programs

 

Simply put, a macro is to use formulas to process parts. For example, ellipse, if there is no macro, we need to calculate the points on the curve point by point, and then slowly approximate it with a straight line. If it is a workpiece with high smoothness requirements, then we need to calculate a lot of points, but after applying the macro, We input the ellipse formula into the system and then we give the Z coordinate and add an amount each time, then the macro will automatically calculate the X coordinate and perform cutting. In fact, the main function of the macro in the program is calculation.

picture

01

About macro programs

What is a macro program

When programming, we will store a series of instructions that can complete a certain function into the memory like a subroutine, and call them with a general instruction. When using it, we only need to give this general instruction to execute the function stored This series of instructions is called the user macro program body, or macro program for short.

This general command is called the user macro call command. When programming, programmers only need to memorize macro instructions but not macro programs.

When will macro programming be used?

1) Manually programmed processing formula curve (simple calculation, fast input)

2) Regular cutting path (as a cutting module)

3) Inter-program control (program scheduling)

4) Tool management (tool wear)

5) Automatic measurement (in-machine probe)

The difference between macro program and normal program

1) In the macro program body, variables can be used, values can be assigned to variables, calculations can be performed between variables, and programs can be jumped.

2) In ordinary programs, only constants can be specified, and operations between constants cannot be performed. Programs can only be executed sequentially and cannot be jumped, so the functions are fixed and cannot be changed.

3) The macro function is a special function for the user to improve the performance of the CNC machine tool, and the skillful use of the macro program in the processing of similar workpieces will achieve twice the result with half the effort.

02

Variables and formats of macro programs

Features of macro programs

The macro program can use the variable, and the variable can be used to perform corresponding operations; the actual variable value can be assigned to the variable by the macro program instruction.

Three Types of Variables

The variable representation form of the CNC system is "#" followed by 1 to 4 digits, and there are three types of variables:

(1) Local variables: #1~#33 are variables used locally in the macro program, which are used for independent variable transfer.


(2) Common variable: the user can use it freely, and it is common to each subroutine and each macro program called by the main program. #100~#149, after turning off the power, all the variable values will be cleared, while #500~#509, after turning off the power, the variable values can be saved.


(3) System variable: It is defined by followed by 4 digits, it can obtain read-only or read/write information contained in the machine tool processor or NC memory, including exchange parameters related to the machine tool processor, machine tool state acquisition parameters, System information such as processing parameters.

Simple calling format of macro program

The simple call of the macro program means that in the main program, the macro program can be called by a single block.

Invocation format:

G65 P (macro program number) L (number of repetitions) (variable assignment).

Among them: G65—macro program call command;

P (macro program number) - the code of the macro program to be called;

L (number of repetitions) - the number of repeated runs of the macro program, when the number of repetitions is 1, it can be omitted;

(Variable Assignment) - Assign values to variables used in the macro program.

The same thing between a macro program and a subroutine is that one macro program can be called by another macro program, up to 4 times.

Macro program writing format

The writing format of a macro program is the same as that of a subroutine. Its format is:

0~(0001~8999 is the macro program number)

N10 command

N~M99

In the content of the above macro program, in addition to the commonly used programming instructions, variables, arithmetic operation instructions and other control instructions can also be used. The variable value is assigned in the macro program call instruction.

03

FANUC system macro program application

(1) Macro program grooving

picture

1) WHILE statement

G00 X52 Z2;

#2=-14;

It is the starting point of the tool in the z direction (because the tool width is 4mm, the starting point is set at Z-14)

WHILE [#2 GE -30] DO2;

It is a constraint in the z direction. When z is equal to -30, the z direction will no longer move

G00 Z〔#2〕;

The current position in the z direction

#2=#2-2;

The moving step in the z direction, moving 2mm each time

#1=52;

is the starting point of the knife in the x direction

WHILE [#1 GE 20] DO1;

Constraints in the X direction, when the diameter is equal to 20, it will no longer cut

G01 X〔#1〕F0.2;

Depth of cut in x direction

G00 X〔#1+1〕;

Relative retraction amount in x direction

#1=#1-1;

Step distance in x direction (cut 1mm each time)

END1;

G00 X52;

END2;

Complete program:

O1234;

G40 G97 G99;

T0101;

S1000 M3;

G00 X52 Z2;

#2=-14;

WHILE〔#2GE-30〕DO2; END1;

G00 Z〔#2〕;

#2=#2-2;

#1=52

WHILE〔#1GE20〕DO1;

G01X〔#1〕F0.2;

G00X〔#1+1〕;

#1=#1-1;

G00 X52;

END2;

G00 X150 Z150;

M30;

2) IF statement

G00 X52 Z-2;

#1=-14;

It is the z-direction starting point of the tool (the width of the tool is 4mm)

N2 #1=#1-2;

is the movement step in the z direction

#2=52;

is the starting point of the tool in the x direction

N1#2=#2-1;

is the step distance in the x direction (cutting depth 1mm each time)

G01 X〔#2〕F0.2;

Current position in X direction

G00 X〔#2+1〕;

Relative retraction amount in X direction

IF [#2 GE 21] GOTO1;

Constraints in the x direction (when the value of x is cut to 20, the following procedure will be performed, and no return will be made)

G00 X52;

X retracts to position 52

G00 Z〔#1〕;

Current position in Z direction

IF [#1 GE -30] GOTO2;

Constraints in the Z direction, when z is equal to -30, the z direction will not move

Complete program:

O1234;

G40G97G99;

T0101;

S1000M3;

G00 X52 Z-2;

#1=-14;

N2 #1=#1-2;

#2=52;

N1#2=#2-1;

G01 X〔#2〕F0.2;

G00 X〔#2+1〕;

IF〔#2GE21〕GOTO1;

G00X52;

G00Z〔#1〕;

IF[#1GE-30]GOTO2;

G00X200;

Z200;

M5;

M30;

(2) Ellipse programming

1) The standard format of the ellipse WHILE statement:

#1=a;

a: The starting point of the tool is at the positive direction a mm relative to the axis Z of the ellipse

WHILE [#1 GE b] DO1;

b: The end point of ellipse processing is at the negative direction b mm relative to the axis Z of the ellipse (if a complete semi-ellipse is processed, then a and b are two values with the same value and different signs)

#2= c*SQRT[1-#1*#1/d*d];

c: the semiminor axis of the ellipse

d: semi-major axis of the ellipse (calculate #2 according to the ellipse formula, the semi-major axis is d, the semi-minor axis is c, #2 represents the value of X, #1 is the value of Z, and SQRT means square root)

G01 X〔±2*#2+e〕Z〔#1±f〕;

e: The offset (diameter value) of the X axis of the ellipse relative to the workpiece coordinate system

f: The offset of the Z axis of the ellipse relative to the workpiece coordinate system

#1=#1-1; step distance (moving 1mm each time)

END1;

Note: When turning a concave ellipse, the "±" in the parenthesis after X is taken as "-"; when turning a convex ellipse, the "±" in the parenthesis after X is taken as "+".

When the X-axis of the ellipse shifts to the positive direction, the "±" in the brackets after Z takes "+"; when the X-axis of the ellipse shifts to the negative direction, the "±" in the brackets after Z takes "-"

2) The standard format of the elliptical IF statement

#1=a;

a: The starting point of the tool is at the positive direction a mm relative to the axis Z of the ellipse

N1#2=b*SQRT〔1-#1*#1/c*c〕;

b: semi-short axis of the ellipse c: semi-major axis of the ellipse (according to the ellipse formula X/c+ Y/b=1, SQRT means square root)

G01X〔±2*#2+d〕Z〔#1±e〕F0.2; d: the offset (diameter value) of the X axis of the ellipse relative to the coordinate zero point e: the Z axis of the ellipse relative to the zero plane Offset

#1=#1-1;

Step distance (moving 1mm each time)

IF [#1 GE -f] GOTO1

f: Termination of ellipse processing

 

Note: When turning a concave ellipse, the "±" in the parenthesis after X is taken as "-"; when turning a convex ellipse, the "±" in the parenthesis after X is taken as "+". When the X-axis of the ellipse deviates to the positive direction, the "±" in the parentheses after Z takes "+"; when the X-axis of the ellipse deviates to the negative direction, the "±" in the parentheses after Z takes "-".

picture

WHILE statement

#1=20;

WHILE〔#1GE-20〕DO1;

#2=10*SQRT〔1-#1*#1/400〕;

G01X〔-2*#2+50〕Z〔#1-25〕;

#1=#1-1;

END1;

IF statement

#1=20;

N1#2=10*SQRT〔1-#1*#1/400〕;

G01X〔-2*#2+50〕Z〔#1-25〕F0.2;

#1=#1-1;

IF[#1GE-20]GOTO1;

complete program

O1234;

G40G97G99;

T0101;

S1000 M3;

G00 X50 Z2;

G73 U5 R5;

G73 P10 Q20 U0.5 F0.2;

N10 G0 G42 Z-5;

#1=20;

WHILE〔#1GE-20〕DO1;

#2=10*SQRT〔1-#1*#1/400〕;

G01X〔-2*#2+50〕Z〔#1-25〕F0.2;

#1=#1-1;

END1;

G00 X50;

N20 G00 G40 Z2;

G70 P10 Q20;

G00 X200;

Z200;

M5;

M30;

The complete format of the IF statement is omitted (the same is true for the IF statement, as long as the cycle is added). In the FANUC-0i system, the macro program can only be added in G73.

(3) Processing of parabola

1) The standard format of the parabolic WHILE statement:

#1=a;

a: The starting point of the tool is a mm in the direction of the parabolic axis Z

WHILE [#1 GE -b] DO1;

b: is the processing length of the ellipse in the z direction

#2=SQRT〔-#1*5/3〕;

(According to the parabolic formula Z=-3/5*X*X, find the value of X, which is #2, where SQRT means the square root)

G01 X〔±2*#2+c〕Z〔#1〕;

c: is the offset (diameter value) of the X axis of the parabola relative to the workpiece coordinate system, "±"

When taking "+", it is convex, and when taking "-", it is concave

#1=#1-1; Step distance (moving 1mm each time)

END1;

2) The standard format of the parabolic IF statement

#1=a;

a: The starting point of the tool is a mm in the direction of the parabolic axis Z

N1 #2=SQRT〔-#1*5/3〕;

(According to the parabolic formula Z=-3/5*X*X, find the value of X, which is #2, where SQRT means the square root)

G01 X〔±2*#2+b〕Z〔#1〕;

b: It is the offset (diameter value) of the X-direction axis of the parabola relative to the coordinate zero point. When "±" takes "+", it is convex, and when "-" is taken, it is concave

#1=#1-1;

(step distance in Z direction, each movement is 1mm)

IF〔#1 GE -c〕GOTO1; c: the processing length of the ellipse in z direction

Parabolic IF

another form of sentence

#1=a;

N1 #2=SQRT〔(+)#1*5/3〕;

The "+" sign can be omitted

G01 X〔2*#2+b〕Z〔-#1〕;

#1=#1+1;

IF [#1 LE c] GOTO1;

Assuming that the parabola is in the positive direction of Z, then use Z〔-#1〕; to make the parabola symmetrical to the negative direction

picture

WHILE statement

#1=0;

WHILE [#1 GE -15] DO1;

#2=SQRT〔-#1*5/3〕;

G01 X〔2*#2+30〕Z〔#1〕;

#1=#1-1;

END1;

IF statement

#1=0;

N1 #2=SQRT〔-#1*5/3〕;

G01X〔2*#2+30〕Z〔#1〕;

#1=#1-1;

IF [#1 GE -15] GOTO1;

complete program

O1234;

G40 G97 G99;

T0101;

S1000 M3;

G00 X42 Z1;

G73 U5 R5;

G73 P10 Q20 U0.5 F0.2;

N10 G00 G42 Z0;

#1=0;

WHILE [#1 GE -15] DO1;

#2=SQRT〔-#1*5/3〕;

G01 X〔2*#2+30〕Z〔#1〕;

#1=#1-1;

END1;

G00 X42;

N20 G00 G40 Z2;

G70 P10 Q20;

G00 X200;

Z200;

M5;

M30;

(4) The difference between WHILE statement and IF statement

1) The directions of the two statements are different

The WHILE statement returns backwards

Example: WHILE〔#1 GE 20〕DO1;

G01 X〔#1〕F0.2;

Assuming that when the machine tool executes this sentence, #1=20, it will continue to execute. After executing #1=#1-1, the value of #1 becomes 19, which no longer meets the constraint conditions, so it will not return. (Cut to 20 in the X direction)

G00 X〔#1+1);

#1=#1-1;

END1;

2) The IF statement returns forward

Example: N1 #2=#2-1;

G01X〔#2〕F0.2; Assuming that #2=20 when the machine tool executes this sentence, it will continue to execute until IF〔#2 GE 20〕GOTO1; if the condition is still satisfied, it will continue to return to N1# 2=#2-1; and the current X value will become 19, which no longer meets the constraint conditions, and then execute another

G01X〔#2〕F0.2; Finally, execute the following program (X direction has been cut to 19)

G00X〔#2+1);

IF [#2 GE 20] GOTO1;

3) As can be seen from the above grooving program, the number of words in the IF statement is much less than that of the WHILE statement.

4) Due to the different return directions, read one less sentence for the WHILE statement and one more sentence for the IF statement during processing.

04

SIEMENS system (lathe) macro program application

Note: The macro program is programmed with variables, and the variable number of the Siemens system is represented by R.

For example, written in common programming method: G01X-10

The macro program can be expressed as:

R1=-10

G01 X=R1

Conditional transfer:

IF GOTOB: jump backwards

IF GOTOF: jump forward

written in common programming

GO1X100

Variables can be expressed as:

R1=0

AA: R1=R1+1

G01X=R1

IF R1<100 GOTOB AA

R1 is an independent variable, the initial value is 0, R1=R1+1 means that the incremental value of the independent variable is 1, when the program goes through this line every time, the value of R1 increases by 1, R1<100 is a conditional expression, IF R1<100 GOTOB AA This line means that if the argument R1<100, the program jumps backward to the mark: AA

If R1 is greater than or equal to 100, the program goes down.

Macro programs can be used in both G90 and G91 modes, but their meanings are different, for example;

R1=0, G90R1=R1+1, G1X=R1, the value of X after the second pass of this program is 2.

R1=0, G91R1=R1+1, G1X=R1, the value of X after the second pass of the program is 3. Explanation: The value of R1 is 1 after the first pass of the program, and the value of R1 is the second pass It is 2, but in G91 mode it is based on the previous one.

(1) Grooving

picture

T1

TC

T1D1

G0G40X100Z100

M03S1000

G0X54Z2

Quickly reach the starting point

Z-10

R1=3

Define the blade width as 3mm

R2=-10-R1-0.2

The starting point of the tool is -10, and the left side of the blade is used when setting the tool;

Tool setting, so the width of the blade should be subtracted, 0.2 is the finishing allowance

G1Z=R2F0.1

The tool reaches the starting point of the Z axis

AA:R2=R2-2.5

R3=50

The X axis of the groove reaches the point

BB: R3=R3-2

Define the cutting depth of each knife as 2 mm

G1X=R3

X=R3+1

0.5mm chip removal on one side every 2mm depth of cut

IF R3>30+0.4 GOTOB BB

Define the groove depth as 10mm, if R3>30mm, the program jumps backward to the mark BB, and 0.4 is the finishing allowance

G0X50

The tool reaches the starting point of the X axis

G1Z=R2

IF R2>-30+0.2 GOTOB AA

Define the groove width as 20mm, and 0.2 is the finishing allowance

G0X50

G01Z-13

finishing

X30

Z-16

G0X50

Z-30

G01X30

Z-16

G0X50

Withdraw

G0X100

Z100

M05

M30

(2) Ellipse

1) Basic format

R1=0

Define the variable R1 with an initial value of 0

AA:R2=b×SQRT(1-R1×R1/a×a)

According to the ellipse equation, a is the semi-major axis of the ellipse, b is the semi-minor axis of the ellipse, and SQRT is the square root symbol.

G1X=±2×R2+X Z=R1-Z

Set the position and shape of the ellipse, +2 is convex, -2 is concave, X, Z are the distances between the axis of the workpiece and the axis of the ellipse (diameter system).

R1=R1-1

Set the processing step

IF R1>=n GOTOB AA

If the variable R1<n, then jump backward to the mark AA, where n is the coordinate of the end point in the Z direction.

2) Programming example:

picture

T1D1

G0G40X100Z100

M3S1000

G0X52Z2

Z-20

CYCLE95 ( )

G42S1500

OO:

R1=20

AA:R2=5×SQRT(1-R1×R1/400)

G1X=-2×R2+50 Z=R1-40

R1=R1-2

IF R1>=-20 GOTOB AA

PP:X42

G0G40X100Z100

M05

M09

M30

(3) Parabola

1) Basic format:

R1=0

Set the initial value of variable R1 to 0

AA: R2=SQRT(-R1×n)

Obtained according to the basic format of the parabola, where SQRT is the square root symbol, and n is the coefficient

G01X=2×R2+n

Z=R1

Processing path, +2 is convex, n is the value of the starting point of X axis

R1=R1-1

The variable increment value is 1mm

IF R1>-30 GOTOB AA

If the variable R1>-30, the program jumps backward to the mark: AA

2) Programming example:

picture

T1

Tc

T1D1

G0G40X100Z100

M03S1000

G0X52Z2

CYCLE95 ( )

G0G42

OO:

R1=0

AA:R2=SQRT(-R1×5/3)

G01X=2×R2+30 Z=R1

R1=R1-2

IF R1>-60 GOTOB AA

PP: X52

G0X100Z100

M05

M30

Send Inquiry

whatsapp

skype

E-mail

Inquiry