0% found this document useful (0 votes)
2 views

Macro Programming Reference Guide

Uploaded by

Yh Nm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Macro Programming Reference Guide

Uploaded by

Yh Nm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Macro Programming Reference Guide

Copyright 2005 ©
Scott Martinez

Section 1. What is macro programming

Section 2. What are Variables

Section 3. What are Expressions

Section 4. What are Functions

Section 5. Using Comparisons

Section 6. Program Levels and Local Variables

Section 7. Common and System Variables

Section 8. Using variable #0

Section 9. Macro Statements

Section 10. Adding Macro Cycles to the Tools Menu

Section 10. Macro Programming Examples


Introduction

This manual is a beginners guide to writing macro programs for CNC. It explains the basic
functionality of the macro programming language. I will assume you have some knowledge of G-Code
programming already, as this is essential before learning the advanced programming techniques
provided by the macro language.

Section 1. What is macro programming

The macro language is a programming language that gives the CNC programmer the ability to
write very flexible programs. This is done through the use of variables, mathematical expressions and
program flow control statements. The macro language combined with standard G-code programming
can create reusable programs much like canned cycles. These programs can do many useful things like
custom pocketing or automatic tool measurement. As they say, you are limited only by your
imagination.

Section 2. What are variables

Variables are the heart of macro programming. Variables are like numbered storage units, each
of which can either be empty or contain a number. When they contain a number, the variables can be
used in place of almost any numeric value in your G-code program.

Variables are designated with the "#" symbol and are followed by a number or expression that
designates the variable number. Variables can used as the value following any letter address except
“N”.

Examples of how variables can be used:

G1 X#100 Y#101 F10. In this example of a feed move, the X and Y endpoints are determined by
the values contained in the variables #100 and #101.

G#100 X#101 Y#102 In this example, the G-code to be executed is determined by the value
contained in variable #100.

What makes them such a powerful tool is the fact that new values can be assigned to variables by
your program.

Here is an example of assigning a value to a variable:

#100=10.0 The value 10.0 is stored into variable #100. Once this has been set, the
value 10.0 will be used in place of #100.
G1 X#100 This command is then equivalent to G1 X10.0

By simply changing the value stored in this variable, you can make the same program do
different things.
Multiple variables may be assigned in the same block. However, variable assignements must be
on a block by themselves and may not be combined with G-code blocks or other macro statements.

Examples:

#101=1 #102=2 #103=3 This is valid

IF [#1EQ0] THEN #105=5 This is the only macro statement where variables can be assigned.

G01 X#101 #102=4 This is not valid

WHILE [#1NE0] DO1 #104=4 This is not valid

You, as the programmer, should decide the meaning of your variables before you begin writing.
For example, you may want variable #100 to be used to specify a pocket width, or you may want
variable #250 to specify a corner radius. It is completely up to you what the variables mean, but it is
good programming practice to decide before you begin writing.

Section 3. What are Expressions

Expressions are formulas that mathematically combine values to get a resulting number. The
result of an expression is then assigned to a variable or used in a conditional statement.

Example of using an expression to assign a value to a variable:

#100 = #101+1 The expression is the part following the "=" sign and is "#101+1". So 1 is
added to the contents of #101, and this value is then stored in variable
#100.

Expressions may use any combination of operators, functions and comparisons. If no brackets
are used, the values are calculated in the standard arithmetic order. That is, multiply and divides are
performed first, followed by addition and subtraction.

Examples of expressions:

#100 = 10 + 2 * 3 - 5 The value 11 is stored in variable #100


#101 = 8 * 3 – 3 * 4 The value 12 is stored in variable #101

Operators are basic mathematical operations and include:

+ Addition
- Subtraction
* Multiplication
/ Division
^ Raised to the power of
MOD Modulus, the remainder of a division operation
XOR Bitwise XOR
OR Bitwise OR
AND Bitwise AND
Brackets may be used to change the order that the expression is evaluated in. When brackets are
used in an expression, the calculations inside the brackets are performed first, then the rest of the
expression is calculated. The calculations inside the brackets are still performed in standard arithmetic
order:

#100= [10 + 2 * 2] * 3 - 5 This expression evaluates to 37


#100= 8 * [3 - 3] * 4 This expression evaluates to 0

The variable numbers themselves may be replaced with expressions, as long the expression
evaluates to a valid variable number. For example:

#[100+5] = 10 Variable number #105 is assigned the value 10

Variables may also be used to store variable numbers. This is called variable indirect and is a
very useful feature. It can also be confusing, so here’s some examples:

#100 = 105
#[#100] = 10 The value 10 is assigned to variable number #105

Here’s what happens:


#100 = 105 Variable #100 now contains the value 105
#[105] = 10 The expression in brackets is evaluated first
#105 = 10 The variable is assigned using the result of the expression as the variable
number.

It is possible to use multiple levels of indirection:


#1 = 2
#2 = 3
###1 = 10 Variable #3 ends up with the value 10.

Section 4. What are functions

In addition to operators, there are many functions that may be used in an expression. A function
takes a value and calculates a resulting value based on it's particular function. Unlike an operator, a
function only needs one number to work from. The values used for functions must be enclosed in
brackets.

For example:

#100 = SQR[2] Variable #100 is assigned the value 1.414213 which is the square
root of 2.
Here is a complete list of available functions:

SIN Calculates the Sine of an angle in degrees


COS Calculates the Cosine of an angle in degrees
TAN Calculates the Tangent of an angle in degrees
ATN, ATAN Calculates the ArcTangent
ACOS Calculates the ArcCosine
ASIN Calculates the ArcSine
LN Calculates the natural logarithm
SQR, SQRT Calculates the square root
ABS Calculates the absolute value
BIN Converts decimal to hexadecimal
BCD Converts hexadecimal to decimal
RND, ROUND Value rounding
FIX Returns the integer portion of a value
FUP Fractional values are rounded up to the next whole number
EXP Exponent

The value passed to a function may be any valid expression:


#102 = SQR[#100 + #101] The contents of variables #100 and #101 are added and the square
root of this result is calculated.

Section 5. Using Comparisons

The comparators are typically used with the IF - GOTO or IF - THEN macro statements.
However, they may be used in any expression. When used as part of an expression, they return a value
of 1 for TRUE or 0 for FALSE as their result. Comparators are like operators in that they require two
operands.

List of comparators:

EQ Equal to
NE Not equal to
LT Less than
LE Less than or equal to
GT Greater than
GE Greater than or equal to

Comparison examples:

#100 = #101 EQ #102 If #101 equals #102 then #100 is assigned a value of 1. If not, it is
assigned a value of 0.

If comparators are used as part of a larger expression, the comparison should be enclosed in
brackets like this:

#100 = 10 * [#101 EQ #102] + 5 If #101 equals #102 then the expression evaluates to 15, otherwise
it evaluates to 5.
Bitwise operators

Bitwise operators are a convenient way of combining comparisons. The bitwise operators are
OR, AND and XOR.

The "OR" operator:

#100 = [#101 EQ #102] OR [#103 EQ #104] Variable #100 is assigned a value of 1 if #101
equals #102 OR #103 equals #104, otherwise it gets
a value of 0.

The "AND" operator:

#100 = [#101 EQ 1] AND [#102 EQ 2] Variable #100 is assigned a value of 1 if #101


equals 1 AND #102 equals 2, otherwise it gets a
value of 0.

The "XOR" operator:


#100 = [#101 EQ 1] XOR [#102 EQ 2] Variable #100 is assigned a value of 0 if both
expressions are true or both expressions are false. If
one is true and the other is false, #100 is assigned a
value of 1.

Section 6. Program Levels and Local Variables

Local variables are variable numbers #1 to #33 and are typically used as temporary use variables
for subprograms. Even though the variable numbers are the same, each program level has it’s own set of
local variables. The program level changes any time you call or return from a subprogram. The main
program is always level 0. A subprogram call from the main program is level 1.
The G65 macro call command allows passing values to the subprogram through local variables.
This table shows the correspondence between the letter addresses and the variable numbers:

The "x" denotes letters that cannot be used to pass variables.

Variable Address
#1 A
#2 B
#3 C
#7 D
#8 E
#9 F
#10 Gx
#11 H
#4 I
#5 J
#6 K
#12 Lx
#13 M
#14 Nx
#15 Ox
#16 Px
#17 Q
#18 R
#19 S
#20 T
#21 U
#22 V
#23 W
#24 X
#25 Y
#26 Z

Local variable example:

G65 P9000 X10 Y5 Z1 A2.5 B3.6

When this block is executed, these values are assigned to local variables before program 9000
begins. Program 9000 can then read these values in these variables:

#24=10 (X)
#25=5 (Y)
#26=1 (Z)
#1=2.5 (A)
#2=3.6 (B)

These are called local variables because these values are only valid for the program
they are passed to. If program 9000 also used a G65 macro call, the local variables are saved
and the new subprogram gets it's own set of local variables. They do not overwrite the values
being used by program 9000. So when the new subprogram is done, the variables being used by
program 9000 have not changed even though both subprograms use the same variable numbers.
When a subprogram is finished, it's local variables are cleared.

Section 7. Common and System Variables

Common variables are variable numbers #100 to #999 and, unlike local variables, can be used
and set by any program. These variables are also retained on exit.

System variables are variables numbers #1000 to #5999. System variables are used by the CNC
to store internal values needed for operation. These values are things like tool length offsets, diameter
offsets and machine positions. These variables may be set by your program, but care should be taken
when doing so.

Section 8. Using variable #0

Variable #0 is a special variable that cannot be set. Instead, it's value is always <empty>. A
variable that is <empty> is not the same as a variable that has been set to 0. An <empty> variable is a
variable that contains no value, not even 0.

This variable cannot be set, but may be assigned to other variables and used in comparisons.
This is especially useful for subprograms in determining if a value has been given for all required
addresses:

G65 P9000 A0 B2

#1=0 (A)
#2=2 (B)

Variables #1 and #2 contain values because values were given in the G65 block. All other local
variables are cleared to <empty>. If, for example, the address C is also required by the subprogram, it
can check to see if a value was given by comparing it to variable #0. If #3 is equal to #0, then address C
was not included in the G65 block that called the subprogram. Even though #1 was set to 0, this is a
valid value and so it is not equal to #0.
Variables may be cleared to <empty> be assigning them the variable #0.

For example:
#100 = #0 The variable #100 is cleared to <empty>.

This also works using the variable indirect method:

#100 = 0
#110 = ##100 The variable #110 is cleared to <empty> because #100 points to variable #0.

When variable #0 is used in an expression, it is handled as the value 0 except for the
comparisons EQ and NE.

Examples of using #0 in expressions:

#0 + 1 = 1
#0 * 10 = 0
#0 + #0 = 0
#[#101-#101] * 50 = 0

[#0 LT 0] FALSE
[#0 LE 0] TRUE

[#0 EQ 0] FALSE These two comparisons are the only two that handle
[#0 NE 0] TRUE #0 and 0 as being different.

[#0 GT 0] FALSE
[#0 GE 0] TRUE

In general, the variable #0 shouldn't be used in expressions. It's use should be limited to EQ and
NE comparisons and variable assignments.

Section 9. Macro Statements

In addition to variables and expressions, the macro language uses a few macro statements that
can control the flow of the program.

Here is a list of the macro statements:


IF - GOTO
IF - THEN
GOTO
WHILE - DO
DO
END

You'll notice that a few of the statements are grouped together. This is because the statements
work together to determine the exact function performed.
Examples:

IF [#100EQ1] GOTO100 When this block is executed, the expression "[#100EQ1]" is


evaluated, and if it is TRUE then the statement GOTO100 causes
the program to jump to N100. If it is FALSE, the program
execution continues to the next block.

The expressions used by IF statements do not have to be comparisons. The expression is


considered to be TRUE if the result of the expression is non-zero. So any valid expression may be used
with the IF statement.

IF [#100LT#101] THEN #102=5 When this block is executed and the expression "[#100LT#101]" is
TRUE, then the variable #102 is assigned a value of 5. Otherwise,
#102 is not changed and execution continues with the next block.

IF [#100GT#101] THEN G0 X#100 If the expression "[#100GT#101]" is TRUE, then the X axis moves
to the position contained in variable #100. Any valid G-code block
may follow the THEN statement.

GOTO200 When this block is executed, the program jumps to N200. Since
no IF statement is used, this is called an unconditional jump.

WHILE [#100LT#101] DO1


...
...
...
END1

The WHILE - DO statements set up a conditional loop. When the WHILE - DO block is first
encountered, the expression "[#100LT#101]" is evaluated. If this expression is FALSE, the program
jumps to the block that contains the END statement. If this expression is TRUE, the program continues
until the END statement is reached. When the END is reached, the WHILE expression is evaluated
again. If this expression is still TRUE, the program jumps back to the WHILE - DO block, and the
process repeats. So essentially, the program blocks between the WHILE - DO block and the END block
are repeated until the expression evaluates to FALSE.

The DO and END statements have numbers after them that identify which END block belongs to
which WHILE - DO block. This is because WHILE - DO loops may nested inside each other. The
valid loop numbers are from 1 to 30.

DO2
...
...
...
END2

This is a WHILE - DO loop without the WHILE. It's conditional expression is always TRUE,
which sets up a never ending loop. This type of loop must have some other means of breaking out of the
loop, such as an IF - GOTO statement or even an M02 or M30 to end the program.
Section 10. Macro Programming Examples

Macro programming is very flexible. The examples given here are not necessarily the right way
or the only way to do something. This section is simply to help you understand how the macro language
works.

Example 1:

This example clears variables #100 through #199 to <empty>.

#1 = 100 (Assign the value 100 to variable #1)


N1 ##1 = #0 (The variable pointed to by variable #1 is cleared to <empty>)
#1 = #1 + 1 (Variable #1 is incremented by 1)
IF [#1 LT 200] GOTO 1 (This jump is taken as long as variable #1 is less than 200)

Here is another way to do the same thing using a WHILE – DO loop.

#1 = 100
WHILE [#1 LT 200] DO 1
##1 = #0
#1 = #1 +1
END1

You might also like