Files

80 lines
3.0 KiB
Markdown

# GCode Small-Move Remover
PowerShell script that removes gcode cutting moves where X and Y coordinates differ from the previous kept position by less than or equal to a configurable threshold (default 0.0006). These near-zero moves cause Komo CNC machines to throw `ERROR: CRC Fault`.
## Problem
Komo controllers reject cutting moves where the actual displacement in X and Y is negligibly small. For example:
```
N7633 G1 X23.1756 Y27.9265 Z0.7700 F150.
N7634 G1 X23.1757 Y27.9268 Z0.7700 F150. <-- dX=0.0001, dY=0.0003 (CRC Fault)
N7635 G1 X23.1962 Y27.9797 Z0.7700 F150.
N7636 G1 X23.1963 Y27.9799 Z0.7700 F150. <-- dX=0.0001, dY=0.0002 (CRC Fault)
N7637 G1 X23.2163 Y28.0330 Z0.7700 F150.
```
Lines N7634 and N7636 move less than 0.0006 in both X and Y from the previous position, which Komo sees as invalid and halts with a CRC Fault error.
## Solution
The script parses `.OUT` gcode files and removes cutting move lines (G1, G2, G3) where **both** X and Y differ from the last kept position by less than or equal to the threshold, **and** there is no Z change. This eliminates the CRC Fault triggers while preserving all meaningful toolpath data.
### What gets removed
- G1/G2/G3 lines where dX <= threshold AND dY <= threshold AND Z is unchanged
### What is always kept
- **Z moves** - any line where Z changed is never removed
- **G0 (rapid)** positioning lines
- **G28** (home return) lines
- **G52** (coordinate offset) lines
- **M-codes**, comments, tool changes, and all other non-cutting lines
## Usage
```powershell
# Basic - creates *_cleaned.OUT next to the original
.\Remove-SmallMoves.ps1 -InputFile "C:\path\to\file.OUT"
# Specify output file
.\Remove-SmallMoves.ps1 -InputFile "file.OUT" -OutputFile "file_fixed.OUT"
# Custom threshold
.\Remove-SmallMoves.ps1 -InputFile "file.OUT" -Threshold 0.001
```
## Parameters
| Parameter | Required | Default | Description |
|-------------|----------|---------|-------------|
| `InputFile` | Yes | - | Path to the input gcode file |
| `OutputFile`| No | `*_cleaned.OUT` | Path for the cleaned output file |
| `Threshold` | No | `0.0006`| Max X or Y difference for removal |
## Example Output
```
GCode Small-Move Remover
========================
Threshold: <= 0.0006 in both X and Y
Input: file.OUT (8864 lines)
Output: file_cleaned.OUT (8310 lines)
Removed: 554 lines
Sample removed lines (first 20):
Line 36 removed: X=28.0339 Y=25.8585 Z=0.77 (dX=0.0002, dY=0.0003, dZ=0)
Line 47 removed: X=27.7644 Y=27.1713 Z=0.77 (dX=0, dY=0, dZ=0)
...
```
## How It Works
1. Reads the input file line by line
2. Extracts X, Y, Z coordinates using regex
3. For cutting moves (G1/G2/G3) only:
- If Z changed significantly (> 0.0001), the line is always kept
- If Z is the same, compares X and Y against the last kept position
- If both dX and dY are within threshold, the line is removed
4. Updates the last known X/Y/Z position from kept lines only (removed lines don't update position tracking)