Skip to content

Commit 2e9716c

Browse files
authored
Merge pull request #30 from sjirwin/master
Add calibration offsets and radii as properties on BNO055 library
2 parents 154efce + 77a8abe commit 2e9716c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

adafruit_bno055.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
_MODE_REGISTER = const(0x3d)
6363
_PAGE_REGISTER = const(0x07)
6464
_CALIBRATION_REGISTER = const(0x35)
65+
_OFFSET_ACCEL_REGISTER = const(0x55)
66+
_OFFSET_MAGNET_REGISTER = const(0x5b)
67+
_OFFSET_GYRO_REGISTER = const(0x61)
68+
_RADIUS_ACCEL_REGISTER = const(0x67)
69+
_RADIUS_MAGNET_REGISTER = const(0x69)
6570
_TRIGGER_REGISTER = const(0x3f)
6671
_POWER_REGISTER = const(0x3e)
6772
_ID_REGISTER = const(0x00)
@@ -85,6 +90,26 @@ class _ReadOnlyUnaryStruct(UnaryStruct): # pylint: disable=too-few-public-method
8590
def __set__(self, obj, value):
8691
raise NotImplementedError()
8792

93+
class _ModeStruct(Struct): # pylint: disable=too-few-public-methods
94+
def __init__(self, register_address, struct_format, mode):
95+
super().__init__(register_address, struct_format)
96+
self.mode = mode
97+
98+
def __get__(self, obj, objtype=None):
99+
last_mode = obj.mode
100+
obj.mode = self.mode
101+
result = super().__get__(obj, objtype)
102+
obj.mode = last_mode
103+
# single value comes back as a one-element tuple
104+
return result[0] if isinstance(result, tuple) and len(result) == 1 else result
105+
106+
def __set__(self, obj, value):
107+
last_mode = obj.mode
108+
obj.mode = self.mode
109+
# underlying __set__() expects a tuple
110+
set_val = value if isinstance(value, tuple) else (value,)
111+
super().__set__(obj, set_val)
112+
obj.mode = last_mode
88113

89114
class BNO055:
90115
"""
@@ -101,6 +126,18 @@ class BNO055:
101126
_gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
102127

103128

129+
offsets_accelerometer = _ModeStruct(_OFFSET_ACCEL_REGISTER, '<hhh', CONFIG_MODE)
130+
"""Calibration offsets for the accelerometer"""
131+
offsets_magnetometer = _ModeStruct(_OFFSET_MAGNET_REGISTER, '<hhh', CONFIG_MODE)
132+
"""Calibration offsets for the magnetometer"""
133+
offsets_gyroscope = _ModeStruct(_OFFSET_GYRO_REGISTER, '<hhh', CONFIG_MODE)
134+
"""Calibration offsets for the gyroscope"""
135+
136+
radius_accelerometer = _ModeStruct(_RADIUS_ACCEL_REGISTER, '<h', CONFIG_MODE)
137+
"""Radius for accelerometer (cm?)"""
138+
radius_magnetometer = _ModeStruct(_RADIUS_MAGNET_REGISTER, '<h', CONFIG_MODE)
139+
"""Radius for magnetometer (cm?)"""
140+
104141
def __init__(self, i2c, address=0x28):
105142
self.i2c_device = I2CDevice(i2c, address)
106143
self.buffer = bytearray(2)

0 commit comments

Comments
 (0)