Compare commits
No commits in common. "master" and "docs-update" have entirely different histories.
master
...
docs-updat
2 changed files with 5 additions and 289 deletions
|
|
@ -56,71 +56,12 @@ M4G_MODE = const(0x0A)
|
|||
NDOF_FMC_OFF_MODE = const(0x0B)
|
||||
NDOF_MODE = const(0x0C)
|
||||
|
||||
ACCEL_2G = const(0x00) # For accel_range property
|
||||
ACCEL_4G = const(0x01) # Default
|
||||
ACCEL_8G = const(0x02)
|
||||
ACCEL_16G = const(0x03)
|
||||
ACCEL_7_81HZ = const(0x00) # For accel_bandwidth property
|
||||
ACCEL_15_63HZ = const(0x04)
|
||||
ACCEL_31_25HZ = const(0x08)
|
||||
ACCEL_62_5HZ = const(0x0C) # Default
|
||||
ACCEL_125HZ = const(0x10)
|
||||
ACCEL_250HZ = const(0x14)
|
||||
ACCEL_500HZ = const(0x18)
|
||||
ACCEL_1000HZ = const(0x1C)
|
||||
ACCEL_NORMAL_MODE = const(0x00) # Default. For accel_mode property
|
||||
ACCEL_SUSPEND_MODE = const(0x20)
|
||||
ACCEL_LOWPOWER1_MODE = const(0x40)
|
||||
ACCEL_STANDBY_MODE = const(0x60)
|
||||
ACCEL_LOWPOWER2_MODE = const(0x80)
|
||||
ACCEL_DEEPSUSPEND_MODE = const(0xA0)
|
||||
|
||||
GYRO_2000_DPS = const(0x00) # Default. For gyro_range property
|
||||
GYRO_1000_DPS = const(0x01)
|
||||
GYRO_500_DPS = const(0x02)
|
||||
GYRO_250_DPS = const(0x03)
|
||||
GYRO_125_DPS = const(0x04)
|
||||
GYRO_523HZ = const(0x00) # For gyro_bandwidth property
|
||||
GYRO_230HZ = const(0x08)
|
||||
GYRO_116HZ = const(0x10)
|
||||
GYRO_47HZ = const(0x18)
|
||||
GYRO_23HZ = const(0x20)
|
||||
GYRO_12HZ = const(0x28)
|
||||
GYRO_64HZ = const(0x30)
|
||||
GYRO_32HZ = const(0x38) # Default
|
||||
GYRO_NORMAL_MODE = const(0x00) # Default. For gyro_mode property
|
||||
GYRO_FASTPOWERUP_MODE = const(0x01)
|
||||
GYRO_DEEPSUSPEND_MODE = const(0x02)
|
||||
GYRO_SUSPEND_MODE = const(0x03)
|
||||
GYRO_ADVANCEDPOWERSAVE_MODE = const(0x04)
|
||||
|
||||
MAGNET_2HZ = const(0x00) # For magnet_rate property
|
||||
MAGNET_6HZ = const(0x01)
|
||||
MAGNET_8HZ = const(0x02)
|
||||
MAGNET_10HZ = const(0x03)
|
||||
MAGNET_15HZ = const(0x04)
|
||||
MAGNET_20HZ = const(0x05) # Default
|
||||
MAGNET_25HZ = const(0x06)
|
||||
MAGNET_30HZ = const(0x07)
|
||||
MAGNET_LOWPOWER_MODE = const(0x00) # For magnet_operation_mode property
|
||||
MAGNET_REGULAR_MODE = const(0x08) # Default
|
||||
MAGNET_ENHANCEDREGULAR_MODE = const(0x10)
|
||||
MAGNET_ACCURACY_MODE = const(0x18)
|
||||
MAGNET_NORMAL_MODE = const(0x00) # for magnet_power_mode property
|
||||
MAGNET_SLEEP_MODE = const(0x20)
|
||||
MAGNET_SUSPEND_MODE = const(0x40)
|
||||
MAGNET_FORCEMODE_MODE = const(0x60) # Default
|
||||
|
||||
_POWER_NORMAL = const(0x00)
|
||||
_POWER_LOW = const(0x01)
|
||||
_POWER_SUSPEND = const(0x02)
|
||||
|
||||
_MODE_REGISTER = const(0x3D)
|
||||
_PAGE_REGISTER = const(0x07)
|
||||
_ACCEL_CONFIG_REGISTER = const(0x08)
|
||||
_MAGNET_CONFIG_REGISTER = const(0x09)
|
||||
_GYRO_CONFIG_0_REGISTER = const(0x0A)
|
||||
_GYRO_CONFIG_1_REGISTER = const(0x0B)
|
||||
_CALIBRATION_REGISTER = const(0x35)
|
||||
_OFFSET_ACCEL_REGISTER = const(0x55)
|
||||
_OFFSET_MAGNET_REGISTER = const(0x5B)
|
||||
|
|
@ -172,7 +113,7 @@ class _ModeStruct(Struct): # pylint: disable=too-few-public-methods
|
|||
obj.mode = last_mode
|
||||
|
||||
|
||||
class BNO055: # pylint: disable=too-many-public-methods
|
||||
class BNO055:
|
||||
"""
|
||||
Base class for the BNO055 9DOF IMU sensor.
|
||||
"""
|
||||
|
|
@ -185,9 +126,6 @@ class BNO055: # pylint: disable=too-many-public-methods
|
|||
self._write_register(_POWER_REGISTER, _POWER_NORMAL)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
self._write_register(_TRIGGER_REGISTER, 0x00)
|
||||
self.accel_range = ACCEL_4G
|
||||
self.gyro_range = GYRO_2000_DPS
|
||||
self.magnet_rate = MAGNET_20HZ
|
||||
time.sleep(0.01)
|
||||
self.mode = NDOF_MODE
|
||||
time.sleep(0.01)
|
||||
|
|
@ -454,184 +392,6 @@ class BNO055: # pylint: disable=too-many-public-methods
|
|||
def _gravity(self):
|
||||
raise NotImplementedError("Must be implemented.")
|
||||
|
||||
@property
|
||||
def accel_range(self):
|
||||
""" Switch the accelerometer range and return the new range. Default value: +/- 4g
|
||||
See table 3-8 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00000011 & value
|
||||
|
||||
@accel_range.setter
|
||||
def accel_range(self, rng=ACCEL_4G):
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
masked_value = 0b11111100 & value
|
||||
self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | rng)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def accel_bandwidth(self):
|
||||
""" Switch the accelerometer bandwidth and return the new bandwidth. Default value: 62.5 Hz
|
||||
See table 3-8 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00011100 & value
|
||||
|
||||
@accel_bandwidth.setter
|
||||
def accel_bandwidth(self, bandwidth=ACCEL_62_5HZ):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
masked_value = 0b11100011 & value
|
||||
self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | bandwidth)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def accel_mode(self):
|
||||
""" Switch the accelerometer mode and return the new mode. Default value: Normal
|
||||
See table 3-8 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b11100000 & value
|
||||
|
||||
@accel_mode.setter
|
||||
def accel_mode(self, mode=ACCEL_NORMAL_MODE):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_ACCEL_CONFIG_REGISTER)
|
||||
masked_value = 0b00011111 & value
|
||||
self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | mode)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def gyro_range(self):
|
||||
""" Switch the gyroscope range and return the new range. Default value: 2000 dps
|
||||
See table 3-9 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00000111 & value
|
||||
|
||||
@gyro_range.setter
|
||||
def gyro_range(self, rng=GYRO_2000_DPS):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
|
||||
masked_value = 0b00111000 & value
|
||||
self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | rng)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def gyro_bandwidth(self):
|
||||
""" Switch the gyroscope bandwidth and return the new bandwidth. Default value: 32 Hz
|
||||
See table 3-9 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00111000 & value
|
||||
|
||||
@gyro_bandwidth.setter
|
||||
def gyro_bandwidth(self, bandwidth=GYRO_32HZ):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
|
||||
masked_value = 0b00000111 & value
|
||||
self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | bandwidth)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def gyro_mode(self):
|
||||
""" Switch the gyroscope mode and return the new mode. Default value: Normal
|
||||
See table 3-9 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_1_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00000111 & value
|
||||
|
||||
@gyro_mode.setter
|
||||
def gyro_mode(self, mode=GYRO_NORMAL_MODE):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_GYRO_CONFIG_1_REGISTER)
|
||||
masked_value = 0b00000000 & value
|
||||
self._write_register(_GYRO_CONFIG_1_REGISTER, masked_value | mode)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def magnet_rate(self):
|
||||
""" Switch the magnetometer data output rate and return the new rate. Default value: 20Hz
|
||||
See table 3-10 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00000111 & value
|
||||
|
||||
@magnet_rate.setter
|
||||
def magnet_rate(self, rate=MAGNET_20HZ):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
masked_value = 0b01111000 & value
|
||||
self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | rate)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def magnet_operation_mode(self):
|
||||
""" Switch the magnetometer operation mode and return the new mode. Default value: Regular
|
||||
See table 3-10 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b00011000 & value
|
||||
|
||||
@magnet_operation_mode.setter
|
||||
def magnet_operation_mode(self, mode=MAGNET_REGULAR_MODE):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
masked_value = 0b01100111 & value
|
||||
self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | mode)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
@property
|
||||
def magnet_mode(self):
|
||||
""" Switch the magnetometer power mode and return the new mode. Default value: Forced
|
||||
See table 3-10 in the datasheet.
|
||||
"""
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
return 0b01100000 & value
|
||||
|
||||
@magnet_mode.setter
|
||||
def magnet_mode(self, mode=MAGNET_FORCEMODE_MODE):
|
||||
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
|
||||
raise RuntimeError("Mode must not be a fusion mode")
|
||||
self._write_register(_PAGE_REGISTER, 0x01)
|
||||
value = self._read_register(_MAGNET_CONFIG_REGISTER)
|
||||
masked_value = 0b00011111 & value
|
||||
self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | mode)
|
||||
self._write_register(_PAGE_REGISTER, 0x00)
|
||||
|
||||
def _write_register(self, register, value):
|
||||
raise NotImplementedError("Must be implemented.")
|
||||
|
||||
|
|
@ -697,9 +457,7 @@ class BNO055_UART(BNO055):
|
|||
if not isinstance(data, bytes):
|
||||
data = bytes([data])
|
||||
self._uart.write(bytes([0xAA, 0x00, register, len(data)]) + data)
|
||||
now = time.monotonic()
|
||||
while self._uart.in_waiting < 2 and time.monotonic() - now < 0.25:
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
resp = self._uart.read(self._uart.in_waiting)
|
||||
if len(resp) < 2:
|
||||
raise OSError("UART access error.")
|
||||
|
|
@ -707,16 +465,9 @@ class BNO055_UART(BNO055):
|
|||
raise RuntimeError("UART write error: {}".format(resp[1]))
|
||||
|
||||
def _read_register(self, register, length=1): # pylint: disable=arguments-differ
|
||||
i = 0
|
||||
while i < 3:
|
||||
self._uart.write(bytes([0xAA, 0x01, register, length]))
|
||||
now = time.monotonic()
|
||||
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.1:
|
||||
pass
|
||||
resp = self._uart.read(self._uart.in_waiting)
|
||||
if len(resp) >= 2 and resp[0] == 0xBB:
|
||||
break
|
||||
i += 1
|
||||
self._uart.write(bytes([0xAA, 0x01, register, length]))
|
||||
time.sleep(0.1)
|
||||
resp = self._uart.read(self._uart.in_waiting)
|
||||
if len(resp) < 2:
|
||||
raise OSError("UART access error.")
|
||||
if resp[0] != 0xBB:
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
"""
|
||||
This example demonstrates how to instantiate the
|
||||
Adafruit BNO055 Sensor using this library and just
|
||||
the I2C bus number.
|
||||
This example will only work on a Raspberry Pi
|
||||
and does require the i2c-gpio kernel module to be
|
||||
installed and enabled. Most Raspberry Pis will
|
||||
already have it installed, however most do not
|
||||
have it enabled. You will have to manually enable it
|
||||
"""
|
||||
|
||||
import time
|
||||
from adafruit_extended_bus import ExtendedI2C as I2C
|
||||
import adafruit_bno055
|
||||
|
||||
# To enable i2c-gpio, add the line `dtoverlay=i2c-gpio` to /boot/config.txt
|
||||
# Then reboot the pi
|
||||
|
||||
# Create library object using our Extended Bus I2C port
|
||||
# Use `ls /dev/i2c*` to find out what i2c devices are connected
|
||||
i2c = I2C(1) # Device is /dev/i2c-1
|
||||
sensor = adafruit_bno055.BNO055_I2C(i2c)
|
||||
|
||||
while True:
|
||||
print("Temperature: {} degrees C".format(sensor.temperature))
|
||||
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))
|
||||
print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
|
||||
print("Gravity (m/s^2): {}".format(sensor.gravity))
|
||||
print()
|
||||
|
||||
time.sleep(1)
|
||||
Loading…
Reference in a new issue