Skip to content

Commit e43b1a2

Browse files
authored
Merge pull request #38 from dherrada/master
Made members that store sensor data properties. Removed deprecated members. disabled members return none
2 parents d3edbc3 + e5798a3 commit e43b1a2

File tree

2 files changed

+83
-34
lines changed

2 files changed

+83
-34
lines changed

adafruit_bno055.py

+81-32
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,15 @@ class BNO055:
9191
Driver for the BNO055 9DOF IMU sensor.
9292
"""
9393

94-
temperature = _ReadOnlyUnaryStruct(0x34, 'b')
95-
"""Measures the temperature of the chip in degrees Celsius."""
96-
accelerometer = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
97-
"""Gives the raw accelerometer readings, in m/s.
98-
99-
.. warning:: This is deprecated. Use ``acceleration`` instead. It'll work
100-
with other drivers too."""
101-
acceleration = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
102-
"""Gives the raw accelerometer readings, in m/s."""
103-
magnetometer = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
104-
"""Gives the raw magnetometer readings in microteslas.
105-
106-
.. warning:: This is deprecated. Use ``magnetic`` instead. It'll work with
107-
other drivers too."""
108-
magnetic = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
109-
"""Gives the raw magnetometer readings in microteslas."""
110-
gyroscope = _ScaledReadOnlyStruct(0x14, '<hhh', 1/16)
111-
"""Gives the raw gyroscope reading in degrees per second.
112-
113-
.. warning:: This is deprecated. Use ``gyro`` instead. It'll work with
114-
other drivers too."""
115-
gyro = _ScaledReadOnlyStruct(0x14, '<hhh', 0.001090830782496456)
116-
"""Gives the raw gyroscope reading in radians per second."""
117-
euler = _ScaledReadOnlyStruct(0x1a, '<hhh', 1/16)
118-
"""Gives the calculated orientation angles, in degrees."""
119-
quaternion = _ScaledReadOnlyStruct(0x20, '<hhhh', 1/(1<<14))
120-
"""Gives the calculated orientation as a quaternion."""
121-
linear_acceleration = _ScaledReadOnlyStruct(0x28, '<hhh', 1/100)
122-
"""Returns the linear acceleration, without gravity, in m/s."""
123-
gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
124-
"""Returns the gravity vector, without acceleration in m/s."""
94+
_temperature = _ReadOnlyUnaryStruct(0x34, 'b')
95+
_acceleration = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
96+
_magnetic = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
97+
_gyro = _ScaledReadOnlyStruct(0x14, '<hhh', 0.001090830782496456)
98+
_euler = _ScaledReadOnlyStruct(0x1a, '<hhh', 1/16)
99+
_quaternion = _ScaledReadOnlyStruct(0x20, '<hhhh', 1/(1<<14))
100+
_linear_acceleration = _ScaledReadOnlyStruct(0x28, '<hhh', 1/100)
101+
_gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
102+
125103

126104
def __init__(self, i2c, address=0x28):
127105
self.i2c_device = I2CDevice(i2c, address)
@@ -166,8 +144,10 @@ def mode(self):
166144
Switch the mode of operation and return the previous mode.
167145
168146
Mode of operation defines which sensors are enabled and whether the
169-
measurements are absolute or relative:
147+
measurements are absolute or relative.
148+
If a sensor is disabled, it will return an empty tuple.
170149
150+
legend: x=on, -=off
171151
+------------------+-------+---------+------+----------+
172152
| Mode | Accel | Compass | Gyro | Absolute |
173153
+==================+=======+=========+======+==========+
@@ -244,3 +224,72 @@ def use_external_crystal(self, value):
244224
self._write_register(_TRIGGER_REGISTER, 0x80 if value else 0x00)
245225
self.mode = last_mode
246226
time.sleep(0.01)
227+
228+
229+
@property
230+
def temperature(self):
231+
"""Measures the temperature of the chip in degrees Celsius."""
232+
return self._temperature
233+
234+
@property
235+
def acceleration(self):
236+
"""Gives the raw accelerometer readings, in m/s.
237+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
238+
"""
239+
if self.mode not in [0x00, 0x02, 0x03, 0x06]:
240+
return self._acceleration
241+
return (None, None, None)
242+
243+
@property
244+
def magnetic(self):
245+
"""Gives the raw magnetometer readings in microteslas.
246+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
247+
"""
248+
if self.mode not in [0x00, 0x03, 0x05, 0x08]:
249+
return self._magnetic
250+
return (None, None, None)
251+
252+
@property
253+
def gyro(self):
254+
"""Gives the raw gyroscope reading in radians per second.
255+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
256+
"""
257+
if self.mode not in [0x00, 0x01, 0x02, 0x04, 0x09, 0x0a]:
258+
return self._gyro
259+
return (None, None, None)
260+
261+
@property
262+
def euler(self):
263+
"""Gives the calculated orientation angles, in degrees.
264+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
265+
"""
266+
if self.mode in [0x09, 0x0b, 0x0c]:
267+
return self._euler
268+
return (None, None, None)
269+
270+
@property
271+
def quaternion(self):
272+
"""Gives the calculated orientation as a quaternion.
273+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
274+
"""
275+
if self.mode in [0x09, 0x0b, 0x0c]:
276+
return self._quaternion
277+
return (None, None, None, None)
278+
279+
@property
280+
def linear_acceleration(self):
281+
"""Returns the linear acceleration, without gravity, in m/s.
282+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
283+
"""
284+
if self.mode in [0x09, 0x0b, 0x0c]:
285+
return self._linear_acceleration
286+
return (None, None, None)
287+
288+
@property
289+
def gravity(self):
290+
"""Returns the gravity vector, without acceleration in m/s.
291+
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
292+
"""
293+
if self.mode in [0x09, 0x0b, 0x0c]:
294+
return self._gravity
295+
return (None, None, None)

examples/bno055_simpletest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
while True:
1010
print('Temperature: {} degrees C'.format(sensor.temperature))
11-
print('Accelerometer (m/s^2): {}'.format(sensor.accelerometer))
12-
print('Magnetometer (microteslas): {}'.format(sensor.magnetometer))
11+
print('Accelerometer (m/s^2): {}'.format(sensor.acceleration))
12+
print('Magnetometer (microteslas): {}'.format(sensor.magnetic))
1313
print('Gyroscope (rad/sec): {}'.format(sensor.gyro))
1414
print('Euler angle: {}'.format(sensor.euler))
1515
print('Quaternion: {}'.format(sensor.quaternion))

0 commit comments

Comments
 (0)