Skip to content

Commit 7903997

Browse files
authored
Merge pull request #119 from CedarGroveStudios/main
add a calibration method to the examples folder
2 parents 66146ff + 5e37d7d commit 7903997

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

examples/bno055_calibrator.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
# pylint: disable=too-few-public-methods
29+
class Mode:
30+
CONFIG_MODE = 0x00
31+
ACCONLY_MODE = 0x01
32+
MAGONLY_MODE = 0x02
33+
GYRONLY_MODE = 0x03
34+
ACCMAG_MODE = 0x04
35+
ACCGYRO_MODE = 0x05
36+
MAGGYRO_MODE = 0x06
37+
AMG_MODE = 0x07
38+
IMUPLUS_MODE = 0x08
39+
COMPASS_MODE = 0x09
40+
M4G_MODE = 0x0A
41+
NDOF_FMC_OFF_MODE = 0x0B
42+
NDOF_MODE = 0x0C
43+
44+
45+
# Uncomment these lines for UART interface connection
46+
# uart = board.UART()
47+
# sensor = adafruit_bno055.BNO055_UART(uart)
48+
49+
# Instantiate I2C interface connection
50+
# i2c = board.I2C() # For board.SCL and board.SDA
51+
i2c = board.STEMMA_I2C() # For the built-in STEMMA QT connection
52+
sensor = adafruit_bno055.BNO055_I2C(i2c)
53+
sensor.mode = Mode.NDOF_MODE # Set the sensor to NDOF_MODE
54+
55+
print("Magnetometer: Perform the figure-eight calibration dance.")
56+
while not sensor.calibration_status[3] == 3:
57+
# Calibration Dance Step One: Magnetometer
58+
# Move sensor away from magnetic interference or shields
59+
# Perform the figure-eight until calibrated
60+
print(f"Mag Calib Status: {100 / 3 * sensor.calibration_status[3]:3.0f}%")
61+
time.sleep(1)
62+
print("... CALIBRATED")
63+
time.sleep(1)
64+
65+
print("Accelerometer: Perform the six-step calibration dance.")
66+
while not sensor.calibration_status[2] == 3:
67+
# Calibration Dance Step Two: Accelerometer
68+
# Place sensor board into six stable positions for a few seconds each:
69+
# 1) x-axis right, y-axis up, z-axis away
70+
# 2) x-axis up, y-axis left, z-axis away
71+
# 3) x-axis left, y-axis down, z-axis away
72+
# 4) x-axis down, y-axis right, z-axis away
73+
# 5) x-axis left, y-axis right, z-axis up
74+
# 6) x-axis right, y-axis left, z-axis down
75+
# Repeat the steps until calibrated
76+
print(f"Accel Calib Status: {100 / 3 * sensor.calibration_status[2]:3.0f}%")
77+
time.sleep(1)
78+
print("... CALIBRATED")
79+
time.sleep(1)
80+
81+
print("Gyroscope: Perform the hold-in-place calibration dance.")
82+
while not sensor.calibration_status[1] == 3:
83+
# Calibration Dance Step Three: Gyroscope
84+
# Place sensor in any stable position for a few seconds
85+
# (Accelerometer calibration may also calibrate the gyro)
86+
print(f"Gyro Calib Status: {100 / 3 * sensor.calibration_status[1]:3.0f}%")
87+
time.sleep(1)
88+
print("... CALIBRATED")
89+
time.sleep(1)
90+
91+
print("\nCALIBRATION COMPLETED")
92+
print("Insert these preset offset values into project code:")
93+
print(f" Offsets_Magnetometer: {sensor.offsets_magnetometer}")
94+
print(f" Offsets_Gyroscope: {sensor.offsets_gyroscope}")
95+
print(f" Offsets_Accelerometer: {sensor.offsets_accelerometer}")

0 commit comments

Comments
 (0)