Skip to content

Expressions and flow control

Terje Io edited this page Jun 1, 2023 · 10 revisions

grblHAL supports parameters, expressions and flow control, LinuxCNC style (with some limitations), if enabled in the config file or in builds made with the Web Builder when RS274 NGC expression support is enabled in the Advanced features tab..

Flow control is limited if streaming G-code from a sender, full support is only available in macros stored on a SD card or in the littlefs filing system.

Numbered parameters passed as arguments to G65 macro call

Word Variable Word Variable Word Variable
A #1 I #4 T #20
B #2 J #5 U #21
C #3 K #6 V #22
D #7 M #13 W #23
E #8 Q #17 X #24
F #9 R #18 Y #25
H #11 S #19 Z #26

Syntax: G65 P<id> (A- B- C- ...)

NOTES: Nesting macro calls is currently not allowed. Axis words ABC and UVW are only available if the corresponding axes are enabled.

Numbered parameters (variables)

Parameters Usage Persistent
31 - 5000 User defined parameters, defaults to 0. No
5061 - 5069 Coordinates of a G38 probe result (X, Y, Z, ...) . No
5070 G38.3 and G38.5 probe result: 1 if successful, 0 if not. No
5161 - 5169 G28 coordinates (X, Y, Z, ...). Yes
5181 - 5189 G30 coordinates (X, Y, Z, ...). Yes
5210 1 if G92 offset active, 0 if not. Depends on $384 setting
5211 - 5219 G92 offset (X, Y, Z, ...). Depends on $384 setting
5220 Coordinate system number for G54 - G59.3, 1 - 9. Yes
5221 - 5229 Coordinate system 1, for G54 (X, Y, Z, ...). Yes
5241 - 5249 Coordinate system 2, for G55 (X, Y, Z, ...). Yes
5261 - 5269 Coordinate system 3, for G56 (X, Y, Z, ...). Yes
5281 - 5289 Coordinate system 4, for G57 (X, Y, Z, ...). Yes
5301 - 5319 Coordinate system 5, for G58 (X, Y, Z, ...). Yes
5321 - 5329 Coordinate system 6, for G59 (X, Y, Z, ...). Yes
5341 - 5349 Coordinate system 7, for G59.1 (X, Y, Z, ...). Yes
5361 - 5369 Coordinate system 8, for G59.2 (X, Y, Z, ...). Yes
5381 - 5389 Coordinate system 9, for G59.3 (X, Y, Z, ...). Yes
5399 Result from last M66, wait for input. -1 if timed out. No
5420 - 5427 Current position including all offsets (X, Y, Z, ...). No

Predefined named parameters (variables)

Name Usage Comment
_vmajor Major grblHAL version number, currently 1.1.
_vminor Minor grblHAL version number, currently 0.
_line Current G Code line number.
_motion_mode Current motion mode. See 1 below.
_plane Current plane: 170 - G17, 180 - G18, 190 - G19.
_ccomp Active cutter compensation: 400 - G40.
_metric 1 if G21 is active, 0 if not.
_imperial 1 if G20 is active, 0 if not.
_absolute 1 if G90 is active, 0 if not.
_incremental 1 if G91 is active, 0 if not.
_inverse_time 1 if G93 is active, 0 if not.
_units_per_minute 1 if G94 is active, 0 if not.
_units_per_rev 1 if G95 is active, 0 if not.
_coord_system Current coordinate system: 540 - G54, 550 - G55, ...
_tool_offset 1 if G43 is active, 0 if not.
_retract_r_plane 1 if G98 is active, 0 if not.
_retract_old_z 1 if G99 is active, 0 if not.
_spindle_rpm_mode 1 if G97 is active, 0 if not.
_spindle_css_mode 1 if G96 is active, 0 if not.
_ijk_absolute_mode 1 if G90.1 is active, 0 if not.
_lathe_diameter_mode 1 if G8 is active, 0 if not.
_lathe_radius_mode 1 if G7 is active, 0 if not.
_spindle_on 1 if spindle is running (M3 or M4), 0 if not.
_spindle_cw 1 if spindle is running clockwise (M3), 0 if not.
_mist 1 if M7 is active, 0 if not.
_flood 1 if M8 is active, 0 if not.
_speed_override 1 if M48 or M50P1 is active, 0 if not.
_feed_override 1 if M48 or M50p2 is active, 0 if not.
_adaptive_feed Always 0.
_feed_hold 1 if M53.1 is active, 0 if not.
_feed Current value of F. Not the actual feed rate.
_rpm Current value of S. Not the actual RPM.
_x Current X coordinate including all offsets. Same as #5420.
_y Current Y coordinate including all offsets. Same as #5421.
_z Current Z coordinate including all offsets. Same as #5422.
_a Current A coordinate including all offsets. Same as #5423.
_b Current B coordinate including all offsets. Same as #5424.
_c Current C coordinate including all offsets. Same as #5425.
_u Current U coordinate including all offsets. Same as #5426.
_v Current U coordinate including all offsets. Same as #5427.
_w Current W coordinate including all offsets. Same as #5428.
_current_tool Number of the tool in the spindle. Same as #5400.
_current_pocket Always 0.
_selected_tool Current value of T. Default -1.
_selected_pocket Always -1.
_value Return value from the last G65 macro call. Set to 0 on entry.
_value_returned 1 if last G65 macro call returned a value, 0 if not.
_call_level N/A

1 Motion modes:

Mode Value
G1 10
G2 20
G3 30
G33 330
G38.2 382
G38.3 383
G38.4 384
G38.5 384
G5 50
G5.1 51
G73 730
G75 760
G80 800
G81 810
G82 820
G83 830
G85 850
G89 890

Operators and precedence

Operators Precedence
** Highest
*, / and MOD
+ and -
EQ, NE, GT, GE, LT and LE
AND, OR and XOR Lowest

Functions

Name Result
ABS[arg] Absolute value
ACOS[arg] Inverse cosine
ASIN[arg] Inverse sine
ATAN[arg][arg] Four quadrant inverse tangent
COS[arg] Cosine
EXISTS[arg] Named parameter exists
EXP[arg] e raised to the given power
FIX[arg] Round down to integer
FUP[arg] Round up to integer
LN[arg] Base-e logarithm
ROUND[arg] Round to nearest integer
SIN[arg] Sine
SQRT[arg] Square root
TAN[arg] Tanget

Flow control statements

Statement File macro Streamed G-Code Comment
if <expr> yes yes
elseif <expr> yes yes
else yes yes
endif yes yes
do yes yes
continue yes no
break yes no
while <expr> yes no
endwhile yes no
repeat <expr> yes no
return [<expr>] yes no
alarm <expr> yes no <expr> must evaluate to a valid alarm code2
error <expr> yes no <expr> must evaluate to a valid error code2

2 grblHAL specific extension.


2023-06-01