Skip to content

Disable LSM6DSL/DSO/DS3 on-chip gyro high-pass filter - #12026

Open
sensei-hacker wants to merge 3 commits into
iNavFlight:release/9.1from
sensei-hacker:lsm6dxx-gyro-hpf-settling
Open

sensei-hacker wants to merge 3 commits into
iNavFlight:release/9.1from
sensei-hacker:lsm6dxx-gyro-hpf-settling

Conversation

@sensei-hacker

Copy link
Copy Markdown
Member

Summary

Fixes a reported post-boot attitude drift on LSM6DSL-based boards: roll/pitch start level then drift to a plateau over ~10-20s after power-on.

Root Cause

lsm6dxxConfig() (the legacy config path used by LSM6DSL/LSM6DSO/LSM6DS3, as opposed to the newer "Gen V" LSM6DSV16X/LSM6DSK320X path) enabled the gyro's on-chip high-pass filter at its slowest available cutoff (16mHz). That filter has an RC time constant of ~10s, so the chip's own reported output keeps settling for ~20-30s after every power-on/reset — well past INAV's ~2s startup gyro calibration window (CALIBRATING_GYRO_TIME_MS). The result is a real step in the raw gyro reading after calibration has already locked in a zero offset, which the AHRS reads as attitude drift.

This reproduced identically across three different physical IMU chip swaps on the reporter's board, which pointed away from a hardware defect and toward firmware/driver behavior. No other accgyro driver in the codebase enables an on-chip gyro HPF, and it's redundant since INAV already zeroes gyro bias in software.

Changes

  • src/main/drivers/accgyro/accgyro_lsm6dxx.c: write 0 to CTRL7_G's HPF-enable bits instead of enabling the on-chip HPF, in the legacy (non-Gen-V) chip config path.
  • src/test/unit/accgyro_lsm6dxx_unittest.cc (new): regression test driving the real lsm6dGyroDetect() → gyro.initFn() → lsm6dxxConfig() path against a faked register-map "chip," asserting the on-chip HPF is never enabled, for both LSM6DSL and LSM6DSO chip IDs.
  • src/test/unit/CMakeLists.txt: wire in the new test.

Testing

  • New unit test passes against the fix; verified it fails against the pre-fix code (reverted the one line, reran, confirmed failure on the exact HP_EN_G bit, then restored the fix).
  • Full unit suite: 43/43 passing.
  • Builds clean for SITL and for a real hardware target that enables USE_IMU_LSM6DXX (DAKEFPVF405), no new warnings.
  • Not yet confirmed on real hardware against the original report. This is fundamentally a sensor-timing fix (on-chip filter settling behavior), which unit tests and SITL cannot simulate end-to-end. Requesting the original reporter (or another LSM6DSL/LSM6DSO/LSM6DS3 board owner) re-flash and re-run the same tethered MSP_RAW_IMU logging that originally showed the raw gyro X step at ~10-20s, to confirm the step is gone. Marking "Testing Required" until that comes back.

The legacy LSM6DXX config path enabled the gyro's on-chip HPF at its
slowest (16mHz) cutoff. That filter has a ~10s RC time constant, so its
output keeps settling for ~20-30s after every power-on - well past
INAV's ~2s startup gyro calibration window. The result is a real step
in the raw gyro reading after calibration has already locked in a
zero, which the AHRS reads as several seconds of attitude drift after
boot. INAV already zeroes gyro bias in software, making the on-chip
HPF redundant, and no other accgyro driver in the tree enables one.
@qodo-code-review

Copy link
Copy Markdown
Contributor

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Disable legacy LSM6DXX gyro high-pass filter

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Disables redundant hardware gyro filtering that caused post-calibration attitude drift.
• Covers LSM6DSL and LSM6DSO initialization with register-level regression tests.
• Preserves the newer Gen V sensor configuration path unchanged.
Diagram

sequenceDiagram
    participant Boot as Firmware Boot
    participant Driver as LSM6DXX Driver
    participant IMU as Legacy IMU
    participant Cal as Gyro Calibration
    participant AHRS as AHRS
    Boot->>Driver: Detect and initialize
    Driver->>IMU: Clear CTRL7_G HPF bits
    IMU-->>Cal: Stable raw samples
    Cal-->>AHRS: Software-zeroed samples
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Extend startup calibration
  • ➕ Could wait until the hardware filter fully settles without changing sensor configuration.
  • ➖ Adds roughly 20–30 seconds to startup.
  • ➖ Treats the symptom while retaining redundant hardware bias removal.
  • ➖ Filter settling may vary across resets and devices.
2. Compensate settling in software
  • ➕ Could preserve the hardware HPF while masking its startup transient.
  • ➖ Introduces sensor-specific timing and compensation complexity.
  • ➖ Requires modeling behavior that the existing software calibration already supersedes.
  • ➖ Creates additional AHRS and calibration risk.

Recommendation: Keep the PR's approach. Disabling the redundant on-chip HPF removes the transient at its source, aligns legacy LSM6DXX behavior with other gyro drivers, and avoids extending startup or adding sensor-specific compensation. Hardware confirmation remains appropriate because register tests cannot validate the complete physical settling behavior.

Files changed (3) +200 / -1

Bug fix (1) +7 / -1
accgyro_lsm6dxx.cKeep the legacy LSM6DXX gyro HPF disabled +7/-1

Keep the legacy LSM6DXX gyro HPF disabled

• Writes zero to the CTRL7_G HPF fields for the legacy LSM6DSL, LSM6DSO, and LSM6DS3 configuration path. This prevents hardware filter settling from shifting raw gyro output after startup calibration completes.

src/main/drivers/accgyro/accgyro_lsm6dxx.c

Tests (2) +193 / -0
CMakeLists.txtRegister the LSM6DXX regression test +4/-0

Register the LSM6DXX regression test

• Adds the LSM6DXX driver dependency and USE_IMU_LSM6DXX definition required to compile the new unit test against production driver code.

src/test/unit/CMakeLists.txt

accgyro_lsm6dxx_unittest.ccVerify legacy LSM6DXX initialization leaves HPF disabled +189/-0

Verify legacy LSM6DXX initialization leaves HPF disabled

• Introduces a fake register-map bus and exercises the real detection and initialization path. Tests both LSM6DSL and LSM6DSO IDs, asserting that CTRL7_G never enables the on-chip gyro high-pass filter.

src/test/unit/accgyro_lsm6dxx_unittest.cc

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can choose which labels appear on a finding, and whether they show icons or text

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Test firmware build ready — commit e08ddd6

Download firmware for PR #12026

245 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

RAM / Flash usage vs. base commit b41db7f — commit e08ddd6

Using the nearest available size baseline — the PR's exact base commit has no stored baseline yet.

Target Flash Δ RAM Δ
MATEKF405 ±0 B (±0.00%) CCM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)
MATEKF722 ±0 B (±0.00%) ITCM_RAM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)
TCM: ±0 B (±0.00%)
MATEKF765 ±0 B (±0.00%) DTCM_RAM: ±0 B (±0.00%)
SRAM1: ±0 B (±0.00%)
MATEKH743 ±0 B (±0.00%) D2_RAM: ±0 B (±0.00%)
DTCM_RAM: ±0 B (±0.00%)
ITCM_RAM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)

See RAM/flash optimization guide for techniques to reduce usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant