|
|||||||||
| Language |
|---|
| Site Search |
|---|
| Polls |
|---|
Table of Contents
- 1. Machining Center Overview
- 1.1. Mechanical Components
- 1.1.1. Axes
- 1.1.2. Spindle
- 1.1.3. Coolant
- 1.1.4. Pallet Shuttle
- 1.1.5. Tool Carousel
- 1.1.6. Tool Changer
- 1.1.7. Message Display
- 1.1.8. Feed and Speed Override Switches
- 1.1.9. Block Delete Switch
- 1.1.10. Optional Program Stop Switch
- 1.2. Control and Data Components
- 1.2.1. Linear Axes
- 1.2.2. Rotational Axes
- 1.2.3. Controlled Point
- 1.2.4. Coordinated Linear Motion
- 1.2.5. Feed Rate
- 1.2.6. Coolant
- 1.2.7. Dwell
- 1.2.8. Units
- 1.2.9. Current Position
- 1.2.10. Selected Plane
- 1.2.11. Tool Carousel
- 1.2.12. Tool Change
- 1.2.13. Pallet Shuttle
- 1.2.14. Feed and Speed Override Switches
- 1.2.15. Path Control Mode
- 1.3. Interpreter Interaction with Switches
- 1.3.1. Feed and Speed Override Switches
- 1.3.2. Block Delete Switch
- 1.3.3. Optional Program Stop Switch
- 1.4. Tool File
- 1.4.1. Mill Format Tool Files
- 1.4.2. Lathe Format Tool Files
- 1.5. Parameters
- 1.6. Coordinate Systems
- 2. Language Overview
- 2.1. Format of a line
- 2.2. Line Number
- 2.3. Word
- 2.3.1. Number
- 2.3.2. Numbered Parameters
- 2.3.3. Named Parameters
- 2.3.4. Expressions
- 2.3.5. Binary Operators
- 2.3.6. Functions
- 2.4. Comments
- 2.4.1. Messages
- 2.4.2. Probe Logging
- 2.4.3. General logging
- 2.4.4. Debugging messages
- 2.4.5. Parameters in special comments
- 2.5. Repeated Items
- 2.6. Item order
- 2.7. Commands and Machine Modes
- 2.8. Modal Groups
- 3. G Codes
- 3.1. G0: Rapid Linear Motion
- 3.2. G1: Linear Motion at Feed Rate
- 3.3. G2, G3: Arc at Feed Rate
- 3.3.1. Center format arcs (preferred format)
- 3.3.2. Radius format arcs (discouraged format)
- 3.4. G4: Dwell
- 3.5. G10: Set Coordinate System Data
- 3.6. G17, G18, G19: Plane Selection
- 3.7. G20, G21: Length Units
- 3.8. G28, G30: Return to Predefined Absolute Position
- 3.9. G33, G33.1: Spindle-Synchronized Motion
- 3.10. G38.x: Straight Probe
- 3.11. G40, G41, G42, G41.1, G42.1: Cutter Radius Compensation.
- 3.11.1. Cutter Radius Compensation from Tool Table
- 3.11.2. Dynamic Cutter Radius Compensation
- 3.12. G43, G43.1, G49: Tool Length Offsets
- 3.12.1. G43, G43.1: Activate Tool length compensation
- 3.12.2. G49: Cancel tool length compensation
- 3.13. G53: Move in absolute coordinates
- 3.14. G54 to G59.3: Select Coordinate System
- 3.15. G61, G61.1, G64: Set Path Control Mode
- 3.16. G80: Cancel Modal Motion
- 3.17. G76: Threading Canned Cycle
- 3.18. G81 to G89: Canned Cycles
- 3.18.1. Preliminary and In-Between Motion
- 3.18.2. G81: Drilling Cycle
- 3.18.3. G82: Drilling Cycle with Dwell
- 3.18.4. G83: Peck Drilling
- 3.18.5. G84: Right-Hand Tapping
- 3.18.6. G85: Boring, No Dwell, Feed Out
- 3.18.7. G86: Boring, Spindle Stop, Rapid Out
- 3.18.8. G87: Back Boring
- 3.18.9. G88: Boring, Spindle Stop, Manual Out
- 3.18.10. G89: Boring, Dwell, Feed Out
- 3.18.11. G90, G91: Set Distance Mode
- 3.19. G92, G92.1, G92.2, G92.3: Coordinate System Offsets
- 3.20. G93, G94, G95: Set Feed Rate Mode
- 3.21. G96, G97: Spindle control mode
- 3.22. G98, G99: Set Canned Cycle Return Level
- 4. M Codes
- 4.1. M0, M1, M2, M30, M60: Program Stopping and Ending
- 4.2. M3, M4, M5: Spindle Control
- 4.3. M6: Tool Change
- 4.4. M7, M8, M9: Coolant Control
- 4.5. M48, M49: Override Control
- 4.6. M50: Feed Override Control
- 4.7. M51: Spindle Speed Override Control
- 4.8. M52: Adaptive Feed Control
- 4.9. M53: Feed Stop Control
- 4.10. M62 to M65: Digital Output Control
- 4.11. M66: Digital and Analog Input Control
- 4.12. M100 to M199: User Defined Commands
- 5. O Codes
- 5.1. Subroutines: “sub”, “endsub”, “return”, “call”
- 5.2. Looping: “do”, “while”, “endwhile”, “break”, “continue”
- 5.3. Conditional: “if”, “else”, “endif”
- 5.4. Indirection
- 5.5. Computing values in O-words
- 6. Other Codes
- 6.1. F: Set Feed Rate
- 6.2. S: Set Spindle Speed
- 6.3. T: Select Tool
- 7. Order of Execution
- 8. G-Code Best Practices
- 8.1. Use an appropriate decimal precision
- 8.2. Use consistent white space
- 8.3. Prefer “Center-format” arcs
- 8.4. Put important modal settings at the top of the file
- 8.5. Don't put too many things on one line
- 8.6. Don't use line numbers
- 8.7. When moving more than one coordinate system, consider inverse time feed mode
- 9. Tool File and Compensation
- 9.1. Tool File
- 9.2. Tool Compensation
- 9.3. Tool Length Offsets
- 9.4. Cutter Radius Compensation
- 9.4.1. Cutter Radius Compensation Detail
- 9.4.2. First Move
- 9.4.3. Jon Elson's Example
- 9.5. Tool Compensation Sources
- 10. Differences between EMC2 gcode and RS274NGC
- 10.1. Differences that change the meaning of well-formed RS274NGC programs
- 10.1.1. Location after a tool change
- 10.1.2. Offset parameters are inifile units
- 10.1.3. Tool table lengths/diameters are in inifile units
- 10.1.4. G84, G87 not implemented
- 10.1.5. G28, G30 with axis words
- 10.1.6. M62, M63 not implemented
- 10.2. Differences that do not change the meaning of well-formed RS274NGC programs
- 10.2.1. G33, G76 threading codes
- 10.2.2. G38.2
- 10.2.3. G38.3…G38.5
- 10.2.4. O-codes
- 10.2.5. M50…M53 overrides
- 10.2.6. G43, G43.1
- 10.2.7. G41.1, G42.1
- 10.2.8. G43 without H word
- 10.2.9. U, V, and W axes
List of figures
List of tables
- Sample Tool File (mill format)
- Sample Tool File (lathe format)
- Parameters Used by the RS274NGC Interpreter
- Parameter File Format
- Words and their meanings
- Operator Precedence
- Functions
- Modal Groups
- Probing codes
1 Machining Center Overview
This section gives a brief description of how a machining center is viewed from the input and output ends of the Interpreter. It is assumed the reader is already familiar with machining centers.
Both the RS274/NGC input language and the output canonical machining functions have a view of (1) mechanical components of a machining center being controlled and (2) what activities of the machining center may be controlled, and what data is used in control.
The view here includes some items that a given machining center may not have, such as a pallet shuttle. The RS274/NGC language and canonical machining functions may be used with such a machine provided that no NC program used with the controller includes commands intended to activate physical capabilities the machine does not have. For such a machine, it would be useful to modify the Interpreter so it will reject input commands and will not produce output canonical function calls addressed to non-existent equipment.
1.1 Mechanical Components
A machining center has many mechanical components that may be controlled or may affect the way in which control is exercised. This section describes the subset of those components that interact with the Interpreter. Mechanical components that do not interact directly with the Interpreter, such as the jog buttons, are not described here, even if they affect control.
1.1.1 Axes
Any machining center has one or more Axes. Different types of machining centers have different combinations. For instance, a “4-axis milling machine” may have XYZA or XYZB axes. A lathe typically has XZ axes. A foam-cutting machine may have XYUZ axes.12
1.1.1.1 Primary Linear Axes
The X, Y, and Z axes produce linear motion in three mutually orthogonal directions
1.1.1.2 Secondary Linear Axes
The U, V, and W axes produce linear motion in three mutually orthogonal directions. Typically, X and U are parallel, Y and V are parallel, and Z and W are parallel.
1.1.1.3 Rotational Axes
The A, B and C axes produce angular motion (rotation). Typically, A rotates around a line parallel to X, B rotates around a line parallel to Y, and C rotates around a line parallel to Z.
1.1.2 Spindle
A machining center has a spindle which holds one cutting tool, probe, or other item. The spindle can rotate in either direction, and it can be made to rotate at a constant rate, which may be changed. Except on machines where the spindle may be moved by moving a rotational axis, the axis of the spindle is kept parallel to the Z-axis and is coincident with the Z-axis when X and Y are zero. The spindle can be stopped in a fixed orientation or stopped without specifying orientation.
1.1.3 Coolant
A machining center has components to provide mist coolant and/or flood coolant.
1.1.4 Pallet Shuttle
A machining center has a pallet shuttle system. The system has two movable pallets on which workpieces can be fixtured. Only one pallet at a time is in position for machining.
1.1.5 Tool Carousel
A machining center has a tool carousel with slots for tools fixed in tool holders.
1.1.6 Tool Changer
A machining center has a mechanism for changing tools (fixed in tool holders) between the spindle and the tool carousel.
1.1.7 Message Display
A machining center has a device that can display messages.
1.1.8 Feed and Speed Override Switches
A machining center has separate feed and speed override switches, which let the operator specify that the actual feed rate or spindle speed used in machining should be some percentage of the programmed rate. See Section [.].
1.1.9 Block Delete Switch
A machining center has a block delete switch. See Section [.].
1.1.10 Optional Program Stop Switch
A machining center has an optional program stop switch. See Section [.].
1.2 Control and Data Components
1.2.1 Linear Axes
The X, Y, and Z axes form a standard right-handed coordinate system of orthogonal linear axes. Positions of the three linear motion mechanisms are expressed using coordinates on these axes.
The U, V and W axes also form a standard right-handed coordinate system. X and U are parallel, Y and V are parallel, and Z and W are parallel.
1.2.2 Rotational Axes
The rotational axes are measured in degrees as wrapped linear axes in which the direction of positive rotation is counterclockwise when viewed from the positive end of the corresponding X, Y, or Z-axis. By “wrapped linear axis,” we mean one on which the angular position increases without limit (goes towards plus infinity) as the axis turns counterclockwise and deceases without limit (goes towards minus infinity) as the axis turns clockwise. Wrapped linear axes are used regardless of whether or not there is a mechanical limit on rotation.
Clockwise or counterclockwise is from the point of view of the workpiece. If the workpiece is fastened to a turntable which turns on a rotational axis, a counterclockwise turn from the point of view of the workpiece is accomplished by turning the turntable in a direction that (for most common machine configurations) looks clockwise from the point of view of someone standing next to the machine.3
1.2.3 Controlled Point
The controlled point is the point whose position and rate of motion are controlled. When the tool length offset is zero (the default value), this is a point on the spindle axis (often called the gauge point) that is some fixed distance beyond the end of the spindle, usually near the end of a tool holder that fits into the spindle. The location of the controlled point can be moved out along the spindle axis by specifying some positive amount for the tool length offset. This amount is normally the length of the cutting tool in use, so that the controlled point is at the end of the cutting tool. On a lathe, tool length offsets can be specified for X and Z axes, and the controlled point is either at the tool tip or slightly outside it (where the perpendicular, axis-aligned lines touched by the “front” and “side” of the tool intersect).
1.2.4 Coordinated Linear Motion
To drive a tool along a specified path, a machining center must often coordinate the motion of several axes. We use the term “coordinated linear motion” to describe the situation in which, nominally, each axis moves at constant speed and all axes move from their starting positions to their end positions at the same time. If only the X, Y, and Z axes (or any one or two of them) move, this produces motion in a straight line, hence the word “linear” in the term. In actual motions, it is often not possible to maintain constant speed because acceleration or deceleration is required at the beginning and/or end of the motion. It is feasible, however, to control the axes so that, at all times, each axis has completed the same fraction of its required motion as the other axes. This moves the tool along same path, and we also call this kind of motion coordinated linear motion.
Coordinated linear motion can be performed either at the prevailing feed rate, or at traverse rate, or it may be synchronized to the spindle rotation. If physical limits on axis speed make the desired rate unobtainable, all axes are slowed to maintain the desired path.
1.2.5 Feed Rate
The rate at which the controlled point or the axes move is nominally a steady rate which may be set by the user. In the Interpreter, the interpretation of the feed rate is as follows unless “inverse time feed” or “feed per revolution” modes are being used (see Section [.]).
- If any of XYZ are moving, F is in units per minute in the XYZ cartesian system, and all other axes (UVWABC) move so as to start and stop in coordinated fashion
- Otherwise, if any of UVW are moving, F is in units per minute in the UVW cartesian system, and all other axes (ABC) move so as to start and stop in coordinated fashion
- Otherwise, the move is pure rotary motion and the F word is in rotary units in the ABC “pseudo-cartesian” system.
1.2.6 Coolant
Flood coolant and mist coolant may each be turned on independently. The RS274/NGC language turns them off together (see Section [.]).
1.2.7 Dwell
A machining center may be commanded to dwell (i.e., keep all axes unmoving) for a specific amount of time. The most common use of dwell is to break and clear chips, so the spindle is usually turning during a dwell. Regardless of the Path Control Mode (see Section [.]) the machine will stop exactly at the end of the previous programmed move, as though it was in exact path mode.
1.2.8 Units
Units used for distances along the X, Y, and Z axes may be measured in millimeters or inches. Units for all other quantities involved in machine control cannot be changed. Different quantities use different specific units. Spindle speed is measured in revolutions per minute. The positions of rotational axes are measured in degrees. Feed rates are expressed in current length units per minute, or degrees per minute, or length units per spindle revolution, as described in Section [.].
1.2.9 Current Position
The controlled point is always at some location called the “current position,” and the controller always knows where that is. The numbers representing the current position must be adjusted in the absence of any axis motion if any of several events take place:
- Length units are changed.
- Tool length offset is changed.
- Coordinate system offsets are changed.
1.2.10 Selected Plane
There is always a “selected plane”, which must be the XY-plane, the YZ-plane, or the XZ-plane of the machining center. The Z-axis is, of course, perpendicular to the XY-plane, the X-axis to the YZ-plane, and the Y-axis to the XZ-plane.
1.2.11 Tool Carousel
Zero or one tool is assigned to each slot in the tool carousel.
1.2.12 Tool Change
A machining center may be commanded to change tools.
1.2.13 Pallet Shuttle
The two pallets may be exchanged by command.
1.2.14 Feed and Speed Override Switches
The feed and speed override switches may be enabled (so they work as expected) or disabled (so they have no effect on the feed rate or spindle speed). The RS274/NGC language has one command that enables both switches and one command that disables both (see Section [.]). See Section [.] for further details.
1.2.15 Path Control Mode
The machining center may be put into any one of three path control modes: (1) exact stop mode, (2) exact path mode, or (3) continuous mode with optional tolerance. In exact stop mode, the machine stops briefly at the end of each programmed move. In exact path mode, the machine follows the programmed path as exactly as possible, slowing or stopping if necessary at sharp corners of the path. In continuous mode, sharp corners of the path may be rounded slightly so that the feed rate may be kept up (but by no more than the tolerance, if specified). See Section [.].
1.3 Interpreter Interaction with Switches
The Interpreter interacts with several switches. This section describes the interactions in more detail. In no case does the Interpreter know what the setting of any of these switches is.
1.3.1 Feed and Speed Override Switches
The Interpreter will interpret RS274/NGC commands which enable (M48) or disable (M49) the feed and speed override switches. For certain moves, such as the traverse out of the end of a thread during a threading cycle, the switches are disabled automatically.
EMC2 reacts to the speed and feed override settings when these switches are enabled.
1.3.2 Block Delete Switch
If the block delete switch is on, lines of RS274/NGC code which start with a slash (the block delete character) are not interpreted. If the switch is off, such lines are interpreted. Normally the block delete switch should be set before starting the NGC program.
1.3.3 Optional Program Stop Switch
If this switch is on and an M1 code is encountered, program execution is paused.
1.4 Tool File
A tool file is required to use the Interpreter. The file tells which tools are in which carousel slots and what the length and diameter of each tool are.
The file consists of any number of header lines, followed by one blank line, followed by any number of lines of data. The header lines are ignored by the interpreter. It is important that there be exactly one blank line (with no spaces or tabs, even) before the data. The header line shown in Table [.] describes the data columns, so it is suggested (but not required) that such a line always be included in the header.
Each data line of the file contains the data for one tool. The line may contain 4 or 5 elements (“mill format”) or 8 or 9 elements (“lathe format”).
The units used for the length and diameter are in machine units.
The lines do not have to be in any particular order. Switching the order of lines has no effect unless the same slot number is used on two or more lines, which should not normally be done, in which case the data for only the last such line will be used.
In emc, the location of the tool file is specified in the ini file. See section [->] for more details.
A tool file may have a mixture of “mill format” and “lathe format” lines, though usually the “lathe format” lines are only required for lathe-type tooling.
1.4.1 Mill Format Tool Files
The “mill format” of a tool file is shown in Table [.].
| FMS | TLO | Diameter | Comment | |
| 1 | 1 | 2.0 | 1.0 | |
| 2 | 2 | 1.0 | 0.2 | |
| 5 | 5 | 1.5 | 0.25 | endmill |
| 10 | 10 | 2.4 | -0.3 | for testing |
Each line has five entries. The first four entries are required. The last entry (a comment) is optional. It makes reading easier if the entries are arranged in columns, as shown in the table, but the only format requirement is that there be at least one space or tab after each of the first three entries on a line and a space, tab, or newline at the end of the fourth entry. The meanings of the columns and the type of data to be put in each are as follows.
The “Pocket” column contains an unsigned integer which represents the pocket number (slot number) of the tool carousel slot in which the tool is placed. The entries in this column must all be different.
The “FMS” column contains an unsigned integer which represents a code number for the tool. The user may use any code for any tool, as long as the codes are unsigned integers. This is typically the same as the pocket number.
The “TLO” column contains a real number which represents the tool length offset. This number will be used if tool length offsets are being used and this pocket is selected. This is normally a positive real number, but it may be zero or any other number if it is never to be used.
The “Diameter” column contains a real number. This number is used only if tool radius compensation is turned on using this pocket. If the programmed path during compensation is the edge of the material being cut, this should be a positive real number representing the measured diameter of the tool. If the programmed path during compensation is the path of a tool whose diameter is nominal, this should be a small number (positive, negative, or zero) representing the difference between the measured diameter of the tool and the nominal diameter. If cutter radius compensation is not used with a tool, it does not matter what number is in this column.
The “Comment” column may optionally be used to describe the tool. Any type of description is OK. This column is for the benefit of human readers only.
1.4.2 Lathe Format Tool Files
The “lathe format” of a tool file is shown in Table [.].
| FMS | ZOFFSET | XOFFSET | DIA | FRONTANGLE | BACKANGLE | ORIENTATION | Comment | |
| 1 | 1 | 0.0 | 0.0 | 0.1 | 95.0 | 155.0 | 1 | |
| 2 | 2 | 0.5 | 0.5 | 0.1 | 120 | 60 | 6 |
The Pocket, FMS, DIA and Comment fields are as for mill format tool files. The ZOFFSET field is the same as the TLO field of mill format tool files.
The XOFFSET field gives an offset for the X coordinate when tool length offsets are in effect.
The ORIENTATION field gives the orientation of the lathe tool, as illustrated in [.]. The red cross is the controlled point. See [.].
The FRONTANGLE and BACKANGLE fields are used by some user interfaces to display a fancy representation of the lathe tool.
1.5 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 persists over time, even if the machining center is powered down. EMC2 uses a parameter file to ensure persistence and gives the Interpreter the responsibility for maintaining the file. The Interpreter reads the file when it starts up, and writes the file when it exits.
| Parameter Number(s) | Meaning |
| 5061-5070 | Result of “G38.2” Probe |
| 5161-5169 | “G28” Home |
| 5181-5189 | “G30” Home |
| 5211-5219 | “G92” offset |
| 5220 | Coordinate System Number |
| 5221-5229 | Coordinate System 1 |
| 5241-5249 | Coordinate System 2 |
| 5261-5269 | Coordinate System 3 |
| 5281-5289 | Coordinate System 4 |
| 5301-5309 | Coordinate System 5 |
| 5321-5329 | Coordinate System 6 |
| 5341-5349 | Coordinate System 7 |
| 5361-5369 | Coordinate System 8 |
| 5381-5389 | Coordinate System 9 |
| 5399 | Result of M66 - Check or wait for input |
The format of a parameter file is shown in Table [.]. The file consists of any number of header lines, followed by one blank line, followed by any number of lines of data. The Interpreter skips over the header lines. It is important that there be exactly one blank line (with no spaces or tabs, even) before the data. The header line shown in Table [.] describes the data columns, so it is suggested (but not required) that that line always be included in the header.
The Interpreter reads only the first two columns of the table. The third column, “Comment,” is not read by the Interpreter.
Each line of the file contains the index number of a parameter in the first column and the value to which that parameter should be set in the second column. The value is represented as a double-precision floating point number inside the Interpreter, but a decimal point is not required in the file. All of the parameters shown in Table [.] are required parameters and must be included in any parameter file, except that any parameter representing a rotational axis value for an unused axis may be omitted. An error will be signalled if any required parameter is missing. A parameter file may include any other parameter, as long as its number is in the range 1 to 5400. The parameter numbers must be arranged in ascending order. An error will be signalled if not. Any parameter included in the file read by the Interpreter will be included in the file it writes as it exits. The original file is saved as a backup file when the new file is written. Comments are not preserved when the file is written.
| Parameter Number | Parameter Value | Comment |
| 5161 | 0.0 | G28 Home X |
| 5162 | 0.0 | G28 Home Y |
1.6 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. See Section [.].
You can select one of the nine systems by using G54, G55, G56, G57, G58, G59, G59.1, G59.2, or G59.3 (see Section [.]). 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. See Section [.].
You can make straight moves in the absolute machine coordinate system by using G53 with either G0 or G1. See Section [.].
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.
2 Language 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 General and 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 Interpreter, however, deals with files. A single program may be in a single file, or a program may be spread across several files. A file may demarcated with percents in the following way. The first non-blank line of a file may contain nothing but a percent sign, “%”, possibly surrounded by white space, and later in the file (normally at the end of the file) there may be a similar line. Demarcating a file with percents is optional if the file has an M2 or M30 in it, but is required if not. An error will be signalled if a file has a percent line at the beginning but not at the end. The useful contents of a file demarcated by percents stop after the second percent line. Anything after that is ignored.
The RS274/NGC language has two commands (M2 or M30), either of which ends a program. A program may end before the end of a file. Lines of a file that occur after the end of a program are not to be executed. The interpreter does not even read them.
2.1 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).
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, except in comments, i.e., any letter outside a comment may be in upper or lower case without changing the meaning of a line.
2.2 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.
2.3 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 [.]. 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. Letters which refer to axis names are not valid on a machine which does not have the corresponding axis.
| Letter | Meaning |
| A | A axis of machine |
| B | B axis of machine |
| C | C axis of machine |
| D | Tool radius compensation number |
| F | Feed rate |
| G | General function (See table 5) |
| H | Tool length offset index |
| I | X offset for arcs and G87 canned cycles |
| J | Y offset for arcs and G87 canned cycles |
| K | Z offset for arcs and G87 canned cycles. |
| Spindle-Motion Ratio for G33 synchronized movements. | |
| M | Miscellaneous function (See table 7) |
| N | Line number |
| P | Dwell time in canned cycles and with G4. |
| Key used with G10. | |
| Q | Feed increment in G83 canned cycle |
| R | Arc radius or canned cycle plane |
| S | Spindle speed |
| T | Tool selection |
| U | U axis of machine |
| V | V axis of machine |
| W | W axis of machine |
| X | X axis of machine |
| Y | Y axis of machine |
| Z | Z axis of machine |
2.3.1 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 which is supposed be close to an integer is considered close enough if it is within 0.0001 of an integer.
2.3.2 Numbered Parameters
A numbered parameter is the pound character # followed by an integer between 1 and 5399. The parameter is referred to by this integer, and its value is whatever number is stored in the parameter.
A value is stored in a parameter with the = operator; for example "#3 = 15" means "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.
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.
2.3.3 Named Parameters
Named parameters work like numbered parameters but are easier to read. All parameter names are converted to lower case and have spaces and tabs removed. Named parameters must be enclosed with < > marks.
#<named parameter here> is a local named parameter. By default, a named parameter is local to the scope in which it is assigned. You can't access a local parameter outside of its subroutine - this is so that two subroutines can use the same parameter names without fear of one subroutine overwriting the values in another.
#<_global named parameter here> is a global named parameter. They are accessible from within called subroutines and may set values within subroutines that are accessible to the caller. As far as scope is concerned, they act just like regular numeric parameters. They are not stored in files.
Examples:
- Declaration of named global variable
#<_endmill_dia> = 0.049
- Reference to previously declared global varaiable
#<_endmill_rad> = [#<_endmill_dia>/2.0]
- Mixed literal and named params
o100 call [0.0] [0.0] [#<_inside_cutout>-#<_endmill_dia>] [#<_Zcut>] [#<_feedrate>]
Notes:
The global parameters _a, _b, _c, ... _z have been reserved for special use. In the future, they may provide access to the last Aword, Bword, Cword, etc.
2.3.4 Expressions
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 is 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]]].
2.3.5 Binary Operators
Binary operators only appear inside expressions. 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 relational operators are equality (EQ), inequality (NE), strictly greater than (GT), greater than or equal to (GE), strictly less than (LT), and less than or equal to (LE).
The binary operations are divided into several groups according to their precedence. (see table [.]) If operations in different precedence groups are strung together (for example in the expression [2.0 / 3 * 1.5 - 5.5 / 11.0]), operations in a higher group are to be performed before operations in a lower 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 is equivalent to 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.
| Operators | Precedence |
| ** | highest |
| * / MOD | |
| + - | |
| EQ NE GT GE LT LE | |
| AND OR XOR | lowest |
2.3.6 Functions
A function is either “ATAN” followed by one expression divided by another expression (for example “ATAN[2]/[1+3]”) or any other function name followed by an expression (for example “SIN[90]”). The available functions are shown in table [.]. 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.
| Function Name | Function result |
| ATAN[Y]/[X] | Four quadrant tangent |
| ATAN[arg] | Two quadrant tangent |
| ABS[arg] | Absolute value |
| ACOS[arg] | Inverse cosine |
| ASIN[arg] | Inverse sine |
| ATAN[arg] | Inverse tangent |
| COS[arg] | Cosine |
| EXP[arg] | e raised to the given power |
| FIX[arg] | Round down to integer |
| FUP[arg] | Round up to integer |
| ROUND[arg] | Round to nearest integer |
| LN[arg] | Base-e logarithm |
| SIN[arg] | Sine |
| SQRT[arg] | Square Root |
| TAN[arg] | Tangent |
2.4 Comments
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.
2.4.1 Messages
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 lower case 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.
2.4.2 Probe Logging
A comment can also be used to specify a file for the results of G38.x probing. See section [.].
Often, general logging is more useful than probe logging. Using general logging, the format of the output data can be controlled.
2.4.3 General logging
2.4.3.1 (LOGOPEN,filename)
Opens the named log file. If the file already exists, it is truncated.
2.4.3.2 (LOGCLOSE)
If the log file is open, it is closed.
2.4.3.3 (LOG,…)
The message “…” is expanded as described below and then written to the log file if it is open.
2.4.4 Debugging messages
Comments that look like: (debug, rest of comment) are the same as comments like (msg, rest of comment) with the addition of special handling for parameters.
Comments that look like: (print, rest of comment) are output to stderr with special handling for parameters.
2.4.5 Parameters in special comments
In the DEBUG, PRINT and LOG comments, the values of parameters in the message are expanded.
For example: to print a named global variable to stderr (the default console window) add a line to your gcode like...
(print,endmill dia = #<_endmill_dia>)
Inside the above types of comments, sequences like #123 are replaced by the value of the parameter 123. Sequences like #<named parameter> are replaced by the value of the named parameter. Remember that named parameters will have whitespace removed from them. So, #<named parameter> is the same as #<namedparameter>.
2.5 Repeated Items
A line may have any number of G words, but two G words from the same modal group (see Section [.]) 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.
2.6 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.
2.7 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.
2.8 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 Table [.].
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.
It is an error to include any unrelated words on a line with O- flow control.
3 G Codes
G codes of the RS274/NGC language are shown in Table 5 and described following that.
In the command prototypes, the hypen (-) stands for a real value. As described earlier, a real value may be (1) an explicit number, 4, for example, (2) an expression, [2+2], for example, (3) a parameter value, #88, for example, or (4) a unary function value, acos[0], for example.
In most cases, if axis words (any or all of X-, Y-, Z-, A-, B-, C-, U-, V-, W-) 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 prototypes, this will be written as axes.
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.
3.1 G0: Rapid Linear Motion
For rapid linear motion, program G0 axes, where all the axis words are optional, except that at least one must be used. The G0 is optional if the current motion mode is G0. This will produce coordinated linear motion to the destination point at the current traverse rate (or slower if the machine will not go that fast). It is expected that cutting will not take place when a G0 command is executing.
It is an error if:
- all axis words are omitted.
If cutter radius compensation is active, the motion will differ from the above; see Section [.]. If G53 is programmed on the same line, the motion will also differ; see Section [.].
3.2 G1: Linear Motion at Feed Rate
For linear motion at feed rate (for cutting or not), program G1 axes, where all the axis words are optional, except that at least one must be used. The G1 is optional if the current motion mode is G1. This will produce coordinated linear motion to the destination point at the current feed rate (or slower if the machine will not go that fast).
It is an error if:
- all axis words are omitted.
If cutter radius compensation is active, the motion will differ from the above; see Section [.]. If G53 is programmed on the same line, the motion will also differ; see Section [.].
3.3 G2, G3: Arc at Feed Rate
A circular or helical arc is specified using either G2 (clockwise arc) or G3 (counterclockwise arc). The axis of the circle or helix must be parallel to the X, Y, or Z-axis of the machine coordinate system. The axis (or, equivalently, the plane perpendicular to the axis) is selected with G17 (Z-axis, XY-plane), G18 (Y-axis, XZ-plane), or G19 (X-axis, YZ-plane). If the arc is circular, it lies in a plane parallel to the selected plane.
If a line of RS274/NGC code makes an arc and includes rotational axis motion, the rotational axes turn at a constant rate so that the rotational motion starts and finishes when the XYZ motion starts and finishes. Lines of this sort are hardly ever programmed.
If cutter radius compensation is active, the motion will differ from what is described here. See Section [.].
Two formats are allowed for specifying an arc: Center Format and Radius Format.
3.3.1 Center format arcs (preferred format)
In the center format, the coordinates of the end point of the arc in the selected plane are specified along with the offsets of the center of the arc from the current location. In this format, it is OK if the end point of the arc is the same as the current point. It is an error if:
- When the arc is projected on the selected plane, the distance from the current point to the center differs from the distance from the end point to the center by more than 0.0002 inch (if inches are being used) or 0.002 millimeter (if millimeters are being used).
When the XY-plane is selected, program G2 axes I- J- (or use G3 instead of G2). The axis words are all optional except that at least one of X and Y must be used. I and J are the offsets from the current location (in the X and Y directions, respectively) of the center of the circle. I and J are optional except that at least one of the two must be used. If only one is specified, the value of the other is taken as 0. It is an error if:
- X and Y are both omitted
- or I and J are both omitted.
When the XZ-plane is selected, program G2 axes I- K- (or use G3 instead of G2). The axis words are all optional except that at least one of X and Z must be used. I and K are the offsets from the current location (in the X and Z directions, respectively) of the center of the circle. I and K are optional except that at least one of the two must be used. If only one is specified, the value of the other is taken as 0. It is an error if:
- X and Z are both omitted,
- or I and K are both omitted.
When the YZ-plane is selected, program G2 axes J- K- (or use G3 instead of G2). The axis words are all optional except that at least one of Y and Z must be used. J and K are the offsets from the current location (in the Y and Z directions, respectively) of the center of the circle. J and K are optional except that at least one of the two must be used. If only one is specified, the value of the other is taken as 0. It is an error if:
- Y and Z are both omitted
- or J and K are both omitted.
Here is an example of a center format command to mill an arc: G17 G2 x10 y16 i3 j4 z9.
That means to make a clockwise (as viewed from the positive z-axis) circular or helical arc whose axis is parallel to the Z-axis, ending where X=10, Y=16, and Z=9, with its center offset in the X direction by 3 units from the current X location and offset in the Y direction by 4 units from the current Y location. If the current location has X=7, Y=7 at the outset, the center will be at X=10, Y=11. If the starting value of Z is 9, this is a circular arc; otherwise it is a helical arc. The radius of this arc would be 5.
In the center format, the radius of the arc is not specified, but it may be found easily as the distance from the center of the circle to either the current point or the end point of the arc.
3.3.2 Radius format arcs (discouraged format)
In the radius format, the coordinates of the end point of the arc in the selected plane are specified along with the radius of the arc. Program G2 axes R- (or use G3 instead of G2). R is the radius. The axis words are all optional except that at least one of the two words for the axes in the selected plane must be used. The R number is the radius. A positive radius indicates that the arc turns through less than 180 degrees, while a negative radius indicates a turn of more than 180 degrees. If the arc is helical, the value of the end point of the arc on the coordinate axis parallel to the axis of the helix is also specified.
It is an error if:
- both of the axis words for the axes of the selected plane are omitted
- the end point of the arc is the same as the current point.
It is not good practice to program radius format arcs that are nearly full circles or nearly semicircles because a small change in the location of the end point will produce a much larger change in the location of the center of the circle (and, hence, the middle of the arc). The magnification effect is large enough that rounding error in a number can produce out-of-tolerance cuts. For instance, a 1% displacement of the endpoint of a 180 degree arc produced a 7% displacement of the point 90 degrees along the arc. Nearly full circles are even worse. Other size arcs (in the range tiny to 165 degrees or 195 to 345 degrees) are OK.
Here is an example of a radius format command to mill an arc: G17 G2 x 10 y 15 r 20 z 5.
That means to make a clockwise (as viewed from the positive Z-axis) circular or helical arc whose axis is parallel to the Z-axis, ending where X=10, Y=15, and Z=5, with a radius of 20. If the starting value of Z is 5, this is an arc of a circle parallel to the XY-plane; otherwise it is a helical arc.
3.4 G4: Dwell
For a dwell, program G4 P- . This will keep the axes unmoving for the period of time in seconds specified by the P number. It is an error if:
- the P number is negative.
3.5 G10: Set Coordinate System Data
The RS274/NGC language view of coordinate systems is described in Section[.].
To set the coordinate values for the origin of a coordinate system, program G10 L2 P- axes, where the P number must evaluate to an integer in the range 1 to 9 (corresponding to G54 to G59.3) and all axis words are optional. The coordinates of the origin of the coordinate system specified by the P number are reset to the coordinate values given (in terms of the absolute coordinate system). Only those coordinates for which an axis word is included on the line will be reset.
It is an error if:
- the P number does not evaluate to an integer in the range 1 to 9.
If origin offsets (made by G92 or G92.3) were in effect before G10 is used, they will continue to be in effect afterwards.
The coordinate system whose origin is set by a G10 command may be active or inactive at the time the G10 is executed.
Example: G10 L2 P1 x 3.5 y 17.2 sets the origin of the first coordinate system (the one selected by G54) to a point where X is 3.5 and Y is 17.2 (in absolute coordinates). The Z coordinate of the origin (and the coordinates for any rotational axes) are whatever those coordinates of the origin were before the line was executed.
3.6 G17, G18, G19: Plane Selection
Program G17 to select the XY-plane, G18 to select the XZ-plane, or G19 to select the YZ-plane. The effects of having a plane selected are discussed in Section [.] and Section [.]
3.7 G20, G21: Length Units
Program G20 to use inches for length units. Program G21 to use millimeters.
It is usually a good idea to program either G20 or G21 near the beginning of a program before any motion occurs, and not to use either one anywhere else in the program. It is the responsibility of the user to be sure all numbers are appropriate for use with the current length units.
3.8 G28, G30: Return to Predefined Absolute Position
Two positions are defined (by parameters 5161-5166 for G28 and parameters 5181-5186 for G30). The parameter values are in terms of the absolute coordinate system and the machine's native coordinate system.
G28 and G30 do not use home switches to find the predefined position. They merely command a rapid motion to the position defined by the parameters, assuming that the machine has already been homed.
To return one or more axes to the predefined position by way of the programmed position, program G28 axes (or use G30). The path is made by a traverse move from the current position to the programmed position, followed by a traverse move of the named axes to the predefined position.
To return all axes to the predefined position without an intermediate position, program G28 or G30 without any axis words.
It is an error if :
- Radius compensation is turned on
3.9 G33, G33.1: Spindle-Synchronized Motion
For spindle-synchronized motion in one direction, code G33 X- Y- Z- K- where K gives the distance moved in XYZ for each revolution of the spindle. For instance, if starting at Z=0, G33 Z-1 K.0625 produces a 1 inch motion in Z over 16 revolutions of the spindle. This command might be part of a program to produce a 16TPI thread.
For rigid tapping (spindle synchronized motion with return) code G33.1 X- Y- Z- K- where K- gives the distance moved for each revolution of the spindle. A rigid tapping move consists of the following sequence:
- A move to the specified coordinate, synchronized with the spindle at the given ratio and starting with a spindle index pulse
- When reaching the endpoint, a command to reverse the spindle (e.g., from 300 RPM clockwise to 300RPM counterclockwise)
- Continued synchronized motion beyond the specified end coordinate until the spindle actually stops and reverses
- Continued synchronized motion back to the original coordinate
- When reaching the original coordinate, a command to reverse the spindle a second time (e.g., from 300RPM counterclockwise to 300RPM clockwise)
- Continued synchronized motion beyond the original coordinate until the spindle actually stops and reverses
- An unsynchronized move back to the original coordinate.
All spindle-synchronized motions wait for spindle index, so multiple passes line up. G33 moves end at the programmed endpoint; G33.1 moves end at the original coordinate.
All the axis words are optional, except that at least one must be used.
It is an error if:
- all axis words are omitted.
- the spindle is not turning when this command is executed
- the requested linear motion exceeds machine velocity limits due to the spindle s

