Iloke Alusala
Published © MIT

Set Up a Kalman Filter on a BNO055 With a Raspberry Pi 5

Watch a raw gyroscope drift while a Kalman filter stays level, live on a BNO055 and a Raspberry Pi 5 🧭

IntermediateFull instructions provided2 hours125
Set Up a Kalman Filter on a BNO055 With a Raspberry Pi 5

Things used in this project

Hardware components

Raspberry Pi 5
Raspberry Pi 5
×1
BNO055 Shuttle Board 3.0 (Bosch Sensortec)
×1

Story

Read more

Code

bno055_bringup.py

Python
#!/usr/bin/env python3
"""BNO055 bring-up test for Raspberry Pi 5 (I2C bus 1, addr 0x28)."""
import time
from smbus2 import SMBus

ADDR = 0x28
REG_CHIP_ID = 0x00      # expect 0xA0
REG_CALIB_STAT = 0x35
REG_OPR_MODE = 0x3D
REG_EULER_H = 0x1A      # 6 bytes: heading, roll, pitch (1 deg = 16 LSB)
MODE_CONFIG = 0x00
MODE_NDOF = 0x0C

with SMBus(1) as bus:
    # Identity check
    chip_id = bus.read_byte_data(ADDR, REG_CHIP_ID)
    print(f"CHIP_ID: 0x{chip_id:02X} (want 0xA0)")
    if chip_id != 0xA0:
        raise SystemExit("Not a BNO055, check PS0/PS1 grounding and address.")

    # Enter NDOF fusion mode
    bus.write_byte_data(ADDR, REG_OPR_MODE, MODE_CONFIG)
    time.sleep(0.025)
    bus.write_byte_data(ADDR, REG_OPR_MODE, MODE_NDOF)
    time.sleep(0.02)
    print("NDOF mode active. Streaming Euler angles (Ctrl+C to stop)...")

    def s16(lo, hi):
        v = lo | (hi << 8)
        return v - 65536 if v & 0x8000 else v

    while True:
        d = bus.read_i2c_block_data(ADDR, REG_EULER_H, 6)
        h = s16(d[0], d[1]) / 16.0
        r = s16(d[2], d[3]) / 16.0
        p = s16(d[4], d[5]) / 16.0
        cal = bus.read_byte_data(ADDR, REG_CALIB_STAT)
        print(f"H: {h:7.2f}  R: {r:7.2f}  P: {p:7.2f} | "
              f"cal S{(cal >> 6) & 3} G{(cal >> 4) & 3} "
              f"A{(cal >> 2) & 3} M{cal & 3}")
        time.sleep(0.1)

Github

Credits

Iloke Alusala
1 project • 0 followers

Comments