Skip to content

Made members that store sensor data properties. Removed deprecated members. disabled members return none #38

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 11 commits into from
Oct 18, 2019
Merged
113 changes: 81 additions & 32 deletions adafruit_bno055.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,15 @@ class BNO055:
Driver for the BNO055 9DOF IMU sensor.
"""

temperature = _ReadOnlyUnaryStruct(0x34, 'b')
"""Measures the temperature of the chip in degrees Celsius."""
accelerometer = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
"""Gives the raw accelerometer readings, in m/s.

.. warning:: This is deprecated. Use ``acceleration`` instead. It'll work
with other drivers too."""
acceleration = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
"""Gives the raw accelerometer readings, in m/s."""
magnetometer = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
"""Gives the raw magnetometer readings in microteslas.

.. warning:: This is deprecated. Use ``magnetic`` instead. It'll work with
other drivers too."""
magnetic = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
"""Gives the raw magnetometer readings in microteslas."""
gyroscope = _ScaledReadOnlyStruct(0x14, '<hhh', 1/16)
"""Gives the raw gyroscope reading in degrees per second.

.. warning:: This is deprecated. Use ``gyro`` instead. It'll work with
other drivers too."""
gyro = _ScaledReadOnlyStruct(0x14, '<hhh', 0.001090830782496456)
"""Gives the raw gyroscope reading in radians per second."""
euler = _ScaledReadOnlyStruct(0x1a, '<hhh', 1/16)
"""Gives the calculated orientation angles, in degrees."""
quaternion = _ScaledReadOnlyStruct(0x20, '<hhhh', 1/(1<<14))
"""Gives the calculated orientation as a quaternion."""
linear_acceleration = _ScaledReadOnlyStruct(0x28, '<hhh', 1/100)
"""Returns the linear acceleration, without gravity, in m/s."""
gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)
"""Returns the gravity vector, without acceleration in m/s."""
_temperature = _ReadOnlyUnaryStruct(0x34, 'b')
_acceleration = _ScaledReadOnlyStruct(0x08, '<hhh', 1/100)
_magnetic = _ScaledReadOnlyStruct(0x0e, '<hhh', 1/16)
_gyro = _ScaledReadOnlyStruct(0x14, '<hhh', 0.001090830782496456)
_euler = _ScaledReadOnlyStruct(0x1a, '<hhh', 1/16)
_quaternion = _ScaledReadOnlyStruct(0x20, '<hhhh', 1/(1<<14))
_linear_acceleration = _ScaledReadOnlyStruct(0x28, '<hhh', 1/100)
_gravity = _ScaledReadOnlyStruct(0x2e, '<hhh', 1/100)


def __init__(self, i2c, address=0x28):
self.i2c_device = I2CDevice(i2c, address)
Expand Down Expand Up @@ -166,8 +144,10 @@ def mode(self):
Switch the mode of operation and return the previous mode.

Mode of operation defines which sensors are enabled and whether the
measurements are absolute or relative:
measurements are absolute or relative.
If a sensor is disabled, it will return an empty tuple.

legend: x=on, -=off
+------------------+-------+---------+------+----------+
| Mode | Accel | Compass | Gyro | Absolute |
+==================+=======+=========+======+==========+
Expand Down Expand Up @@ -244,3 +224,72 @@ def use_external_crystal(self, value):
self._write_register(_TRIGGER_REGISTER, 0x80 if value else 0x00)
self.mode = last_mode
time.sleep(0.01)


@property
def temperature(self):
"""Measures the temperature of the chip in degrees Celsius."""
return self._temperature

@property
def acceleration(self):
"""Gives the raw accelerometer readings, in m/s.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode not in [0x00, 0x02, 0x03, 0x06]:
return self._acceleration
return (None, None, None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also update the docstrings of the mode property and the new properties that may return None to describe the new behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change anything in the mode property, so I'm not really sure what I'd change on that docstring, but I'll do the docstrings for the new properties

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstrings in the individual properties are great. All I would do for the mode property is to add a legend at the top of the table to describe what each symbol in the table means (x=on, -=off and will return an empty tuple)


@property
def magnetic(self):
"""Gives the raw magnetometer readings in microteslas.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode not in [0x00, 0x03, 0x05, 0x08]:
return self._magnetic
return (None, None, None)

@property
def gyro(self):
"""Gives the raw gyroscope reading in radians per second.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode not in [0x00, 0x01, 0x02, 0x04, 0x09, 0x0a]:
return self._gyro
return (None, None, None)

@property
def euler(self):
"""Gives the calculated orientation angles, in degrees.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode in [0x09, 0x0b, 0x0c]:
return self._euler
return (None, None, None)

@property
def quaternion(self):
"""Gives the calculated orientation as a quaternion.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode in [0x09, 0x0b, 0x0c]:
return self._quaternion
return (None, None, None, None)

@property
def linear_acceleration(self):
"""Returns the linear acceleration, without gravity, in m/s.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode in [0x09, 0x0b, 0x0c]:
return self._linear_acceleration
return (None, None, None)

@property
def gravity(self):
"""Returns the gravity vector, without acceleration in m/s.
Returns an empty tuple of length 3 when this property has been disabled by the current mode.
"""
if self.mode in [0x09, 0x0b, 0x0c]:
return self._gravity
return (None, None, None)
4 changes: 2 additions & 2 deletions examples/bno055_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

while True:
print('Temperature: {} degrees C'.format(sensor.temperature))
print('Accelerometer (m/s^2): {}'.format(sensor.accelerometer))
print('Magnetometer (microteslas): {}'.format(sensor.magnetometer))
print('Accelerometer (m/s^2): {}'.format(sensor.acceleration))
print('Magnetometer (microteslas): {}'.format(sensor.magnetic))
print('Gyroscope (rad/sec): {}'.format(sensor.gyro))
print('Euler angle: {}'.format(sensor.euler))
print('Quaternion: {}'.format(sensor.quaternion))
Expand Down