Skip to content

Commit 3a88c78

Browse files
authored
Merge pull request #1 from sjirwin/master
expose calibration and radii as r/w properties on BNO055
2 parents 0afc334 + a85d9c1 commit 3a88c78

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

adafruit_bno055.py

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ class _ReadOnlyUnaryStruct(UnaryStruct): # pylint: disable=too-few-public-method
9090
def __set__(self, obj, value):
9191
raise NotImplementedError()
9292

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+
return result
104+
105+
def __set__(self, obj, value):
106+
last_mode = obj.mode
107+
obj.mode = self.mode
108+
super().__set__(obj, value)
109+
obj.mode = last_mode
110+
time.sleep(0.01)
93111

94112
class BNO055:
95113
"""
@@ -123,6 +141,18 @@ class BNO055:
123141
gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
124142
"""Returns the gravity vector, without acceleration in m/s."""
125143

144+
offsets_accelerometer = _ModeStruct(_OFFSET_ACCEL_REGISTER, '<hhh', CONFIG_MODE)
145+
"""Calibration offsets for the accelerometer"""
146+
offsets_magnetometer = _ModeStruct(_OFFSET_MAGNET_REGISTER, '<hhh', CONFIG_MODE)
147+
"""Calibration offsets for the magnetometer"""
148+
offsets_gyroscope = _ModeStruct(_OFFSET_GYRO_REGISTER, '<hhh', CONFIG_MODE)
149+
"""Calibration offsets for the gyroscope"""
150+
151+
radius_accelerometer = _ModeStruct(_RADIUS_ACCEL_REGISTER, '<h', CONFIG_MODE)
152+
"""Radius for accelerometer (cm?)"""
153+
radius_magnetometer = _ModeStruct(_RADIUS_MAGNET_REGISTER, '<h', CONFIG_MODE)
154+
"""Radius for magnetometer (cm?)"""
155+
126156
def __init__(self, i2c, address=0x28):
127157
self.i2c_device = I2CDevice(i2c, address)
128158
self.buffer = bytearray(2)
@@ -218,57 +248,6 @@ def calibrated(self):
218248
sys, gyro, accel, mag = self.calibration_status
219249
return sys == gyro == accel == mag == 0x03
220250

221-
_offsets_accelerometer = Struct(_OFFSET_ACCEL_REGISTER, '<hhh')
222-
_offsets_magnetometer = Struct(_OFFSET_MAGNET_REGISTER, '<hhh')
223-
_offsets_gyroscope = Struct(_OFFSET_GYRO_REGISTER, '<hhh')
224-
_radius_accelerometer = Struct(_RADIUS_ACCEL_REGISTER, '<h')
225-
_radius_magnetometer = Struct(_RADIUS_MAGNET_REGISTER, '<h')
226-
227-
@property
228-
def offsets_accelerometer(self):
229-
"""Returns a 3-value Struct with the (x, y, z) accelerometer calibration offsets"""
230-
last_mode = self.mode
231-
self.mode = CONFIG_MODE
232-
vector = self._offsets_accelerometer
233-
self.mode = last_mode
234-
return vector
235-
236-
@property
237-
def offsets_magnetometer(self):
238-
"""Returns a 3-value Struct with the (x, y, z) magnetometer calibration offsets"""
239-
last_mode = self.mode
240-
self.mode = CONFIG_MODE
241-
vector = self._offsets_magnetometer
242-
self.mode = last_mode
243-
return vector
244-
245-
@property
246-
def offsets_gyroscope(self):
247-
"""Returns a 3-value Struct with the (x, y, z) gyroscope calibration offsets"""
248-
last_mode = self.mode
249-
self.mode = CONFIG_MODE
250-
vector = self._offsets_gyroscope
251-
self.mode = last_mode
252-
return vector
253-
254-
@property
255-
def radius_accelerometer(self):
256-
"""Returns the current accelerometer calibration radius"""
257-
last_mode = self.mode
258-
self.mode = CONFIG_MODE
259-
vector = self._radius_accelerometer
260-
self.mode = last_mode
261-
return vector[0]
262-
263-
@property
264-
def radius_magnetometer(self):
265-
"""Returns the current magnetometer calibration radius"""
266-
last_mode = self.mode
267-
self.mode = CONFIG_MODE
268-
vector = self._radius_magnetometer
269-
self.mode = last_mode
270-
return vector[0]
271-
272251
@mode.setter
273252
def mode(self, new_mode):
274253
self._write_register(_MODE_REGISTER, new_mode)

0 commit comments

Comments
 (0)