adafruit / Adafruit_CircuitPython_BNO055

CircuitPython driver for BNO055 absolute orientation sensor
MIT License
85 stars 51 forks source link

How do I use the BNO055 COMPASS_MODE to determine compass heading? #100

Open KingFuGitHub opened 2 years ago

KingFuGitHub commented 2 years ago

I saw that there is a compass mode for the BNO055 in the https://circuitpython.readthedocs.io/_/downloads/bno055/en/5.1.0/pdf/ documentation. Furthermore, I would love to know how to turn on that mode so that I can use the magnetometer to determine the compass heading direction. For example, print("Heading: " + str(180+math.tan2(sensor.magnetic[1], sensor.magnetic[0])*180/math.pi). Currently, I am using that to determine the heading but it fluctuate a lot and it's not consistent. Thanks in advance.

tekktrik commented 2 years ago

It looks like COMPASS MODE isn't currently set up for the library, but you should be able to manually do it for now this way:


import time
import board
import adafruit_bno055

i2c = board.I2C()
sensor = adafruit_bno055.BNO055_I2C(i2c)

# Set the sensor to mode manually
sensor.mode = 0x09

# Read the registers without help of methods
values = sensor._magnetic

I don't have the sensor so I can't test it, but that shoooouuuld be it. I think this would could be a great addition to the library. The mode would just need to be added and then the magnetic property would to allow returning the results if it is in COMPASS MODE. If you're up for it, you can submit a pull request!