G31 CNC Code | Skip Function

Mastering G31: The CNC Skip Function for Probing and Adaptive Machining

The G31 G-code is a powerful tool in CNC programming that enables conditional motion. Unlike standard motion commands (G00, G01, G02, G03) that move the tool to a specified position, G31 commands a feed move that stops when an external signal is received. This “skip function” is primarily used for probing operations (measuring workpieces and setting offsets), but it also has applications in tool setting, tool breakage detection, and adaptive machining.

This article provides a comprehensive guide to understanding and using G31 on both CNC lathes and machining centers (mills). We’ll cover:

  • The fundamental concept of the skip function.
  • The syntax of the G31 command.
  • Applications on lathes.
  • Applications on mills.
  • How to access the skip position data (system variables).
  • Interaction with other G-codes.
  • Control-specific variations (Fanuc, Siemens, Haas, Mazak, Mitsubishi).
  • Troubleshooting.
  • The role of CAM software.
  • Best Practices

Crucial Note: Always consult the programming manuals for your specific CNC control system and machine tool. While G31 is a generally standardized G-code, there might be subtle variations in implementation or additional control-specific features.

1. What is the Skip Function (G31)?

The G31 command is a conditional feed move. It’s like a G01 (linear interpolation) command, but with an important difference:

  1. You program a target position (X, Y, Z, etc.) and a feed rate (F).
  2. The machine starts moving towards that target position at the specified feed rate.
  3. The machine continuously monitors an external signal (usually from a touch probe).
  4. If the signal is triggered before the target position is reached, the motion stops immediately.
  5. If the signal is not triggered, the machine completes the move to the target position.
  6. The machine records the position where the skip occurred (if it occurred). This is crucial for probing applications.

2. G31 Syntax (Fanuc and Common Variations)

The general syntax for G31 on Fanuc and many compatible controls is:

G31 X... Y... Z... F... P... ; (Mill)
G31 X... Z... F... P... ; (Lathe)
  • G31: The skip function command.
  • X..., Y..., Z...: The target coordinates of the move (just like G01). You only specify the axes that are moving.
  • F...: The feed rate for the move.
  • P... (Optional): On some controls (especially Fanuc), the P parameter specifies which skip input signal to use. Many machines have multiple probe inputs (e.g., for different probes or sensors).
    • If P is omitted, it usually defaults to P1 (the primary skip input).
    • Example: G31 X100.0 F50 P2; (Use skip signal #2).
    • Some controls use other methods to select the input.

Important: G31 is a non-modal G-code. It only applies to the block in which it is programmed.

3. Applications on CNC Lathes

On CNC lathes, G31 is used for:

  • Tool Setting/Touch-Off: Finding the exact location of the tool tip relative to the workpiece. This is often done by touching the tool off on a known surface of the workpiece or a dedicated tool setter.
Touch off tool 1 on the face of the workpiece (Z-axis)
G90 ; Absolute mode
G00 X... Z5.0 ; Rapid to a safe position near the face
G31 Z0.0 F50 ; Feed towards the face until the probe triggers
G10 L2 P1 Z#5063 ; Use the value in #5063 to set the tool geometry offset.
G00 Z5.0 ; Rapid retract
  • Workpiece Measurement: Measuring diameters, lengths, or the location of shoulders or other features.
; Measure the diameter of a turned feature
G90 ; Absolute mode
G00 X... Z... ; Rapid to a safe position near the diameter
G31 X50.0 F50 ; Feed towards the diameter until the probe triggers (X is diameter)
G10 L1 P1 R#[5061/2] ; Example of using the value to set tool wear.
G00 X... ; Rapid retract
; ... (Use the value in #5061, which is a diameter, to calculate the actual diameter)
  • Adaptive Machining (Limited): In very specific applications, with advanced macro programming, G31 could be used to adjust cutting parameters based on measured values. However, this is complex and not common.

4. Applications on CNC Machining Centers (Mills)

On machining centers, G31 is most commonly used for probing operations:

  • Workpiece Measurement:
    • Finding edges, corners, and centers of features.
    • Measuring hole diameters and positions.
    • Measuring workpiece height.
    • Measuring flatness, parallelism, and perpendicularity.
  • Setting Work Offsets Automatically: Many modern controls have built-in probing cycles that use G31 internally to automatically set work offsets (G54-G59, etc.) based on probe measurements. You often don’t program the G31directly in these cases; you use a higher-level probing cycle command.
  • Tool Setting: Measuring tool length and diameter (often using a tool setting probe).
  • Tool Breakage Detection: Moving the tool towards a sensor; if the sensor isn’t triggered (because the tool is broken), an alarm is generated.
  • Adaptive Machining: Adjusting toolpaths or cutting parameters based on in-process measurements (requires macro programming and a suitable probing system).

Examples (Mill):

  • Finding an Edge (X+ Edge):
G91 ; Incremental mode
G00 X-10.0 Y0 Z0 ; Position the probe near the edge (assuming X+ is the target)
G31 X20.0 F100 ; Feed in X+ until probe triggers
G90 ; Absolute mode
; ... (The X-coordinate where the probe triggered is stored in a system variable, e.g., #5061 on Fanuc)
; ... (Use this value to calculate the edge location and update the work offset, if desired) ...
  • Finding the Center of a Bore:
This example is going to find the center of a bore, and update the current work offset to that location.
G91 ; Incremental mode
G00 X-20.0 Y0 Z0 ; Position the probe near the edge of the circle.
G31 X40.0 F100 ; Feed in X+ until probe triggers
#101 = #5061; Save X+ position
G00 X-20.0 ; Rapid to opposite side
G31 X-40.0 F100 ; Probe X- direction
#102 = #5061; Save X- position
G90 ; Absolute mode
; ... (The X-coordinate where the probe triggered is stored in a system variable, e.g., #5061 on Fanuc)
; ... (Use this value to calculate the edge location and update the work offset, if desired) ...
G00 X[#101 + #102]/2; Move to the calculated center
G10 L2 P1 X[#101 + #102]/2; Update the current work offset.
  • Tool Breakage Detection (Conceptual):
(Assume a tool breakage sensor is installed)
G00 X... Y... Z... ; Position the tool near the sensor
G31 Z-10.0 F50 ; Move towards the sensor
; ... (Check if the skip signal was triggered.  If *not*, the tool is likely broken) ...
; ... (If broken, execute an error handling routine) ...

5. Accessing the Skip Position (System Variables)

The most important aspect of using G31 for probing is knowing where the machine stopped. This information is stored in system variables. The specific variable numbers vary between control systems, but here are the common ones for Fanuc:

  • #5061: X-axis position where the skip signal occurred.
  • #5062: Y-axis position where the skip signal occurred.
  • #5063: Z-axis position where the skip signal occurred.
  • #5064: A-axis position where the skip signal occurred. (And similar variables for other axes)
  • #5066 C-Axis position.
  • #5069 The number of the input that caused the skip.

Important:

  • These are machine coordinate values (unless a special parameter setting is active – always check your manual).
  • You access these variables within your program (usually in a macro) to perform calculations, make decisions, or update offsets.
  • The values are updated every time a G31 move triggers a skip.

Example (Fanuc - Using System Variables):

Find the Z-height of a surface
G91 ; Incremental mode
G00 Z-10.0 ; Position probe above surface
G31 Z-20.0 F50 ; Probe downwards
#100 = #5063 ; Store the Z-axis skip position in variable #100
G90 ; Absolute mode
G00 Z[#100 + 5.0] ; Retract 5mm above the probed surface
; ...

6. Interaction with Other G-Codes

  • G00/G01: G31 is a feed move, similar in nature to G01. You would not use G31 with G00.
  • G90/G91: The X, Y, Z coordinates in the G31 block are interpreted according to the active mode (absolute or incremental). The skip position stored in the system variables is usually in machine coordinates (but this can be affected by parameter settings – check your manual).
  • G54-G59 (Work Offsets): G31 moves are typically performed relative to the active work offset. The skip position variables will reflect this.
  • G10: You can use G10 to update work offsets based on the results of G31 probing moves (see the bore centering example above).

7. Control-Specific Variations

  • Fanuc: G31 is the standard skip function. The P parameter can select different skip input signals. The system variables #5061-#5069 (and others) store the skip positions.
  • Siemens: Siemens controls use MEAS and MEAW for similar functionality.
    • MEAS: Measurement with deletion of distance-to-go (like G31).
    • MEAW: Measurement without deletion of distance-to-go.
    • Siemens also has extensive built-in measuring cycles.
  • Haas: Haas controls use G31, very similar to Fanuc.
  • Mazak: In Mazatrol, probing is usually handled through dedicated probing cycles and menus. In EIA/ISO mode, G31 is likely used, but consult the documentation.
  • Mitsubishi: Refer to specific machine documentation
  • Other Controls: Always consult your documentation.

8. Troubleshooting

  • Probe Not Triggering:
    • Check probe connections.
    • Verify the probe is enabled (often with an M-code).
    • Check the P parameter (if used) to make sure you’re monitoring the correct skip input.
    • Check for electrical noise.
  • Unexpected Stops:
    • Make sure the G31 move is actually reaching a surface that will trigger the probe.
    • Check for obstructions.
  • Inaccurate Measurements:
    • Probe calibration.
    • Machine backlash.
    • Incorrect feed rate (too fast for accurate probing).

9. CAM Software

CAM software typically handles probing operations through dedicated probing cycles. You define the probe, the features to be measured, and the desired actions (e.g., update work offset), and the CAM system generates the appropriate G-code (often using the control’s built-in probing cycles, which may or may not involve G31 directly). You usually don’t need to program G31 manually when using CAM for probing.

10. Best Practices

  • Use appropriate feed rates for probing. These are typically much slower than cutting feed rates.
  • Test your probing routines thoroughly in a safe environment (single block mode, reduced feed rate override).
  • Understand the system variables used to store the skip positions.
  • Document your probing routines clearly, including the purpose of each move and the expected results.
  • Use a tool setter to set tool lengths

11. Frequently Asked Questions (FAQ)

  • Q: What is the G31 skip function used for?
    • A: Primarily for probing (measuring workpiece features, setting work offsets, tool setting), tool breakage detection, and adaptive machining.
  • Q: Can I use G31 for cutting operations?
    • A: G31 is a feed move, so it could theoretically be used for cutting, but this is extremely unusual. Its primary purpose is to stop on contact.
  • Q: What happens if the probe doesn’t trigger during a G31 move?
    • A: The machine will complete the programmed move to the specified endpoint (just like a G01 move).
  • Q: How do I access the position where the G31 skip occurred?
    • A: Use the control system’s system variables (e.g., #5061-#5069 on many Fanuc controls).
  • Q: Is G31 modal?
    • A: No.

12. Conclusion

The G31 skip function is a powerful tool for CNC programming, enabling precise probing, tool setting, and adaptive machining techniques. By understanding its function, syntax, applications, and interaction with other G-codes, you can significantly enhance the accuracy, efficiency, and automation capabilities of your CNC lathe or machining center. Remember to always consult your machine’s documentation for control-specific details and to use appropriate safety precautions when working with probes and sensors.