-
Notifications
You must be signed in to change notification settings - Fork 2
fix(filter): Add PT1 smoothing to Kalman gain #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Description: The filtered gyro output from the IMU was observed to be significantly noisier than the output from a standard EmuFlight FC, even with comparable tunes. This resulted in a less effective overall filtering pipeline for HELIOSPRING boards. The root cause was traced to the signal used to adapt the Kalman filter's measurement noise covariance (r). The update_kalman_covariance() function was being fed the raw, unfiltered gyro data. This caused the calculated noise variance to remain high, which in turn kept the Kalman gain high, allowing more noise to pass through the filter. This change corrects the signal flow by moving the update_kalman_covariance() call to after the PTN filter has run. The function is now fed the cleaner, filtered gyro data. This mirrors the more effective implementation in the main EmuFlight firmware, creating a self-reinforcing loop where a cleaner signal leads to a lower Kalman gain and more aggressive noise rejection. This should align the IMU's filtering performance with the expected "floored" noise spectrum seen on other flight controllers. bump version 257
Add temporal smoothing to Kalman filter gain using PT1 low-pass filter. This aligns IMU-F implementation with EmuFlight's proven Kalman design. Changes: - Export pt1Filter_t type and functions from filter.h - Add pt1Filter_t kFilter to kalman_t structure - Initialize PT1 filter with 50 Hz cutoff in init_kalman() - Apply PT1 filter to calculated gain in kalman_process() Benefits: - Smoother filter transitions - Reduced oscillation risk - More predictable filter behavior - Complete alignment with EmuFlight implementation This enhancement builds upon the covariance signal fix (v257) and provides additional stability through gain smoothing. Bump version to 258
📝 WalkthroughWalkthroughPromotes PT1 filter types/functions to the public header, integrates a PT1 filter into the Kalman state for gain smoothing, applies PT1 smoothing to the Kalman gain, changes covariance update to use filtered rate data, and increments firmware version from 256 to 258. Changes
Sequence Diagram(s)sequenceDiagram
participant Raw as Gyro Raw Data
participant PTN as PTN LPF
participant Kalman as Kalman Filter
participant Cov as Covariance Update
participant PT1 as PT1 Smoother
Raw->>PTN: raw gyroRateData
PTN->>Kalman: filteredData.rateData
Kalman->>Kalman: compute raw k
Kalman->>PT1: k
PT1->>Kalman: k_smoothed
Kalman->>Cov: (NEW) update_kalman_covariance(filteredData.rateData)
Cov->>Cov: update measurement noise r
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Pre-merge checks and finishing touches✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)src/filter/filter.c (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
🔇 Additional comments (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/filter/kalman.c (1)
17-20: PT1 filter initialization looks good.The 50 Hz cutoff is appropriately conservative for smoothing the Kalman gain at 32 kHz sampling. The initial state of 0.0f will cause a brief startup transient as the filter ramps up to the actual gain value.
Optionally, you could initialize the filter state to a typical steady-state gain value (e.g., 0.5f) to reduce the startup transient, though the impact is minimal given the slow cutoff frequency.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/filter/filter.c(2 hunks)src/filter/filter.h(2 hunks)src/filter/kalman.c(2 hunks)src/filter/kalman.h(1 hunks)src/version.h(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-25T17:53:38.888Z
Learnt from: nerdCopter
Repo: emuflight/imu-f PR: 19
File: build.sh:90-98
Timestamp: 2025-10-25T17:53:38.888Z
Learning: In the emuflight/imu-f repository, the FIRMWARE_VERSION in src/version.h is typically a 3-digit integer, but can be a 4-digit integer for testing purposes.
Applied to files:
src/version.h
🧬 Code graph analysis (3)
src/filter/kalman.c (1)
src/filter/filter.c (3)
pt1FilterInit(119-123)pt1FilterGain(113-117)pt1FilterApply(125-129)
src/filter/filter.h (1)
src/filter/filter.c (3)
pt1FilterGain(113-117)pt1FilterInit(119-123)pt1FilterApply(125-129)
src/filter/filter.c (1)
src/filter/kalman.c (1)
update_kalman_covariance(35-90)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (7)
src/filter/kalman.c (1)
111-111: LGTM! Kalman gain smoothing correctly applied.The PT1 filter is applied after computing the raw Kalman gain and before using it to update the state estimate. This prevents abrupt changes in filtering strength and aligns with EmuFlight's design.
src/filter/filter.c (2)
26-26: LGTM! Documentation updated correctly.Comment accurately reflects that the PT1 filter type definition has been moved to filter.h.
97-97: Critical fix correctly implemented! This resolves issue #20.The call to
update_kalman_covariancenow receives the fully filtered gyro data (after Kalman and PTN/LPF filtering) instead of raw gyro data. This creates the intended feedback loop where reduced measured noise lowers Kalman gain, enabling stronger filtering and improved noise suppression, aligning IMU-F with EmuFlight's design.Based on PR objectives.
src/version.h (1)
6-6: LGTM! Firmware version appropriately updated to 258.The version bump from 256 to 258 correctly reflects the release incorporating PT1 Kalman gain smoothing and the covariance feedback fix.
Based on learnings.
src/filter/kalman.h (1)
23-23: LGTM! New member added for PT1 gain smoothing.The
kFiltermember of typept1Filter_tappropriately extends thekalman_tstruct to support Kalman gain smoothing. The comment clearly documents its purpose.Note that this change increases the size of the
kalman_tstruct, which may have ABI implications if there are external dependencies or persistent storage of this struct. Verify that existing filter configurations or calibration data are properly handled across the firmware update.src/filter/filter.h (2)
11-15: LGTM! PT1 filter type appropriately made public.The
pt1Filter_ttype definition has been correctly moved fromfilter.cto this header, making it part of the public API. The struct design is clean and appropriate for a first-order low-pass filter.
50-53: LGTM! PT1 filter function declarations correctly added.The three PT1 filter functions are appropriately declared as public API, with implementations verified in
filter.c. The function signatures are consistent and provide a complete interface for PT1 filtering (gain calculation, initialization, and application).
Proof of IMUF 258 Effectiveness vs 257 and 256This comment provides direct evidence supporting the IMUF 258 enhancement, using both code review and real flight data samples (hover and full flight): Code Review:
Mathematical Rationale:
Data Samples:
Control & Tuning:
Conclusion: |
|
CODACY results are FALSE POSITIVES: Static Analysis False Positives ReportDate: November 3, 2025 SummaryAll three warnings reported by the static analyzer are FALSE POSITIVES. The analyzer claims these struct members are unused, but in reality they are actively used and essential to the filter implementations. Issue 1: Claim:
|
src/filter/filter.c
Outdated
| }; | ||
|
|
||
| // PT1 Low Pass filter | ||
| // PT1 Low Pass filter (type now in filter.h) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't leave this AI boilerplate comment in the code.
Resolves #20
UNOFFICIAL / TESTING: IMUF_258.bin.zip
IMUF 258: Kalman Gain Smoothing Enhancement
Summary
This PR implements a recommended enhancement to the Kalman filter in IMU-F by adding PT1 (low-pass) smoothing to the Kalman gain. This aligns IMU-F with the proven EmuFlight design and addresses the secondary finding from issue #20: 'Kalman gain smoothing for more predictable filter behavior.'
Technical Rationale
Validation
Comparison to Previous Versions
Implementation Details
Recommendation
Summary by CodeRabbit
New Features
Improvements
Chores