Macro B Reference Guide
Macro B programming may not be included on your MachMotion control. If you are not able to use these macros, contact us for an upgrade option.
1. Calling macros
Call Formats | Example | Notes | License Required |
M98 P__ Q__ L__ |
Run subroutine 0100 twice, starting at line 5 (N5) |
M98 calls the subroutine specified by P (must be 4 digits), and starts at the line number specified by Q (optional). It will repeat it the number of times passed to L (default is 1) | Any |
G65 P__ L__ A__ B__ …. |
Run macro 0150 twice, passing 5 into #1 (A) and 10 into #2 (B) |
M65 calls a non-modal macro specified by P and passes A-Z (whichever are specified) to the macro. M65 repeats a number of times set by L and does not need to be cancelled. Pound variables A-Z are wiped after the macro completes. | Industrial |
|
Run macro 1001 with A = 12 and B = 6, then run it again with A = 10 and B = 5, then cancel the modal. |
G66 is a modal macro call. It is cancelled by G67. Any lines in between will call the same macro with whichever parameters you pass it. |
Industrial |
|
|
Cancels the modal G66 |
Industrial |
|
|
Specifies end of subroutine or macro |
Any |
2. Passing Variables
You can pass variables to macros using letters A-Z, which correspond to pound variables. These are not places to store important information! Once you leave a function it is unknown what will be in that pound variable. The letters G, L, N, O, and P cannot be used as variables (because they are used as parameters for G65
or M98
or have a special syntactic use, i.e. N
for line numbers or G
for G codes).
Example:
G65 P0100 A5 (Invoke the macro 100, pass in the value of 5 for A.)
M30 (Rewind the file.)
O0100 (Start of macro.)
#500 = #1 (The value of the A parameter, stored in #1, is stored in #500.)
M99 (Return from the macro.)
%
2.1 (Local) Variables
A |
B |
C |
D |
E |
F |
H |
I |
J |
K |
M |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
1 |
2 |
3 |
7 |
8 |
9 |
11 |
4 |
5 |
6 |
13 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
A | B | C | I | J | K | D | E | F | H | M | Q | R | S | T | U | V | W | X | Y | Z |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 13 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
3. In a macro
3.1 Storage Variables
There are two ranges of pound variables for the user to store values in.
- #100-#199 : Values will be cleared every time Mach starts up
- #500-#1000 : Values will be saved every time Mach shuts down
3.2 Arithmetic
There are the basic add/subtract/multiply/divide functions available to the user, as well as some named functions for more advanced math. When performing arithmetic, the control follows the order of [Brackets, Functions, Multiply, Divide, Add, Subtract].
Function |
Symbol |
Example |
Notes |
Assignment |
= |
|
Sets #100 equal to 5 |
Addition |
+ |
|
Sets #100 equal to the value of 2+3 (5) |
Subtraction |
- |
|
Sets #100 equal to the value of 2-3 (-1) |
Multiplication |
* |
|
Sets #100 equal to the value of 2*3 (6) |
Division |
/ |
|
Sets #100 equal to the value of 2/3 (0.66) |
Absolute Value |
ABS[ ] |
|
Sets #100 equal to the absolute value of -3 (3) |
Rounding |
ROUND[ ] |
|
Sets #100 equal to the rounded value of 3/2 (2) |
Round Down (floor) |
FIX[ ] |
|
Sets #100 equal to the floor of 3/2 (1) |
Round Up (ceiling) |
FUP[ ] |
|
Sets #100 equal to the ceiling of 3/2 (2) |
Sine |
SIN[ ] |
|
Sets #100 equal to the sine of the value (0.707). Takes degrees. |
Arcsine |
ASIN[] |
|
Sets #100 equal to the sin‑1(0.5). The return value is in degrees. |
Cosine |
COS[ ] |
|
Sets #100 equal to the cosine of the value (0.707). Takes degrees. |
Arccosine |
ACOS[ ] |
|
Sets #100 equal to the arc cosine of the value in degrees (45) |
Tangent |
TAN[ ] |
|
Sets #100 equal to the tangent of the value (1). Takes degrees. |
Arctangent |
ATAN[ ] |
|
Sets #100 equal to the arc tangent of the value in degrees (45) |
Square Root |
SQRT[ ] |
|
Sets #100 equal to the square root of the value (3) |
Natural Logarithm |
LN[ ] |
|
Sets #100 equal to the natural logarithm of the value (0.83) |
Exponent |
EXP[ ] |
|
Sets #100 equal to the constant e raised to the value passed |
Modulus |
MOD |
|
Sets #100 equal to the remainder of |
(Logical/Bitwise) And |
AND |
|
Returns the 2nd LSB (least significant bit) from the current value of #3003. (E.g. |
(Logical/Bitwise) OR |
OR |
|
Returns the current value of #3003 with the 2nd LSB set to 1. (E.g. |
(Logical/Bitwise) XOR (exclusive OR) |
XOR |
|
Returns the current value of #3003 with the state of the 2nd LSB toggled. (E.g. |
Conversion from BCD to BIN |
BIN[] |
|
|
Conversion from BIN to BCD |
BCD[] |
|
|
Read Machine Parameter Value |
PRM[<prm>] |
|
Sets #100 equal to the value of parameter #1916 (ODID Dresser Location) |
Read Machine Parameter Value Bit |
PRM[<prm>,<bit>] |
|
Sets #100 equal to the state of bit 7 (0x40) in PV_CM2 (#6001): whether #1-#33 get cleared on reset. |
3.3 Logic Statements
Operator GOTO, IF, and WHILE are used for decision making within a macro. Be careful not to create infinite loops.
Operator |
Example |
Notes |
N |
N5 |
Labels the line with a number, which can be referenced on other lines. |
GOTO |
GOTO 5 |
Skip to line 5 (line labeled N5). Can be above or below current line. |
IF |
IF [condition] GOTO 5 #100 = 1 GOTO 6 N5 #100 = 0 N6 #500 = #100 |
If the condition is true, it will execute the statement on that line. If it is false, it will skip it. It is combined with GOTO to create true and false sections of code. |
WHILE |
WHILE [condition] DO 1 Statements END 1 |
While the condition is true, the code will loop through the code between DO 1 and END 1. The numbers 1 correspond to each other. It can be any number as long as it is the same at both the start and the end. |
3.4 Logic Operators
These operators are used inside of IF/WHILE conditions
Operator |
Example |
Notes |
EQ |
IF [ #500 EQ 1] GOTO 5 |
Equal To |
NE |
IF [ #500 NE 1] GOTO 5 |
Not Equal |
LT |
IF [ #500 LT 1] GOTO 5 |
Less Than |
GT |
IF [ #500 GT 1] GOTO 5 |
Greater Than |
LE |
IF [ #500 LE 1] GOTO 5 |
Less Than or Equal To |
GE |
IF [ #500 GE 1] GOTO 5 |
Greater Than or Equal To |
3.5 Auxiliary Functions
There are additional pound variables which have functionality that may be useful to the user.
Pound Variable |
Functionality |
Example |
#0 |
NIL |
IF [#500 = #0] THEN M99 |
#1000-#1035 |
Input Signals |
|
#1100-#1135 |
Output Signals |
|
#3001 |
Millisecond timer. Setting it to 0 will reset the timer, and its value at any point can be stored in another pound variable. |
(This function waits 1 second) #3001 = 0 WHILE [#3001 LE 1000] DO 1 END 1 |
#3002 |
Hour timer. Setting it to 0 will reset the timer, and its value at any point can be stored in another pound variable. |
#100 = #3002 WHILE[condition] DO 1 Statements END 1 #101 = #3002 - #100 (#101 now holds the difference in hours to run the while loop) |
#3000 |
Generates alarm. Gcode will be stop and the operator must reset to continue. |
#3000 = 1 (Example Message) |
#3006 |
Generates an alarm with an associated message. Requires a reset before continuing. |
#3006 = 101 (Example Message) |
#3007 |
Reports the state of Mirror Image, cannot be set (use G50 or G51 to set) |
#100 = #3007 |
#3004 |
Set this variable with a 3-digit number. Digit 0 will enable/disable feed hold, digit 1 will enable/disable feedrate override, digit 2 will enable/disable exact stop. |
#3004 = 110 (disable exact stop, disable feedrate override, and enable feed holds) |
#3005 |
Changes the units of measurement between imperial and metric |
#3005 = 1 (sets units to metric) |
#3011 |
Returns the current data in YYYYMMDD form |
#100 = #3011 (get date) #100 = FIX[ #100 / 10000] (#100 now has just the year) |
#3012 |
Returns the current time in HHMMSS form |
#100 = #3012 |
#3100 |
Returns nil |
#100 = #3100 (clear #100) |
#3101 |
Returns pi |
#100 = #3101 * [3 * 3] (area of a circle = pi * r-squared) |
#3102 |
Returns the constant e |
#100 = #3102 |
#6000 |
Set bit b00X00000 to 1 to step into a subroutine or macro. Set it to 0 step over it. |
For example, use this gcode to step into a subroutine: G10 L50 |
3.6 System Variables
It is not recommended that you change the values of these pound variables, though they are available to the user. Changing values can result in unexpected machine movement.
When setting modal groups, multiply the desired value by ten. For instance, to set the active motion g-code (#4001) to a feed rate move (G01), set #4001 = 10.
Modal Groups |
|
Pound Variable |
Description |
#4001 |
Active motion G-code |
#4002 |
Active motion plane |
#4003 |
Motion mode (incremental or absolute) |
#4004 |
Arc center mode |
#4005 |
G93 (inverse time) or G94 unit/min |
#4006 |
Imperial or metric units |
#4007 |
Current cutter compensation mode |
#4008 |
Tool length offset |
#4009 |
Canned cycles |
#4010 |
Used for canned cycles, old z or r-plane |
#4011 |
Scale mode |
#4012 |
Modal macro |
#4013 |
Spindle mode |
#4014 |
Coordinate systems |
#4015 |
Exact path or cutting mode |
#4016 |
Coordinate system rotation mode |
#4017 |
Polar mode |
#4018 |
Compensation mode |
|
|
Buffered Values |
|
Pound Variable |
Description |
#4102 |
Last buffered B code value |
#4107 |
Last buffered D code value |
#4108 |
Last buffered E code value |
#4109 |
Last buffered F code value |
#4111 |
Last buffered H code value |
#4113 |
Last buffered M code value |
#4114 |
Last buffered N code value |
#4115 |
Last buffered O code value |
#4119 |
Last buffered S code value |
#4120 |
Last buffered T code value |
|
|
Original Offsets |
|
Pound Variable |
Description |
#4140 |
Original X Offset |
#4141 |
Original Y Offset |
#4142 |
Original Z Offset |
#4143 |
Original A Offset |
#4144 |
Original B Offset |
#4145 |
Original C Offset |
|
|
Last Outputs |
|
Pound Variable |
Description |
#5001 |
Last X Output |
#5002 |
Last Y Output |
#5003 |
Last Z Output |
#5004 |
Last A Output |
#5005 |
Last B Output |
#5006 |
Last C Output |
|
|
Machine Positions |
|
Pound Variable |
Description |
#5021 |
Current Mach X Position |
#5022 |
Current Mach Y Position |
#5023 |
Current Mach Z Position |
#5024 |
Current Mach A Position |
#5025 |
Current Mach B Position |
#5026 |
Current Mach C Position |
|
|
Fixture Offsets |
|
Pound Variable |
Description |
#5030 |
Fixture X Offset |
#5031 |
Fixture Y Offset |
#5032 |
Fixture Z Offset |
#5033 |
Fixture A Offset |
#5034 |
Fixture B Offset |
#5035 |
Fixture C Offset |
|
|
Part Positions |
|
Pound Variable |
Description |
#5041 |
Current X Part Position |
#5042 |
Current Y Part Position |
#5043 |
Current Z Part Position |
#5044 |
Current A Part Position |
#5045 |
Current B Part Position |
#5046 |
Current C Part Position |
|
|
G92 Offsets |
|
Pound Variable |
Description |
#5050 |
G92 X Offset |
#5051 |
G92 Y Offset |
#5052 |
G92 Z Offset |
#5053 |
G92 A Offset |
#5054 |
G92 B Offset |
#5055 |
G92 C Offset |
|
|
G31 Positions |
|
Pound Variable |
Description |
#5061 |
Probe Part X Coordinate |
#5062 |
Probe Part Y Coordinate |
#5063 |
Probe Part Z Coordinate |
#5064 |
Probe Part A Coordinate |
#5065 |
Probe Part B Coordinate |
#5066 |
Probe Part C Coordinate |
#5071 |
Probe Machine X Coordinate |
#5072 |
Probe Machine Y Coordinate |
#5073 |
Probe Machine Z Coordinate |
#5074 |
Probe Machine A Coordinate |
#5075 |
Probe Machine B Coordinate |
#5076 |
Probe Machine C Coordinate |
|
|
Head Shift Offsets |
|
Pound Variable |
Description |
#5081 |
X Position Head Shift |
#5082 |
Y Position Head Shift |
#5083 |
Z Position Head Shift |
#5084 |
A Position Head Shift |
#5085 |
B Position Head Shift |
#5086 |
C Position Head Shift |
|
|
G76 Values (Lathe Only) |
|
Pound Variable |
Description |
#5140 |
Minimum pass depth |
#5141 |
Finish pass depth |
#5142 |
Number of finish passes |
#5143 |
Thread angle |
#5144 |
Chamfer amount |
#5145 |
Cutting method |
|
|
Cutter Compensation |
|
Pound Variable |
Description |
#5157 |
Cutter compensation X value |
#5158 |
Cutter compensation Y value |
#5159 |
Cutter compensation Z value |
|
|
G30 Coordinates |
|
Pound Variable |
Description |
#5181 |
Active G30 X Coordinate |
#5182 |
Active G30 Y Coordinate |
#5183 |
Active G30 Z Coordinate |
#5184 |
Active G30 A Coordinate |
#5185 |
Active G30 B Coordinate |
#5186 |
Active G30 C Coordinate |
|
|
G30 P2 Coordinates |
|
Pound Variable |
Description |
#5351 |
G30 P2 X Coordinate |
#5352 |
G30 P2 Y Coordinate |
#5353 |
G30 P2 Z Coordinate |
#5354 |
G30 P2 A Coordinate |
#5355 |
G30 P2 B Coordinate |
#5356 |
G30 P2 C Coordinate |
|
|
G30 P3 Coordinates |
|
Pound Variable |
Description |
#5361 |
G30 P3 X Coordinate |
#5362 |
G30 P3 Y Coordinate |
#5363 |
G30 P3 Z Coordinate |
#5364 |
G30 P3 A Coordinate |
#5365 |
G30 P3 B Coordinate |
#5366 |
G30 P3 C Coordinate |
|
|
G30 P4 Coordinates |
|
Pound Variable |
Description |
#5371 |
G30 P4 X Coordinate |
#5372 |
G30 P4 Y Coordinate |
#5373 |
G30 P4 Z Coordinate |
#5374 |
G30 P4 A Coordinate |
#5375 |
G30 P4 B Coordinate |
#5376 |
G30 P4 C Coordinate |
|
|
Work Shift Offsets |
|
Pound Variable |
Description |
#5201 |
X Work Shift |
#5202 |
Y Work Shift |
#5203 |
Z Work Shift |
#5204 |
A Work Shift |
#5205 |
B Work Shift |
#5206 |
C Work Shift |
|
|
G60 Approach Distances |
|
Pound Variable |
Description |
#5440 |
G60 X Distance |
#5441 |
G60 Y Distance |
#5442 |
G60 Z Distance |
#5443 |
G60 A Distance |
#5444 |
G60 B Distance |
#5445 |
G60 C Distance |
|
|
|
|
G54 Fixture Offsets |
|
Pound Variable |
Description |
#5221 |
X Offset |
#5222 |
Y Offset |
#5223 |
Z Offset |
#5224 |
A Offset |
#5225 |
B Offset |
#5226 |
C Offset |
|
|
G55 Fixture Offsets |
|
Pound Variable |
Description |
#5241 |
X Offset |
#5242 |
Y Offset |
#5243 |
Z Offset |
#5244 |
A Offset |
#5245 |
B Offset |
#5246 |
C Offset |
|
|
G56 Fixture Offsets |
|
Pound Variable |
Description |
#5261 |
X Offset |
#5262 |
Y Offset |
#5263 |
Z Offset |
#5264 |
A Offset |
#5265 |
B Offset |
#5266 |
C Offset |
|
|
G57 Fixture Offsets |
|
Pound Variable |
Description |
#5281 |
X Offset |
#5282 |
Y Offset |
#5283 |
Z Offset |
#5284 |
A Offset |
#5285 |
B Offset |
#5286 |
C Offset |
|
|
G58 Fixture Offsets |
|
Pound Variable |
Description |
#5301 |
X Offset |
#5302 |
Y Offset |
#5303 |
Z Offset |
#5304 |
A Offset |
#5305 |
B Offset |
#5306 |
C Offset |
|
|
G59 Fixture Offsets |
|
Pound Variable |
Description |
#5321 |
X Offset |
#5322 |
Y Offset |
#5323 |
Z Offset |
#5324 |
A Offset |
#5325 |
B Offset |
#5326 |
C Offset |
|
|
G54.1 P1 |
|
Pound Variable |
Description |
#7001 |
X Offset |
#7002 |
Y Offset |
#7003 |
Z Offset |
#7004 |
A Offset |
#7005 |
B Offset |
#7006 |
C Offset |
|
|
G54.1 P2 |
|
Pound Variable |
Description |
#7021 |
X Offset |
#7022 |
Y Offset |
#7023 |
Z Offset |
#7024 |
A Offset |
#7025 |
B Offset |
#7026 |
C Offset |
3.7 Tool Variables
Tool Wear Offsets
# | X | Y | R | T |
1 | #2001 | #2101 | #2201 | #2301 |
2 | #2002 | #2102 | #2202 | #2302 |
3 | #2003 | #2103 | #2203 | #2303 |
. | . | . | . | . |
32 | #2032 | #2132 | #2232 | #2332 |
Tool Geometry Offsets
# | X | Y | R | T |
1 | #2401 | #2501 | #2601 | |
2 | #2402 | #2502 | #2602 | |
3 | #2403 | #2503 | #2603 | |
. | . | . | ||
32 | #2432 | #2532 | #2632 |
3.8 Naming #500 Series Variables
SETVN 500 [RADIUS]
SETVN 501 [X+ O&D]
SETVN 502 [X- O&D]
SETVN 503 [Y+ O&D]
SETVN 504 [Y- O&D]
SETVN 505 [LENGTH]
Example:
SETVN 500[X_TOP_LEFT, X_TOP_RIGHT, X_BOTTOM_LEFT, X_BOTTOM_RIGHT, Y_TOP_LEFT, Y_TOP_RIGHT, Y_BOTTOM_LEFT, Y_BOTTOM_RIGHT, FAST, SLOW]
#X_TOP_LEFT = -2
#X_TOP_RIGHT = 2
#X_BOTTOM_LEFT = -2
#X_BOTTOM_RIGHT = 2
#Y_TOP_LEFT = 2
#Y_TOP_RIGHT = 2
#Y_BOTTOM_LEFT = -2
#Y_BOTTOM_RIGHT = -2
#FAST = 1000
#SLOW = 500
G90 G01
X [#X_TOP_LEFT] Y [#Y_TOP_LEFT] F [#FAST]
X [#X_TOP_RIGHT] Y [#Y_TOP_RIGHT] F [#FAST]
IF [#501 EQ 1] THEN #3000 = 5
X [#X_BOTTOM_RIGHT] Y [#Y_BOTTOM_RIGHT] F [#FAST]
X [#X_BOTTOM_LEFT] Y [#X_BOTTOM_LEFT] F [#SLOW]
X [#X_TOP_LEFT] Y [#Y_TOP_LEFT] F [#SLOW]
M30
%
For more information about Macro B please see the Macro B Quick Reference Guide.
Keywords: Advanced M code MCode M-Code G code GCode G-Code Macro B MacroB macros # pound variables