G-Code Macro Programming
G-Code Macro programming enables logic statements and variable access for creating dynamic programs, offering many features similar to BASIC-style programming languages or IEC61131 Structured Text.
In general, all logic here is done as pre-processor before G-Code processing
Thus, the following two situations are equivalent to the core system
#200
=
1000
G01
X10
Y10
F#200
and
G01
X10
Y10
F1000
- Clear use of spacing is recommended for more readable math
- Avoid too many operators on a line for readability
Operator Precedence and Associativity
- Standard math order of operations generally, but when in doubt add
[ ]to group operations in an easier to read order - https://learn.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?view=msvc-170
Langauge Keywords
| Symbol | Example | Description |
|---|---|---|
A
to
Z
|
|
G-Code register. See GCode Table |
%
|
|
Ignored. |
$
|
$LOG["Start"]
|
Predefined system commands. See Sys Functions |
|
|
|
|
;
|
|
Single line comment. See: Comments |
//
|
|
Single line comment. See: Comments |
( )
|
|
Single line comment. See: Comments |
GCode Variables
See: G-Code Variables
| Symbol | Example | Description |
|---|---|---|
#0
|
|
Special read-only value for
NULL. See:
GCode Variables
|
#
integer
|
#100
|
Local NC Variable access |
#
letter
|
#X
|
Read-only access to last set value to the G-Code register |
#<>
|
#<NC.DefaultUnits>
|
See: System parameters |
PLC
|
PLC.DINTS[42]
|
External Variable access |
#[#]
|
#[#100]
|
See: Indirect Variables |
|
|
|
|
Program Flow
| Symbol | Example | Description |
|---|---|---|
IF
|
|
|
ELSE
|
|
Must be on new line, cannot be on IF line. |
WHILE
|
|
|
FOR
|
|
FOR [#1 = 1, 10, 1] DO . Last two arguments cannot be expressions. |
END
|
|
Ends IF/WHILE/FOR . Must be on a new line. |
Conditional Operators
| Symbol | Example | Description |
|---|---|---|
EQ
or
==
|
|
Equal comparison |
LT
or
<
|
|
Less than |
GT
or
>
|
|
Greater than |
GE
or
>=
|
|
Greater than or equals |
LE
or
<=
|
|
Less than or equals |
NE
or
!=
|
|
Not equals |
|
|
|
|
Boolean Operators
| Symbol | Example | Description |
|---|---|---|
AND
or
&&
|
|
Boolean AND |
OR or ||
|
|
Boolean OR |
NOT
or
!=
|
|
Boolean NOT |
Bitwise Operators
| Symbol | Example | Description |
|---|---|---|
| | |
|
Bitwise OR |
&
|
|
Bitwise AND |
~
|
|
Bitwise NOT or 1's complement |
Math Operators
See: Math Operators
| Symbol | Example | Description |
|---|---|---|
=
|
#100 = #1 + #2
|
Value assignment |
+
|
|
Addition |
-
|
|
Subtraction |
*
|
|
Multiplication |
/
|
|
Division |
**
|
5**3
= 5³ = 125;
|
Power.
* *
not allowed
|
MOD
|
101 MOD 10
= 1
|
Modulus |
[ ]
|
5*[90+#10]
|
Expression grouping |
Math Functions
See: Math Functions
| Symbol | Description |
|---|---|
SIN[]
|
Sine, in degrees |
ASIN[]
|
Arcsine, in degrees |
COS[]
|
Cosine, in degrees |
ACOS[]
|
Arccosine, in degrees |
TAN[]
|
Tangent, in degrees |
ATAN[]
|
Arctangent, in degrees |
SQRT[]
|
Square root |
ABS[]
|
Absolute value |
ROUND[]
|
round to the nearest integer. Midpoint rounding rounds up, e.g. 1.5 => 2.0 |
FIX[]
|
round down to nearest integer (truncate) |
FUP[]
|
round up to the nearest integer |
LN[]
|
Natural log |
LOG[]
|
Base10 log |
|
Natural exponent (e.g.,
EXP 3
= e³). Use
**
for arbitrary exponents/powers
|
Trig Functions
- Trigonometric functions always use degrees (not radians)
- Allowed spacing formats:
-SIN[45]==SIN[ 45 ]==SIN [45]==SIN [ 45 ]
Multi-line statements are not supported
IF[
SIN[#5]
+
45
>
COS[#6]] // this must be on the same line as 'IF' above
Conditional Operators (EQ/LT/GT/GE/LE/NE)
| Operator | Description |
|---|---|
EQ
or
==
|
Equal comparison |
LT
or
<
|
Less than |
GT
or
>
|
Greater than |
GE
or
>=
|
Greater than or equals |
LE
or
<=
|
Less than or equals |
NE
or
!=
|
Not equals |
Indirect Variable References
Since a variable may have an integer value and # registers are identified by integers, the value of a register may be used as a pointer to a # register.
A variable pointer to a
#
register is formed by the
#
followed by an expression that is contained in square brackets
[ ]
that resolves to an integer value. Functions are not allowed within the
[ ]
brackets when using indirect variable referencing.
For example:
#100
=
50
#[#100] =
77
(#100 points to #50, thus setting #50 to 77)
#[#100
+
2] =
85
(this is an error. functions and operations aren't allowed for indirect variable referencing)
#101
=
#100
+
2(instead, use a temporary intermediate variable, #101 is now 52)
#[#101] =
85
(now #52 can be written to)
It is generally recommended to always macro variables with a tolerance when working with any non-integer data. This also applies to IF/WHILE comparison checks.
For example, a program might use either an intermediate variable and apply either aROUND
(to closest),
FIX
(always rounds down), or
FUP
(always rounds up) operators to ensure that the expression resolves to the desired integer.
For example:
// for indirect variable references to ensure proper integer indexing
#100
=
0.6
/
2
*
10(#100 is ~2.9999999).
#[#100] =
77
(#100 is auto rounded to 3, 77 is written to #3)
#101
=
5.4
(#101 is 5.4)
#102
=
FIX[#101](5.4 is truncated to 5)
#[#102] =
47
(47 is written to #5)
#103
=
FUP[#101](5.4 is rounded up to 6)
#[#103] =
24
(24 is written to #6)
#104
=
ROUND[#101](5.4 is rounded to the closest integer 5)
#[#104] =
35
(35 is written to #5)
#[#101] =
77
(error, 5.4 is outside the automatic rounding tolerance)