G-code

Language standard

24min

This chapter describes the input language, RS274/NGC.

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. Lines of code may be collected in a file to make a program.

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."

The RS274/NGC language has no indicator for the start of a program. The RS274/NGC language has two commands (M2 or M30), either of which ends a program.

System parameters/Variables

In the RS274/NGC language view, a machining center maintains 5999 numerical parameters. They can be accessed by #1 .. #5999. The specific parameters with a dedicated function are listed in the table below. Other parameters in the range of 1..5999 are free to use in your G-Code program. Some of them have a special function and are read-only; see the table below.

Parameter Number

Purpose

1 - 26*

Used for parameters when overriding m-functions.

In the g-code there is e.g. M999 X100 S1000

And you have in your macro.cnc:

Sub m999 msg “this is my m999 X=”#24” S=”#19 Endsub

Inside the subroutine, the given X and S parameters are at #24 and #19

#1-#26 = A – Z parameter value.

Values are negative -1e10 if not provided with m999 in this example.

27 - 4999

Free to use, note that 4995 – 4999 are used by the tool length measurement function under user button 2.

Z#4996 safe height for tool measurement above the fixed tool setter. x#4997 y#4998 fixed tool setter position. z#4999 chuck height, or zero length tool height, chuck just touches the fixed tool setter at this height. Z#4995 is used for the tool setter height for zeroing Z under user button 1.

4000 - 4999

Free to use, persistent, see above some of these are used by tool length measurement and zeroing.

5001 - 5006*

Position of X – C, interpreter position = work position

5008*

Current tool number

5009*

Current tool radius

5010*

Current tool Z offset (length + zDelta)

5011*

New tool number during tool change (M6)

5012*

Current tool X offset (X offset + xDelta)

5013*

Current G43 Z offset (Z offset + zDelta)

5014*

Current G43 X offset (X offset + xDelta)

5015 - 5050

Used in tool change sub routine

5051 - 5056*

Probe position X - C in machine coordinates

5061 - 5066*

Probe position X - C in work coordinates

5067*

1 if the probe is triggered after G38.2, 0 otherwise

5068*

Current probe value

5069*

Handwheel counter

5070*

Spindle rate in rev/second

5071 - 5076*

Position of X - C, interpreter position without offsets = machine position

5081 - 5086*

Probe position X - C in joint coordinates

5101 - 5106*

MCA negative limit X - C

5111 - 5116*

MCA positive limit X - C

5121 - 5126*

Home position X - C

5131 - 5133*

TCA negative limit X - Z

5141 - 5143*

TCA positive limit X - Z

5150*

Active kinematics type:

1: Trivial 2: 4_AX_ACYLINDER (Y -> A mapping) 3: Virtual C 4 - 17: System reserved 18 - 30: Custom 1 – Custom 12

5151*

ZHC is active

5152*

1 if the spindle is on, 0 if the spindle is off.

5161 - 5166*

G28 home X - C

5181 - 5186*

G30 home X - C

5190*

G68 rotation method (0 = off, 1 = on)

5191 - 5193*

G68/G51 rotation point X - C

5194*

G68 rotation angle XY

5195*

G68 rotation angle YZ

5196*

G68 rotation angle XZ

5200*

0 if G51 scaling is off, 1 if G51 scaling is on

5204*

G51 scaling factor X

5205*

G51 scaling factor Y

5206*

G51 scaling factor Z (always 1.0)

5211 - 5216

G92 offset X - C

5220

Coordinate system number

5221 - 5226

Coordinate system 1 X - C

5241 - 5246

Coordinate system 2 X - C

5261 - 5266

Coordinate system 3 X - C

5281 - 5286

Coordinate system 4 X - C

5301 - 5306

Coordinate system 5 X - C

5321 - 5326

Coordinate system 6 X - C

5341 - 5346

Coordinate system 7 X - C

5361 - 5366

Coordinate system 8 X - C

5381 - 5386

Coordinate system 9 X - C

5390*

Spindle selection 0 = M90, 1 = M91, 2 = M92

5391 - 5393*

Alternative spindle offset X- Z

5394 - 5397*

Spindle speed max M90 - M93

5230, 5250, 5270, 5290, 5310, 5330, 5350, 5370

Reserved for rotation coordinate system

5380*

Simulation mode, 0: controller connected, 1: simulation mode

5397*

Running mode, 0: running, 1: rendering

Use e.g. if you have G38.2 movements in your macro file because during rendering G38.2 always runs until the given end point which may give incorrect tool measurement results. Always take care that the tool table contains (approx.) correct tool data

5398*

Return value of dlgmsg (1 = OK, -1 = Cancel)

5399*

Return value for M55, M56

5401 - 5499

Tool Z offset (length) for tools 1 - 99

5501 - 5599

Tool diameter for tools 1 - 99

5601 - 5699

Tool X offset (for lathe) for tools 1 - 99

5701 - 5799

Tool orientation (for turning) for tools 1 - 99

5801 - 5899

Tool X delta due to wear

5901 - 5999

Tool Z delta due to wear

*parameters in italics are read-only

Tool data

Tool ID

zOffset (Length)

xOffset (For turning)

Diameter

orientation

1







1-9

2







1-9

..







..

99







1-9

Tool Orientation for lathes

When the G18 plane (X-Z) is selected, special LATHE tool radius compensation can be used (G41, G42). Depending on the tool orientation and tool radius an extra offset is applied.

Document image


The blue crosses show the radius center of the tool.

The green crosses show the controlled point depending on the tool orientation. For orientation 9 there is no offset compensation. For orientation 2, the compensation in X is –tool radius, in Z also –tool radius.

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 canceled 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, see the previous section.

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.

The g-code are described in detail in section 3.6

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 line number.
  • Any number of words, parameter settings, and comments.

Any input not explicitly allowed is illegal and will cause the Interpreter to signal an error.

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.

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.

Word

A word is a letter other than N followed by a real value.

Words may begin with any of the letters shown in Table 3-2. The table includes N for completeness, even though, as defined above, line numbers are not words. Several letters (I, J, K, L, P, and R) may have different meanings in different contexts.

Letter

Meaning

D

Tool radius compensation number

F

Feed rate

G

General function (see Table 3-4)

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 3-6)

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, clear_z distance in canned cycle

S

spindle speed

T

tool selection

X

X-axis of machine

Y

Y-axis of machine

Z

Z-axis of machine

A

A-axis of machine

B

B-axis of machine

C

C-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. Definitions of these follow immediately. Processing characters to come up with a number is called "evaluating". An explicit number evaluates itself.

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. Only about seventeen significant figures will be retained, however (enough for all known applications).
  • 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.

Numbers used for specific purposes in RS274/NGC are often restricted to some finite set of values or some to some range of values. In many uses, decimal numbers must be close to integers; this includes the values of indexes (for parameters and carousel slot numbers, for example), M codes, and G codes multiplied by ten. A decimal number that is supposed to be close to an integer is considered close enough if it is within 0.0001 of an integer.

Parameter Value

A parameter number is the pound character # followed by an integer value between 1 and 5399. The value of the parameter 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.

Examples using a parameter:

#100 = 1.1234

#101 = [#100 + 1]

G1 F[#100] X[#101 + 10]

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.

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.

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.

Comments and Messages

Printable characters and white space inside parentheses are 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’s an example of a line containing a comment: G80 M5 (stop motion). Comments do not cause a machining center to do anything.

A comment contains a message if MSG, appears after the left parenthesis and before any other printing characters. Variants of MSG, which include white space and lowercase characters are allowed. The rest of the characters before the right parenthesis are considered to be a message. Messages should be displayed on the message display device. Comments not containing messages need not be displayed there.

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.

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. Putting more than one comment on a line is expected to be very rare.

Item order

The three types of items 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 items 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 modification of the parameter will take effect. For example, after 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.

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 are available on the line, unless an explicit command is given on that next line using the axis words or canceling motion.

"Non-modal" codes have an effect only on the lines on which they occur. For example, G4 (dwell) is non-modal.

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 shown in the following tables.

Modal groups for G-Codes

Name

G-Codes

Group

Group 1

G0, G1, G2, G3, G38.2, G76, G80, G81, G82, G83, G84, G85, G86, G87, G88, G89

Motion

Group 2

G17, G18, G19

Plane selection

Group 3

G90, G91

Distance mode

Group 5

G93, G94

Feed rate mode

Group 6

G20, G21

Units

Group 7

G40, G41, G42

Cutter radius compensation

Group 8

G43, G49

Tool length offset

Group 10

G98, G99

Return mode in canned cycles

Group 12

G54, G55, G56, G57, G58, G59, G59.1, G59.2, G59.3

Coordinate system selection

Group 13

G61, G61.1, G64

Path control mode

Group 14

G68, G69

XY plane rotation

Modal groups for M-Codes

Name

M-Codes

Group

Group 4

M0, M1, M2, M30, M60

Stopping

Group 5

M54, M55, M56, M64, M65, M66

AUX and general purpose I/O

Group 6

M6

Tool change

Group 7

M3, M4, M5

Spindle turning

Group 8

M7, M8, M9

Coolant

Group 9

M48, M49, M50, M51, M52

Enable/disable feed and speed override switches

Group 10

M90, M91, M92, M95, M97

Select standard or alternate spindle or touch probe or camera offset, M90 = standard

Enable THC

M20, M21

THC ON | THC OFF (Torch height control)

A axis clamp

M26, M27

Clamp on | clamp off

Non-modal groups

Name

M-Codes

Group

Group 0

G4, G10, G28, G30, G53, G92, G92.1, G92.2, G92.3



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. The default values are automatically in effect when the machining center is turned on or otherwise re-initialized.

Group 1 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.