adafruit_bno055 / Adafruit_BNO055.h @ 05b57936
History | View | Annotate | Download (10.173 KB)
1 |
/*!
|
---|---|
2 |
* @file Adafruit_BNO055.h
|
3 |
*
|
4 |
* This is a library for the BNO055 orientation sensor
|
5 |
*
|
6 |
* Designed specifically to work with the Adafruit BNO055 Breakout.
|
7 |
*
|
8 |
* Pick one up today in the adafruit shop!
|
9 |
* ------> https://www.adafruit.com/product/2472
|
10 |
*
|
11 |
* These sensors use I2C to communicate, 2 pins are required to interface.
|
12 |
*
|
13 |
* Adafruit invests time and resources providing this open source code,
|
14 |
* please support Adafruit andopen-source hardware by purchasing products
|
15 |
* from Adafruit!
|
16 |
*
|
17 |
* K.Townsend (Adafruit Industries)
|
18 |
*
|
19 |
* MIT license, all text above must be included in any redistribution
|
20 |
*/
|
21 |
|
22 |
#ifndef __ADAFRUIT_BNO055_H__
|
23 |
#define __ADAFRUIT_BNO055_H__
|
24 |
|
25 |
#include "Arduino.h" |
26 |
#include <Wire.h> |
27 |
|
28 |
#include <Adafruit_Sensor.h> |
29 |
#include "utility/imumaths.h" |
30 |
|
31 |
/** BNO055 Address A **/
|
32 |
#define BNO055_ADDRESS_A (0x28) |
33 |
/** BNO055 Address B **/
|
34 |
#define BNO055_ADDRESS_B (0x29) |
35 |
/** BNO055 ID **/
|
36 |
#define BNO055_ID (0xA0) |
37 |
|
38 |
/** Offsets registers **/
|
39 |
#define NUM_BNO055_OFFSET_REGISTERS (22) |
40 |
|
41 |
/** A structure to represent offsets **/
|
42 |
typedef struct { |
43 |
int16_t accel_offset_x; /**< x acceleration offset */
|
44 |
int16_t accel_offset_y; /**< y acceleration offset */
|
45 |
int16_t accel_offset_z; /**< z acceleration offset */
|
46 |
|
47 |
int16_t mag_offset_x; /**< x magnetometer offset */
|
48 |
int16_t mag_offset_y; /**< y magnetometer offset */
|
49 |
int16_t mag_offset_z; /**< z magnetometer offset */
|
50 |
|
51 |
int16_t gyro_offset_x; /**< x gyroscrope offset */
|
52 |
int16_t gyro_offset_y; /**< y gyroscrope offset */
|
53 |
int16_t gyro_offset_z; /**< z gyroscrope offset */
|
54 |
|
55 |
int16_t accel_radius; /**< acceleration radius */
|
56 |
|
57 |
int16_t mag_radius; /**< magnetometer radius */
|
58 |
} adafruit_bno055_offsets_t; |
59 |
|
60 |
/*!
|
61 |
* @brief Class that stores state and functions for interacting with
|
62 |
* BNO055 Sensor
|
63 |
*/
|
64 |
class Adafruit_BNO055 : public Adafruit_Sensor { |
65 |
public:
|
66 |
/** BNO055 Registers **/
|
67 |
typedef enum { |
68 |
/* Page id register definition */
|
69 |
BNO055_PAGE_ID_ADDR = 0X07,
|
70 |
|
71 |
/* PAGE0 REGISTER DEFINITION START*/
|
72 |
BNO055_CHIP_ID_ADDR = 0x00,
|
73 |
BNO055_ACCEL_REV_ID_ADDR = 0x01,
|
74 |
BNO055_MAG_REV_ID_ADDR = 0x02,
|
75 |
BNO055_GYRO_REV_ID_ADDR = 0x03,
|
76 |
BNO055_SW_REV_ID_LSB_ADDR = 0x04,
|
77 |
BNO055_SW_REV_ID_MSB_ADDR = 0x05,
|
78 |
BNO055_BL_REV_ID_ADDR = 0X06,
|
79 |
|
80 |
/* Accel data register */
|
81 |
BNO055_ACCEL_DATA_X_LSB_ADDR = 0X08,
|
82 |
BNO055_ACCEL_DATA_X_MSB_ADDR = 0X09,
|
83 |
BNO055_ACCEL_DATA_Y_LSB_ADDR = 0X0A,
|
84 |
BNO055_ACCEL_DATA_Y_MSB_ADDR = 0X0B,
|
85 |
BNO055_ACCEL_DATA_Z_LSB_ADDR = 0X0C,
|
86 |
BNO055_ACCEL_DATA_Z_MSB_ADDR = 0X0D,
|
87 |
|
88 |
/* Mag data register */
|
89 |
BNO055_MAG_DATA_X_LSB_ADDR = 0X0E,
|
90 |
BNO055_MAG_DATA_X_MSB_ADDR = 0X0F,
|
91 |
BNO055_MAG_DATA_Y_LSB_ADDR = 0X10,
|
92 |
BNO055_MAG_DATA_Y_MSB_ADDR = 0X11,
|
93 |
BNO055_MAG_DATA_Z_LSB_ADDR = 0X12,
|
94 |
BNO055_MAG_DATA_Z_MSB_ADDR = 0X13,
|
95 |
|
96 |
/* Gyro data registers */
|
97 |
BNO055_GYRO_DATA_X_LSB_ADDR = 0X14,
|
98 |
BNO055_GYRO_DATA_X_MSB_ADDR = 0X15,
|
99 |
BNO055_GYRO_DATA_Y_LSB_ADDR = 0X16,
|
100 |
BNO055_GYRO_DATA_Y_MSB_ADDR = 0X17,
|
101 |
BNO055_GYRO_DATA_Z_LSB_ADDR = 0X18,
|
102 |
BNO055_GYRO_DATA_Z_MSB_ADDR = 0X19,
|
103 |
|
104 |
/* Euler data registers */
|
105 |
BNO055_EULER_H_LSB_ADDR = 0X1A,
|
106 |
BNO055_EULER_H_MSB_ADDR = 0X1B,
|
107 |
BNO055_EULER_R_LSB_ADDR = 0X1C,
|
108 |
BNO055_EULER_R_MSB_ADDR = 0X1D,
|
109 |
BNO055_EULER_P_LSB_ADDR = 0X1E,
|
110 |
BNO055_EULER_P_MSB_ADDR = 0X1F,
|
111 |
|
112 |
/* Quaternion data registers */
|
113 |
BNO055_QUATERNION_DATA_W_LSB_ADDR = 0X20,
|
114 |
BNO055_QUATERNION_DATA_W_MSB_ADDR = 0X21,
|
115 |
BNO055_QUATERNION_DATA_X_LSB_ADDR = 0X22,
|
116 |
BNO055_QUATERNION_DATA_X_MSB_ADDR = 0X23,
|
117 |
BNO055_QUATERNION_DATA_Y_LSB_ADDR = 0X24,
|
118 |
BNO055_QUATERNION_DATA_Y_MSB_ADDR = 0X25,
|
119 |
BNO055_QUATERNION_DATA_Z_LSB_ADDR = 0X26,
|
120 |
BNO055_QUATERNION_DATA_Z_MSB_ADDR = 0X27,
|
121 |
|
122 |
/* Linear acceleration data registers */
|
123 |
BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR = 0X28,
|
124 |
BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR = 0X29,
|
125 |
BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR = 0X2A,
|
126 |
BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR = 0X2B,
|
127 |
BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR = 0X2C,
|
128 |
BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR = 0X2D,
|
129 |
|
130 |
/* Gravity data registers */
|
131 |
BNO055_GRAVITY_DATA_X_LSB_ADDR = 0X2E,
|
132 |
BNO055_GRAVITY_DATA_X_MSB_ADDR = 0X2F,
|
133 |
BNO055_GRAVITY_DATA_Y_LSB_ADDR = 0X30,
|
134 |
BNO055_GRAVITY_DATA_Y_MSB_ADDR = 0X31,
|
135 |
BNO055_GRAVITY_DATA_Z_LSB_ADDR = 0X32,
|
136 |
BNO055_GRAVITY_DATA_Z_MSB_ADDR = 0X33,
|
137 |
|
138 |
/* Temperature data register */
|
139 |
BNO055_TEMP_ADDR = 0X34,
|
140 |
|
141 |
/* Status registers */
|
142 |
BNO055_CALIB_STAT_ADDR = 0X35,
|
143 |
BNO055_SELFTEST_RESULT_ADDR = 0X36,
|
144 |
BNO055_INTR_STAT_ADDR = 0X37,
|
145 |
|
146 |
BNO055_SYS_CLK_STAT_ADDR = 0X38,
|
147 |
BNO055_SYS_STAT_ADDR = 0X39,
|
148 |
BNO055_SYS_ERR_ADDR = 0X3A,
|
149 |
|
150 |
/* Unit selection register */
|
151 |
BNO055_UNIT_SEL_ADDR = 0X3B,
|
152 |
BNO055_DATA_SELECT_ADDR = 0X3C,
|
153 |
|
154 |
/* Mode registers */
|
155 |
BNO055_OPR_MODE_ADDR = 0X3D,
|
156 |
BNO055_PWR_MODE_ADDR = 0X3E,
|
157 |
|
158 |
BNO055_SYS_TRIGGER_ADDR = 0X3F,
|
159 |
BNO055_TEMP_SOURCE_ADDR = 0X40,
|
160 |
|
161 |
/* Axis remap registers */
|
162 |
BNO055_AXIS_MAP_CONFIG_ADDR = 0X41,
|
163 |
BNO055_AXIS_MAP_SIGN_ADDR = 0X42,
|
164 |
|
165 |
/* SIC registers */
|
166 |
BNO055_SIC_MATRIX_0_LSB_ADDR = 0X43,
|
167 |
BNO055_SIC_MATRIX_0_MSB_ADDR = 0X44,
|
168 |
BNO055_SIC_MATRIX_1_LSB_ADDR = 0X45,
|
169 |
BNO055_SIC_MATRIX_1_MSB_ADDR = 0X46,
|
170 |
BNO055_SIC_MATRIX_2_LSB_ADDR = 0X47,
|
171 |
BNO055_SIC_MATRIX_2_MSB_ADDR = 0X48,
|
172 |
BNO055_SIC_MATRIX_3_LSB_ADDR = 0X49,
|
173 |
BNO055_SIC_MATRIX_3_MSB_ADDR = 0X4A,
|
174 |
BNO055_SIC_MATRIX_4_LSB_ADDR = 0X4B,
|
175 |
BNO055_SIC_MATRIX_4_MSB_ADDR = 0X4C,
|
176 |
BNO055_SIC_MATRIX_5_LSB_ADDR = 0X4D,
|
177 |
BNO055_SIC_MATRIX_5_MSB_ADDR = 0X4E,
|
178 |
BNO055_SIC_MATRIX_6_LSB_ADDR = 0X4F,
|
179 |
BNO055_SIC_MATRIX_6_MSB_ADDR = 0X50,
|
180 |
BNO055_SIC_MATRIX_7_LSB_ADDR = 0X51,
|
181 |
BNO055_SIC_MATRIX_7_MSB_ADDR = 0X52,
|
182 |
BNO055_SIC_MATRIX_8_LSB_ADDR = 0X53,
|
183 |
BNO055_SIC_MATRIX_8_MSB_ADDR = 0X54,
|
184 |
|
185 |
/* Accelerometer Offset registers */
|
186 |
ACCEL_OFFSET_X_LSB_ADDR = 0X55,
|
187 |
ACCEL_OFFSET_X_MSB_ADDR = 0X56,
|
188 |
ACCEL_OFFSET_Y_LSB_ADDR = 0X57,
|
189 |
ACCEL_OFFSET_Y_MSB_ADDR = 0X58,
|
190 |
ACCEL_OFFSET_Z_LSB_ADDR = 0X59,
|
191 |
ACCEL_OFFSET_Z_MSB_ADDR = 0X5A,
|
192 |
|
193 |
/* Magnetometer Offset registers */
|
194 |
MAG_OFFSET_X_LSB_ADDR = 0X5B,
|
195 |
MAG_OFFSET_X_MSB_ADDR = 0X5C,
|
196 |
MAG_OFFSET_Y_LSB_ADDR = 0X5D,
|
197 |
MAG_OFFSET_Y_MSB_ADDR = 0X5E,
|
198 |
MAG_OFFSET_Z_LSB_ADDR = 0X5F,
|
199 |
MAG_OFFSET_Z_MSB_ADDR = 0X60,
|
200 |
|
201 |
/* Gyroscope Offset register s*/
|
202 |
GYRO_OFFSET_X_LSB_ADDR = 0X61,
|
203 |
GYRO_OFFSET_X_MSB_ADDR = 0X62,
|
204 |
GYRO_OFFSET_Y_LSB_ADDR = 0X63,
|
205 |
GYRO_OFFSET_Y_MSB_ADDR = 0X64,
|
206 |
GYRO_OFFSET_Z_LSB_ADDR = 0X65,
|
207 |
GYRO_OFFSET_Z_MSB_ADDR = 0X66,
|
208 |
|
209 |
/* Radius registers */
|
210 |
ACCEL_RADIUS_LSB_ADDR = 0X67,
|
211 |
ACCEL_RADIUS_MSB_ADDR = 0X68,
|
212 |
MAG_RADIUS_LSB_ADDR = 0X69,
|
213 |
MAG_RADIUS_MSB_ADDR = 0X6A
|
214 |
} adafruit_bno055_reg_t; |
215 |
|
216 |
/** BNO055 power settings */
|
217 |
typedef enum { |
218 |
POWER_MODE_NORMAL = 0X00,
|
219 |
POWER_MODE_LOWPOWER = 0X01,
|
220 |
POWER_MODE_SUSPEND = 0X02
|
221 |
} adafruit_bno055_powermode_t; |
222 |
|
223 |
/** Operation mode settings **/
|
224 |
typedef enum { |
225 |
OPERATION_MODE_CONFIG = 0X00,
|
226 |
OPERATION_MODE_ACCONLY = 0X01,
|
227 |
OPERATION_MODE_MAGONLY = 0X02,
|
228 |
OPERATION_MODE_GYRONLY = 0X03,
|
229 |
OPERATION_MODE_ACCMAG = 0X04,
|
230 |
OPERATION_MODE_ACCGYRO = 0X05,
|
231 |
OPERATION_MODE_MAGGYRO = 0X06,
|
232 |
OPERATION_MODE_AMG = 0X07,
|
233 |
OPERATION_MODE_IMUPLUS = 0X08,
|
234 |
OPERATION_MODE_COMPASS = 0X09,
|
235 |
OPERATION_MODE_M4G = 0X0A,
|
236 |
OPERATION_MODE_NDOF_FMC_OFF = 0X0B,
|
237 |
OPERATION_MODE_NDOF = 0X0C
|
238 |
} adafruit_bno055_opmode_t; |
239 |
|
240 |
/** Remap settings **/
|
241 |
typedef enum { |
242 |
REMAP_CONFIG_P0 = 0x21,
|
243 |
REMAP_CONFIG_P1 = 0x24, // default |
244 |
REMAP_CONFIG_P2 = 0x24,
|
245 |
REMAP_CONFIG_P3 = 0x21,
|
246 |
REMAP_CONFIG_P4 = 0x24,
|
247 |
REMAP_CONFIG_P5 = 0x21,
|
248 |
REMAP_CONFIG_P6 = 0x21,
|
249 |
REMAP_CONFIG_P7 = 0x24
|
250 |
} adafruit_bno055_axis_remap_config_t; |
251 |
|
252 |
/** Remap Signs **/
|
253 |
typedef enum { |
254 |
REMAP_SIGN_P0 = 0x04,
|
255 |
REMAP_SIGN_P1 = 0x00, // default |
256 |
REMAP_SIGN_P2 = 0x06,
|
257 |
REMAP_SIGN_P3 = 0x02,
|
258 |
REMAP_SIGN_P4 = 0x03,
|
259 |
REMAP_SIGN_P5 = 0x01,
|
260 |
REMAP_SIGN_P6 = 0x07,
|
261 |
REMAP_SIGN_P7 = 0x05
|
262 |
} adafruit_bno055_axis_remap_sign_t; |
263 |
|
264 |
/** A structure to represent revisions **/
|
265 |
typedef struct { |
266 |
uint8_t accel_rev; /**< acceleration rev */
|
267 |
uint8_t mag_rev; /**< magnetometer rev */
|
268 |
uint8_t gyro_rev; /**< gyroscrope rev */
|
269 |
uint16_t sw_rev; /**< SW rev */
|
270 |
uint8_t bl_rev; /**< bootloader rev */
|
271 |
} adafruit_bno055_rev_info_t; |
272 |
|
273 |
/** Vector Mappings **/
|
274 |
typedef enum { |
275 |
VECTOR_ACCELEROMETER = BNO055_ACCEL_DATA_X_LSB_ADDR, |
276 |
VECTOR_MAGNETOMETER = BNO055_MAG_DATA_X_LSB_ADDR, |
277 |
VECTOR_GYROSCOPE = BNO055_GYRO_DATA_X_LSB_ADDR, |
278 |
VECTOR_EULER = BNO055_EULER_H_LSB_ADDR, |
279 |
VECTOR_LINEARACCEL = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR, |
280 |
VECTOR_GRAVITY = BNO055_GRAVITY_DATA_X_LSB_ADDR |
281 |
} adafruit_vector_type_t; |
282 |
|
283 |
Adafruit_BNO055(int32_t sensorID = -1, uint8_t address = BNO055_ADDRESS_A,
|
284 |
TwoWire *theWire = &Wire); |
285 |
|
286 |
bool begin(adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF);
|
287 |
void setMode(adafruit_bno055_opmode_t mode);
|
288 |
void setAxisRemap(adafruit_bno055_axis_remap_config_t remapcode);
|
289 |
void setAxisSign(adafruit_bno055_axis_remap_sign_t remapsign);
|
290 |
void getRevInfo(adafruit_bno055_rev_info_t *);
|
291 |
void setExtCrystalUse(boolean usextal);
|
292 |
void getSystemStatus(uint8_t *system_status, uint8_t *self_test_result,
|
293 |
uint8_t *system_error); |
294 |
void getCalibration(uint8_t *system, uint8_t *gyro, uint8_t *accel,
|
295 |
uint8_t *mag); |
296 |
|
297 |
imu::Vector<3> getVector(adafruit_vector_type_t vector_type);
|
298 |
imu::Quaternion getQuat(); |
299 |
int8_t getTemp(); |
300 |
|
301 |
/* Adafruit_Sensor implementation */
|
302 |
bool getEvent(sensors_event_t *);
|
303 |
bool getEvent(sensors_event_t *, adafruit_vector_type_t);
|
304 |
void getSensor(sensor_t *);
|
305 |
|
306 |
/* Functions to deal with raw calibration data */
|
307 |
bool getSensorOffsets(uint8_t *calibData);
|
308 |
bool getSensorOffsets(adafruit_bno055_offsets_t &offsets_type);
|
309 |
void setSensorOffsets(const uint8_t *calibData); |
310 |
void setSensorOffsets(const adafruit_bno055_offsets_t &offsets_type); |
311 |
bool isFullyCalibrated();
|
312 |
|
313 |
/* Power managments functions */
|
314 |
void enterSuspendMode();
|
315 |
void enterNormalMode();
|
316 |
|
317 |
private:
|
318 |
byte read8(adafruit_bno055_reg_t); |
319 |
bool readLen(adafruit_bno055_reg_t, byte *buffer, uint8_t len);
|
320 |
bool write8(adafruit_bno055_reg_t, byte value);
|
321 |
|
322 |
uint8_t _address; |
323 |
TwoWire *_wire; |
324 |
|
325 |
int32_t _sensorID; |
326 |
adafruit_bno055_opmode_t _mode; |
327 |
}; |
328 |
|
329 |
#endif
|