Adafruit driver converted to Mbed OS 6.x.

Dependents:   Adafruit-BNO055-test

Committer:
MACRUM
Date:
Tue Mar 16 05:41:43 2021 +0000
Revision:
3:7db662f5d402
Parent:
0:22c544c8741a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simonscott 0:22c544c8741a 1 /***************************************************************************
simonscott 0:22c544c8741a 2 This is a library for the BNO055 orientation sensor
simonscott 0:22c544c8741a 3
simonscott 0:22c544c8741a 4 Designed specifically to work with the Adafruit BNO055 Breakout.
simonscott 0:22c544c8741a 5
simonscott 0:22c544c8741a 6 Pick one up today in the adafruit shop!
simonscott 0:22c544c8741a 7 ------> http://www.adafruit.com/products
simonscott 0:22c544c8741a 8
simonscott 0:22c544c8741a 9 These sensors use I2C to communicate, 2 pins are required to interface.
simonscott 0:22c544c8741a 10
simonscott 0:22c544c8741a 11 Adafruit invests time and resources providing this open source code,
simonscott 0:22c544c8741a 12 please support Adafruit andopen-source hardware by purchasing products
simonscott 0:22c544c8741a 13 from Adafruit!
simonscott 0:22c544c8741a 14
simonscott 0:22c544c8741a 15 Written by KTOWN for Adafruit Industries.
simonscott 0:22c544c8741a 16
simonscott 0:22c544c8741a 17 MIT license, all text above must be included in any redistribution
simonscott 0:22c544c8741a 18 ***************************************************************************/
simonscott 0:22c544c8741a 19
simonscott 0:22c544c8741a 20 #ifndef __ADAFRUIT_BNO055_H__
simonscott 0:22c544c8741a 21 #define __ADAFRUIT_BNO055_H__
simonscott 0:22c544c8741a 22
simonscott 0:22c544c8741a 23 #include "Adafruit_Sensor.h"
simonscott 0:22c544c8741a 24 #include "imumaths.h"
simonscott 0:22c544c8741a 25 #include "mbed.h"
simonscott 0:22c544c8741a 26
simonscott 0:22c544c8741a 27 #define BNO055_ADDRESS_A (0x28)
simonscott 0:22c544c8741a 28 #define BNO055_ADDRESS_B (0x29)
simonscott 0:22c544c8741a 29 #define BNO055_ID (0xA0)
simonscott 0:22c544c8741a 30
simonscott 0:22c544c8741a 31 class Adafruit_BNO055 : public Adafruit_Sensor
simonscott 0:22c544c8741a 32 {
simonscott 0:22c544c8741a 33 public:
simonscott 0:22c544c8741a 34 typedef enum
simonscott 0:22c544c8741a 35 {
simonscott 0:22c544c8741a 36 /* Page id register definition */
simonscott 0:22c544c8741a 37 BNO055_PAGE_ID_ADDR = 0X07,
simonscott 0:22c544c8741a 38
simonscott 0:22c544c8741a 39 /* PAGE0 REGISTER DEFINITION START*/
simonscott 0:22c544c8741a 40 BNO055_CHIP_ID_ADDR = 0x00,
simonscott 0:22c544c8741a 41 BNO055_ACCEL_REV_ID_ADDR = 0x01,
simonscott 0:22c544c8741a 42 BNO055_MAG_REV_ID_ADDR = 0x02,
simonscott 0:22c544c8741a 43 BNO055_GYRO_REV_ID_ADDR = 0x03,
simonscott 0:22c544c8741a 44 BNO055_SW_REV_ID_LSB_ADDR = 0x04,
simonscott 0:22c544c8741a 45 BNO055_SW_REV_ID_MSB_ADDR = 0x05,
simonscott 0:22c544c8741a 46 BNO055_BL_REV_ID_ADDR = 0X06,
simonscott 0:22c544c8741a 47
simonscott 0:22c544c8741a 48 /* Accel data register */
simonscott 0:22c544c8741a 49 BNO055_ACCEL_DATA_X_LSB_ADDR = 0X08,
simonscott 0:22c544c8741a 50 BNO055_ACCEL_DATA_X_MSB_ADDR = 0X09,
simonscott 0:22c544c8741a 51 BNO055_ACCEL_DATA_Y_LSB_ADDR = 0X0A,
simonscott 0:22c544c8741a 52 BNO055_ACCEL_DATA_Y_MSB_ADDR = 0X0B,
simonscott 0:22c544c8741a 53 BNO055_ACCEL_DATA_Z_LSB_ADDR = 0X0C,
simonscott 0:22c544c8741a 54 BNO055_ACCEL_DATA_Z_MSB_ADDR = 0X0D,
simonscott 0:22c544c8741a 55
simonscott 0:22c544c8741a 56 /* Mag data register */
simonscott 0:22c544c8741a 57 BNO055_MAG_DATA_X_LSB_ADDR = 0X0E,
simonscott 0:22c544c8741a 58 BNO055_MAG_DATA_X_MSB_ADDR = 0X0F,
simonscott 0:22c544c8741a 59 BNO055_MAG_DATA_Y_LSB_ADDR = 0X10,
simonscott 0:22c544c8741a 60 BNO055_MAG_DATA_Y_MSB_ADDR = 0X11,
simonscott 0:22c544c8741a 61 BNO055_MAG_DATA_Z_LSB_ADDR = 0X12,
simonscott 0:22c544c8741a 62 BNO055_MAG_DATA_Z_MSB_ADDR = 0X13,
simonscott 0:22c544c8741a 63
simonscott 0:22c544c8741a 64 /* Gyro data registers */
simonscott 0:22c544c8741a 65 BNO055_GYRO_DATA_X_LSB_ADDR = 0X14,
simonscott 0:22c544c8741a 66 BNO055_GYRO_DATA_X_MSB_ADDR = 0X15,
simonscott 0:22c544c8741a 67 BNO055_GYRO_DATA_Y_LSB_ADDR = 0X16,
simonscott 0:22c544c8741a 68 BNO055_GYRO_DATA_Y_MSB_ADDR = 0X17,
simonscott 0:22c544c8741a 69 BNO055_GYRO_DATA_Z_LSB_ADDR = 0X18,
simonscott 0:22c544c8741a 70 BNO055_GYRO_DATA_Z_MSB_ADDR = 0X19,
simonscott 0:22c544c8741a 71
simonscott 0:22c544c8741a 72 /* Euler data registers */
simonscott 0:22c544c8741a 73 BNO055_EULER_H_LSB_ADDR = 0X1A,
simonscott 0:22c544c8741a 74 BNO055_EULER_H_MSB_ADDR = 0X1B,
simonscott 0:22c544c8741a 75 BNO055_EULER_R_LSB_ADDR = 0X1C,
simonscott 0:22c544c8741a 76 BNO055_EULER_R_MSB_ADDR = 0X1D,
simonscott 0:22c544c8741a 77 BNO055_EULER_P_LSB_ADDR = 0X1E,
simonscott 0:22c544c8741a 78 BNO055_EULER_P_MSB_ADDR = 0X1F,
simonscott 0:22c544c8741a 79
simonscott 0:22c544c8741a 80 /* Quaternion data registers */
simonscott 0:22c544c8741a 81 BNO055_QUATERNION_DATA_W_LSB_ADDR = 0X20,
simonscott 0:22c544c8741a 82 BNO055_QUATERNION_DATA_W_MSB_ADDR = 0X21,
simonscott 0:22c544c8741a 83 BNO055_QUATERNION_DATA_X_LSB_ADDR = 0X22,
simonscott 0:22c544c8741a 84 BNO055_QUATERNION_DATA_X_MSB_ADDR = 0X23,
simonscott 0:22c544c8741a 85 BNO055_QUATERNION_DATA_Y_LSB_ADDR = 0X24,
simonscott 0:22c544c8741a 86 BNO055_QUATERNION_DATA_Y_MSB_ADDR = 0X25,
simonscott 0:22c544c8741a 87 BNO055_QUATERNION_DATA_Z_LSB_ADDR = 0X26,
simonscott 0:22c544c8741a 88 BNO055_QUATERNION_DATA_Z_MSB_ADDR = 0X27,
simonscott 0:22c544c8741a 89
simonscott 0:22c544c8741a 90 /* Linear acceleration data registers */
simonscott 0:22c544c8741a 91 BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR = 0X28,
simonscott 0:22c544c8741a 92 BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR = 0X29,
simonscott 0:22c544c8741a 93 BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR = 0X2A,
simonscott 0:22c544c8741a 94 BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR = 0X2B,
simonscott 0:22c544c8741a 95 BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR = 0X2C,
simonscott 0:22c544c8741a 96 BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR = 0X2D,
simonscott 0:22c544c8741a 97
simonscott 0:22c544c8741a 98 /* Gravity data registers */
simonscott 0:22c544c8741a 99 BNO055_GRAVITY_DATA_X_LSB_ADDR = 0X2E,
simonscott 0:22c544c8741a 100 BNO055_GRAVITY_DATA_X_MSB_ADDR = 0X2F,
simonscott 0:22c544c8741a 101 BNO055_GRAVITY_DATA_Y_LSB_ADDR = 0X30,
simonscott 0:22c544c8741a 102 BNO055_GRAVITY_DATA_Y_MSB_ADDR = 0X31,
simonscott 0:22c544c8741a 103 BNO055_GRAVITY_DATA_Z_LSB_ADDR = 0X32,
simonscott 0:22c544c8741a 104 BNO055_GRAVITY_DATA_Z_MSB_ADDR = 0X33,
simonscott 0:22c544c8741a 105
simonscott 0:22c544c8741a 106 /* Temperature data register */
simonscott 0:22c544c8741a 107 BNO055_TEMP_ADDR = 0X34,
simonscott 0:22c544c8741a 108
simonscott 0:22c544c8741a 109 /* Status registers */
simonscott 0:22c544c8741a 110 BNO055_CALIB_STAT_ADDR = 0X35,
simonscott 0:22c544c8741a 111 BNO055_SELFTEST_RESULT_ADDR = 0X36,
simonscott 0:22c544c8741a 112 BNO055_INTR_STAT_ADDR = 0X37,
simonscott 0:22c544c8741a 113
simonscott 0:22c544c8741a 114 BNO055_SYS_CLK_STAT_ADDR = 0X38,
simonscott 0:22c544c8741a 115 BNO055_SYS_STAT_ADDR = 0X39,
simonscott 0:22c544c8741a 116 BNO055_SYS_ERR_ADDR = 0X3A,
simonscott 0:22c544c8741a 117
simonscott 0:22c544c8741a 118 /* Unit selection register */
simonscott 0:22c544c8741a 119 BNO055_UNIT_SEL_ADDR = 0X3B,
simonscott 0:22c544c8741a 120 BNO055_DATA_SELECT_ADDR = 0X3C,
simonscott 0:22c544c8741a 121
simonscott 0:22c544c8741a 122 /* Mode registers */
simonscott 0:22c544c8741a 123 BNO055_OPR_MODE_ADDR = 0X3D,
simonscott 0:22c544c8741a 124 BNO055_PWR_MODE_ADDR = 0X3E,
simonscott 0:22c544c8741a 125
simonscott 0:22c544c8741a 126 BNO055_SYS_TRIGGER_ADDR = 0X3F,
simonscott 0:22c544c8741a 127 BNO055_TEMP_SOURCE_ADDR = 0X40,
simonscott 0:22c544c8741a 128
simonscott 0:22c544c8741a 129 /* Axis remap registers */
simonscott 0:22c544c8741a 130 BNO055_AXIS_MAP_CONFIG_ADDR = 0X41,
simonscott 0:22c544c8741a 131 BNO055_AXIS_MAP_SIGN_ADDR = 0X42,
simonscott 0:22c544c8741a 132
simonscott 0:22c544c8741a 133 /* SIC registers */
simonscott 0:22c544c8741a 134 BNO055_SIC_MATRIX_0_LSB_ADDR = 0X43,
simonscott 0:22c544c8741a 135 BNO055_SIC_MATRIX_0_MSB_ADDR = 0X44,
simonscott 0:22c544c8741a 136 BNO055_SIC_MATRIX_1_LSB_ADDR = 0X45,
simonscott 0:22c544c8741a 137 BNO055_SIC_MATRIX_1_MSB_ADDR = 0X46,
simonscott 0:22c544c8741a 138 BNO055_SIC_MATRIX_2_LSB_ADDR = 0X47,
simonscott 0:22c544c8741a 139 BNO055_SIC_MATRIX_2_MSB_ADDR = 0X48,
simonscott 0:22c544c8741a 140 BNO055_SIC_MATRIX_3_LSB_ADDR = 0X49,
simonscott 0:22c544c8741a 141 BNO055_SIC_MATRIX_3_MSB_ADDR = 0X4A,
simonscott 0:22c544c8741a 142 BNO055_SIC_MATRIX_4_LSB_ADDR = 0X4B,
simonscott 0:22c544c8741a 143 BNO055_SIC_MATRIX_4_MSB_ADDR = 0X4C,
simonscott 0:22c544c8741a 144 BNO055_SIC_MATRIX_5_LSB_ADDR = 0X4D,
simonscott 0:22c544c8741a 145 BNO055_SIC_MATRIX_5_MSB_ADDR = 0X4E,
simonscott 0:22c544c8741a 146 BNO055_SIC_MATRIX_6_LSB_ADDR = 0X4F,
simonscott 0:22c544c8741a 147 BNO055_SIC_MATRIX_6_MSB_ADDR = 0X50,
simonscott 0:22c544c8741a 148 BNO055_SIC_MATRIX_7_LSB_ADDR = 0X51,
simonscott 0:22c544c8741a 149 BNO055_SIC_MATRIX_7_MSB_ADDR = 0X52,
simonscott 0:22c544c8741a 150 BNO055_SIC_MATRIX_8_LSB_ADDR = 0X53,
simonscott 0:22c544c8741a 151 BNO055_SIC_MATRIX_8_MSB_ADDR = 0X54,
simonscott 0:22c544c8741a 152
simonscott 0:22c544c8741a 153 /* Accelerometer Offset registers */
simonscott 0:22c544c8741a 154 ACCEL_OFFSET_X_LSB_ADDR = 0X55,
simonscott 0:22c544c8741a 155 ACCEL_OFFSET_X_MSB_ADDR = 0X56,
simonscott 0:22c544c8741a 156 ACCEL_OFFSET_Y_LSB_ADDR = 0X57,
simonscott 0:22c544c8741a 157 ACCEL_OFFSET_Y_MSB_ADDR = 0X58,
simonscott 0:22c544c8741a 158 ACCEL_OFFSET_Z_LSB_ADDR = 0X59,
simonscott 0:22c544c8741a 159 ACCEL_OFFSET_Z_MSB_ADDR = 0X5A,
simonscott 0:22c544c8741a 160
simonscott 0:22c544c8741a 161 /* Magnetometer Offset registers */
simonscott 0:22c544c8741a 162 MAG_OFFSET_X_LSB_ADDR = 0X5B,
simonscott 0:22c544c8741a 163 MAG_OFFSET_X_MSB_ADDR = 0X5C,
simonscott 0:22c544c8741a 164 MAG_OFFSET_Y_LSB_ADDR = 0X5D,
simonscott 0:22c544c8741a 165 MAG_OFFSET_Y_MSB_ADDR = 0X5E,
simonscott 0:22c544c8741a 166 MAG_OFFSET_Z_LSB_ADDR = 0X5F,
simonscott 0:22c544c8741a 167 MAG_OFFSET_Z_MSB_ADDR = 0X60,
simonscott 0:22c544c8741a 168
simonscott 0:22c544c8741a 169 /* Gyroscope Offset register s*/
simonscott 0:22c544c8741a 170 GYRO_OFFSET_X_LSB_ADDR = 0X61,
simonscott 0:22c544c8741a 171 GYRO_OFFSET_X_MSB_ADDR = 0X62,
simonscott 0:22c544c8741a 172 GYRO_OFFSET_Y_LSB_ADDR = 0X63,
simonscott 0:22c544c8741a 173 GYRO_OFFSET_Y_MSB_ADDR = 0X64,
simonscott 0:22c544c8741a 174 GYRO_OFFSET_Z_LSB_ADDR = 0X65,
simonscott 0:22c544c8741a 175 GYRO_OFFSET_Z_MSB_ADDR = 0X66,
simonscott 0:22c544c8741a 176
simonscott 0:22c544c8741a 177 /* Radius registers */
simonscott 0:22c544c8741a 178 ACCEL_RADIUS_LSB_ADDR = 0X67,
simonscott 0:22c544c8741a 179 ACCEL_RADIUS_MSB_ADDR = 0X68,
simonscott 0:22c544c8741a 180 MAG_RADIUS_LSB_ADDR = 0X69,
simonscott 0:22c544c8741a 181 MAG_RADIUS_MSB_ADDR = 0X6A
simonscott 0:22c544c8741a 182 } adafruit_bno055_reg_t;
simonscott 0:22c544c8741a 183
simonscott 0:22c544c8741a 184 typedef enum
simonscott 0:22c544c8741a 185 {
simonscott 0:22c544c8741a 186 POWER_MODE_NORMAL = 0X00,
simonscott 0:22c544c8741a 187 POWER_MODE_LOWPOWER = 0X01,
simonscott 0:22c544c8741a 188 POWER_MODE_SUSPEND = 0X02
simonscott 0:22c544c8741a 189 } adafruit_bno055_powermode_t;
simonscott 0:22c544c8741a 190
simonscott 0:22c544c8741a 191 typedef enum
simonscott 0:22c544c8741a 192 {
simonscott 0:22c544c8741a 193 /* Operation mode settings*/
simonscott 0:22c544c8741a 194 OPERATION_MODE_CONFIG = 0X00,
simonscott 0:22c544c8741a 195 OPERATION_MODE_ACCONLY = 0X01,
simonscott 0:22c544c8741a 196 OPERATION_MODE_MAGONLY = 0X02,
simonscott 0:22c544c8741a 197 OPERATION_MODE_GYRONLY = 0X03,
simonscott 0:22c544c8741a 198 OPERATION_MODE_ACCMAG = 0X04,
simonscott 0:22c544c8741a 199 OPERATION_MODE_ACCGYRO = 0X05,
simonscott 0:22c544c8741a 200 OPERATION_MODE_MAGGYRO = 0X06,
simonscott 0:22c544c8741a 201 OPERATION_MODE_AMG = 0X07,
simonscott 0:22c544c8741a 202 OPERATION_MODE_IMUPLUS = 0X08,
simonscott 0:22c544c8741a 203 OPERATION_MODE_COMPASS = 0X09,
simonscott 0:22c544c8741a 204 OPERATION_MODE_M4G = 0X0A,
simonscott 0:22c544c8741a 205 OPERATION_MODE_NDOF_FMC_OFF = 0X0B,
simonscott 0:22c544c8741a 206 OPERATION_MODE_NDOF = 0X0C
simonscott 0:22c544c8741a 207 } adafruit_bno055_opmode_t;
simonscott 0:22c544c8741a 208
simonscott 0:22c544c8741a 209 typedef struct
simonscott 0:22c544c8741a 210 {
simonscott 0:22c544c8741a 211 uint8_t accel_rev;
simonscott 0:22c544c8741a 212 uint8_t mag_rev;
simonscott 0:22c544c8741a 213 uint8_t gyro_rev;
simonscott 0:22c544c8741a 214 uint16_t sw_rev;
simonscott 0:22c544c8741a 215 uint8_t bl_rev;
simonscott 0:22c544c8741a 216 } adafruit_bno055_rev_info_t;
simonscott 0:22c544c8741a 217
simonscott 0:22c544c8741a 218 typedef enum
simonscott 0:22c544c8741a 219 {
simonscott 0:22c544c8741a 220 VECTOR_ACCELEROMETER = BNO055_ACCEL_DATA_X_LSB_ADDR,
simonscott 0:22c544c8741a 221 VECTOR_MAGNETOMETER = BNO055_MAG_DATA_X_LSB_ADDR,
simonscott 0:22c544c8741a 222 VECTOR_GYROSCOPE = BNO055_GYRO_DATA_X_LSB_ADDR,
simonscott 0:22c544c8741a 223 VECTOR_EULER = BNO055_EULER_H_LSB_ADDR,
simonscott 0:22c544c8741a 224 VECTOR_LINEARACCEL = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR,
simonscott 0:22c544c8741a 225 VECTOR_GRAVITY = BNO055_GRAVITY_DATA_X_LSB_ADDR
simonscott 0:22c544c8741a 226 } adafruit_vector_type_t;
simonscott 0:22c544c8741a 227
simonscott 0:22c544c8741a 228 Adafruit_BNO055 ( int32_t sensorID = -1, uint8_t address = BNO055_ADDRESS_A, I2C* i2c_ptr = 0 );
simonscott 0:22c544c8741a 229
simonscott 0:22c544c8741a 230 bool begin ( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF );
simonscott 0:22c544c8741a 231 void setMode ( adafruit_bno055_opmode_t mode );
simonscott 0:22c544c8741a 232 void getRevInfo ( adafruit_bno055_rev_info_t* );
simonscott 0:22c544c8741a 233 void displayRevInfo ( void );
simonscott 0:22c544c8741a 234 void setExtCrystalUse ( bool usextal );
simonscott 0:22c544c8741a 235 void getSystemStatus ( uint8_t *system_status,
simonscott 0:22c544c8741a 236 uint8_t *self_test_result,
simonscott 0:22c544c8741a 237 uint8_t *system_error);
simonscott 0:22c544c8741a 238 void displaySystemStatus ( void );
simonscott 0:22c544c8741a 239 void getCalibration ( uint8_t* system, uint8_t* gyro, uint8_t* accel, uint8_t* mag);
simonscott 0:22c544c8741a 240
simonscott 0:22c544c8741a 241 imu::Vector<3> getVector ( adafruit_vector_type_t vector_type );
simonscott 0:22c544c8741a 242 imu::Quaternion getQuat ( void );
simonscott 0:22c544c8741a 243 int8_t getTemp ( void );
simonscott 0:22c544c8741a 244
simonscott 0:22c544c8741a 245 /* Adafruit_Sensor implementation */
simonscott 0:22c544c8741a 246 bool getEvent ( sensors_event_t* );
simonscott 0:22c544c8741a 247 void getSensor ( sensor_t* );
simonscott 0:22c544c8741a 248
simonscott 0:22c544c8741a 249 private:
simonscott 0:22c544c8741a 250 char read8 ( adafruit_bno055_reg_t );
simonscott 0:22c544c8741a 251 bool readLen ( adafruit_bno055_reg_t, char* buffer, int len );
simonscott 0:22c544c8741a 252 bool write8 ( adafruit_bno055_reg_t, char value );
simonscott 0:22c544c8741a 253
simonscott 0:22c544c8741a 254 uint8_t _address;
simonscott 0:22c544c8741a 255 int32_t _sensorID;
simonscott 0:22c544c8741a 256 adafruit_bno055_opmode_t _mode;
simonscott 0:22c544c8741a 257 I2C* i2c;
simonscott 0:22c544c8741a 258 };
simonscott 0:22c544c8741a 259
simonscott 0:22c544c8741a 260 #endif