Skip to content

BMI160: apply target IMU alignment to the accelerometer - #12182

Open
sensei-hacker wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
sensei-hacker:fix-bmi160-acc-alignment
Open

sensei-hacker wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
sensei-hacker:fix-bmi160-acc-alignment

Conversation

@sensei-hacker

Copy link
Copy Markdown
Member

Summary

bmi160GyroDetect() takes the gyro alignment from the target's IMU_BMI160_ALIGN (the bus device param). bmi160AccDetect() never set acc->accAlign, so the accelerometer stayed at ALIGN_DEFAULT.

On targets with a rotated BMI160, the gyro and accelerometer end up 90° apart in yaw. All four SYNERDUINO* targets use CW270_DEG. Board alignment can't correct this, because it rotates both sensors together. The board maker reported it: setting the accelerometer orientation right makes the gyro wrong, and the reverse.

This PR sets acc->accAlign = acc->busDev->param;, the same as every other IMU driver (BMI088, BMI270, ICM*, LSM6DXX, MPU*).

The BMI160 accelerometer and gyro share one axis system (datasheet BST-BMI160-DS000, §5.2, Fig. 42), so no per-sensor remap is needed in the driver.

Affected targets

  • Changed: SYNERDUINOH7, SYNERDUINOH7A, SYNERDUINOSTM_F405, SYNERDUINOSTM_F405A (CW270_DEG). After this PR the accelerometer is rotated to match the gyro.
  • Unchanged: RADIX (CW0_DEG).
  • release/9.1 has no rotated BMI160 target, which is why this targets maintenance-10.x.

Testing

  • Unit test: new accgyro_bmi160_unittest. It runs the real detect functions with a CW270 bus device and checks that the gyro and accelerometer both get the target alignment.
    • Before the fix it fails (acc=0, gyro=4). After the fix it passes.
  • Full unit suite: 621/621 pass.
  • Builds: SYNERDUINOSTM_F405 builds with no warnings.
    • SYNERDUINOH7 doesn't build on maintenance-10.x even without this change. The HSE_MHZ 25 setting fails the H7 HSE_VALUE % 1600000 static assert, and the target is SKIP_RELEASES. That's a separate issue.
  • Not hardware-tested. Testing on a Synerduino board would be appreciated. Boards running a local workaround that remaps the accelerometer axes in bmi160AccReadScratchpad() should drop that workaround with this fix. Otherwise the accelerometer is rotated twice.

bmi160GyroDetect() takes the gyro alignment from the bus device param
(the target's IMU_BMI160_ALIGN), but bmi160AccDetect() never set
acc->accAlign, so the accelerometer stayed at ALIGN_DEFAULT. On targets
with a rotated BMI160 (SYNERDUINO* use CW270_DEG) the gyro and
accelerometer ended up 90 degrees apart in yaw, which board alignment
cannot correct because it rotates both sensors together.

Set accAlign from the bus device param, as every other IMU driver does.
The BMI160 accelerometer and gyro share one axis system (datasheet
section 5.2), so no per-sensor remap is needed.

Add a unit test that runs the real detect functions with a CW270 bus
device and checks both sensors get the same alignment.
@sensei-hacker sensei-hacker added this to the 10.0 milestone Oct 25, 2026
@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

Apply target BMI160 alignment to the accelerometer

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Apply target IMU alignment to the BMI160 accelerometer, matching its gyro on rotated boards.
• Add a regression test that checks both sensors receive the same CW270 alignment.
Diagram

graph TD
  Target["Target IMU alignment"] --> Bus["Bus device parameter"] --> Gyro["Gyro detection"] --> Axes["Matched sensor axes"]
  Bus --> Acc["Accelerometer detection"] --> Axes
Loading
High-Level Assessment

Assigning alignment during accelerometer detection matches the existing gyro path and other IMU drivers. Board-level rotation affects both sensors together, while a separate axis remap is unnecessary for the BMI160's shared axis system.

Files changed (3) +87 / -0

Bug fix (1) +1 / -0
accgyro_bmi160.cAssign target alignment during BMI160 accelerometer detection +1/-0

Assign target alignment during BMI160 accelerometer detection

• Sets accAlign from the bus device parameter after successful accelerometer detection, matching the gyro's existing behavior. Rotated BMI160 targets no longer leave the accelerometer at default alignment.

src/main/drivers/accgyro/accgyro_bmi160.c

Other (2) +86 / -0
CMakeLists.txtRegister the BMI160 driver for its unit test +4/-0

Register the BMI160 driver for its unit test

• Links the production BMI160 driver into the new unit test and enables its USE_IMU_BMI160 definition.

src/test/unit/CMakeLists.txt

accgyro_bmi160_unittest.ccTest matching BMI160 gyro and accelerometer alignment +82/-0

Test matching BMI160 gyro and accelerometer alignment

• Uses a fake CW270 bus device to exercise the real gyro and accelerometer detection functions. Asserts that both receive the target alignment and agree with each other.

src/test/unit/accgyro_bmi160_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 enable the Remediation agent and Qodo fixes findings in a dedicated fix PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@sensei-hacker

Copy link
Copy Markdown
Member Author

@synersignart

@github-actions

Copy link
Copy Markdown

RAM / Flash usage vs. base commit bc76329 — commit 2b1017b

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.

@github-actions

Copy link
Copy Markdown

Test firmware build ready — commit 2b1017b

Download firmware for PR #12182

251 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.

@synersignart

synersignart commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

@synersignart

yes , did we kept the BMI 160 as is or did we added the fix ?
I have not seen the synerduino in the hex list

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants