adafruit_bno055 / Adafruit_BNO055.cpp @ 8cee904f
History | View | Annotate | Download (12.968 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 |
#if ARDUINO >= 100 |
21 |
#include "Arduino.h" |
22 |
#else
|
23 |
#include "WProgram.h" |
24 |
#endif
|
25 |
|
26 |
#include <math.h> |
27 |
#include <limits.h> |
28 |
|
29 |
#include "Adafruit_BNO055.h" |
30 |
|
31 |
/***************************************************************************
|
32 |
CONSTRUCTOR
|
33 |
***************************************************************************/
|
34 |
|
35 |
/**************************************************************************/
|
36 |
/*!
|
37 |
@brief Instantiates a new Adafruit_BNO055 class
|
38 |
*/
|
39 |
/**************************************************************************/
|
40 |
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address) |
41 |
{ |
42 |
_sensorID = sensorID; |
43 |
_address = address; |
44 |
} |
45 |
|
46 |
/***************************************************************************
|
47 |
PUBLIC FUNCTIONS
|
48 |
***************************************************************************/
|
49 |
|
50 |
/**************************************************************************/
|
51 |
/*!
|
52 |
@brief Sets up the HW
|
53 |
*/
|
54 |
/**************************************************************************/
|
55 |
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode)
|
56 |
{ |
57 |
/* Enable I2C */
|
58 |
Wire.begin(); |
59 |
|
60 |
/* Make sure we have the right device */
|
61 |
uint8_t id = read8(BNO055_CHIP_ID_ADDR); |
62 |
if(id != BNO055_ID)
|
63 |
{ |
64 |
delay(1000); // hold on for boot |
65 |
id = read8(BNO055_CHIP_ID_ADDR); |
66 |
if(id != BNO055_ID) {
|
67 |
return false; // still not? ok bail |
68 |
} |
69 |
} |
70 |
|
71 |
/* Switch to config mode (just in case since this is the default) */
|
72 |
setMode(OPERATION_MODE_CONFIG); |
73 |
|
74 |
/* Reset */
|
75 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
|
76 |
while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
|
77 |
{ |
78 |
delay(10);
|
79 |
} |
80 |
delay(50);
|
81 |
|
82 |
/* Set to normal power mode */
|
83 |
write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL); |
84 |
delay(10);
|
85 |
|
86 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
87 |
|
88 |
/* Set the output units */
|
89 |
/*
|
90 |
uint8_t unitsel = (0 << 7) | // Orientation = Android
|
91 |
(0 << 4) | // Temperature = Celsius
|
92 |
(0 << 2) | // Euler = Degrees
|
93 |
(1 << 1) | // Gyro = Rads
|
94 |
(0 << 0); // Accelerometer = m/s^2
|
95 |
write8(BNO055_UNIT_SEL_ADDR, unitsel);
|
96 |
*/
|
97 |
|
98 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
|
99 |
delay(10);
|
100 |
/* Set the requested operating mode (see section 3.3) */
|
101 |
setMode(mode); |
102 |
delay(20);
|
103 |
|
104 |
return true; |
105 |
} |
106 |
|
107 |
/**************************************************************************/
|
108 |
/*!
|
109 |
@brief Puts the chip in the specified operating mode
|
110 |
*/
|
111 |
/**************************************************************************/
|
112 |
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
|
113 |
{ |
114 |
_mode = mode; |
115 |
write8(BNO055_OPR_MODE_ADDR, _mode); |
116 |
delay(30);
|
117 |
} |
118 |
|
119 |
/**************************************************************************/
|
120 |
/*!
|
121 |
@brief Use the external 32.768KHz crystal
|
122 |
*/
|
123 |
/**************************************************************************/
|
124 |
void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
|
125 |
{ |
126 |
adafruit_bno055_opmode_t modeback = _mode; |
127 |
|
128 |
/* Switch to config mode (just in case since this is the default) */
|
129 |
setMode(OPERATION_MODE_CONFIG); |
130 |
delay(25);
|
131 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
132 |
if (usextal) {
|
133 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
|
134 |
} else {
|
135 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
|
136 |
} |
137 |
delay(10);
|
138 |
/* Set the requested operating mode (see section 3.3) */
|
139 |
setMode(modeback); |
140 |
delay(20);
|
141 |
} |
142 |
|
143 |
|
144 |
/**************************************************************************/
|
145 |
/*!
|
146 |
@brief Gets the latest system status info
|
147 |
*/
|
148 |
/**************************************************************************/
|
149 |
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
|
150 |
{ |
151 |
adafruit_bno055_opmode_t backupmode = _mode; |
152 |
|
153 |
setMode(OPERATION_MODE_CONFIG); |
154 |
delay(20);
|
155 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
156 |
|
157 |
write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
|
158 |
delay(1000);
|
159 |
|
160 |
/* System Status (see section 4.3.58)
|
161 |
---------------------------------
|
162 |
0 = Idle
|
163 |
1 = System Error
|
164 |
2 = Initializing Peripherals
|
165 |
3 = System Iniitalization
|
166 |
4 = Executing Self-Test
|
167 |
5 = Sensor fusio algorithm running
|
168 |
6 = System running without fusion algorithms */
|
169 |
|
170 |
if (system_status != 0) |
171 |
*system_status = read8(BNO055_SYS_STAT_ADDR); |
172 |
|
173 |
/* Self Test Results (see section )
|
174 |
--------------------------------
|
175 |
1 = test passed, 0 = test failed
|
176 |
|
177 |
Bit 0 = Accelerometer self test
|
178 |
Bit 1 = Magnetometer self test
|
179 |
Bit 2 = Gyroscope self test
|
180 |
Bit 3 = MCU self test
|
181 |
|
182 |
0x0F = all good! */
|
183 |
|
184 |
if (self_test_result != 0) |
185 |
*self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR); |
186 |
|
187 |
/* System Error (see section 4.3.59)
|
188 |
---------------------------------
|
189 |
0 = No error
|
190 |
1 = Peripheral initialization error
|
191 |
2 = System initialization error
|
192 |
3 = Self test result failed
|
193 |
4 = Register map value out of range
|
194 |
5 = Register map address out of range
|
195 |
6 = Register map write error
|
196 |
7 = BNO low power mode not available for selected operat ion mode
|
197 |
8 = Accelerometer power mode not available
|
198 |
9 = Fusion algorithm configuration error
|
199 |
A = Sensor configuration error */
|
200 |
|
201 |
if (system_error != 0) |
202 |
*system_error = read8(BNO055_SYS_ERR_ADDR); |
203 |
|
204 |
setMode(backupmode); |
205 |
delay(20);
|
206 |
} |
207 |
|
208 |
/**************************************************************************/
|
209 |
/*!
|
210 |
@brief Gets the chip revision numbers
|
211 |
*/
|
212 |
/**************************************************************************/
|
213 |
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
|
214 |
{ |
215 |
uint8_t a, b; |
216 |
|
217 |
memset(info, 0, sizeof(adafruit_bno055_rev_info_t)); |
218 |
|
219 |
/* Check the accelerometer revision */
|
220 |
info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR); |
221 |
|
222 |
/* Check the magnetometer revision */
|
223 |
info->mag_rev = read8(BNO055_MAG_REV_ID_ADDR); |
224 |
|
225 |
/* Check the gyroscope revision */
|
226 |
info->gyro_rev = read8(BNO055_GYRO_REV_ID_ADDR); |
227 |
|
228 |
/* Check the SW revision */
|
229 |
info->bl_rev = read8(BNO055_BL_REV_ID_ADDR); |
230 |
|
231 |
a = read8(BNO055_SW_REV_ID_LSB_ADDR); |
232 |
b = read8(BNO055_SW_REV_ID_MSB_ADDR); |
233 |
info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
|
234 |
} |
235 |
|
236 |
/**************************************************************************/
|
237 |
/*!
|
238 |
@brief Gets teh temperature in degrees celsius
|
239 |
*/
|
240 |
/**************************************************************************/
|
241 |
int8_t Adafruit_BNO055::getTemp(void)
|
242 |
{ |
243 |
int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR)); |
244 |
return temp;
|
245 |
} |
246 |
|
247 |
/**************************************************************************/
|
248 |
/*!
|
249 |
@brief Gets a vector reading from the specified source
|
250 |
*/
|
251 |
/**************************************************************************/
|
252 |
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
|
253 |
{ |
254 |
imu::Vector<3> xyz;
|
255 |
uint8_t buffer[6];
|
256 |
memset (buffer, 0, 6); |
257 |
|
258 |
int16_t x, y, z; |
259 |
x = y = z = 0;
|
260 |
|
261 |
/* Read vector data (6 bytes) */
|
262 |
readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
|
263 |
|
264 |
x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8); |
265 |
y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8); |
266 |
z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8); |
267 |
|
268 |
/* Convert the value to an appropriate range (section 3.6.4) */
|
269 |
/* and assign the value to the Vector type */
|
270 |
switch(vector_type)
|
271 |
{ |
272 |
case VECTOR_MAGNETOMETER:
|
273 |
/* 1uT = 16 LSB */
|
274 |
xyz[0] = ((double)x)/16.0; |
275 |
xyz[1] = ((double)y)/16.0; |
276 |
xyz[2] = ((double)z)/16.0; |
277 |
break;
|
278 |
case VECTOR_GYROSCOPE:
|
279 |
/* 1rps = 900 LSB */
|
280 |
xyz[0] = ((double)x)/900.0; |
281 |
xyz[1] = ((double)y)/900.0; |
282 |
xyz[2] = ((double)z)/900.0; |
283 |
break;
|
284 |
case VECTOR_EULER:
|
285 |
/* 1 degree = 16 LSB */
|
286 |
xyz[0] = ((double)x)/16.0; |
287 |
xyz[1] = ((double)y)/16.0; |
288 |
xyz[2] = ((double)z)/16.0; |
289 |
break;
|
290 |
case VECTOR_ACCELEROMETER:
|
291 |
case VECTOR_LINEARACCEL:
|
292 |
case VECTOR_GRAVITY:
|
293 |
/* 1m/s^2 = 100 LSB */
|
294 |
xyz[0] = ((double)x)/100.0; |
295 |
xyz[1] = ((double)y)/100.0; |
296 |
xyz[2] = ((double)z)/100.0; |
297 |
break;
|
298 |
} |
299 |
|
300 |
return xyz;
|
301 |
} |
302 |
|
303 |
/**************************************************************************/
|
304 |
/*!
|
305 |
@brief Gets a quaternion reading from the specified source
|
306 |
*/
|
307 |
/**************************************************************************/
|
308 |
imu::Quaternion Adafruit_BNO055::getQuat(void)
|
309 |
{ |
310 |
uint8_t buffer[8];
|
311 |
memset (buffer, 0, 8); |
312 |
|
313 |
int16_t x, y, z, w; |
314 |
x = y = z = w = 0;
|
315 |
|
316 |
/* Read quat data (8 bytes) */
|
317 |
readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
|
318 |
w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]); |
319 |
x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]); |
320 |
y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]); |
321 |
z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]); |
322 |
|
323 |
/* Assign to Quaternion */
|
324 |
/* See http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
|
325 |
3.6.5.5 Orientation (Quaternion) */
|
326 |
const double scale = (1.0 / (1<<14)); |
327 |
imu::Quaternion quat(scale * w, scale * x, scale * y, scale * z); |
328 |
return quat;
|
329 |
} |
330 |
|
331 |
/**************************************************************************/
|
332 |
/*!
|
333 |
@brief Provides the sensor_t data for this sensor
|
334 |
*/
|
335 |
/**************************************************************************/
|
336 |
void Adafruit_BNO055::getSensor(sensor_t *sensor)
|
337 |
{ |
338 |
/* Clear the sensor_t object */
|
339 |
memset(sensor, 0, sizeof(sensor_t)); |
340 |
|
341 |
/* Insert the sensor name in the fixed length char array */
|
342 |
strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1); |
343 |
sensor->name[sizeof(sensor->name)- 1] = 0; |
344 |
sensor->version = 1;
|
345 |
sensor->sensor_id = _sensorID; |
346 |
sensor->type = SENSOR_TYPE_ORIENTATION; |
347 |
sensor->min_delay = 0;
|
348 |
sensor->max_value = 0.0F; |
349 |
sensor->min_value = 0.0F; |
350 |
sensor->resolution = 0.01F; |
351 |
} |
352 |
|
353 |
/**************************************************************************/
|
354 |
/*!
|
355 |
@brief Reads the sensor and returns the data as a sensors_event_t
|
356 |
*/
|
357 |
/**************************************************************************/
|
358 |
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
|
359 |
{ |
360 |
/* Clear the event */
|
361 |
memset(event, 0, sizeof(sensors_event_t)); |
362 |
|
363 |
event->version = sizeof(sensors_event_t);
|
364 |
event->sensor_id = _sensorID; |
365 |
event->type = SENSOR_TYPE_ORIENTATION; |
366 |
event->timestamp = millis(); |
367 |
|
368 |
/* Get a Euler angle sample for orientation */
|
369 |
imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
|
370 |
event->orientation.x = euler.x(); |
371 |
event->orientation.y = euler.y(); |
372 |
event->orientation.z = euler.z(); |
373 |
|
374 |
return true; |
375 |
} |
376 |
|
377 |
/***************************************************************************
|
378 |
PRIVATE FUNCTIONS
|
379 |
***************************************************************************/
|
380 |
|
381 |
/**************************************************************************/
|
382 |
/*!
|
383 |
@brief Writes an 8 bit value over I2C
|
384 |
*/
|
385 |
/**************************************************************************/
|
386 |
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
|
387 |
{ |
388 |
Wire.beginTransmission(_address); |
389 |
#if ARDUINO >= 100 |
390 |
Wire.write((uint8_t)reg); |
391 |
Wire.write((uint8_t)value); |
392 |
#else
|
393 |
Wire.send(reg); |
394 |
Wire.send(value); |
395 |
#endif
|
396 |
Wire.endTransmission(); |
397 |
|
398 |
/* ToDo: Check for error! */
|
399 |
return true; |
400 |
} |
401 |
|
402 |
/**************************************************************************/
|
403 |
/*!
|
404 |
@brief Reads an 8 bit value over I2C
|
405 |
*/
|
406 |
/**************************************************************************/
|
407 |
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg ) |
408 |
{ |
409 |
byte value = 0;
|
410 |
|
411 |
Wire.beginTransmission(_address); |
412 |
#if ARDUINO >= 100 |
413 |
Wire.write((uint8_t)reg); |
414 |
#else
|
415 |
Wire.send(reg); |
416 |
#endif
|
417 |
Wire.endTransmission(); |
418 |
Wire.requestFrom(_address, (byte)1);
|
419 |
#if ARDUINO >= 100 |
420 |
value = Wire.read(); |
421 |
#else
|
422 |
value = Wire.receive(); |
423 |
#endif
|
424 |
|
425 |
return value;
|
426 |
} |
427 |
|
428 |
/**************************************************************************/
|
429 |
/*!
|
430 |
@brief Reads the specified number of bytes over I2C
|
431 |
*/
|
432 |
/**************************************************************************/
|
433 |
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
|
434 |
{ |
435 |
Wire.beginTransmission(_address); |
436 |
#if ARDUINO >= 100 |
437 |
Wire.write((uint8_t)reg); |
438 |
#else
|
439 |
Wire.send(reg); |
440 |
#endif
|
441 |
Wire.endTransmission(); |
442 |
Wire.requestFrom(_address, (byte)len); |
443 |
|
444 |
/* Wait until data is available */
|
445 |
while (Wire.available() < len);
|
446 |
|
447 |
for (uint8_t i = 0; i < len; i++) |
448 |
{ |
449 |
#if ARDUINO >= 100 |
450 |
buffer[i] = Wire.read(); |
451 |
#else
|
452 |
buffer[i] = Wire.receive(); |
453 |
#endif
|
454 |
} |
455 |
|
456 |
/* ToDo: Check for errors! */
|
457 |
return true; |
458 |
} |