User Tools

Site Tools


fr:x300:g-code_and_m-code

G-code and M-code protocol for the Cyborg controller


Important note: This manual describes several M and G codes which are relevant for the use of the Cyborg engraving machines.
The controller which we use handles a lot of other M and G codes, useful for other types of machinery, such as 3d-printers, plasma cutters, etc.
The codes which are of no use for an engraver without toolchanger are not described in this manual.

Input: the RS274/NGC Language

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.

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 Numbers

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.

Words

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.

DTool radius compensation number
FFeed rate
GGenral function
HTool length offset index
IX-axis offset for arcs X offset in G87 canned cycle
JY-axis offset for arcs Y offset in G87 canned cycle
KZ-axis offset for arcs Z offset in G87 canned cycle
Lnumber of repetitions in canned cycles key used with G10
Mmiscellaneous function (see Table 3-6)
Nline number
Pdwell time in canned cycles dwell time with G4 key used with G10
Qfeed increment in G83 canned cycle
Rarc radius, clear_z distance in canned cycle
SSpindle speed
Ttool selection
XX-axis of machine
YY-axis of machine
ZZ-axis of machine
AA-axis of machine
BB-axis of machine
CC-axis of machine

Numbers

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.

Comments and Messages

Printable characters and white space inside parentheses is a comment. A left parenthesis always starts a comment. The comment ends at the first right parenthesis found thereafter.
Once a left parenthesis is placed on a line, a matching right parenthesis must appear before the end of the line. Comments may not be nested; it is an error if a left parenthesis is found after the start of a comment and before the end of the comment.
Here’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 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.

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.

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 cancelling motion.
“Non-modal” codes have effect only on the lines on which they occur. For example, G4 (dwell) is non-modal.

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

Modal Groups The modal groups for G codes are:

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.

The modal groups for M codes are:

group 4{M0, M1, M2, M30, M60} stopping
group 5{54, 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 (special case: M7 and M8 may be active at the same time)
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.
In addition to the above modal groups, there is a group for non-modal G codes:
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. 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.

G Codes

G codes of the RS274/NGC language are shown in the table below and described in this Section. The descriptions contain command prototypes, set in bold type. In the command prototypes, three dots (…) stand 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…) 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.

Rapid Linear Motion - G0

For rapid linear motion, program G0 X… Y… Z… A…, 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. If G53 is programmed on the same line, the motion will also differ.

G-codes

G-codemeaning
G0rapid positioning
G1linear interpolation
G2helical interpolation (clockwise)
G3circular/helical interpolation (counterclockwise)
G4dwell
G10coordinate system origin setting
G17XY-plane selection
G18XZ-plane selection
G19YZ-plane selection
G20inch system selection
G21millimeter system selection
G28move to park position 1, setup on variable page
G30move to park position 2, setup on variable page
G33Lathe, motion synchronized to spindle
G38.2straight probe
G40cancel cutter radius compensation
G41start cutter radius compensation left
G42start cutter radius compensation right
G43tool length offset (plus) , tool X offset for lathe
G49cancel tool length offset
G53motion in machine coordinate system
G54use preset work coordinate system 1
G55use preset work coordinate system 2
G56use preset work coordinate system 3
G57use preset work coordinate system 4
G58use preset work coordinate system 5
G59use preset work coordinate system 6
G59.1use preset work coordinate system 7
G59.2use preset work coordinate system 8
G59.3use preset work coordinate system 9
G61set path control mode: exact path
G61.1set path control mode: exact stop
G64set path control mode: continuous
G68XY rotation
G76Lathe, threading
G80cancel motion mode (including any canned cycle)
G81canned cycle: drilling
G82canned cycle: drilling with dwell
G83canned cycle: peck drilling
G84canned cycle: right hand tapping
G85canned cycle: boring, no dwell, feed out
G86canned cycle: boring, spindle stop, rapid out
G87canned cycle: back boring
G88canned cycle: boring, spindle stop, manual out
G89canned cycle: boring, dwell, feed out
G90absolute distance mode
G91incremental distance mode
G92offset coordinate systems and set parameters
G92.1cancel offset coordinate systems and set parameters to zero
G92.2cancel offset coordinate systems but do not reset parameters
G92.3apply parameters to offset coordinate systems
G93inverse time feed rate mode
G94units per minute feed rate mode
G98initial level return in canned cycles
G99R-point level return in canned cycles

Linear Motion at Feed Rate - G1

For linear motion at feed rate (for cutting or not), program G1 X… Y… Z… A…, 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.

Arc at Feed Rate - G2 and G3

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.
Two formats are allowed for specifying an arc. We will call these the center format and the radius format. In both formats the G2 or G3 is optional if it is the current motion mode.

Radius format arc

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 X… Y… Z… A… 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 180 degrees or less, while a negative radius indicates a turn of 180 degrees to 359.999 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 not good practice to program radius format arcs that are nearly full circles or are semicircles (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. Nearly full circles are outrageously bad, semicircles (and nearly so) are only very bad. 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.

Center format arc

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 X… Y… Z… A… 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. It is an error if:
* I and J are both omitted.

When the XZ-plane is selected, program G2 X… Y… Z… A… 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. It is an error if:
* I and K are both omitted.

When the YZ-plane is selected, program G2 X… Y… Z… A… B… C… 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. It is an error if:
* 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

Dwell - G4

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.

Length Units - G20/G21 and G70/G71

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. G70/G71 is added for CAM software compatibility.

Return to Home - G28 and G30

Two home 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, but are in unspecified length units.
To return to home position by way of the programmed position, program G28 X… Y… Z… A… (or use G30). All axis words are optional.
The path is made by a traverse move from the current position to the programmed position, followed by a traverse move to the home position.
If no axis words are programmed, the intermediate point is the current point, so only one move is made. The order of Z depends on its position, if the end position is higher as the current Z position Z will move first, otherwise Z will move last, this is to prevent collision.

Input M codes

M codes of the RS274/NGC language are shown in the following table.

M codeMeaning
M0program stop
M1optional program stop
M2program end
M3turn spindle clockwise
M4turn spindle counterclockwise
M5stop spindle turning
M6tool change
M7mist coolant on
M8flood coolant on
M9mist and flood coolant off
M30progrma end, spindle and coolants off and rewind.
M48enable speed and feed overrides.
M49disable speed and feed overrides.
M90standard head/spindle
Program Stopping and Ending - M0, M1, M2, M30, M60

To halt a running program temporarily, program M0. If a program is stopped by an M0, pressing the cycle start button will restart the program at the following line, so the program will continue.
M30 for next effects:
• Distance mode is set to MODE_ABSOLUTE (like G90).
• Feed rate mode is set to UNITS_PER_MINUTE (like G94).
• Feed and speed overrides are set to ON (like M48).
• The spindle is stopped (like M5).
• Coolants are turned off (like M9).
• Program is re-winded to the first line, ready for next start.

Program M60 instead of M30 if the spindle and coolants should remain ON.

Spindle/Head Control - M3, M4, M5, M90-M97

To start the spindle turning clockwise at the currently programmed speed, program M3.
To start the spindle turning counterclockwise at the currently programmed speed, program M4.
To stop the spindle from turning, program M5.
It is OK to use M3 or M4 if the spindle speed is set to zero. If this is done (or if the speed override switch is enabled and set to zero), the spindle will not start turning.
If, later, the spindle speed is set above zero (or the override switch is turned up), the spindle will start turning. It is OK to use M3 or M4 when the spindle is already turning or to use M5 when the spindle is already stopped.

Note: It is not possible to make the spindle rotate counterclockwise in the Cyborg engravers.

Coolant Control - M7, M8, M9

To turn mist coolant on, program M7. To turn flood coolant on, program M8. To turn all coolant off, program M9. It is always OK to use any of these commands, regardless of what coolant is on or off.

Continue to the table of contents.

fr/x300/g-code_and_m-code.txt · Last modified: 2023/10/20 12:30 by jan.vangelder