G-Code

This information HAS errors and is made available WITHOUT ANY WARRANTY OF ANY KIND and without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. It is not permissible to be read by anyone who has ever met a lawyer or attorney. Use is confined to Engineers with more than 370 course hours of engineering.




G-codes - and M codes, S codes

Archaic Terminology notes

Block is what computer programmers would call a line today.

Modality of G-codes

Most G-codes are Modal  or within a group only one of a group of G-codes can be active.   As an example - only one of the motion commands can be active. G0 (Rapid move) and G1 (feedrate move) obviously contradict each other.  In Axis under the MDI tab - there is a window that lists which G-codes are active at any time.  There are a few G-codes that are unmodal.   Most programs will start with a preamble to set the default mode that the rest of the program assumes are in play.  LinuxCNC does not have all the G-codes that every company uses (there would be conflicts). There is a way to remap gcodes - but that topic won't be covered here.

Thus the following overview of G-codes is organized by G-code groups.

Units  - Inch vs mm G20 G21

G20 - to use inches for length units.
G21 - to use millimeters for length units.

Plane Selection G17 G18 G19 G17.1 G18.1 G19.1

These codes set the current plane as follows:

    G17 - XY (default)
    G18 - ZX
    G19 - YZ
    G17.1 - UV
    G18.1 - WU
    G19.1 - VW

The UV, WU and VW planes do not support arcs.

The effects of having a plane selected are discussed in section G2 G3 Arcs and section G81 G89.


Coordinate system - one of the user offset modes G54 G55 G56 G57 G58 G59 G59.1 G59.2 G59.3

When we home the machine, it sets zeros for the axis - this is the Machine coordinate system.

Most of the time, you will work in one of the user offset modes - a new zero is calculated from the machine system minus the offset.

G53 cancels any offsets and causes the move to be in the Machine Coordinate System

    G54 - select coordinate system 1
    G55 - select coordinate system 2
    G56 - select coordinate system 3
    G57 - select coordinate system 4
    G58 - select coordinate system 5
    G59 - select coordinate system 6
    G59.1 - select coordinate system 7
    G59.2 - select coordinate system 8
    G59.3 - select coordinate system 9

The coordinate systems store the axis values and the XY rotation angle around the Z axis in the parameters shown in the following table.

Table 3. Coordinate System Parameters
Select CS X Y Z A B C U V W R

G54

1

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

G55

2

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

G56

3

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

G57

4

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

G58

5

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

G59

6

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

G59.1

7

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

G59.2

8

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

G59.3

9

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390


Coordinate System Offset G92 G92.1 -G92.3 G52

Caution
 As a temporary offset, set and unset within the localized scope of a part program, in other G-code interpreters G52 does not persist after machine reset, M02 or M30. In LinuxCNC, G52 shares parameters with G92, which, for historical reasons, does persist these parameters. See G92 Persistence Cautions below.
Caution
 G52 and G92 share the same offset registers. Therefore, setting G52 will override any earlier G92 setting, and G52 will persist across machine reset when G92 persistence is enabled. These interactions may result in unexpected offsets. See G92 and G52 Interaction Cautions below.
G92 sets G92 sets the current point(location) to have the coordinates specified (without motion),

G92 X5.0 -- the current(displayed) location of the X axis becomes 5

G90 - absolute mode

G92 Coordinate System Offset

G92 axes Warning Only use G92 after your machine has been positioned to the desired point.

G92 makes the current point have the coordinates you want (without motion), where the axis words contain the axis numbers you want. All axis words are optional, except that at least one must be used. If an axis word is not used for a given axis, the offset for that axis will be zero.

When G92 is executed, the origins of all coordinate systems move. They move such that the value of the current controlled point, in the currently active coordinate system, becomes the specified value. All of the coordinate system's origins (G53-G59.3) are offset this same distance.

G92 uses the values stored in parameters 5211-5219 as the X Y Z A B C U V W offset values for each axis. The parameter values are absolute machine coordinates in the native machine units as specified in the INI file. All axes defined in the INI file will be offset when G92 is active. If an axis was not entered following the G92, that axis' offset will be zero.

For example, suppose the current point is at X=4 and there is currently no G92 offset active. Then G92 X7 is programmed. This moves all origins -3 in X, which causes the current point to become X=7. This -3 is saved in parameter 5211.

Being in incremental distance mode (G91 instead of G90) has no effect on the action of G92.

G92 offsets may be already be in effect when the G92 is called. If this is the case, the offset is replaced with a new offset that makes the current point become the specified value.

It is an error if all axis words are omitted.

LinuxCNC stores the G92 offsets and reuses them on the next run of a program. To prevent this, one can program a G92.1 (to erase them), or program a G92.2 (to remove them - they are still stored).

Note The G52 command can also be used to change this offset; see the Offsets section for more details about G92 and G52 and how they interact.

See the Coordinate System section for an overview of coordinate systems.

See the Parameters section for more information.

G92.1, G92.2 Reset G92 Offsets

Note G92.1 only clears G92 offsets, to change G53-G59.3 coordinate system offsets in G-code use either G10 L2 or G10 L20.

G92.3 Restore G92 Offsets

You can set axis offsets in one program and use the same offsets in another program. Program G92 in the first program. This will set parameters 5211 to 5219. Do not use G92.1 in the remainder of the first program. The parameter values will be saved when the first program exits and restored when the second one starts up. Use G92.3 near the beginning of the second program. That will restore the offsets saved in the first program.

Absolute vs Relative Modes G90 G91



G90 - absolute distance mode In absolute distance mode, axis numbers (X, Y, Z, A, B, C, U, V, W) usually represent positions in terms of the currently active coordinate system. Any exceptions to that rule are described explicitly in the G80 G89 section.
Example G90 (set absolute distance mode) G0 X2.5 (rapid move to coordinate X2.5 including any offsets in effect) G91 Example G91 (set incremental distance mode) G0 X2.5 (rapid move 2.5 from current position along the X axis) See G0 section for more information.

G91 - incremental distance mode In incremental distance mode, axis numbers usually represent increments from the current coordinate. G90

Arc IJK Relative vs Absolute mode G90.1 G91.1

G90.1 - absolute distance mode for I, J & K offsets. When G90.1 is in effect I and J both must be specified with G2/3 for the XY plane or J and K for the XZ plane or it is an error.

G91.1 - incremental distance mode for I, J & K offsets. G91.1 Returns I, J & K to their default behavior.

Feedrate Mode G93 G94 G95

G94 (most common mode) - is Units per Minute Mode. In units per minute feed mode, an F word is interpreted to mean the controlled point should move at a certain number of inches per minute, millimeters per minute, or degrees per minute, depending upon what length units are being used and which axis or axes are moving.

G93 - is Inverse Time Mode. In inverse time feed rate mode, an F word means the move should be completed in [one divided by the F number] minutes. For example, if the F number is 2.0, the move should be completed in half a minute.

When the inverse time feed rate mode is active, an F word must appear on every line which has a G1, G2, or G3 motion, and an F word on a line that does not have G1, G2, or G3 is ignored. Being in inverse time feed rate mode does not affect G0 (rapid move) motions.

G95 - is Units per Revolution Mode (useful for lathes)  In this mode, an F word is interpreted to mean the controlled point should move a certain number of inches per revolution of the spindle, depending on what length units are being used and which axis or axes are moving. G95 is not suitable for threading, for threading use G33 or G76. G95 requires that spindle.N.speed-in to be connected. The actual spindle to which the feed is synchronised is chosen by the $ parameter.
 It is an error if:

Cutter Radius Offset or Cutter compensation mode G40 G41 G42 G41.1 G42.1

 G40 - turn cutter compensation off. If tool compensation was on the next move must be a linear move and longer than the tool diameter. It is OK to turn compensation off when it is already off.

G40 Example ; current location is X1 after finishing cutter compensated move G40 (turn compensation off) G0 X1.6 (linear move longer than current cutter diameter) M2 (end program)

It creates an error if:

    A G2/G3 arc move is programmed next after a G40.
    The linear move after turning compensation off is less than the tool diameter.

G41 <D-> (left of programmed path or part profile) G41 starts cutter compensation to the left of the programmed line as viewed from the positive end of the axis perpendicular to the plane.
G42 <D-> (right of programmed path or part profile) G42 starts cutter compensation to the right of the programmed line as viewed from the positive end of the axis perpendicular to the plane.
Where D is an optional tool number

The D word is optional; if there is no D word the radius of the currently loaded tool will be used (if no tool is loaded and no D word is given, a radius of 0 will be used).

If supplied, the D word is the tool number to use. This would normally be the number of the tool in the spindle (in which case the D word is redundant and need not be supplied), but it may be any valid tool number.

Note G41/G42 D0 is a little special. Its behavior is different on random tool changer machines and nonrandom tool changer machines On nonrandom tool changer machines, G41/G42 D0 applies the Tool Length Offset of the tool currently in the spindle, or a TLO of 0 if no tool is in the spindle. On random tool changer machines, G41/G42 D0 applies the TLO of the tool T0 defined in the tool table file (or causes an error if T0 is not defined in the tool table).

The lead in move must be at least as long as the tool radius. The lead in move can be a rapid move.

Cutter compensation may be performed if the XY-plane or XZ-plane is active.

User M100-M199 commands are allowed when Cutter Compensation is on.

The behavior of the machining center when cutter compensation is on is described in the Cutter Compensation section along with code examples.

It creates an error if: The D number is not a valid tool number or 0 or
The YZ plane is active or
Cutter compensation is commanded to turn on when it is already on.

G41.1, G42.1 Dynamic Cutter Compensation

G41.1 D- <L-> (left of programmed path)
G42.1 D- <L-> (right of programmed path) D - cutter diameter
Where L - tool orientation (see lathe tool orientation)

G41.1 & G42.1 function the same as G41 & G42 with the added scope of being able to program the tool diameter. The L word defaults to 0 if unspecified.

It creates an error if:

Tool length offset or compensation  G43, G43.1, G49

G43.1 Dynamic Tool Length Offset


G43.1 axes - change subsequent motions by replacing the current offset(s) of axes. G43.1 does not cause any motion. The next time a compensated axis is moved, that axis's endpoint is the compensated location.
Example
G90 (set absolute mode)
T1 M6 G43 (load tool 1 and tool length offsets, Z is at machine 0 and DRO shows Z1.500)
G43.1 Z0.250 (offset current tool offset by 0.250, DRO now shows Z1.250) M2 (end program)

It is an error if:



G43.2 H- axes-  Apply additional Tool Length Offset
Where H is the  tool number

G43.2 - applies an additional simultaneous tool offset.
G43.2 Example
G90 (set absolute mode)
T1 M6 (load tool 1)
G43 (or G43 H1 - replace all tool offsets with T1's offset)
G43.2 H10 (also add in T10's tool offset)
M2 (end program)

You can sum together an arbitrary number of offsets by calling G43.2 more times. There are no built-in assumptions about which numbers are geometry offsets and which are wear offsets, or that you should have only one of each.

Like the other G43 commands, G43.2 does not cause any motion. The next time a compensated axis is moved, that axis's endpoint is the compensated location.

It is an error if:

Cutting mode G61 G61.1 G64


G61  Set Exact path mode
movement will be exactly as programmed. Moves will slow or stop as needed to reach every programmed point. If two sequential moves are exactly co-linear movement will not stop.

G61.1
Set Exact stop mode
Movement will stop at the end of each programmed segment.

G64 <P-> <Q>  Path Blending

   
Where:
           P - motion blending tolerance
           Q - naive cam tolerance

    G64 - Without P means to keep the best speed possible, no matter how far away from the programmed point you end up.

    G64 P-  With A P number -  Blend between best speed and deviation tolerance

    G64 P- <Q- > blending with tolerance. It is a way to fine tune your system for best compromise between speed and accuracy. The P- tolerance means that the actual path will be no more than P- away from the programmed endpoint. The velocity will be reduced if needed to maintain the path.

If you set Q to a non-zero value, it turns on the Naive CAM Detector: when there are a series of linear XYZ feed moves at the same feed rate that are less than Q- away from being collinear, they are collapsed into a single linear move.

On G2/G3 moves in the G17 (XY) plane, when the maximum deviation of an arc from a straight line is less than the G64 P- tolerance,  the arc is broken into two lines (from start of arc to midpoint, and from midpoint to end). those lines are then subject to the naive cam algorithm for lines. Thus, line-arc, arc-arc, and arc-line cases as well as line-line benefit from the Naive CAM Detector.

This improves contouring performance by simplifying the path. It is OK to program for the mode that is already active.

See also the Trajectory Control section for more information on these modes.

If Q is not specified then it will have the same behavior as before and use the value of P-. Set Q to zero to disable the Naive CAM Detector.

G64 P- Example
G64 P0.015 (set path following to be within 0.015 of the actual path)

It is a good idea to include a path control specification in the preamble of each G-code file.


Spindle Control Mode G96 G97


Motion Commands - cutting cycles AND Canned cycles G0 G1 G2 G3 G33 G38.n G73 G76 G80 G81 G82 G83 G84 G85 G86 G87 G88 G89


Canned Cycle Return Level G98 G99

Lathe diameter mode G7 G8

Non-modal codes G4 G10 G28 G30 G52 G53 G92 G92.1 G92.2 G92.3

M-Codes

Spindle M3 M4 M5

Stopping      M0, M1, M2, M30, M60

Coolant (M7 M8 can both be on), M9

Override SwitchesM48, M49

User Defined M100-M199


Coordinates - Systems

Machine Coordinate System

At restart of LinuxCNC - Machine Coordinate System is set to zero for whereever the machine was left.
When you home LinuxCNC you set the Machine Coordinate System to 0 for each axis homed - at their homed position.

Absolute Coordinate


User Coordinates G54-59.3

Touch-off system

Tool Length offset

Gets addded to Z

Heigarchy of offsets









It is OK to program using the same offset already in use. It is also OK to program using no tool length offset if none is currently being used.