adafruit_bno055 / Adafruit_BNO055.cpp @ a6b8a32b
History | View | Annotate | Download (22.242 KB)
| 1 | 4bc1c0c1 | Kevin Townsend | /***************************************************************************
|
|---|---|---|---|
| 2 | This is a library for the BNO055 orientation sensor
|
||
| 3 | |||
| 4 | Designed specifically to work with the Adafruit BNO055 Breakout.
|
||
| 5 | |||
| 6 | Pick one up today in the adafruit shop!
|
||
| 7 | a6b8a32b | Limor "Ladyada" Fried | ------> https://www.adafruit.com/product/2472
|
| 8 | 4bc1c0c1 | Kevin Townsend | |
| 9 | These sensors use I2C to communicate, 2 pins are required to interface.
|
||
| 10 | |||
| 11 | Adafruit invests time and resources providing this open source code,
|
||
| 12 | please support Adafruit andopen-source hardware by purchasing products
|
||
| 13 | from Adafruit!
|
||
| 14 | |||
| 15 | Written by KTOWN for Adafruit Industries.
|
||
| 16 | |||
| 17 | MIT license, all text above must be included in any redistribution
|
||
| 18 | 463eabf7 | Wetmelon | ***************************************************************************/
|
| 19 | 4bc1c0c1 | Kevin Townsend | |
| 20 | #if ARDUINO >= 100 |
||
| 21 | 463eabf7 | Wetmelon | #include "Arduino.h" |
| 22 | 4bc1c0c1 | Kevin Townsend | #else
|
| 23 | 463eabf7 | Wetmelon | #include "WProgram.h" |
| 24 | 4bc1c0c1 | Kevin Townsend | #endif
|
| 25 | |||
| 26 | #include <math.h> |
||
| 27 | #include <limits.h> |
||
| 28 | |||
| 29 | #include "Adafruit_BNO055.h" |
||
| 30 | |||
| 31 | /***************************************************************************
|
||
| 32 | CONSTRUCTOR
|
||
| 33 | ***************************************************************************/
|
||
| 34 | 40f91f6f | Tony DiCola | |
| 35 | 4bc1c0c1 | Kevin Townsend | /**************************************************************************/
|
| 36 | /*!
|
||
| 37 | 463eabf7 | Wetmelon | @brief Instantiates a new Adafruit_BNO055 class
|
| 38 | */
|
||
| 39 | 4bc1c0c1 | Kevin Townsend | /**************************************************************************/
|
| 40 | Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address) |
||
| 41 | {
|
||
| 42 | 463eabf7 | Wetmelon | _sensorID = sensorID; |
| 43 | _address = address; |
||
| 44 | 4bc1c0c1 | Kevin Townsend | } |
| 45 | |||
| 46 | /***************************************************************************
|
||
| 47 | PUBLIC FUNCTIONS
|
||
| 48 | ***************************************************************************/
|
||
| 49 | |||
| 50 | /**************************************************************************/
|
||
| 51 | /*!
|
||
| 52 | 463eabf7 | Wetmelon | @brief Sets up the HW
|
| 53 | */
|
||
| 54 | 4bc1c0c1 | Kevin Townsend | /**************************************************************************/
|
| 55 | bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode)
|
||
| 56 | {
|
||
| 57 | 463eabf7 | Wetmelon | /* Enable I2C */
|
| 58 | Wire.begin(); |
||
| 59 | |||
| 60 | 78cc710f | ladyada | // BNO055 clock stretches for 500us or more!
|
| 61 | #ifdef ESP8266
|
||
| 62 | Wire.setClockStretchLimit(1000); // Allow for 1000us of clock stretching |
||
| 63 | #endif
|
||
| 64 | |||
| 65 | 463eabf7 | Wetmelon | /* Make sure we have the right device */
|
| 66 | uint8_t id = read8(BNO055_CHIP_ID_ADDR); |
||
| 67 | if(id != BNO055_ID)
|
||
| 68 | {
|
||
| 69 | delay(1000); // hold on for boot |
||
| 70 | id = read8(BNO055_CHIP_ID_ADDR); |
||
| 71 | if(id != BNO055_ID) {
|
||
| 72 | return false; // still not? ok bail |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /* Switch to config mode (just in case since this is the default) */
|
||
| 77 | setMode(OPERATION_MODE_CONFIG); |
||
| 78 | |||
| 79 | /* Reset */
|
||
| 80 | write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
|
||
| 81 | while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
|
||
| 82 | {
|
||
| 83 | delay(10);
|
||
| 84 | } |
||
| 85 | delay(50);
|
||
| 86 | |||
| 87 | /* Set to normal power mode */
|
||
| 88 | write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL); |
||
| 89 | delay(10);
|
||
| 90 | |||
| 91 | write8(BNO055_PAGE_ID_ADDR, 0);
|
||
| 92 | |||
| 93 | /* Set the output units */
|
||
| 94 | /*
|
||
| 95 | uint8_t unitsel = (0 << 7) | // Orientation = Android
|
||
| 96 | (0 << 4) | // Temperature = Celsius
|
||
| 97 | (0 << 2) | // Euler = Degrees
|
||
| 98 | (1 << 1) | // Gyro = Rads
|
||
| 99 | (0 << 0); // Accelerometer = m/s^2
|
||
| 100 | write8(BNO055_UNIT_SEL_ADDR, unitsel);
|
||
| 101 | */
|
||
| 102 | |||
| 103 | 378858ec | Shunya Sato | /* Configure axis mapping (see section 3.4) */
|
| 104 | /*
|
||
| 105 | write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
|
||
| 106 | delay(10);
|
||
| 107 | write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
|
||
| 108 | delay(10);
|
||
| 109 | */
|
||
| 110 | a97e0fe1 | kA®0šhî | |
| 111 | 463eabf7 | Wetmelon | write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
|
| 112 | delay(10);
|
||
| 113 | /* Set the requested operating mode (see section 3.3) */
|
||
| 114 | setMode(mode); |
||
| 115 | delay(20);
|
||
| 116 | |||
| 117 | return true; |
||
| 118 | 4bc1c0c1 | Kevin Townsend | } |
| 119 | |||
| 120 | /**************************************************************************/
|
||
| 121 | /*!
|
||
| 122 | 463eabf7 | Wetmelon | @brief Puts the chip in the specified operating mode
|
| 123 | */
|
||
| 124 | 4bc1c0c1 | Kevin Townsend | /**************************************************************************/
|
| 125 | void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
|
||
| 126 | {
|
||
| 127 | 463eabf7 | Wetmelon | _mode = mode; |
| 128 | write8(BNO055_OPR_MODE_ADDR, _mode); |
||
| 129 | delay(30);
|
||
| 130 | 4bc1c0c1 | Kevin Townsend | } |
| 131 | |||
| 132 | /**************************************************************************/
|
||
| 133 | /*!
|
||
| 134 | a97e0fe1 | kA®0šhî | @brief Changes the chip's axis remap
|
| 135 | */
|
||
| 136 | /**************************************************************************/
|
||
| 137 | void Adafruit_BNO055::setAxisRemap( adafruit_bno055_axis_remap_config_t remapcode )
|
||
| 138 | {
|
||
| 139 | adafruit_bno055_opmode_t modeback = _mode; |
||
| 140 | |||
| 141 | setMode(OPERATION_MODE_CONFIG); |
||
| 142 | delay(25);
|
||
| 143 | write8(BNO055_AXIS_MAP_CONFIG_ADDR, remapcode); |
||
| 144 | delay(10);
|
||
| 145 | /* Set the requested operating mode (see section 3.3) */
|
||
| 146 | setMode(modeback); |
||
| 147 | delay(20);
|
||
| 148 | } |
||
| 149 | |||
| 150 | /**************************************************************************/
|
||
| 151 | /*!
|
||
| 152 | @brief Changes the chip's axis signs
|
||
| 153 | */
|
||
| 154 | /**************************************************************************/
|
||
| 155 | void Adafruit_BNO055::setAxisSign( adafruit_bno055_axis_remap_sign_t remapsign )
|
||
| 156 | {
|
||
| 157 | adafruit_bno055_opmode_t modeback = _mode; |
||
| 158 | |||
| 159 | setMode(OPERATION_MODE_CONFIG); |
||
| 160 | delay(25);
|
||
| 161 | write8(BNO055_AXIS_MAP_SIGN_ADDR, remapsign); |
||
| 162 | delay(10);
|
||
| 163 | /* Set the requested operating mode (see section 3.3) */
|
||
| 164 | setMode(modeback); |
||
| 165 | delay(20);
|
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | /**************************************************************************/
|
||
| 170 | /*!
|
||
| 171 | 463eabf7 | Wetmelon | @brief Use the external 32.768KHz crystal
|
| 172 | */
|
||
| 173 | c4f272e1 | ladyada | /**************************************************************************/
|
| 174 | void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
|
||
| 175 | {
|
||
| 176 | 463eabf7 | Wetmelon | adafruit_bno055_opmode_t modeback = _mode; |
| 177 | |||
| 178 | /* Switch to config mode (just in case since this is the default) */
|
||
| 179 | setMode(OPERATION_MODE_CONFIG); |
||
| 180 | delay(25);
|
||
| 181 | write8(BNO055_PAGE_ID_ADDR, 0);
|
||
| 182 | if (usextal) {
|
||
| 183 | write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
|
||
| 184 | } else {
|
||
| 185 | write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
|
||
| 186 | } |
||
| 187 | delay(10);
|
||
| 188 | /* Set the requested operating mode (see section 3.3) */
|
||
| 189 | setMode(modeback); |
||
| 190 | delay(20);
|
||
| 191 | c4f272e1 | ladyada | } |
| 192 | |||
| 193 | |||
| 194 | /**************************************************************************/
|
||
| 195 | /*!
|
||
| 196 | 463eabf7 | Wetmelon | @brief Gets the latest system status info
|
| 197 | */
|
||
| 198 | 4bc1c0c1 | Kevin Townsend | /**************************************************************************/
|
| 199 | 3b2655dc | ladyada | void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
|
| 200 | 4bc1c0c1 | Kevin Townsend | {
|
| 201 | 463eabf7 | Wetmelon | write8(BNO055_PAGE_ID_ADDR, 0);
|
| 202 | |||
| 203 | /* System Status (see section 4.3.58)
|
||
| 204 | ---------------------------------
|
||
| 205 | 0 = Idle
|
||
| 206 | 1 = System Error
|
||
| 207 | 2 = Initializing Peripherals
|
||
| 208 | 3 = System Iniitalization
|
||
| 209 | 4 = Executing Self-Test
|
||
| 210 | 5 = Sensor fusio algorithm running
|
||
| 211 | 6 = System running without fusion algorithms */
|
||
| 212 | |||
| 213 | if (system_status != 0) |
||
| 214 | *system_status = read8(BNO055_SYS_STAT_ADDR); |
||
| 215 | |||
| 216 | /* Self Test Results (see section )
|
||
| 217 | --------------------------------
|
||
| 218 | 1 = test passed, 0 = test failed
|
||
| 219 | |||
| 220 | Bit 0 = Accelerometer self test
|
||
| 221 | Bit 1 = Magnetometer self test
|
||
| 222 | Bit 2 = Gyroscope self test
|
||
| 223 | Bit 3 = MCU self test
|
||
| 224 | |||
| 225 | 0x0F = all good! */
|
||
| 226 | |||
| 227 | if (self_test_result != 0) |
||
| 228 | *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR); |
||
| 229 | |||
| 230 | /* System Error (see section 4.3.59)
|
||
| 231 | ---------------------------------
|
||
| 232 | 0 = No error
|
||
| 233 | 1 = Peripheral initialization error
|
||
| 234 | 2 = System initialization error
|
||
| 235 | 3 = Self test result failed
|
||
| 236 | 4 = Register map value out of range
|
||
| 237 | 5 = Register map address out of range
|
||
| 238 |