Find Z contact with accelerometer

Idea

Klipper should get a simple function that detects nozzle contact with the bed while lowering Z. The detection uses the accelerometer. Once the noise on a chosen axis changes clearly the move stops and Klipper reports the Z height. This helps to set the probe offset for example with an Eddy probe.

Why this helps

During stepwise lowering the Z axis noise changes clearly when the nozzle touches the bed. You can take this point to get the delta in millimeters and use it for the offset.

What the function should do

  • Start at a height for example 1.00 mm

  • End at a height for example 0.00 mm

  • Step down with a chosen step for example 0.05 mm

  • After every step measure the accelerometer

  • Choose the axis X or Y or Z

  • Threshold for the change for example 20

  • Stop when the threshold is reached

  • Stop when the mechanical probe triggers

  • Print the measured values and the detected contact height to the console

Extra request Delta display and jump detection

  • Show the delta to the baseline for each step

  • Option to mark jumps if the current change is much larger than the recent history

  • Add parameter OUTLIER_FACTOR for example 2.0. If the current delta is larger than OUTLIER_FACTOR times the moving average of the last N steps then print Jump detected

  • Add parameter HISTORY_N for example 5 for the window length

Proposed new command

ACCEL_SWEEP_CONTACT START_Z=<mm> STOP_Z=<mm> STEP=<mm> AXIS=<X|Y|Z> THRESH=<value>
DWELL_MS=<ms> FEED=<mm_min> USE_PROBE=<0|1> BASELINE=<auto|value>
SAMPLES_MS=<ms> AVERAGING=<n> OUTLIER_FACTOR=<float> HISTORY_N=<int>

Brief explanation

  • START_Z and STOP_Z set the range

  • STEP sets the step size

  • AXIS selects the sensor axis

  • THRESH is the threshold for the change

  • DWELL_MS is the pause after each step

  • FEED is the Z speed

  • USE_PROBE also stops on a probe trigger

  • BASELINE is the reference either the first point or a fixed value

  • SAMPLES_MS is the measurement time per step

  • AVERAGING averages several measurements per step

  • OUTLIER_FACTOR controls the jump detection

  • HISTORY_N controls the number of steps for the moving average

Output

  • For each step one line with time Z height noise value delta and an optional Jump mark

  • At the end a summary with baseline chosen axis threshold contact height number of steps and optionally average and standard deviation of the deltas

Safety

  • Never go below STOP_Z

  • Stop immediately if the probe triggers

  • Abort if no sensor is available

  • Only run after homing

Examples from Test with contact

Contact was between 209 and 266

09:45 Axes noise for xy-axis accelerometer: 69.287699 (x), 167.124218 (y), 267.540133 (z)
09:45 probe: open
09:45 Axes noise for xy-axis accelerometer: 80.265442 (x), 161.748106 (y), 266.987957 (z)
09:45 probe: open
09:45 Axes noise for xy-axis accelerometer: 99.552443 (x), 149.589675 (y), 226.684763 (z)
09:45 probe: open
09:45 Axes noise for xy-axis accelerometer: 117.082680 (x), 143.481282 (y), 209.015430 (z)
09:45 probe: open
09:45 Axes noise for xy-axis accelerometer: 122.674991 (x), 137.963436 (y), 204.088180 (z)
09:45 probe: open
09:45 Axes noise for xy-axis accelerometer: 159.315922 (x), 143.087131 (y), 194.847656 (z)
09:44 probe: open
09:44 Axes noise for xy-axis accelerometer: 134.348266 (x), 138.880622 (y), 195.788834 (z)
09:44 probe: open
09:44 Axes noise for xy-axis accelerrometer: 158.296255 (x), 139.710164 (y), 193.174493 (z)

Z noise deltas per step on Z only

Step 1 delta −0.552176 abs 0.552176 threshold 15 not reached
Step 2 delta −40.303194 abs 40.303194 threshold reached
Step 3 delta −17.669333 abs 17.669333 threshold reached
Step 4 delta −4.927250 abs 4.927250 threshold not reached
Step 5 delta −9.240524 abs 9.240524 threshold not reached
Step 6 delta 0.941178 abs 0.941178 threshold not reached
Step 7 delta −2.614341 abs 2.614341 threshold not reached

Result

With this command you can find the true contact point in a repeatable way. The delta display and the automatic jump detection help to spot outliers and to set a clean offset for the Probe.

There is maybe already something like you are requesting:

jniebuhr/adxl345-probe

However based on my own testings with a Delta and CoreXY printer it is not very reliable.
When used on mechanics that is not working smoothly you might receive false triggers.
Otherwise a particular speed and movement distance might be needed to get a proper hit detection.

All that won’t be given for example if you are running multiple printers on the same desk. If one printer is already working, its vibrations can cause false triggers on the other printer that tries to use an accelerometer offset detection.

I think you misunderstood me. I’m not trying to level the bed with this. I want to use it to adjust my probe.

The principle is as follows:
we have vibrations in the toolhead, and when the toolhead or nozzle touches the bed, the vibration pattern (noise ) changes. It becomes weaker or different compared to when the toolhead is just hovering in the air.

So you can actually detect the contact point quite precisely by analyzing the noise. I’ve already included examples of this. And as I said, I don’t want to use it as a bed leveling probe, just to set the correct offset.

1 Like

Interesting concept. I see 3 potential issues:

  1. The accelerometers currently in use are very noisy and nonlinear at small inputs. The way they are uses for resonance measurements (by running through a PSD) masks the nonlinearity as the process (essentially) ignores the magnitude and only looks the frequency.
  2. You are essentially relying on the heatsink fan to “dither” the toolhead. An exceptionally well balanced fan could “break” your system. Adding a “shaker” (hypatic feedback from a game controller) may be necessary.
  3. PSD conversion is a computationally heavy process and may require your process to take considerable time.

But YES, I feel your concept is viable if someone is clever enough to write the code.

1 Like