From 6f5883876d1f32a63715c6347e64fda004bba9ea Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 4 Feb 2017 22:13:45 +0100 Subject: [PATCH] Initial commit --- LICENSE | 2 +- README.rst | 56 +++++++++++--- adafruit_bno055.py | 182 ++++++++++++++++++++++++++++++++++++++++++++- api.rst | 2 - conf.py | 4 +- requirements.txt | 3 +- 6 files changed, 227 insertions(+), 22 deletions(-) diff --git a/LICENSE b/LICENSE index e096cee..7511d62 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries +Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index f46bbde..c7ebd35 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,3 @@ - Introduction ============ @@ -6,27 +5,59 @@ Introduction :target: https://circuitpython.readthedocs.io/projects/bno055/en/latest/ :alt: Documentation Status -.. image :: https://badges.gitter.im/adafruit/circuitpython.svg +.. image:: https://badges.gitter.im/adafruit/circuitpython.svg :target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge :alt: Gitter -TODO Dependencies ============= -This driver depends on: -* `Adafruit CircuitPython `_ -* `Bus Device `_ +This driver depends on the `Register +`_ and `Bus Device +`_ libraries. +Please ensure they are also available on the CircuitPython filesystem. This is +easily achieved by downloading `a library and driver bundle +`_. -Please ensure all dependencies are available on the CircuitPython filesystem. -This is easily achieved by downloading -`the Adafruit library and driver bundle `_. +Usage Notes +=========== -Usage Example -============= +Of course, you must import the library to use it: + +.. code:: python + + import adafruit_bno055 + + +This driver takes an instantiated and active I2C object (from the `nativeio` or +the `bitbangio` library) as an argument to its constructor. The way to create +an I2C object depends on the board you are using. For boards with labeled SCL +and SDA pins, you can: + +.. code:: python + + from nativeio import I2C + #from bitbangio import I2C + from board import SDA, SCL + + i2c = I2C(SCL, SDA) + +Once you have the I2C object, you can create the sensor object: + +.. code:: python + + sensor = adafruit_bno055.BNO055(i2c) + + +And then you can start reading the measurements: + +.. code:: python + + print(sensor.temperature) + print(sensor.euler) + print(sensor.gravity) -TODO Contributing ============ @@ -35,6 +66,7 @@ Contributions are welcome! Please read our `Code of Conduct `_ before contributing to help this project stay welcoming. + API Reference ============= diff --git a/adafruit_bno055.py b/adafruit_bno055.py index bc9f9dc..77d6ff4 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -1,6 +1,6 @@ # The MIT License (MIT) # -# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries +# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -19,11 +19,185 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. + + """ -`adafruit_bno055` -==================================================== +``adafruit_bno055`` +=================== -TODO(description) +This is a CircuitPython driver for the Bosch BNO055 nine degree of freedom +inertial measurement unit module with sensor fusion. * Author(s): Radomir Dopieralski """ + +from micropython import const +from adafruit_bus_device.i2c_device import I2CDevice +from adafruit_register.i2c_struct import Struct, UnaryStruct +import time + + +_CHIP_ID = const(0xa0) + +CONFIG_MODE = const(0x00) +ACCONLY_MODE = const(0x01) +MAGONLY_MODE = const(0x02) +GYRONLY_MODE = const(0x03) +ACCMAG_MODE = const(0x04) +ACCGYRO_MODE = const(0x05) +MAGGYRO_MODE = const(0x06) +AMG_MODE = const(0x07) +IMUPLUS_MODE = const(0x08) +COMPASS_MODE = const(0x09) +M4G_MODE = const(0x0a) +NDOF_FMC_OFF_MODE = const(0x0b) +NDOF_MODE = const(0x0c) + +_POWER_NORMAL = const(0x00) +_POWER_LOW = const(0x01) +_POWER_SUSPEND = const(0x02) + +_MODE_REGISTER = const(0x3d) +_PAGE_REGISTER = const(0x07) +_TRIGGER_REGISTER = const(0x3f) +_POWER_REGISTER = const(0x3e) +_ID_REGISTER = const(0x00) + + +class _ScaledReadOnlyStruct(Struct): + def __init__(self, register_address, struct_format, scale): + super(_ScaledReadOnlyStruct, self).__init__( + register_address, struct_format) + self.scale = scale + + def __get__(self, obj, objtype=None): + result = super(_ScaledReadOnlyStruct, self).__get__(obj, objtype) + return tuple(self.scale * v for v in result) + + def __set__(self, obj, value): + raise NotImplementedError() + + +class _ReadOnlyUnaryStruct(UnaryStruct): + def __set__(self, obj, value): + raise NotImplementedError() + + +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, '=0.1.1