G31

Machining center, LatheOne-shot, group 00

Skip function (moves in a straight line until a signal comes in)

Format and notes

G31 X_ Y_ Z_ F_ (lathe: G31 X(U)_ Z(W)_ F_)

Example

G00 G90 G54 X0 Y0 ;
G43 H01 Z10. ;        (probe tool length)
G31 Z-5. F200 ;       (stops on contact. Always write F)
#100 = #5063 ;        (Z where it stopped. #5063 is the Haas number)
G00 Z10. ;

A skeleton example written by Kezuriba for measuring the height of the top surface of a workpiece with a touch probe. The variable number that stores the stopped position differs by model.

Diagram: G31: how the next block moves after the signal stops the axes

Path if no signal comesActual path★ Signal① Next block incrementalG31 Z-60.★Next blockG91 X40. Z-10.② One axis absoluteG31 Z-60.★Next blockG90 X40.③ Both axes absoluteG31 Z-60.★Next blockG90 X40. Z-50.
Example: on the way to Z−60 with G31, the signal comes in at ★, the rest of the move is discarded and the control goes on to the next block. ① With an incremental command, the axes move the commanded amount from where they stopped, so the end point shifts too. ② If only one axis is absolute, only the axis you wrote goes to its commanded position; the axis you did not write (Z here) stays where it stopped. ③ If both axes are absolute, they go to the same point wherever the stop happened (redrawn by Kezuriba based on the Siemens ISO dialect and Mitsubishi Electric explanations).

Differences between controls and machines

The same code can be written or assigned differently depending on the CNC control and the machine builder. Always check your machine’s manual.

SourceMachineItemDetail
Mitsubishi ElectricLatheNotesIf no signal comes in, it moves to the commanded position and ends (no alarm). If F is omitted, the speed in parameter #1174 is used. Feed override is fixed at 100%, and dry run has no effect
HaasMachining centerNotesIf no signal is received, the end point position is recorded and the program continues (no alarm). The stopped position is stored in #5061–#5066 (per axis). Alarm if F is missing. Recommended feed is less than 100 inch/min (about 2540 mm/min)
Siemens (ISO dialect)LatheNotesIf it reaches the end point without a signal, an alarm occurs. The stopped position is in the system variable $AA_IM[axis]
Siemens (native)—FormatMEAS (a signal deletes the rest of the move), MEAW (moves on to the end point without deleting it)
LinuxCNC—CodeThere is no G31. G38.2 (error if no contact), G38.3 (no error), G38.4/G38.5 (moving away). Stopped positions are in #5061–#5069, and success or failure is in #5070

Frequently asked questions about G31

What is G31 (Skip function (moves in a straight line until a signal comes in)) used for and how is it formatted in a CNC program?
G31 commands "Skip function (moves in a straight line until a signal comes in)" on Machining center, Lathe. The standard program format is "G31 X_ Y_ Z_ F_ (lathe: G31 X(U)_ Z(W)_ F_)". Moves in a straight line like G01 until a signal, such as from a touch probe, comes in; when the signal arrives it stops on the spot, discards the rest of the move and goes on to the next line. Used to measure workpiece height and position, and for feeding until contact
Is G31 a modal code or a one-shot (non-modal) code?
G31 is a one-shot (non-modal, Group 00) code. It acts only in the single block where it is written and does not carry over to the next block, so you must write G31 on every line where that action is required.
Does G31 work differently on machining centers (mills) versus CNC lathes?
G31 is primarily used on Machining center, Lathe. Depending on the machine type and the lathe G-code system (System A, B, or C), this code may be unavailable or assigned to a different function, so verify against your machine tool manual.
Are there differences in G31 across FANUC, Mitsubishi, Okuma, Haas, and Siemens controls?
Yes. Control builders such as FANUC, Mitsubishi Electric (M80/M800), Okuma (OSP), Haas, Mazak, Siemens (SINUMERIK ISO dialect), and Heidenhain can differ in argument letters, decimal-point rules, and optional parameters for G31. Review the control comparison table above and verify the exact syntax in your machine’s programming manual before proving out the program.

Sources

The meaning of this code agrees in two or more sources from different publishers. Explanations are written in our own words.

  1. Siemens AG — SINUMERIK 802D sl/840D/840D sl/840Di/840Di sl/810D Programming Manual ISO Turning, 04.2007 Edition cache.industry.siemens.com
  2. Siemens AG — SINUMERIK 802D sl/840D/840D sl/840Di/840Di sl/810D Programming Manual ISO Milling, 04.2007 Edition (6FC5398-7BP10-0BA0) cache.industry.siemens.com
  3. Mitsubishi Electric — MITSUBISHI CNC 700/70 Series Programming Manual (Lathe System) (IB-1500057) mitsubishielectric.com
  4. Haas Automation, Inc. — G31 Feed Until Skip (Group 00) (machining center, per-code explanation page) haascnc.com
  5. Haas Automation, Inc. — Lathe Operator's Manual – 16 Lathe G-Codes (online version) haascnc.com
  6. LinuxCNC project — G-Codes (LinuxCNC Documentation) linuxcnc.org
  7. Siemens AG — SINUMERIK 840D sl/840D/840Di sl/810D Fundamentals, Programming Manual, Edition 08/2005 (6FC5398-1BP10-0BA0) cache.industry.siemens.com