A suite of Python scripts designed to automatically calculate and apply compensation for mechanical backlash (slop/deadband) in motion control systems, specifically tailored for G-code used with FluidNC or similar controllers. This tool ensures greater precision by eliminating movement inconsistencies caused by gear train or screw wear.
-
Precise Compensation: Calculates and inserts necessary motion offsets (
G0moves) to physically engage the drivetrain and eliminate mechanical play. - Targeted Axes: Supports independent compensation for X and Y axes.
-
Safety Integration: Inserts compensation moves with a defined safe feedrate (
F200default) and includes safety checks to prevent negative coordinates (Soft Limit Alarms). -
Threshold Filtering: Ignores very small movements (below
$0.05$ mm default) to filter out noise and avoid unnecessary compensation moves. -
Variable Backlash Support: The advanced script (
variable_backlash.py) allows for dynamic backlash compensation that changes linearly across the X-axis.
To run this script, you need:
- Python 3.x
- No external libraries are required (uses standard
reandargparse).
-
Clone the repository to your local machine:
git clone [https://github.com/MihaNormalen/Baclash_fix.git](https://github.com/MihaNormalen/Baclash_fix.git) cd Baclash_fix -
Ensure the
backlash_fix.pyscript is executable (optional, but good practice):chmod +x backlash_fix.py
The script backlash_fix.py applies a single, constant backlash value for each axis.
python backlash_fix.py <INPUT_FILE> [OUTPUT_FILE] [--bx <VALUE>] [--by <VALUE>]
python backlash_fix.py raw_cut.gcode fixed_cut.gcode --bx 0.10 --by 0.05
Argument,Description,Default
input,Path to the source G-code file.,N/A
output,"Path for the corrected output G-code file. If omitted, defaults to fixed_<input_file>.",fixed_...
--bx,Backlash magnitude for the X-axis (in mm).,0.5
--by,Backlash magnitude for the Y-axis (in mm).,1.6
โก Advanced Usage: Variable Backlash Compensation (advanced/variable_backlash.py)
The script advanced/variable_backlash.py accounts for backlash magnitude changing linearly across the X-axis travel (e.g., due to wear or non-uniform leadscrew alignment).
Command Line Syntax (Variable Mode)
python advanced/variable_backlash.py <INPUT_FILE> <OUTPUT_FILE> --max_x <MAX> --bx_start <X0> --bx_end <XMAX> --by_start <Y0> --by_end <YMAX>
python advanced/variable_backlash.py raw_cut.gcode fixed_variable.gcode --max_x 300.0 --bx_start 0.05 --bx_end 0.15 --by_start 0.20 --by_end 0.10
Argument,Description,Default
input,Path to the source G-code file.,N/A
output,Path for the corrected output G-code file.,N/A
--max_x,"Maximum physical travel of the X-axis (in mm), used for interpolation.",150.0
--bx_start,Backlash magnitude for X-axis at X=0 (in mm).,0.5
--bx_end,Backlash magnitude for X-axis at X=max_x (in mm).,0.5
--by_start,Backlash magnitude for Y-axis at X=0 (in mm).,3.2
--by_end,Backlash magnitude for Y-axis at X=max_x (in mm).,1.6