Introduction
This chapter describes the input language, RS274/NGC. 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. 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.
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.
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.
G Codes
G codes of the RS274/NGC language are shown on a specific page each.
The descriptions contain command prototypes, formatted as a code block.
In the command prototypes, three dots … substitute a real value. As described earlier, a real value may be (1) an explicit number, 4, for example, (2) an expression like [2+2], (3) a parameter value, #86, for example, or (4) a unary function value like acos[0].
In most cases, if axis words (any or all of X…, Y…, Z…, A…, B…, C…) are given, they specify a destination point. Axis numbers are in the currently active coordinate system unless explicitly described as being in the absolute coordinate system. Where axis words are optional, any omitted axes will have their current value. Any items in the command prototypes not explicitly described as optional are required. It is an error if a required item is omitted.
In the prototypes, the values following letters are often given as explicit numbers. Unless stated otherwise, the explicit numbers can be real values. For example, G10 L2 could equally well be written G[2*5] L[1+1]. If the value of parameter 100 were 2, G10 L#100 would also mean the same. Using real values which are not explicit numbers as just shown in the examples is rarely useful.
If L… is written in a prototype the … will often be referred to as the "L number". Similarly the … in H… may be called the "H number", and so on for any other letter.