Skip to content

Commit d842e79

Browse files
add a calibration method to the examples folder
1 parent 66146ff commit d842e79

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

examples/bno055_calibrator.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# SPDX-FileCopyrightText: 2023 JG for Cedar Grove Maker Studios
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
`bno055_calibrator.py`
6+
===============================================================================
7+
A CircuitPython module for calibrating the BNo055 9-DoF sensor. After manually
8+
calibrating the sensor, the module produces calibration offset tuples for use
9+
in project code.
10+
11+
* Author(s): JG for Cedar Grove Maker Studios
12+
13+
Implementation Notes
14+
--------------------
15+
**Hardware:**
16+
* Adafruit BNo055 9-DoF sensor
17+
**Software and Dependencies:**
18+
* Driver library for the sensor in the Adafruit CircuitPython Library Bundle
19+
* Adafruit CircuitPython firmware for the supported boards:
20+
https://circuitpython.org/downloads
21+
"""
22+
23+
import time
24+
import board
25+
import adafruit_bno055
26+
27+
28+
class Mode:
29+
CONFIG_MODE = 0x00
30+
ACCONLY_MODE = 0x01
31+
MAGONLY_MODE = 0x02
32+
GYRONLY_MODE = 0x03
33+
ACCMAG_MODE = 0x04
34+
ACCGYRO_MODE = 0x05
35+
MAGGYRO_MODE = 0x06
36+
AMG_MODE = 0x07
37+
IMUPLUS_MODE = 0x08
38+
COMPASS_MODE = 0x09
39+
M4G_MODE = 0x0A
40+
NDOF_FMC_OFF_MODE = 0x0B
41+
NDOF_MODE = 0x0C
42+
43+
44+
# Uncomment these lines for UART interface connection
45+
# uart = board.UART()
46+
# sensor = adafruit_bno055.BNO055_UART(uart)
47+
48+
# Instantiate I2C interface connection
49+
# i2c = board.I2C() # For board.SCL and board.SDA
50+
i2c = board.STEMMA_I2C() # For the built-in STEMMA QT connection
51+
sensor = adafruit_bno055.BNO055_I2C(i2c)
52+
sensor.mode = Mode.NDOF_MODE # Set the sensor to NDOF_MODE
53+
54+
print("Magnetometer: Perform the figure-eight calibration dance.")
55+
while not sensor.calibration_status[3] == 3:
56+
# Calibration Dance Step One: Magnetometer
57+
# Move sensor away from magnetic interference or shields
58+
# Perform the figure-eight until calibrated
59+
print(f"Mag Calib Status: {100 / 3 * sensor.calibration_status[3]:3.0f}%")
60+
time.sleep(1)
61+
print("... CALIBRATED")
62+
time.sleep(1)
63+
64+
print("Accelerometer: Perform the six-step calibration dance.")
65+
while not sensor.calibration_status[2] == 3:
66+
# Calibration Dance Step Two: Accelerometer
67+
# Place sensor board into six stable positions for a few seconds each:
68+
# 1) x-axis right, y-axis up, z-axis away
69+
# 2) x-axis up, y-axis left, z-axis away
70+
# 3) x-axis left, y-axis down, z-axis away
71+
# 4) x-axis down, y-axis right, z-axis away
72+
# 5) x-axis left, y-axis right, z-axis up
73+
# 6) x-axis right, y-axis left, z-axis down
74+
# Repeat the steps until calibrated
75+
print(f"Accel Calib Status: {100 / 3 * sensor.calibration_status[2]:3.0f}%")
76+
time.sleep(1)
77+
print("... CALIBRATED")
78+
time.sleep(1)
79+
80+
print("Gyroscope: Perform the hold-in-place calibration dance.")
81+
while not sensor.calibration_status[1] == 3:
82+
# Calibration Dance Step Three: Gyroscope
83+
# Place sensor in any stable position for a few seconds
84+
# (Accelerometer calibration may also calibrate the gyro)
85+
print(f"Gyro Calib Status: {100 / 3 * sensor.calibration_status[1]:3.0f}%")
86+
time.sleep(1)
87+
print("... CALIBRATED")
88+
time.sleep(1)
89+
90+
print("\nCALIBRATION COMPLETED")
91+
print("Insert these preset offset values into project code:")
92+
print(f" Offsets_Magnetometer: {sensor.offsets_magnetometer}")
93+
print(f" Offsets_Gyroscope: {sensor.offsets_gyroscope}")
94+
print(f" Offsets_Accelerometer: {sensor.offsets_accelerometer}")

0 commit comments

Comments
 (0)