Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.h @ 60c8690f

History | View | Annotate | Download (14.421 KB)

1
/***************************************************************************
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
  ------> http://www.adafruit.com/products
8

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
 ***************************************************************************/
19

    
20
#ifndef __ADAFRUIT_BNO055_H__
21
#define __ADAFRUIT_BNO055_H__
22

    
23
#if (ARDUINO >= 100)
24
 #include "Arduino.h"
25
#else
26
 #include "WProgram.h"
27
#endif
28

    
29
#ifdef __AVR_ATtiny85__
30
 #include <TinyWireM.h>
31
 #define Wire TinyWireM
32
#else
33
 #include <Wire.h>
34
#endif
35

    
36
#include <Adafruit_Sensor.h>
37
#include <utility/imumaths.h>
38

    
39
#define BNO055_ADDRESS_A (0x28)
40
#define BNO055_ADDRESS_B (0x29)
41
#define BNO055_ID        (0xA0)
42

    
43
#define NUM_BNO055_OFFSET_REGISTERS (22)
44

    
45
typedef struct
46
{
47
    uint16_t accel_offset_x;
48
    uint16_t accel_offset_y;
49
    uint16_t accel_offset_z;
50
    uint16_t gyro_offset_x;
51
    uint16_t gyro_offset_y;
52
    uint16_t gyro_offset_z;
53
    uint16_t mag_offset_x;
54
    uint16_t mag_offset_y;
55
    uint16_t mag_offset_z;
56

    
57
    uint16_t accel_radius;
58
    uint16_t mag_radius;
59
} adafruit_bno055_offsets_t;
60

    
61
class Adafruit_BNO055 : public Adafruit_Sensor
62
{
63
  public:
64
    typedef enum
65
    {
66
      /* Page id register definition */
67
      BNO055_PAGE_ID_ADDR                                     = 0X07,
68

    
69
      /* PAGE0 REGISTER DEFINITION START*/
70
      BNO055_CHIP_ID_ADDR                                     = 0x00,
71
      BNO055_ACCEL_REV_ID_ADDR                                = 0x01,
72
      BNO055_MAG_REV_ID_ADDR                                  = 0x02,
73
      BNO055_GYRO_REV_ID_ADDR                                 = 0x03,
74
      BNO055_SW_REV_ID_LSB_ADDR                               = 0x04,
75
      BNO055_SW_REV_ID_MSB_ADDR                               = 0x05,
76
      BNO055_BL_REV_ID_ADDR                                   = 0X06,
77

    
78
      /* Accel data register */
79
      BNO055_ACCEL_DATA_X_LSB_ADDR                            = 0X08,
80
      BNO055_ACCEL_DATA_X_MSB_ADDR                            = 0X09,
81
      BNO055_ACCEL_DATA_Y_LSB_ADDR                            = 0X0A,
82
      BNO055_ACCEL_DATA_Y_MSB_ADDR                            = 0X0B,
83
      BNO055_ACCEL_DATA_Z_LSB_ADDR                            = 0X0C,
84
      BNO055_ACCEL_DATA_Z_MSB_ADDR                            = 0X0D,
85

    
86
      /* Mag data register */
87
      BNO055_MAG_DATA_X_LSB_ADDR                              = 0X0E,
88
      BNO055_MAG_DATA_X_MSB_ADDR                              = 0X0F,
89
      BNO055_MAG_DATA_Y_LSB_ADDR                              = 0X10,
90
      BNO055_MAG_DATA_Y_MSB_ADDR                              = 0X11,
91
      BNO055_MAG_DATA_Z_LSB_ADDR                              = 0X12,
92
      BNO055_MAG_DATA_Z_MSB_ADDR                              = 0X13,
93

    
94
      /* Gyro data registers */
95
      BNO055_GYRO_DATA_X_LSB_ADDR                             = 0X14,
96
      BNO055_GYRO_DATA_X_MSB_ADDR                             = 0X15,
97
      BNO055_GYRO_DATA_Y_LSB_ADDR                             = 0X16,
98
      BNO055_GYRO_DATA_Y_MSB_ADDR                             = 0X17,
99
      BNO055_GYRO_DATA_Z_LSB_ADDR                             = 0X18,
100
      BNO055_GYRO_DATA_Z_MSB_ADDR                             = 0X19,
101

    
102
      /* Euler data registers */
103
      BNO055_EULER_H_LSB_ADDR                                 = 0X1A,
104
      BNO055_EULER_H_MSB_ADDR                                 = 0X1B,
105
      BNO055_EULER_R_LSB_ADDR                                 = 0X1C,
106
      BNO055_EULER_R_MSB_ADDR                                 = 0X1D,
107
      BNO055_EULER_P_LSB_ADDR                                 = 0X1E,
108
      BNO055_EULER_P_MSB_ADDR                                 = 0X1F,
109

    
110
      /* Quaternion data registers */
111
      BNO055_QUATERNION_DATA_W_LSB_ADDR                       = 0X20,
112
      BNO055_QUATERNION_DATA_W_MSB_ADDR                       = 0X21,
113
      BNO055_QUATERNION_DATA_X_LSB_ADDR                       = 0X22,
114
      BNO055_QUATERNION_DATA_X_MSB_ADDR                       = 0X23,
115
      BNO055_QUATERNION_DATA_Y_LSB_ADDR                       = 0X24,
116
      BNO055_QUATERNION_DATA_Y_MSB_ADDR                       = 0X25,
117
      BNO055_QUATERNION_DATA_Z_LSB_ADDR                       = 0X26,
118
      BNO055_QUATERNION_DATA_Z_MSB_ADDR                       = 0X27,
119

    
120
      /* Linear acceleration data registers */
121
      BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR                     = 0X28,
122
      BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR                     = 0X29,
123
      BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR                     = 0X2A,
124
      BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR                     = 0X2B,
125
      BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR                     = 0X2C,
126
      BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR                     = 0X2D,
127

    
128
      /* Gravity data registers */
129
      BNO055_GRAVITY_DATA_X_LSB_ADDR                          = 0X2E,
130
      BNO055_GRAVITY_DATA_X_MSB_ADDR                          = 0X2F,
131
      BNO055_GRAVITY_DATA_Y_LSB_ADDR                          = 0X30,
132
      BNO055_GRAVITY_DATA_Y_MSB_ADDR                          = 0X31,
133
      BNO055_GRAVITY_DATA_Z_LSB_ADDR                          = 0X32,
134
      BNO055_GRAVITY_DATA_Z_MSB_ADDR                          = 0X33,
135

    
136
      /* Temperature data register */
137
      BNO055_TEMP_ADDR                                        = 0X34,
138

    
139
      /* Status registers */
140
      BNO055_CALIB_STAT_ADDR                                  = 0X35,
141
      BNO055_SELFTEST_RESULT_ADDR                             = 0X36,
142
      BNO055_INTR_STAT_ADDR                                   = 0X37,
143

    
144
      BNO055_SYS_CLK_STAT_ADDR                                = 0X38,
145
      BNO055_SYS_STAT_ADDR                                    = 0X39,
146
      BNO055_SYS_ERR_ADDR                                     = 0X3A,
147

    
148
      /* Unit selection register */
149
      BNO055_UNIT_SEL_ADDR                                    = 0X3B,
150
      BNO055_DATA_SELECT_ADDR                                 = 0X3C,
151

    
152
      /* Mode registers */
153
      BNO055_OPR_MODE_ADDR                                    = 0X3D,
154
      BNO055_PWR_MODE_ADDR                                    = 0X3E,
155

    
156
      BNO055_SYS_TRIGGER_ADDR                                 = 0X3F,
157
      BNO055_TEMP_SOURCE_ADDR                                 = 0X40,
158

    
159
      /* Axis remap registers */
160
      BNO055_AXIS_MAP_CONFIG_ADDR                             = 0X41,
161
      BNO055_AXIS_MAP_SIGN_ADDR                               = 0X42,
162

    
163
      /* SIC registers */
164
      BNO055_SIC_MATRIX_0_LSB_ADDR                            = 0X43,
165
      BNO055_SIC_MATRIX_0_MSB_ADDR                            = 0X44,
166
      BNO055_SIC_MATRIX_1_LSB_ADDR                            = 0X45,
167
      BNO055_SIC_MATRIX_1_MSB_ADDR                            = 0X46,
168
      BNO055_SIC_MATRIX_2_LSB_ADDR                            = 0X47,
169
      BNO055_SIC_MATRIX_2_MSB_ADDR                            = 0X48,
170
      BNO055_SIC_MATRIX_3_LSB_ADDR                            = 0X49,
171
      BNO055_SIC_MATRIX_3_MSB_ADDR                            = 0X4A,
172
      BNO055_SIC_MATRIX_4_LSB_ADDR                            = 0X4B,
173
      BNO055_SIC_MATRIX_4_MSB_ADDR                            = 0X4C,
174
      BNO055_SIC_MATRIX_5_LSB_ADDR                            = 0X4D,
175
      BNO055_SIC_MATRIX_5_MSB_ADDR                            = 0X4E,
176
      BNO055_SIC_MATRIX_6_LSB_ADDR                            = 0X4F,
177
      BNO055_SIC_MATRIX_6_MSB_ADDR                            = 0X50,
178
      BNO055_SIC_MATRIX_7_LSB_ADDR                            = 0X51,
179
      BNO055_SIC_MATRIX_7_MSB_ADDR                            = 0X52,
180
      BNO055_SIC_MATRIX_8_LSB_ADDR                            = 0X53,
181
      BNO055_SIC_MATRIX_8_MSB_ADDR                            = 0X54,
182

    
183
      /* Accelerometer Offset registers */
184
      ACCEL_OFFSET_X_LSB_ADDR                                 = 0X55,
185
      ACCEL_OFFSET_X_MSB_ADDR                                 = 0X56,
186
      ACCEL_OFFSET_Y_LSB_ADDR                                 = 0X57,
187
      ACCEL_OFFSET_Y_MSB_ADDR                                 = 0X58,
188
      ACCEL_OFFSET_Z_LSB_ADDR                                 = 0X59,
189
      ACCEL_OFFSET_Z_MSB_ADDR                                 = 0X5A,
190

    
191
      /* Magnetometer Offset registers */
192
      MAG_OFFSET_X_LSB_ADDR                                   = 0X5B,
193
      MAG_OFFSET_X_MSB_ADDR                                   = 0X5C,
194
      MAG_OFFSET_Y_LSB_ADDR                                   = 0X5D,
195
      MAG_OFFSET_Y_MSB_ADDR                                   = 0X5E,
196
      MAG_OFFSET_Z_LSB_ADDR                                   = 0X5F,
197
      MAG_OFFSET_Z_MSB_ADDR                                   = 0X60,
198

    
199
      /* Gyroscope Offset register s*/
200
      GYRO_OFFSET_X_LSB_ADDR                                  = 0X61,
201
      GYRO_OFFSET_X_MSB_ADDR                                  = 0X62,
202
      GYRO_OFFSET_Y_LSB_ADDR                                  = 0X63,
203
      GYRO_OFFSET_Y_MSB_ADDR                                  = 0X64,
204
      GYRO_OFFSET_Z_LSB_ADDR                                  = 0X65,
205
      GYRO_OFFSET_Z_MSB_ADDR                                  = 0X66,
206

    
207
      /* Radius registers */
208
      ACCEL_RADIUS_LSB_ADDR                                   = 0X67,
209
      ACCEL_RADIUS_MSB_ADDR                                   = 0X68,
210
      MAG_RADIUS_LSB_ADDR                                     = 0X69,
211
      MAG_RADIUS_MSB_ADDR                                     = 0X6A
212
    } adafruit_bno055_reg_t;
213

    
214
    typedef enum
215
    {
216
      POWER_MODE_NORMAL                                       = 0X00,
217
      POWER_MODE_LOWPOWER                                     = 0X01,
218
      POWER_MODE_SUSPEND                                      = 0X02
219
    } adafruit_bno055_powermode_t;
220

    
221
    typedef enum
222
    {
223
      /* Operation mode settings*/
224
      OPERATION_MODE_CONFIG                                   = 0X00,
225
      OPERATION_MODE_ACCONLY                                  = 0X01,
226
      OPERATION_MODE_MAGONLY                                  = 0X02,
227
      OPERATION_MODE_GYRONLY                                  = 0X03,
228
      OPERATION_MODE_ACCMAG                                   = 0X04,
229
      OPERATION_MODE_ACCGYRO                                  = 0X05,
230
      OPERATION_MODE_MAGGYRO                                  = 0X06,
231
      OPERATION_MODE_AMG                                      = 0X07,
232
      OPERATION_MODE_IMUPLUS                                  = 0X08,
233
      OPERATION_MODE_COMPASS                                  = 0X09,
234
      OPERATION_MODE_M4G                                      = 0X0A,
235
      OPERATION_MODE_NDOF_FMC_OFF                             = 0X0B,
236
      OPERATION_MODE_NDOF                                     = 0X0C
237
    } adafruit_bno055_opmode_t;
238

    
239
    typedef enum
240
    {
241
      REMAP_CONFIG_P0                                         = 0x21,
242
      REMAP_CONFIG_P1                                         = 0x24, // default
243
      REMAP_CONFIG_P2                                         = 0x24,
244
      REMAP_CONFIG_P3                                         = 0x21,
245
      REMAP_CONFIG_P4                                         = 0x24,
246
      REMAP_CONFIG_P5                                         = 0x21,
247
      REMAP_CONFIG_P6                                         = 0x21,
248
      REMAP_CONFIG_P7                                         = 0x24
249
    } adafruit_bno055_axis_remap_config_t;
250

    
251
    typedef enum
252
    {
253
      REMAP_SIGN_P0                                           = 0x04,
254
      REMAP_SIGN_P1                                           = 0x00, // default
255
      REMAP_SIGN_P2                                           = 0x06,
256
      REMAP_SIGN_P3                                           = 0x02,
257
      REMAP_SIGN_P4                                           = 0x03,
258
      REMAP_SIGN_P5                                           = 0x01,
259
      REMAP_SIGN_P6                                           = 0x07,
260
      REMAP_SIGN_P7                                           = 0x05
261
    } adafruit_bno055_axis_remap_sign_t;
262

    
263
    typedef struct
264
    {
265
      uint8_t  accel_rev;
266
      uint8_t  mag_rev;
267
      uint8_t  gyro_rev;
268
      uint16_t sw_rev;
269
      uint8_t  bl_rev;
270
    } adafruit_bno055_rev_info_t;
271

    
272
    typedef enum
273
    {
274
      VECTOR_ACCELEROMETER = BNO055_ACCEL_DATA_X_LSB_ADDR,
275
      VECTOR_MAGNETOMETER  = BNO055_MAG_DATA_X_LSB_ADDR,
276
      VECTOR_GYROSCOPE     = BNO055_GYRO_DATA_X_LSB_ADDR,
277
      VECTOR_EULER         = BNO055_EULER_H_LSB_ADDR,
278
      VECTOR_LINEARACCEL   = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR,
279
      VECTOR_GRAVITY       = BNO055_GRAVITY_DATA_X_LSB_ADDR
280
    } adafruit_vector_type_t;
281

    
282
#if defined (ARDUINO_SAMD_ZERO) && ! (ARDUINO_SAMD_FEATHER_M0)
283
#error "On an arduino Zero, BNO055's ADR pin must be high. Fix that, then delete this line."
284
    Adafruit_BNO055 ( int32_t sensorID = -1, uint8_t address = BNO055_ADDRESS_B );
285
#else
286
    Adafruit_BNO055 ( int32_t sensorID = -1, uint8_t address = BNO055_ADDRESS_A );
287
#endif
288
    bool  begin               ( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF );
289
    void  setMode             ( adafruit_bno055_opmode_t mode );
290
    void  getRevInfo          ( adafruit_bno055_rev_info_t* );
291
    void  displayRevInfo      ( void );
292
    void  setExtCrystalUse    ( boolean usextal );
293
    void  getSystemStatus     ( uint8_t *system_status,
294
                                uint8_t *self_test_result,
295
                                uint8_t *system_error);
296
    void  displaySystemStatus ( void );
297
    void  getCalibration      ( uint8_t* system, uint8_t* gyro, uint8_t* accel, uint8_t* mag);
298

    
299
    imu::Vector<3>  getVector ( adafruit_vector_type_t vector_type );
300
    imu::Quaternion getQuat   ( void );
301
    int8_t          getTemp   ( void );
302

    
303
    /* Adafruit_Sensor implementation */
304
    bool  getEvent  ( sensors_event_t* );
305
    void  getSensor ( sensor_t* );
306

    
307
    /* Functions to deal with raw calibration data */
308
    bool  getSensorOffsets(uint8_t* calibData);
309
    bool  getSensorOffsets(adafruit_bno055_offsets_t &offsets_type);
310
    void  setSensorOffsets(const uint8_t* calibData);
311
    void  setSensorOffsets(const adafruit_bno055_offsets_t &offsets_type);
312
    bool  isFullyCalibrated(void);
313

    
314
  private:
315
    byte  read8   ( adafruit_bno055_reg_t );
316
    bool  readLen ( adafruit_bno055_reg_t, byte* buffer, uint8_t len );
317
    bool  write8  ( adafruit_bno055_reg_t, byte value );
318

    
319
    uint8_t _address;
320
    int32_t _sensorID;
321
    adafruit_bno055_opmode_t _mode;
322
};
323

    
324
#endif