G22 and G23 CNC Code | Stored Stroke Limits

Mastering G22 and G23: Stored Stroke Limits for Safe CNC Machining

In CNC machining, preventing collisions between the tool, workpiece, fixtures, and machine components is paramount. While careful programming and toolpath verification are essential, stored stroke limits (also known as travel limits or soft limits) provide an additional layer of safety. The G22 and G23 G-codes are used to activate and deactivate these pre-defined limits, respectively.

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

  • The concept of stored stroke limits.
  • The function of G22 (activate) and G23 (deactivate).
  • How to set the limit values (usually through parameters, not directly in the G-code).
  • Applications on lathes and mills.
  • Interaction with other G-codes.
  • Control-specific considerations (Fanuc, Siemens, Haas, Mazak, Mitsubishi).
  • Troubleshooting.
  • The role of CAM software.

Crucial Note: Always consult the programming and parameter manuals for your specific CNC control system and machine tool. The details of how stroke limits are set and managed can vary significantly between manufacturers and machine models.

1. What are Stored Stroke Limits?

Stored stroke limits define a “safe zone” or “working envelope” for each axis of your CNC machine. They specify the maximum and minimum allowable positions (in machine coordinates) that the tool can reach. If a programmed move would cause the tool to exceed these limits, the control system will:

  • Generate an alarm (typically an “overtravel” alarm).
  • Stop the machine’s movement (preventing a potential crash).

Key Points:

  • Machine Coordinates: Stroke limits are defined in the machine coordinate system, not the work coordinate system (G54, etc.). This means they are relative to the machine’s home position (machine zero), not the part zero.
  • Software Limits: G22/G23 control software limits. These are in addition to any hardware limit switches that might be installed on your machine. The software limits are usually set to be slightly smaller than the physical travel limits of the machine.
  • Multiple Limit Sets: Many controls allow for multiple sets of stored stroke limits (e.g., “stroke limit 1,” “stroke limit 2”). This can be useful for different setups or for defining different safe zones for different parts of a program.
  • Parameters: The values for the stroke limits (the actual maximum and minimum positions) are typically set in machine parameters, not directly within the G22 G-code block itself. G22 simply activates the check against those pre-set values.

2. G22 and G23: Activation and Deactivation

  • G22: Activates the stored stroke limit check. After a G22 command, the control system will continuously monitor the tool’s position and prevent any movement that would exceed the defined limits.
  • G23: Deactivates the stored stroke limit check. After a G23 command, the machine can move freely (within the limits of its physical travel and any hardware limit switches).

Modality: G22 and G23 are modal G-codes. Once G22 is activated, the stroke limit check remains active until G23 is programmed.

It is extremely important to use G23 to deactivate the stroke limit check when it’s no longer needed. Leaving G22 active can restrict machine movement in unexpected ways and make it difficult to perform operations like referencing the machine (homing).

3. Syntax (Fanuc and Common Variations)

The syntax for G22 and G23 is typically very simple:

G22 ; Activate stored stroke limit check
G23 ; Deactivate stored stroke limit check

Important: On most modern controls, G22 and G23 do not take any parameters directly in the G-code block. The limit values themselves are set separately, usually in machine parameters.

4. Setting Stroke Limit Values (Parameters - Control-Specific)

This is where you must consult your machine’s documentation. The procedure for setting stroke limit values varies significantly between control systems and machine tool builders. However, here’s a general overview and an example for Fanuc:

  • General Procedure:
    1. Put the machine in a mode that allows parameter changes (often MDI or a dedicated parameter setting mode).
    2. Navigate to the appropriate parameter settings screen.
    3. Locate the parameters that define the stroke limits for each axis.
    4. Enter the desired limit values (in machine coordinates).
    5. Save the changes.
  • Fanuc (Example - Consult Your Manual!): On many Fanuc controls, the following parameters are commonly used for stroke limit 1 (the outer limits, usually set by the machine tool builder):
    • Parameter 1320: Positive (+) stroke limit for the X-axis (machine coordinates).
    • Parameter 1321: Negative (-) stroke limit for the X-axis (machine coordinates).
    • Parameter 1320: Positive (+) stroke limit for the Y-axis (machine coordinates).
    • Parameter 1321: Negative (-) stroke limit for the Y-axis (machine coordinates).
      • The above two parameters will be different numbers for the Y-Axis.
    • Parameter 1320: Positive (+) stroke limit for the Z-axis (machine coordinates).
    • Parameter 1321: Negative (-) stroke limit for the Z-axis (machine coordinates).
      • The above two parameters will be different numbers for the Z-Axis. There will be similar parameters for other axes (A, B, C) if your machine has them. There may also be parameters for additional sets of stroke limits (stroke limit 2, stroke limit 3, etc.). Example (Conceptual - Do Not Use Without Verification): Let’s say you have a Fanuc mill, and you want to set the following stroke limits (stroke limit 1):
    • X-axis: +500.0 mm to -100.0 mm
    • Y-axis: +300.0 mm to -200.0 mm
    • Z-axis: +200.0 mm to -50.0 mm You would (after putting the machine in the correct mode and navigating to the parameter settings):
    1. Find parameter 1320 for the X-axis and set it to 500.0.
    2. Find parameter 1321 for the X-axis and set it to -100.0.
    3. Find parameter 1320 for the Y-axis and set it to 300.0.
    4. Find parameter 1321 for the Y-axis and set it to -200.0.
    5. Find parameter 1320 for the Z-axis and set it to 200.0.
    6. Find parameter 1321 for the Z-axis and set it to -50.0.

Again, these are example parameter numbers. Your machine’s parameters will likely be different. Consult your machine’s parameter manual.

  • Siemens: Siemens controls use a different system of parameters and machine data to define stroke limits. Consult your Siemens documentation (e.g., the “SINUMERIK 840D sl/828D Basic Functions” manual).
  • Haas: Haas controls have settings for travel limits, typically accessed through the control panel’s settings menus, not through G-codes like G22.
  • Mazak: Will likely use parameters.
  • Mitsubishi: Will likely use parameters.
  • Other Controls: Always consult your documentation.

Warning: Never modify machine parameters without a thorough understanding of their function and the potential consequences. Incorrect parameter settings can lead to serious problems. Always back up your parameters before making any changes. If in doubt, consult a qualified technician.

5. G22/G23 on CNC Lathes

On CNC lathes, G22 activates the stored stroke limit check, and G23 deactivates it. The limits themselves are set in parameters, as described above.

Conceptual Example (Lathe):

; (Assume stroke limits have already been set in parameters)
O0001 (Turning with Stroke Limits)
G21 G18 G40 G99 ; Metric, XZ plane, cancel comp, feed/rev
T0101 ; Select tool
G97 S1000 M03 ; Spindle on
G22 ; Activate stored stroke limit check  <-- IMPORTANT
G00 X50.0 Z5.0 ; Rapid to start
; ... (Turning operations within the defined limits) ...
G23 ; Deactivate stored stroke limit check <-- ESSENTIAL
G28 U0 W0 ; Home
M30 ; Program end
%

6. G22/G23 on CNC Machining Centers (Mills)

The principle is the same on mills: G22 activates, G23 deactivates. The limits are set in parameters.

Conceptual Example (Mill):

; (Assume stroke limits have already been set in parameters)
O0001 (Milling with Stroke Limits)
G21 G17 G40 G49 G80 G90 ; Metric, XY plane, cancel comp, absolute
T01 M06 ; Tool change
G00 G54 X0 Y0 Z10.0 ; Rapid to start
S2000 M03 ; Spindle on
G22 ; Activate stored stroke limit check <-- IMPORTANT
; ... (Milling operations within the defined limits) ...
G23 ; Deactivate stored stroke limit check <-- ESSENTIAL
G28 G91 Z0;
M30 ; Program end
%

7. Interaction with Other G-Codes

  • G92/G50 (Lathes): Be very careful when using G92 (or G50 on older Fanuc lathes) to set the coordinate system. G92/G50 can shift the coordinate system, which can effectively change the stroke limits relative to the workpiece. It’s generally best to use work offsets (G54-G59) instead of G92/G50 for defining the part zero, as work offsets do not affect the stroke limit check (which is always in machine coordinates).
  • G00 (Rapid Traverse): Stroke limits are typically active during rapid moves (G00). This is a crucial safety feature.
  • G28 (Reference Return): The behavior of G28 with respect to stroke limits can vary. On some machines, G28 might temporarily bypass the stroke limits to allow the machine to reach its home position. On other machines, the stroke limits might still be active during G28. Check your machine’s manual.
  • G10: As stroke limits are set by parameters, it may be possible to use G10 to change them.

8. Troubleshooting

  • “Overtravel” Alarm: This means the machine has attempted to move beyond the defined stroke limits. This could be due to:
    • Incorrectly set stroke limit parameters.
    • A programming error (e.g., incorrect coordinates).
    • Incorrect work offset values.
    • G22 being active when it shouldn’t be.
  • Unexpected Machine Stops: The machine might stop before reaching the programmed position if G22 is active and the programmed position is outside the stroke limits.

9. CAM Software

CAM software typically handles stroke limits automatically. The CAM system will be aware of the machine’s travel limits (either through a machine definition file or through user input) and will generate toolpaths that stay within those limits. You usually don’t need to explicitly program G22 or G23 when using CAM. However, it’s essential to ensure that the machine’s actual stroke limit parameters are set correctly, as the CAM system relies on this information.

10. Frequently Asked Questions (FAQ)

  • Q: What’s the difference between G22 and G23?
    • A: G22 activates the stored stroke limit check. G23 deactivates it.
  • Q: Where do I set the actual stroke limit values?
    • A: Usually in machine parameters. Consult your machine’s parameter manual.
  • Q: Are stroke limits defined in machine coordinates or work coordinates?
    • A: Machine coordinates.
  • Q: Should I always use G22?
    • A: Not necessarily. Use G22 when you need to restrict the machine’s movement to a specific “safe zone” within its full travel range. Always use G23 to deactivate the limits when they’re no longer needed.
  • Q: Can I change the stroke limit parameters?
    • A: Yes, but you must consult your machine’s manual and understand the implications. The outer stroke limits (often called “stroke limit 1”) are usually set by the machine tool builder and should not be changed without proper authorization and understanding.

11. Conclusion

The G22 and G23 commands provide a valuable safety feature in CNC machining by allowing you to activate and deactivate pre-set stored stroke limits (travel limits). These limits, defined in machine coordinates through machine parameters, prevent the tool from moving beyond a defined safe zone, reducing the risk of collisions. While the concept is straightforward, the details of setting the limit values are highly machine-specific. Always consult your machine’s and control system’s documentation, and use G23 to deactivate the limits when they are no longer required. CAM software typically handles stroke limits implicitly, but it’s still crucial to ensure the machine parameters are set correctly.