##Syntax
G-Code is essentially a pair of 'letters' and 'numbers' that tell the machine what to do. It is well described somewhere else, so this is only a quick overview. G-Codes are typically organized in lines. These are typical commands:
G1 X10 Y20 ; move to X-coordinate 10 and Y=20
G1X0Y8 ; no need for spaces between pairs - but readability sucks.
G1 X10 (small move) Y200 (big move) ; comments can be in-line with parenthesis
G1 X10 Y100 G1 X0 Y0 ; multiple commands can be in one line.
G1 means: move in a coordinated move, i.e. the axis move in a way that the
final move resembles a straight line in the N-dimensional space they are in.
Comments can be at the end of line and start with a semicolon.
There can be comments between pairs with parenthesis. This is not supported by every G-Code interpreter, but BeagleG does:
G1(coordinated move) X10(to this position)
BeagleG supportes the use of parameters. A parameter is specified by a pound
character # followed by an integer value. Currently parameters 0 to 5399 are
supported. Parameter 0 is read-only and always evaluates to 0.0f. Some of the
parameters are used internally, these are all in the > 5000 range and should
not be used.
Parameter setting is done by:
- a pound character
# - an integer value between
1and5399 - an equal sign
= - a real value
For example #1=123.4 is a parameter setting meaning set parameter 1 to 123.4.
Unlike the NIST RS274NGC specification, parameter setting takes effect immediately.
For example, G1 #1=10 X#1 will result in a coordinated move to X=10.
Parameters are not currently persistent. All parameters default to 0.0f.
Expressions are also supported along with Binary/Unary operations. An expression
is a set of character starting with a left bracket [ and ending with a balanced
right bracket ]. In between the brackets are numbers, parameter values,
mathematical operations, and other expressions. Expressions are evaluated to
produce a number. An example of an expression is [1 + cos[0] - [#3 ** [4.0/2]]].
Binary operations appear only inside excpressions. Nine binary operations are
defined. These are four basic mathematical operations: addition +, subtraction -,
multiplication *, and division /. There are three logical operations:
non-exclusive or OR, exculsive or XOR, and logical and AND. The eighth
operation is the modulus operation MOD. The ninth operation is the "power"
operation ** of raising the number on the left of the operation to the power
on the right.
A unary operation is either ATAN followed by one expression divided by another
expression (for example ATAN[2]/[1+3]) or any other unary operation followed
by an expression (for example SIN[90]). The unary operations are: absolute value
ABS, arc cosine ACOS, arc sine ASIN, arc tangent ATAN, cosine COS,
e raised to the given power EXP, round down FIX, round up FUP, natural
logarithm LN, round down to the nearest whole number ROUND, sine SIN,
square root SQRT, and tangent TAN. Arguments to unary operations which
take angle measurements (COS, SIN, and TAN) are in degrees. Values returned
by unary operations which return angle measurements (ACOS, ASIN, and ATAN)
are also in degrees.
Supported commands are currently added on a need-to-have basis. They are a subset of G-Codes found documented in LinuxCNC and RepRap Wiki.
The following commands are supported. A place-holder of [coordinates] means
a combination of axis coordinates (such as X10 Y20) and an optional feedrate
(F1000).
Current set of supported axis-letters is X, Y, Z, E, A, B, C, U, V, W (the
--axis-mapping flag decides which make it to motors).
Line numbers Nxx and checksums *xx are parsed, but discarded and ignored
for now.
| Command | Callback | Description |
|---|---|---|
| G0 [coordinates] | rapid_move() |
Move to coordinates |
| G1 [coordinates] | coordinated_move() |
Like G0, but guarantee linear move |
| G2 [end] [offset] | coordinated_move() |
Clockwise arc |
| G3 [end] [offset] | coordinated_move() |
Counterclockwise arc |
| G4 Pnnn | dwell() |
Dwell (wait) for nnn milliseconds. |
| G5 [see below] | coordinated_move() |
Cubic spline in XY plane |
| G5.1 [see below] | coordinated_move() |
Quadratic spline in XY plane |
| G10 L2 Px [coord] | - | Set coordinate system data |
| G17 | - | XY plane selection. |
| G18 | - | ZX plane selection. |
| G19 | - | YZ plane selection. |
| G20 | - | Set coordinates to inches. |
| G21 | - | Set coordinates to millimeter. |
| G28 [coordinates] | handle_home() |
Home the machine on given axes. |
| G30 [Z] | handle_z_probe() |
Z Probe, with optional target thickness. |
| G54 | - | Select coordinate system 1 (G10 L2 P1 ...) |
| G55 | - | Select coordinate system 2 (G10 L2 P2 ...) |
| G56 | - | Select coordinate system 3 (G10 L2 P3 ...) |
| G57 | - | Select coordinate system 4 (G10 L2 P4 ...) |
| G58 | - | Select coordinate system 5 (G10 L2 P5 ...) |
| G59 | - | Select coordinate system 6 (G10 L2 P6 ...) |
| G59.1 | - | Select coordinate system 7 (G10 L2 P7 ...) |
| G59.2 | - | Select coordinate system 8 (G10 L2 P8 ...) |
| G59.3 | - | Select coordinate system 9 (G10 L2 P9 ...) |
| G70 | - | Set coordinates to inches. |
| G71 | - | Set coordinates to millimeter. |
| G90 | - | Coordinates are absolute. |
| G91 | - | Coordinates are relative. |
| G92 [coordinates] | - | Set position to be the new zero. |
| G92.1 | - | Reset G92 offset |
| G92.2 | - | Suspend G92 offset |
| G92.3 | - | Restore G92 offset |
G5 creates a cubic B-spline in the XY plane with the X and Y axes only. P and Q must both be specified for every G5 command.
For the first G5 command in a series of G5 commands, I and J must both be specified. For subsequent G5 commands, either bot I and J must be specified, or neither. If I and J are unspecified, the first control point will be the negation of the previous second control point.
G5 X- Y- <I- J-> P- Q-
X- Y-- end point of spline (absolute or relative depending on current mode)I- J-- relative offset from start point to first control pointP- Q-- relative offset from end point to second control point
G5.1 creates a quadratic B-spline in the XY plane with the X and Y axes only. Not specifying I or J gives zero offset for the unspecified axis, so one or both must be given.
G5.1 X- Y- I- J-
X- Y-- end point of spline (absolute or relative depending on current mode)I- J-- relative offset from start point to control point
| Command | Callback | Description |
|---|---|---|
| M2 | gcode_finished() |
Program end. Resets back to defaults. |
| M24 | wait_for_start() |
Start/resume a program. Waits for the start input if available. |
| M17 | motors_enable() |
Switch on motors. |
| M18 | motors_enable() |
Switch off motors. |
| M30 | gcode_finished() |
Program end. Resets back to defaults. |
| M84 | motors_enable() |
Switch off motors. |
| M82 | - | Set E-axis to absolute. |
| M83 | - | Set E-axis to relative. |
| M104 Snnn | set_temperature() |
Set temperature in celsius. |
| M116 | wait_temperature() |
Wait for temperature to be reached |
| M109 Snnn | set_t.., wait_t..() |
Combination of M104, M116: Set temperature and wait for it to be reached. |
| M106 Snnn | set_fanspeed() |
set speed of fan; 0..255 |
| M107 | set_fanspeed(0) |
switch off fan. |
| M111 Snnn | - | Set debug level. |
| M220 Snnn | set_speed_factor() |
Set output speed factor. |
| M500 | save_params() |
Save parameters. |
| M501 | load_params() |
Load parameters. |
The standard M-Code are directly handled by the G-code parser and result
in parametrized callbacks. Other not quite standard G-codes are handled in
gcode-machine-control when receiving
the unprocessed() callback (see API below):
| Command | Description |
|---|---|
| M0 | Unconditional stop, sets Software E-Stop. |
| M3 Sxx | Spindle On Clockwise at speed Sxx |
| M4 Sxx | Spindle On Counterclockwise at speed Sxx |
| M5 | Spindle Off |
| M7 | Turn mist on |
| M8 | Turn flood on |
| M9 | Turn all coolant off |
| M10 | Turn on vacuum |
| M11 | Turn off vacuum |
| M42 Pnn | Get state of AUX Pin nn. |
| M42 Pnn Sxx | Set AUX Pin nn to value xx |
| M62 Pnn | Set AUX Pin nn to 1 |
| M63 Pnn | Set AUX Pin nn to 0 |
| M64 Pnn | Set AUX Pin nn to 1; updates immediately, independent of buffered moves. |
| M65 Pnn | Set AUX Pin nn to 0; updates immediately, independent of buffered moves. |
| M80 | ATX Power On. |
| M81 | ATX Power Off. |
| M105 | Get current extruder temperature. |
| M114 | Get current position; coordinate units in mm. |
| M115 | Get firmware version. |
| M117 | Display message. |
| M119 | Get endstop status. |
| M120 | Enable pause switch detection. |
| M121 | Disable pause switch detection. |
| M245 | Start cooler |
| M246 | Stop cooler |
| M355 | Turn case lights on/off |
| M400 | Wait for queue to be empty. Equivalent to G4 P0. |
| M999 | Clear Software E-Stop. |
The axes X, Y, and Z are dealt with specially by gcode-machine-control: they are
understood as representing coordinates in an Euclidian space (not entirely
unwarranted :) ) and thus applies a feedrate in a way that the resulting
path sees the given speed in space, not each individual axis:
G28 G1 X100 F100 ; moves X with feedrate 100mm/min
G28 G1 X100 Y100 F100 ; moves X and Y with feedrate 100/sqrt(2) ~ 70.7mm/min
G-code parsing as provided by the G-Code parse API receives
G-code from a file-descriptor (via the int gcodep_parse_stream() function)
and calls parametrized parse-callbacks representing slightly more higher-level commands.
The coordinates passed to callbacks are always pre-converted to machine-absolute and
metric to make implementation of the callback receivers easy.
The codes G20/G21/G70/G71 and G90/G91/G92 as well as
M82, M83 are handled internally to always output absolute, metric coordinates.
Currently, the GCode parser also implements G2 and G3 and emits line-segments with
coordinated_move() callbacks (this should probably move outside the parser).
Commands that are not recognized are passed on to the unprocessed() callback
for the user to handle (see description in API).