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