Thanks for sharing your CNC G-code subroutine (likely for LinuxCNC or similar), which appears to be a remap of
M6 for tool changes and probing. You mentioned you'd like it
debugged.
Here’s a structured debug review of your code, addressing:
- ✅ Correctness and syntax
- Potential issues / risks
- ❌ Likely bugs or logic errors
- Suggestions / improvements
KEY OBSERVATIONS
✅ 1. Remap structure looks correct
You're using:
Code:
o<m6remap> sub
...
o<m6remap> endsub
M2
Which is the right way to define a remapped M6 subroutine.
✅ 2. Initial parameter setup
You're assigning feed rates and probe parameters depending on
#<_imperial> or
#<_metric>:
Code:
o300 if[#<_imperial>]
...
o310 if[#<_metric>]
These are environment variables defined by LinuxCNC, so this setup looks fine.
❌ 3. Logic: Use of #5403 without checking tool number
You're checking:
Code:
o500 if [ [#<_current_tool> EQ #<_selected_tool>] AND [#5403 NE 0.00000] ]
Issue:
#5403 refers to the length of the
current tool in the tool table.
But
#5403 only updates after a successful
M6 and
G43. Before that, it may still reflect the last active tool or even be zero if no tool has been loaded.
⚠️ You might want to use
#[5330 + #<_selected_tool>] to directly access the length of the selected tool in the tool table.
✅ Suggested fix:
Code:
#<selected_tool_length> = #[5330 + #<_selected_tool>]
o500 if [ [#<_current_tool> EQ #<_selected_tool>] AND [#<selected_tool_length> NE 0.00000] ]
❌ 4. Potential bug in tool length probing logic
In the probing section:
If
#2001 is still 0 (which it is unless
#<_imperial> or
#<_metric> is correctly defined), this command becomes:
Which is a
no-op and could fail or not probe at all.
✅
Fix: Ensure a fallback default:
Code:
o320 if[ #2001 EQ 0 ]
#2001 = 2.5 (or 0.1 depending on machine)
o320 endif
5. Hardcoded probing offsets without tool setter presence check
You assume the tool setter exists at:
Code:
G53 X#<_ini[TOOLSENSOR]X_LOC> Y#<_ini[TOOLSENSOR]Y_LOC>
But there’s
no check if the probe is available or enabled.
✅
Suggestion: Add an optional INI parameter to enable/disable probing:
Code:
#<use_probe> = #<_ini[TOOLSENSOR]ENABLE>
o400 if [#<use_probe> EQ 1]
(run probing routine)
o400 else
(skip probing, maybe throw a warning)
o400 endif
6. G10 L2 logic is unclear
Code:
G10 L2 P#<coordinate_system> Z#[4000 + #<coordinate_system>]
You're trying to
set the Z-offset in the active work coordinate system, but:
#[4000 + #<coordinate_system>] isn't standard; you may be intending to pull from a probe value?
- This line may not have the effect you want.
Check if your intention is to
set the Z-zero to match the probe length + previous zero?
If you're doing dynamic tool length probing, you might instead want:
Code:
G10 L1 P#<_selected_tool> Z[#<workspace_z> - #5063]
But this depends heavily on your coordinate system setup.
❌ 7. Missing oendif inside nested probing logic
Your nested
o600 block ends with:
But if
#5403 NE 0.00000, it runs:
Code:
G10 L2 P#<coordinate_system> Z#[4000 + #<coordinate_system>]
Then it continues into the else path, because you're missing an
o600 endif before the
else.
✅
Fix:
Code:
o600 if[#5403 NE 0.00000]
...
o600 else
...
o600 endif
8. Restore coordinate system
This block is fine but verbose:
Code:
o100 if[#1900 EQ 540]
G54
...
o107 endif
✅
Alternative: Use expression-based G-code if your controller supports it:
But this may not be portable or safe depending on your interpreter.
✅ Summary of Fixes
| Line/Section | Issue | Fix or Suggestion |
|---|
#5403 tool length check | May not be accurate for selected tool | Use #[5330 + #<_selected_tool>] |
| Probing feed/Z distance | #2001 could be 0 | Add fallback default if zero |
Probing logic G38.2 Z0 | Risk of no movement | Guard with a valid probe distance |
G10 L2 use | Possibly misused or unclear intent | Double-check your coordinate setting logic |
Missing o600 endif | Logic bug, causes both if/else to run | Add proper endif |
| Tool setter existence | Assumed always present | Add check via #<_ini> param |
| Coordinate system restore block | Verbose | Optionally simplify with G#<1900> |
✅ Final Notes
If you apply the above fixes, your tool change remap should be far more robust and safer to run.
Would you like me to return a
corrected version of this entire G-code subroutine with fixes applied?