Subtle filtering while keeping fast gyro response #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Asymmetric Filtering
The key insight is that gyro and accelerometer serve different purposes in a balance robot:
So the system uses asymmetric filtering — aggressive spike removal but minimal smoothing on gyro, while applying more smoothing to accelerometer.
Filter 1: Hampel Filter (Spike Removal)
How it minimizes latency:
The math: A value is a "spike" if it deviates from the median by more than
3.5 × 1.4826 × MAD(Median Absolute Deviation). This catches sensor glitches without affecting legitimate fast movements.Filter 2: EMA (Exponential Moving Average)
Formula:
output = α × new_value + (1-α) × previous_outputHow it minimizes latency:
The Asymmetric Strategy
Architecture: Single-Slot Queue
The background thread reads the IMU at the sampling frequency and always overwrites old data:
Why this minimizes latency:
get_data()never waitsVisual Summary
The result: gyro responds instantly to body tilts (critical for not falling over), while accelerometer is smoothed to provide stable drift correction without being affected by high-frequency vibrations.