7.1. RS-274 G-code Reference¶
Note
Text derived from several sources, like NIST RS-274 paper and Linux CNC project. cf. supra for references.
7.1.1. History¶
The G-code language, also called RS-274, is a programming language for numerical control. It was developed by the EIA in the early 1960s, and finally standardised by ISO in February 1980 as RS274D / ISO 6983.
The G-code language has several flavours and historical versions. A list of reference documents follows :
- The NIST RS274NGC Interpreter - Version 3, T. Kramer, F. Proctor, E. Messina, National Institute of Standards and Technology, NISTIR 6556, August 17, 2000
- The documentation of the Linux CNC project, formerly Enhanced Machine Controller developed at NIST,
- EIA Standard RS-274-D Interchangeable Variable Block Data Format for Positioning, Contouring, and Contouring/Positioning Numerically Controlled Machines, 2001 Eye Street, NW, Washington, D.C. 20006: Electronic Industries Association, February 1979
7.1.2. Overview¶
The RS274/NGC language is based on lines of code. Each line (also called a “block”) may include commands to a machining center to do several different things.
A typical line of code consists of an optional line number at the beginning followed by one or more
“words.” A word consists of a letter followed by a number (or something that evaluates to a
number). A word may either give a command or provide an argument to a command. For example,
G1 X3
is a valid line of code with two words. G1
is a command meaning “move in a
straight line at the programmed feed rate,” and X3
provides an argument value (the value of
X should be 3 at the end of the move). Most RS274/NGC commands start with either G or M (for
miscellaneous). The words for these commands are called “G codes” and “M codes.”
7.1.3. Language View of a Machining Center¶
7.1.3.1. Parameters¶
In the RS274/NGC language view, a machining center maintains an array of 5400 numerical parameters. Many of them have specific uses. The parameter array should persist over time, even if the machining center is powered down.
Parameter Number | Parameter Value | Comment |
---|---|---|
5161 | 0.0 | G28 home X |
5162 | 0.0 | G28 home Y |
5163 | 0.0 | G28 home Z |
5164 | 0.0 | G28 home A |
5165 | 0.0 | G28 home B |
5166 | 0.0 | G28 home C |
5181 | 0.0 | G30 home X |
5182 | 0.0 | G30 home Y |
5183 | 0.0 | G30 home Z |
5184 | 0.0 | G30 home A |
5185 | 0.0 | G30 home B |
5186 | 0.0 | G30 home C |
5211 | 0.0 | G92 offset X |
5212 | 0.0 | G92 offset Y |
5213 | 0.0 | G92 offset Z |
5214 | 0.0 | G92 offset A |
5215 | 0.0 | G92 offset B |
5216 | 0.0 | G92 offset C |
5220 | 1.0 | coordinate system number |
5221 | 0.0 | coordinate system 1 X |
5222 | 0.0 | coordinate system 1 Y |
5223 | 0.0 | coordinate system 1 Z |
5224 | 0.0 | coordinate system 1 A |
5225 | 0.0 | coordinate system 1 B |
5226 | 0.0 | coordinate system 1 C |
5241 | 0.0 | coordinate system 2 X |
5242 | 0.0 | coordinate system 2 Y |
5243 | 0.0 | coordinate system 2 Z |
5244 | 0.0 | coordinate system 2 A |
5245 | 0.0 | coordinate system 2 B |
5246 | 0.0 | coordinate system 2 C |
5261 | 0.0 | coordinate system 3 X |
5262 | 0.0 | coordinate system 3 Y |
5263 | 0.0 | coordinate system 3 Z |
5264 | 0.0 | coordinate system 3 A |
5265 | 0.0 | coordinate system 3 B |
5266 | 0.0 | coordinate system 3 C |
5281 | 0.0 | coordinate system 4 X |
5282 | 0.0 | coordinate system 4 Y |
5283 | 0.0 | coordinate system 4 Z |
5284 | 0.0 | coordinate system 4 A |
5285 | 0.0 | coordinate system 4 B |
5286 | 0.0 | coordinate system 4 C |
5301 | 0.0 | coordinate system 5 X |
5302 | 0.0 | coordinate system 5 Y |
5303 | 0.0 | coordinate system 5 Z |
5304 | 0.0 | coordinate system 5 A |
5305 | 0.0 | coordinate system 5 B |
5306 | 0.0 | coordinate system 5 C |
5321 | 0.0 | coordinate system 6 X |
5322 | 0.0 | coordinate system 6 Y |
5323 | 0.0 | coordinate system 6 Z |
5324 | 0.0 | coordinate system 6 A |
5325 | 0.0 | coordinate system 6 B |
5326 | 0.0 | coordinate system 6 C |
5341 | 0.0 | coordinate system 7 X |
5342 | 0.0 | coordinate system 7 Y |
5343 | 0.0 | coordinate system 7 Z |
5344 | 0.0 | coordinate system 7 A |
5345 | 0.0 | coordinate system 7 B |
5346 | 0.0 | coordinate system 7 C |
5361 | 0.0 | coordinate system 8 X |
5362 | 0.0 | coordinate system 8 Y |
5363 | 0.0 | coordinate system 8 Z |
5364 | 0.0 | coordinate system 8 A |
5365 | 0.0 | coordinate system 8 B |
5366 | 0.0 | coordinate system 8 C |
5381 | 0.0 | coordinate system 9 X |
5382 | 0.0 | coordinate system 9 Y |
5383 | 0.0 | coordinate system 9 Z |
5384 | 0.0 | coordinate system 9 A |
5385 | 0.0 | coordinate system 9 B |
5386 | 0.0 | coordinate system 9 C |
7.1.3.2. Coordinate Systems¶
In the RS274/NGC language view, a machining center has an absolute coordinate system and nine program coordinate systems.
You can set the offsets of the nine program coordinate systems using G10 L2 Pn (n is the number of the coordinate system) with values for the axes in terms of the absolute coordinate system.
You can select one of the nine systems by using G54, G55, G56, G57, G58, G59, G59.1, G59.2, or G59.3. It is not possible to select the absolute coordinate system directly. You can offset the current coordinate system using G92 or G92.3. This offset will then apply to all nine program coordinate systems. This offset may be cancelled with G92.1 or G92.2.
You can make straight moves in the absolute machine coordinate system by using G53 with either G0 or G1.
Data for coordinate systems is stored in parameters.
During initialization, the coordinate system is selected that is specified by parameter 5220. A value of 1 means the first coordinate system (the one G54 activates), a value of 2 means the second coordinate system (the one G55 activates), and so on. It is an error for the value of parameter 5220 to be anything but a whole number between one and nine.
7.1.4. Format of a Line¶
A permissible line of input RS274/NGC code consists of the following, in order, with the restriction that there is a maximum (currently 256) to the number of characters allowed on a line.
- an optional block delete character, which is a slash
/
. - an optional line number.
- any number of words, parameter settings, and comments.
- an end of line marker (carriage return or line feed or both).
Spaces and tabs are allowed anywhere on a line of code and do not change the meaning of the line,
except inside comments. This makes some strange-looking input legal. The line g0x +0. 12 34y
7
is equivalent to g0 x+0.1234 y7
, for example.
Blank lines are allowed in the input. They are to be ignored.
Input is case insensitive, except in comments, i.e., any letter outside a comment may be in upper or lower case without changing the meaning of a line.
7.1.4.1. Line Number¶
A line number is the letter N
followed by an integer (with no sign) between 0 and 99999
written with no more than five digits (000009 is not OK, for example). Line numbers may be repeated
or used out of order, although normal practice is to avoid such usage. Line numbers may also be
skipped, and that is normal practice. A line number is not required to be used, but must be in the
proper place if used.
7.1.4.2. Word¶
A word is a letter other than N
followed by a real value.
Words may begin with any of the letters shown in the following table. The table includes N
for completeness, even though, as defined above, line numbers are not words. Several letters
(I
, J, K
, L
, P
, R
) may have different meanings in different
contexts.
Table. Linux CNC Words and their meanings Letter
Letter | Meaning |
---|---|
A | A-axis of machine |
B | B-axis of machine |
C | C-axis of machine |
D | tool radius compensation number |
F | feedrate |
G | general function (See table G codes) |
H | tool length offset index |
I | X-axis offset for arcs / X offset in G87 canned cycle |
J | Y-axis offset for arcs / Y offset in G87 canned cycle |
K | Z-axis offset for arcs / Z offset in G87 canned cycle |
L | number of repetitions in canned cycles / key used with G10 |
M | miscellaneous function (see Table M codes) |
N | line number |
P | dwell time in canned cycles / dwell time with G4 / key used with G10 |
Q | feed increment in G83 canned cycle |
R | arc radius |
S | canned cycle plane / spindle speed |
T | tool selection |
X | X-axis of machine |
Y | Y-axis of machine |
Z | Z-axis of machine |
A real value is some collection of characters that can be processed to come up with a number. A real value may be an explicit number (such as 341 or -0.8807), a parameter value, an expression, or a unary operation value.
7.1.4.3. Number¶
The following rules are used for (explicit) numbers. In these rules a digit is a single character between 0 and 9.
- A number consists of (1) an optional plus or minus sign, followed by (2) zero to many digits, followed, possibly, by (3) one decimal point, followed by (4) zero to many digits — provided that there is at least one digit somewhere in the number.
- There are two kinds of numbers: integers and decimals. An integer does not have a decimal point in it; a decimal does.
- Numbers may have any number of digits, subject to the limitation on line length.
- A non-zero number with no sign as the first character is assumed to be positive. Notice that initial (before the decimal point and the first non-zero digit) and trailing (after the decimal point and the last non-zero digit) zeros are allowed but not required. A number written with initial or trailing zeros will have the same value when it is read as if the extra zeros were not there.
7.1.4.4. Parameter Value¶
A parameter value is the pound character #
followed by a real value. The real value must
evaluate to an integer between 1 and 5399. The integer is a parameter number, and the value of the
parameter value is whatever number is stored in the numbered parameter.
The #
character takes precedence over other operations, so that, for example, #1+2
means the number found by adding 2 to the value of parameter 1, not the value found in parameter
3. Of course, #[1+2]
does mean the value found in parameter 3. The #
character may
be repeated; for example ##2
means the value of the parameter whose index is the (integer)
value of parameter 2.
7.1.4.5. Expressions and Binary Operations¶
An expression is a set of characters starting with a left bracket [
and ending with a
balancing right bracket ]
. In between the brackets are numbers, parameter values,
mathematical operations, and other expressions. An expression may be evaluated to produce a
number. The expressions on a line are evaluated when the line is read, before anything on the line
is executed. An example of an expression is [ 1 + acos[0] - [#3 ** [4.0/2]]]
.
Binary operations appear only inside expressions. Nine binary operations are defined. There are four
basic mathematical operations: addition +
, subtraction -
, multiplication *
,
and division /
. There are three logical operations: non-exclusive or OR
, exclusive
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. The binary operations are divided into three
groups. The first group is: power. The second group is: multiplication, division, and modulus. The
third group is: addition, subtraction, logical non- exclusive or, logical exclusive or, and logical
and. If operations are strung together (for example in the expression [2.0 / 3 * 1.5 - 5.5 /
11.0]
), operations in the first group are to be performed before operations in the second group and
operations in the second group before operations in the third group. If an expression contains more
than one operation from the same group (such as the first / and * in the example), the operation on
the left is performed first. Thus, the example is equivalent to: [((2.0 / 3) * 1.5) - (5.5 /
11.0)]
, which simplifies to [1.0 - 0.5]
, which is 0.5.
The logical operations and modulus are to be performed on any real numbers, not just on integers. The number zero is equivalent to logical false, and any non-zero number is equivalent to logical true.
7.1.4.6. Unary Operation Value¶
A unary operation value is either ATAN
followed by one expression divided by another
expression (for example ATAN[2]/[1+3]
) or any other unary operation name followed by an
expression (for example SIN[90]
). The unary operations are: ABS
(absolute value),
ACOS
(arc cosine), ASIN
(arc sine), ATAN
(arc tangent), COS
(cosine), EXP
(e raised to the given power), FIX
(round down), FUP
(round
up), LN
(natural logarithm), ROUND
(round to the nearest whole number), SIN
(sine), SQRT
(square root), and TAN
(tangent). Arguments to unary operations which
take angle measures (COS
, SIN
, and TAN
) are in degrees. Values returned by
unary operations which return angle measures (ACOS
, ASIN
, and ATAN
) are also
in degrees.
The FIX
operation rounds towards the left (less positive or more negative) on a number line,
so that FIX[2.8] = 2
and FIX[-2.8] = -3
, for example. The FUP
operation
rounds towards the right (more positive or less negative) on a number line; FUP[2.8] = 3
and
FUP[-2.8] = -2
, for example.
7.1.4.7. Parameter Setting¶
A parameter setting is the following four items one after the other: (1) a pound character
#
, (2) a real value which evaluates to an integer between 1 and 5399, (3) an equal sign
=
, and (4) a real value. For example #3 = 15
is a parameter setting meaning “set
parameter 3 to 15.”
A parameter setting does not take effect until after all parameter values on the same line have
been found. For example, if parameter 3 has been previously set to 15 and the line #3=6 G1
x#3
is interpreted, a straight move to a point where x equals 15 will occur and the value of
parameter 3 will be 6.
7.1.4.8. Comments and Messages¶
Printable characters and white space inside parentheses is a comment. A left parenthesis always
starts a comment. The comment ends at the first right parenthesis found thereafter. Once a left
parenthesis is placed on a line, a matching right parenthesis must appear before the end of the
line. Comments may not be nested; it is an error if a left parenthesis is found after the start of
a comment and before the end of the comment. Here is an example of a line containing a comment:
G80 M5 (stop motion)
. Comments do not cause a machining center to do anything.
7.1.4.9. Item Repeats¶
A line may have any number of G
words, but two G
words from the same modal group may
not appear on the same line.
A line may have zero to four M
words. Two M
words from the same modal group may not
appear on the same line.
For all other legal letters, a line may have only one word beginning with that letter.
If a parameter setting of the same parameter is repeated on a line, #3=15 #3=6
, for example,
only the last setting will take effect. It is silly, but not illegal, to set the same parameter
twice on the same line.
If more than one comment appears on a line, only the last one will be used; each of the other comments will be read and its format will be checked, but it will be ignored thereafter. It is expected that putting more than one comment on a line will be very rare.
7.1.4.10. Item order¶
The three types of item whose order may vary on a line (as given at the beginning of this section) are word, parameter setting, and comment. Imagine that these three types of item are divided into three groups by type.
The first group (the words) may be reordered in any way without changing the meaning of the line.
If the second group (the parameter settings) is reordered, there will be no change in the meaning of
the line unless the same parameter is set more than once. In this case, only the last setting of the
parameter will take effect. For example, after the line #3=15 #3=6
has been interpreted, the
value of parameter 3 will be 6. If the order is reversed to #3=6 #3=15
and the line is
interpreted, the value of parameter 3 will be 15.
If the third group (the comments) contains more than one comment and is reordered, only the last comment will be used.
If each group is kept in order or reordered without changing the meaning of the line, then the three
groups may be interleaved in any way without changing the meaning of the line. For example, the line
g40 g1 #3=15 (foo) #4=-7.0
has five items and means exactly the same thing in any of the 120
possible orders (such as #4=-7.0 g1 #3=15 g40 (foo)
) for the five items.
7.1.4.11. Commands and Machine Modes¶
In RS274/NGC, many commands cause a machining center to change from one mode to another, and the
mode stays active until some other command changes it implicitly or explicitly. Such commands are
called “modal”. For example, if coolant is turned on, it stays on until it is explicitly turned
off. The G
codes for motion are also modal. If a G1
(straight move) command is given
on one line, for example, it will be executed again on the next line if one or more axis words is
available on the line, unless an explicit command is given on that next line using the axis words or
cancelling motion.
“Non-modal” codes have effect only on the lines on which they occur. For example, G4
(dwell)
is non-modal.
7.1.5. Modal Groups¶
Modal commands are arranged in sets called “modal groups”, and only one member of a modal group may be in force at any given time. In general, a modal group contains commands for which it is logically impossible for two members to be in effect at the same time — like measure in inches vs. measure in millimeters. A machining center may be in many modes at the same time, with one mode from each modal group being in effect.
The modal groups are
Group | G-codes | Comment |
---|---|---|
0 | G4 G10 G28 G30 G53 G92 G92.1 G92.2 G92.3 | group for non-modal G codes |
1 | G0 G1 G2 G3 G38.2 G80 G81 G82 G83 G84 G85 G86 G87 G88 G89 | None |
2 | G17 G18 G19 | plane selection |
3 | G90 G91 | distance mode |
4 | M0 M1 M2 M30 M60 | stopping |
5 | G93 G94 | feed rate mode |
6 | M6 | tool change |
7 | M3 M4 M5 | spindle turning |
8 | M7 M8 M9 | coolant (special case: M7 and M8 may be active at the same time) |
9 | M48 M49 | enable/disable feed and speed override switches |
10 | G98 G99 | return mode in canned cycles |
12 | G54 G55 G56 G57 G58 G59 G59.1 G59.2 G59.3 | coordinate system selection |
13 | G61 G61.1 G64 | path control mode |
For several modal groups, when a machining center is ready to accept commands, one member of the group must be in effect. There are default settings for these modal groups. When the machining center is turned on or otherwise re-initialized, the default values are automatically in effect.
Group 1, the first group on the table, is a group of G
codes for motion. One of these is
always in effect. That one is called the current motion mode.
It is an error to put a G-code from group 1 and a G-code from group 0 on the same line if both of
them use axis words. If an axis word-using G-code from group 1 is implicitly in effect on a line (by
having been activated on an earlier line), and a group 0 G-code that uses axis words appears on the
line, the activity of the group 1 G-code is suspended for that line. The axis word-using G-codes
from group 0 are G10
, G28
, G30
, and G92
.
7.1.6. G and Input Codes¶
For a documentation on G-codes, see PythonicGcodeMachine.Gcode.Rs274.GcodeDoc
.
G-code | Meaning |
---|---|
F | set feed rate |
G0 | rapid positioning |
G1 | linear interpolation |
G10 | coordinate system origin setting |
G17 | XY-plane selection |
G18 | XZ-plane selection |
G19 | YZ-plane selection |
G2 | circular/helical interpolation (clockwise) |
G20 | inch system selection |
G21 | millimeter system selection |
G28 | return to home |
G3 | circular/helical interpolation (counterclockwise) |
G30 | return to secondary home |
G38.2 | straight probe |
G4 | dwell |
G40 | cancel cutter radius compensation |
G41 | start cutter radius compensation left |
G42 | start cutter radius compensation right |
G43 | tool length offset (plus) |
G49 | cancel tool length offset |
G53 | motion in machine coordinate system |
G54 | use preset work coordinate system 1 |
G55 | use preset work coordinate system 2 |
G56 | use preset work coordinate system 3 |
G57 | use preset work coordinate system 4 |
G58 | use preset work coordinate system 5 |
G59 | use preset work coordinate system 6 |
G59.1 | use preset work coordinate system 7 |
G59.2 | use preset work coordinate system 8 |
G59.3 | use preset work coordinate system 9 |
G61 | set path control mode: exact path |
G61.1 | set path control mode: exact stop |
G64 | set path control mode: continuous |
G80 | cancel motion mode (including any canned cycle) |
G81 | canned cycle: drilling |
G82 | canned cycle: drilling with dwell |
G83 | canned cycle: peck drilling |
G84 | canned cycle: right hand tapping |
G85 | canned cycle: boring, no dwell, feed out |
G86 | canned cycle: boring, spindle stop, rapid out |
G87 | canned cycle: back boring |
G88 | canned cycle: boring, spindle stop, manual out |
G89 | canned cycle: boring, dwell, feed out |
G90 | absolute distance mode |
G91 | incremental distance mode |
G92 | offset coordinate systems and set parameters |
G92.1 | cancel offset coordinate systems and set parameters to zero |
G92.2 | cancel offset coordinate systems but do not reset parameters |
G92.3 | apply parameters to offset coordinate systems |
G93 | inverse time feed rate mode |
G94 | units per minute feed rate mode |
G98 | initial level return in canned cycles |
G99 | R-point level return in canned cycles |
M0 | program stop |
M1 | optional program stop |
M2 | program end |
M3 | turn spindle clockwise |
M30 | program end, pallet shuttle, and reset |
M4 | turn spindle counterclockwise |
M48 | enable speed and feed overrides |
M49 | disable speed and feed overrides |
M5 | stop spindle turning |
M6 | tool change |
M60 | pallet shuttle and program stop |
M7 | mist coolant on |
M8 | flood coolant on |
M9 | mist and flood coolant off |
S | set spindle speed |
T | select tool |
7.1.7. Order of Execution¶
The order of execution of items on a line is critical to safe and effective machine operation. Items are executed in a particular order if they occur on the same line.
Order | G-codes | Comment |
---|---|---|
1 | COMMENT | includes message |
2 | G93 G94 | set feed rate mode (inverse time or per minute) |
3 | F | set feed rate |
4 | S | set spindle speed |
5 | T | select tool |
6 | M6 | change tool |
7 | M3 M4 M5 | spindle on or off |
8 | M7 M8 M9 | coolant on or off |
9 | M48 M49 | enable or disable overrides |
10 | G4 | dwell |
11 | G17 G18 G19 | set active plane |
12 | G20 G21 | set length units |
13 | G40 G41 G42 | cutter radius compensation on or off |
14 | G43 G49 | cutter length compensation on or off |
15 | G54 G55 G56 G57 G58 G59 G59.1 G59.2 G59.3 | coordinate system selection |
16 | G61 G61.1 G64 | set path control mode |
17 | G90 G91 | set distance mode |
18 | G98 G99 | set retract mode |
19 | G28 G30 G10 G92 G92.1 G92.2 G94 | home or change coordinate system data or set axis offsets |
20 | G0-G3 G80-G89 G53 | perform motion, as modified (possibly) by G53 |
21 | M0 M1 M2 M30 M60 | stop |