Skip to content

bno080x_heading_example #13

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/bno08x_find_heading
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import time
from math import atan2, sqrt, pi
from board import SCL, SDA
from busio import I2C
from adafruit_bno08x import (
BNO_REPORT_STEP_COUNTER,
BNO_REPORT_ROTATION_VECTOR,
BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR
)
from adafruit_bno08x.i2c import BNO08X_I2C

i2c = I2C(SCL, SDA, frequency=800000)
bno = BNO08X_I2C(i2c)
bno.enable_feature(BNO_REPORT_STEP_COUNTER)
bno.enable_feature(BNO_REPORT_ROTATION_VECTOR)
bno.enable_feature(BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR)

# quat_real, quat_i, quat_j, quat_k


def find_heading(dqw, dqx, dqy, dqz):
norm = sqrt(dqw*dqw + dqx*dqx + dqy*dqy + dqz*dqz)
dqw = dqw/norm
dqx = dqx/norm
dqy = dqy/norm
dqz = dqz/norm

ysqr = dqy * dqy

t3 = +2.0 * (dqw * dqz + dqx * dqy)
t4 = +1.0 - 2.0 * (ysqr + dqz * dqz)
yaw_raw = atan2(t3, t4)
yaw = yaw_raw * 180.0 / pi
if yaw > 0:
yaw = 360 - yaw
else:
yaw = abs(yaw)
return yaw # heading in 360 clockwise


while True:
quat_i, quat_j, quat_k, quat_real = bno.quaternion
heading = find_heading(quat_real, quat_i, quat_j, quat_k)
print("Heading using rotation vector:", heading)

# the geomagnetic sensor is unstable
# Heading is calculated using geomagnetic vector
geo_quat_i, geo_quat_j, geo_quat_k, geo_quat_real = bno.geomagnetic_quaternion
heading_geo = find_heading(
geo_quat_real, geo_quat_i, geo_quat_j, geo_quat_k)
print("Heading using geomagnetic rotation vector:", heading_geo)
print("")
time.sleep(0.1)