Advanced G-Code Error Handling & Recovery: Alarms, G-Code Checks, and AI-Based Fault Prediction
CNC machines are only as reliable as their ability to detect, handle, and recover from errors.
With G-code alarm macros, conditional safety logic, and even AI-driven predictive analysis, modern machines can prevent costly crashes and downtime.
This guide covers real G-code alarm examples, macro recovery logic, and future-ready AI fault prediction systems.
📌 1. Why Error Handling Matters
A small G-code mistake can lead to:
- Tool breakage
- Machine crash
- Workpiece scrap
- Hours of downtime
That’s why professional CNC setups include error checking and recovery macros before and during production.
📌 2. Fanuc Alarm System Overview
Fanuc allows custom alarm messages and stops using #3000 and #3006 macro variables.
| Variable | Function |
|---|---|
| #3000 | Alarm — stops program and resets spindle/feed |
| #3006 | Message — displays text but continues (optional pause) |
| #3003 | Program stop (M00 equivalent) |
| #3004 | Dwell or pause time |
📌 3. #3000 — Hard Alarm (Stops Program)
IF [#4120 EQ 0] THEN #3000=1 (NO TOOL LOADED)
If no tool is active, program stops with alarm “NO TOOL LOADED”.
| Parameter | Description |
|---|---|
#4120 | Current tool number |
#3000=1 | Alarm number 1 |
(message) | Operator message |
Always use descriptive alarm messages for clarity.
📌 4. #3006 — Operator Message (Non-Stop)
IF [#500 GT 100] THEN #3006=1 (TOOL WEAR LIMIT REACHED)
Displays a message on-screen without stopping the machine — ideal for monitoring conditions like tool wear.
📌 5. Combined Example — Smart Alarm Logic
IF [#4120 EQ 0] THEN #3000=1 (INSERT TOOL)
IF [#4119 EQ 0] THEN #3006=1 (NO OFFSET SELECTED)
IF [#500 GT 100] THEN #3006=2 (TOOL REPLACEMENT RECOMMENDED)
Checks tool and offset states, issues alarms or messages accordingly.
📌 6. G-Code Syntax Verification Macro
IF [#4121 NE 54] THEN #3006=3 (G54 OFFSET REQUIRED)
IF [#4003 NE 17] THEN #3000=5 (PLANE ERROR)
| System Variable | Description |
|---|---|
| #4003 | Current plane (17=X-Y, 18=Z-X, 19=Y-Z) |
| #4121 | Active work offset (54–59) |
Prevents running code with wrong plane or offset configuration.
📌 7. Safe Start Block (Fanuc & Haas)
O9000 (SAFE START)
G90 G40 G80 G17 G21 G54
IF [#4003 NE 17] THEN #3000=1 (WRONG PLANE)
IF [#4119 EQ 0] THEN #3000=2 (NO OFFSET)
M99
Automatically checks system state before machining begins — can be called at program start.
📌 8. Haas Example — Message Display
#3006=1 (CHECK TOOL OFFSET)
M00
Displays “CHECK TOOL OFFSET” and pauses program until operator presses Cycle Start.
📌 9. Siemens Example — Error Handling Syntax
IF TOOL_ID==0 THEN
STOPRE ("NO TOOL LOADED")
ENDIF
Siemens uses human-readable syntax — stops with message when condition is true.
📌 10. Heidenhain Example — STOPRE and MSG
IF +Q1 EQ +0 STOPRE
MSG("TOOL OFFSET MISSING")
STOPREhalts program;MSG()displays message.
These functions are equivalent to Fanuc’s #3000 and #3006.
📌 11. Error Logging via Macro Variables
#501 = #4119 (Active offset)
#502 = #5003 (Spindle load)
#503 = #3000 (Last alarm)
Stores critical runtime data for post-error analysis — useful in DNC or MES systems.
📌 12. Automatic Recovery Routine Example
O9100 (AUTO RECOVERY)
IF [#3000 NE 0] THEN #3006=5 (RUNNING RECOVERY)
G28 Z0.
G91 G28 X0. Y0.
M30
When called after an alarm, the machine safely retracts and resets all axes.
📌 13. Axis Limit & Overtravel Protection (G22/G23)
G22 X-200. X200.
IF [#5021 LT -200.] THEN #3000=4 (X UNDERTRAVEL)
IF [#5021 GT 200.] THEN #3000=5 (X OVERTRAVEL)
Defines safe movement zone; automatically stops if motion exceeds limits.
📌 14. Tool Load Monitoring & Alarm
IF [#5003 GT 80.] THEN #3006=3 (HIGH LOAD)
IF [#5003 GT 90.] THEN #3000=2 (OVERLOAD SHUTDOWN)
| Variable | Description |
|---|---|
| #5003 | Spindle load (%) |
Creates dynamic alarm system based on live spindle torque data.
📌 15. Automatic Retry System (Loop-Based)
#100 = 0
N10
G81 Z-10. R2. F150
IF [#3000 NE 0] THEN #100 = [#100 + 1]
IF [#100 LT 3] GOTO 10
#3006=4 (RETRY LIMIT REACHED)
Attempts drilling 3 times before giving up — fully automated recovery cycle.
📌 16. Real Application — Tool Break Detection & Halt
G65 P9814 Z-50. F200.
IF [#5063 GT -49.9] THEN #3000=1 (TOOL BROKEN)
Uses probing data to detect broken tool and stop program safely.
📌 17. AI-Based Fault Prediction (2025–2030)
| Technology | Function |
|---|---|
| AI Load Pattern Recognition | Predicts spindle bearing failure |
| Acoustic Signature Analysis | Detects tool wear and chatter |
| Thermal Mapping | Prevents axis drift errors |
| Machine Learning Models | Forecasts alarm patterns |
| Digital Twin Fault Simulation | Tests G-code for collision risks before cutting |
Future CNCs will predict and prevent errors — not just react to them.
📌 18. Integration with MES & Cloud Diagnostics
- Upload alarms via MTConnect / OPC-UA
- Generate maintenance reports with timestamp and error code
- Sync machine logs to central server
- Enable remote predictive diagnostics
📌 19. Preventive Error Handling Best Practices
| Goal | Recommended Approach |
|---|---|
| Avoid crashes | Use G22, limit switches, macro checks |
| Prevent bad code | Validate work offsets before start |
| Detect wear early | Monitor spindle load and vibration |
| Ensure traceability | Log all #3000 alarms and tool changes |
| Reduce downtime | Create structured recovery subprograms |
📌 20. Example — Full Intelligent Error Routine
O9800 (SMART ERROR SYSTEM)
IF [#4120 EQ 0] THEN #3000=1 (NO TOOL)
IF [#5003 GT 85.] THEN #3006=2 (HIGH LOAD WARNING)
IF [#5003 GT 95.] THEN #3000=3 (LOAD OVERLIMIT)
IF [#5023 LT -150.] THEN #3000=4 (Z UNDERTRAVEL)
IF [#3000 GT 0] THEN GOTO 900
GOTO 1000
N900
G91 G28 Z0.
M00
N1000
M99
Monitors multiple safety conditions in real time, retracts if alarm is triggered, and allows manual recovery.
✅ Conclusion
Error handling is the backbone of CNC reliability.
By mastering #3000/#3006 macros, G22/G23 safety checks, and predictive AI diagnostics, you create a machine environment that not only avoids crashes but also anticipates them.
Modern CNCs don’t just execute G-code — they think, detect, and protect.
Leave a comment