Skip to content

Add calibration offsets and radii as properties on BNO055 library #30

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

Merged
merged 6 commits into from
Jan 22, 2020
Merged
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
37 changes: 37 additions & 0 deletions adafruit_bno055.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
_MODE_REGISTER = const(0x3d)
_PAGE_REGISTER = const(0x07)
_CALIBRATION_REGISTER = const(0x35)
_OFFSET_ACCEL_REGISTER = const(0x55)
_OFFSET_MAGNET_REGISTER = const(0x5b)
_OFFSET_GYRO_REGISTER = const(0x61)
_RADIUS_ACCEL_REGISTER = const(0x67)
_RADIUS_MAGNET_REGISTER = const(0x69)
_TRIGGER_REGISTER = const(0x3f)
_POWER_REGISTER = const(0x3e)
_ID_REGISTER = const(0x00)
Expand All @@ -85,6 +90,26 @@ class _ReadOnlyUnaryStruct(UnaryStruct): # pylint: disable=too-few-public-method
def __set__(self, obj, value):
raise NotImplementedError()

class _ModeStruct(Struct): # pylint: disable=too-few-public-methods
def __init__(self, register_address, struct_format, mode):
super().__init__(register_address, struct_format)
self.mode = mode

def __get__(self, obj, objtype=None):
last_mode = obj.mode
obj.mode = self.mode
result = super().__get__(obj, objtype)
obj.mode = last_mode
# single value comes back as a one-element tuple
return result[0] if isinstance(result, tuple) and len(result) == 1 else result

def __set__(self, obj, value):
last_mode = obj.mode
obj.mode = self.mode
# underlying __set__() expects a tuple
set_val = value if isinstance(value, tuple) else (value,)
super().__set__(obj, set_val)
obj.mode = last_mode

class BNO055:
"""
Expand Down Expand Up @@ -118,6 +143,18 @@ class BNO055:
gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
"""Returns the gravity vector, without acceleration in m/s."""

offsets_accelerometer = _ModeStruct(_OFFSET_ACCEL_REGISTER, '<hhh', CONFIG_MODE)
"""Calibration offsets for the accelerometer"""
offsets_magnetometer = _ModeStruct(_OFFSET_MAGNET_REGISTER, '<hhh', CONFIG_MODE)
"""Calibration offsets for the magnetometer"""
offsets_gyroscope = _ModeStruct(_OFFSET_GYRO_REGISTER, '<hhh', CONFIG_MODE)
"""Calibration offsets for the gyroscope"""

radius_accelerometer = _ModeStruct(_RADIUS_ACCEL_REGISTER, '<h', CONFIG_MODE)
"""Radius for accelerometer (cm?)"""
radius_magnetometer = _ModeStruct(_RADIUS_MAGNET_REGISTER, '<h', CONFIG_MODE)
"""Radius for magnetometer (cm?)"""

def __init__(self, i2c, address=0x28):
self.i2c_device = I2CDevice(i2c, address)
self.buffer = bytearray(2)
Expand Down