Skip to content

Add integrated Axis Twist Compensation for BDsensor on Klipper#246

Draft
Reubertt wants to merge 3 commits intomarkniu:newfrom
Reubertt:new
Draft

Add integrated Axis Twist Compensation for BDsensor on Klipper#246
Reubertt wants to merge 3 commits intomarkniu:newfrom
Reubertt:new

Conversation

@Reubertt
Copy link

Description

This Pull Request introduces integrated Axis Twist Compensation support directly into the BDsensor module for Klipper. This feature allows for more accurate bed leveling by compensating for mechanical twist in the X or Y axes, which is common in many printer kinematics.

Key Changes

  • New Command: Added BDSENSOR_AXIS_TWIST command.
    • Supports scanning X, Y, or BOTH axes.
    • Includes an Auto-Tune feature (enabled by default or by setting DEGREE=0) that automatically selects the best polynomial degree (1-4) to fit the measured twist data, preventing overfitting.
    • Allows forcing a specific polynomial degree via the DEGREE parameter.
    • Configurable probe point count via POINTS parameter (default: 20).
  • Real-time Compensation: Updated scan_sample_event to apply the calculated axis twist compensation in real-time during probe scanning. This uses a cosine interpolation method consistent with Klipper's native axis_twist_compensation module.
  • Math Engine: Implemented necessary mathematical helper functions (polynomial_regression, eval_poly) to handle the curve fitting internally without external heavy dependencies.

How to Use

  1. Home your printer (G28).
  2. Run BDSENSOR_AXIS_TWIST to calibrate both axes automatically.
    • Optional: BDSENSOR_AXIS_TWIST AXIS=X to calibrate only X.
    • Optional: BDSENSOR_AXIS_TWIST DEGREE=2 to force a 2nd-degree polynomial fit.
  3. The system will scan the bed, calculate the optimal compensation profile, and output the results.
  4. Run SAVE_CONFIG to persist the compensation values to your printer.cfg.

Implementation Details

  • The calibration process measures the sensor readings at multiple points along the specified axis while holding the other axis fixed at the center.
  • It calculates the deviation and fits a polynomial curve to model the twist.
  • The "Auto-Tune" logic compares the residual range of different polynomial degrees and selects the one that offers the best improvement, applying a penalty for higher degrees to ensure robustness against sensor noise.

This enhancement significantly improves first-layer consistency on printers with axis twist issues.

@Reubertt
Copy link
Author

I have implemented the BDSENSOR_AXIS_TWIST command with an integrated auto-tune feature for polynomial regression. While this functions correctly on my test setup, tests are still needed to ensure integration with other printers. Reviewers are kindly requested to evaluate the following:

  1. Kinematic Compatibility: Ensure the scanning sequence and manual moves behave correctly on non-Cartesian kinematics (e.g., CoreXY, Delta).
  2. Auto-Tune Robustness: Confirm that the automatic degree selection logic (and its penalty system) consistently identifies the optimal polynomial fit across various bed sizes and sensor noise levels without overfitting.
  3. Real-time Compensation: Verify that the cosine interpolation added to scan_sample_event integrates smoothly with Klipper's existing motion planning and does not introduce performance regressions or conflicts with other compensation modules.

@Reubertt Reubertt changed the title Add Axis Twist Compensation Support for BDsensor on Klipper Add integrated Axis Twist Compensation for BDsensor on Klipper Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant