adafruit_bno055 / Adafruit_BNO055.cpp @ 510d0f10
History | View | Annotate | Download (22.681 KB)
1 |
/*!
|
---|---|
2 |
* @file Adafruit_BNO055.cpp
|
3 |
*
|
4 |
* @mainpage Adafruit BNO055 Orientation Sensor
|
5 |
*
|
6 |
* @section intro_sec Introduction
|
7 |
*
|
8 |
* This is a library for the BNO055 orientation sensor
|
9 |
*
|
10 |
* Designed specifically to work with the Adafruit BNO055 Breakout.
|
11 |
*
|
12 |
* Pick one up today in the adafruit shop!
|
13 |
* ------> https://www.adafruit.com/product/2472
|
14 |
*
|
15 |
* These sensors use I2C to communicate, 2 pins are required to interface.
|
16 |
*
|
17 |
* Adafruit invests time and resources providing this open source code,
|
18 |
* please support Adafruit andopen-source hardware by purchasing products
|
19 |
* from Adafruit!
|
20 |
*
|
21 |
* @section author Author
|
22 |
*
|
23 |
* K.Townsend (Adafruit Industries)
|
24 |
*
|
25 |
* @section license License
|
26 |
*
|
27 |
* MIT license, all text above must be included in any redistribution
|
28 |
*/
|
29 |
|
30 |
#include "Arduino.h" |
31 |
|
32 |
#include <limits.h> |
33 |
#include <math.h> |
34 |
|
35 |
#include "Adafruit_BNO055.h" |
36 |
|
37 |
/*!
|
38 |
* @brief Instantiates a new Adafruit_BNO055 class
|
39 |
* @param sensorID
|
40 |
* sensor ID
|
41 |
* @param address
|
42 |
* i2c address
|
43 |
* @param *theWire
|
44 |
* Wire object
|
45 |
*/
|
46 |
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address, |
47 |
TwoWire *theWire) { |
48 |
_sensorID = sensorID; |
49 |
_address = address; |
50 |
_wire = theWire; |
51 |
} |
52 |
|
53 |
/*!
|
54 |
* @brief Sets up the HW
|
55 |
* @param mode
|
56 |
* mode values
|
57 |
* [OPERATION_MODE_CONFIG,
|
58 |
* OPERATION_MODE_ACCONLY,
|
59 |
* OPERATION_MODE_MAGONLY,
|
60 |
* OPERATION_MODE_GYRONLY,
|
61 |
* OPERATION_MODE_ACCMAG,
|
62 |
* OPERATION_MODE_ACCGYRO,
|
63 |
* OPERATION_MODE_MAGGYRO,
|
64 |
* OPERATION_MODE_AMG,
|
65 |
* OPERATION_MODE_IMUPLUS,
|
66 |
* OPERATION_MODE_COMPASS,
|
67 |
* OPERATION_MODE_M4G,
|
68 |
* OPERATION_MODE_NDOF_FMC_OFF,
|
69 |
* OPERATION_MODE_NDOF]
|
70 |
* @return true if process is successful
|
71 |
*/
|
72 |
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode) {
|
73 |
#if defined(ARDUINO_SAMD_ZERO) && (_address == BNO055_ADDRESS_A)
|
74 |
#error \
|
75 |
"On an arduino Zero, BNO055's ADR pin must be high. Fix that, then delete this line."
|
76 |
_address = BNO055_ADDRESS_B; |
77 |
#endif
|
78 |
|
79 |
/* Enable I2C */
|
80 |
_wire->begin(); |
81 |
|
82 |
// BNO055 clock stretches for 500us or more!
|
83 |
#ifdef ESP8266
|
84 |
_wire->setClockStretchLimit(1000); // Allow for 1000us of clock stretching |
85 |
#endif
|
86 |
|
87 |
/* Make sure we have the right device */
|
88 |
uint8_t id = read8(BNO055_CHIP_ID_ADDR); |
89 |
if (id != BNO055_ID) {
|
90 |
delay(1000); // hold on for boot |
91 |
id = read8(BNO055_CHIP_ID_ADDR); |
92 |
if (id != BNO055_ID) {
|
93 |
return false; // still not? ok bail |
94 |
} |
95 |
} |
96 |
|
97 |
/* Switch to config mode (just in case since this is the default) */
|
98 |
setMode(OPERATION_MODE_CONFIG); |
99 |
|
100 |
/* Reset */
|
101 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
|
102 |
while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID) {
|
103 |
delay(10);
|
104 |
} |
105 |
delay(50);
|
106 |
|
107 |
/* Set to normal power mode */
|
108 |
write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL); |
109 |
delay(10);
|
110 |
|
111 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
112 |
|
113 |
/* Set the output units */
|
114 |
/*
|
115 |
uint8_t unitsel = (0 << 7) | // Orientation = Android
|
116 |
(0 << 4) | // Temperature = Celsius
|
117 |
(0 << 2) | // Euler = Degrees
|
118 |
(1 << 1) | // Gyro = Rads
|
119 |
(0 << 0); // Accelerometer = m/s^2
|
120 |
write8(BNO055_UNIT_SEL_ADDR, unitsel);
|
121 |
*/
|
122 |
|
123 |
/* Configure axis mapping (see section 3.4) */
|
124 |
/*
|
125 |
write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
|
126 |
delay(10);
|
127 |
write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
|
128 |
delay(10);
|
129 |
*/
|
130 |
|
131 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
|
132 |
delay(10);
|
133 |
/* Set the requested operating mode (see section 3.3) */
|
134 |
setMode(mode); |
135 |
delay(20);
|
136 |
|
137 |
return true; |
138 |
} |
139 |
|
140 |
/*!
|
141 |
* @brief Puts the chip in the specified operating mode
|
142 |
* @param mode
|
143 |
* mode values
|
144 |
* [OPERATION_MODE_CONFIG,
|
145 |
* OPERATION_MODE_ACCONLY,
|
146 |
* OPERATION_MODE_MAGONLY,
|
147 |
* OPERATION_MODE_GYRONLY,
|
148 |
* OPERATION_MODE_ACCMAG,
|
149 |
* OPERATION_MODE_ACCGYRO,
|
150 |
* OPERATION_MODE_MAGGYRO,
|
151 |
* OPERATION_MODE_AMG,
|
152 |
* OPERATION_MODE_IMUPLUS,
|
153 |
* OPERATION_MODE_COMPASS,
|
154 |
* OPERATION_MODE_M4G,
|
155 |
* OPERATION_MODE_NDOF_FMC_OFF,
|
156 |
* OPERATION_MODE_NDOF]
|
157 |
*/
|
158 |
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode) {
|
159 |
_mode = mode; |
160 |
write8(BNO055_OPR_MODE_ADDR, _mode); |
161 |
delay(30);
|
162 |
} |
163 |
|
164 |
/*!
|
165 |
* @brief Changes the chip's axis remap
|
166 |
* @param remapcode
|
167 |
* remap code possible values
|
168 |
* [REMAP_CONFIG_P0
|
169 |
* REMAP_CONFIG_P1 (default)
|
170 |
* REMAP_CONFIG_P2
|
171 |
* REMAP_CONFIG_P3
|
172 |
* REMAP_CONFIG_P4
|
173 |
* REMAP_CONFIG_P5
|
174 |
* REMAP_CONFIG_P6
|
175 |
* REMAP_CONFIG_P7]
|
176 |
*/
|
177 |
void Adafruit_BNO055::setAxisRemap(
|
178 |
adafruit_bno055_axis_remap_config_t remapcode) { |
179 |
adafruit_bno055_opmode_t modeback = _mode; |
180 |
|
181 |
setMode(OPERATION_MODE_CONFIG); |
182 |
delay(25);
|
183 |
write8(BNO055_AXIS_MAP_CONFIG_ADDR, remapcode); |
184 |
delay(10);
|
185 |
/* Set the requested operating mode (see section 3.3) */
|
186 |
setMode(modeback); |
187 |
delay(20);
|
188 |
} |
189 |
|
190 |
/*!
|
191 |
* @brief Changes the chip's axis signs
|
192 |
* @param remapsign
|
193 |
* remap sign possible values
|
194 |
* [REMAP_SIGN_P0
|
195 |
* REMAP_SIGN_P1 (default)
|
196 |
* REMAP_SIGN_P2
|
197 |
* REMAP_SIGN_P3
|
198 |
* REMAP_SIGN_P4
|
199 |
* REMAP_SIGN_P5
|
200 |
* REMAP_SIGN_P6
|
201 |
* REMAP_SIGN_P7]
|
202 |
*/
|
203 |
void Adafruit_BNO055::setAxisSign(adafruit_bno055_axis_remap_sign_t remapsign) {
|
204 |
adafruit_bno055_opmode_t modeback = _mode; |
205 |
|
206 |
setMode(OPERATION_MODE_CONFIG); |
207 |
delay(25);
|
208 |
write8(BNO055_AXIS_MAP_SIGN_ADDR, remapsign); |
209 |
delay(10);
|
210 |
/* Set the requested operating mode (see section 3.3) */
|
211 |
setMode(modeback); |
212 |
delay(20);
|
213 |
} |
214 |
|
215 |
/*!
|
216 |
* @brief Use the external 32.768KHz crystal
|
217 |
* @param usextal
|
218 |
* use external crystal boolean
|
219 |
*/
|
220 |
void Adafruit_BNO055::setExtCrystalUse(boolean usextal) {
|
221 |
adafruit_bno055_opmode_t modeback = _mode; |
222 |
|
223 |
/* Switch to config mode (just in case since this is the default) */
|
224 |
setMode(OPERATION_MODE_CONFIG); |
225 |
delay(25);
|
226 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
227 |
if (usextal) {
|
228 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
|
229 |
} else {
|
230 |
write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
|
231 |
} |
232 |
delay(10);
|
233 |
/* Set the requested operating mode (see section 3.3) */
|
234 |
setMode(modeback); |
235 |
delay(20);
|
236 |
} |
237 |
|
238 |
/*!
|
239 |
* @brief Gets the latest system status info
|
240 |
* @param system_status
|
241 |
* system status info
|
242 |
* @param self_test_result
|
243 |
* self test result
|
244 |
* @param system_error
|
245 |
* system error info
|
246 |
*/
|
247 |
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status,
|
248 |
uint8_t *self_test_result, |
249 |
uint8_t *system_error) { |
250 |
write8(BNO055_PAGE_ID_ADDR, 0);
|
251 |
|
252 |
/* System Status (see section 4.3.58)
|
253 |
0 = Idle
|
254 |
1 = System Error
|
255 |
2 = Initializing Peripherals
|
256 |
3 = System Iniitalization
|
257 |
4 = Executing Self-Test
|
258 |
5 = Sensor fusio algorithm running
|
259 |
6 = System running without fusion algorithms
|
260 |
*/
|
261 |
|
262 |
if (system_status != 0) |
263 |
*system_status = read8(BNO055_SYS_STAT_ADDR); |
264 |
|
265 |
/* Self Test Results
|
266 |
1 = test passed, 0 = test failed
|
267 |
|
268 |
Bit 0 = Accelerometer self test
|
269 |
Bit 1 = Magnetometer self test
|
270 |
Bit 2 = Gyroscope self test
|
271 |
Bit 3 = MCU self test
|
272 |
|
273 |
0x0F = all good!
|
274 |
*/
|
275 |
|
276 |
if (self_test_result != 0) |
277 |
*self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR); |
278 |
|
279 |
/* System Error (see section 4.3.59)
|
280 |
0 = No error
|
281 |
1 = Peripheral initialization error
|
282 |
2 = System initialization error
|
283 |
3 = Self test result failed
|
284 |
4 = Register map value out of range
|
285 |
5 = Register map address out of range
|
286 |
6 = Register map write error
|
287 |
7 = BNO low power mode not available for selected operat ion mode
|
288 |
8 = Accelerometer power mode not available
|
289 |
9 = Fusion algorithm configuration error
|
290 |
A = Sensor configuration error
|
291 |
*/
|
292 |
|
293 |
if (system_error != 0) |
294 |
*system_error = read8(BNO055_SYS_ERR_ADDR); |
295 |
|
296 |
delay(200);
|
297 |
} |
298 |
|
299 |
/*!
|
300 |
* @brief Gets the chip revision numbers
|
301 |
* @param info
|
302 |
* revision info
|
303 |
*/
|
304 |
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t *info) {
|
305 |
uint8_t a, b; |
306 |
|
307 |
memset(info, 0, sizeof(adafruit_bno055_rev_info_t)); |
308 |
|
309 |
/* Check the accelerometer revision */
|
310 |
info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR); |
311 |
|
312 |
/* Check the magnetometer revision */
|
313 |
info->mag_rev = read8(BNO055_MAG_REV_ID_ADDR); |
314 |
|
315 |
/* Check the gyroscope revision */
|
316 |
info->gyro_rev = read8(BNO055_GYRO_REV_ID_ADDR); |
317 |
|
318 |
/* Check the SW revision */
|
319 |
info->bl_rev = read8(BNO055_BL_REV_ID_ADDR); |
320 |
|
321 |
a = read8(BNO055_SW_REV_ID_LSB_ADDR); |
322 |
b = read8(BNO055_SW_REV_ID_MSB_ADDR); |
323 |
info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
|
324 |
} |
325 |
|
326 |
/*!
|
327 |
* @brief Gets current calibration state. Each value should be a uint8_t
|
328 |
* pointer and it will be set to 0 if not calibrated and 3 if
|
329 |
* fully calibrated.
|
330 |
* See section 34.3.54
|
331 |
* @param sys
|
332 |
* Current system calibration status, depends on status of all sensors,
|
333 |
* read-only
|
334 |
* @param gyro
|
335 |
* Current calibration status of Gyroscope, read-only
|
336 |
* @param accel
|
337 |
* Current calibration status of Accelerometer, read-only
|
338 |
* @param mag
|
339 |
* Current calibration status of Magnetometer, read-only
|
340 |
*/
|
341 |
void Adafruit_BNO055::getCalibration(uint8_t *sys, uint8_t *gyro,
|
342 |
uint8_t *accel, uint8_t *mag) { |
343 |
uint8_t calData = read8(BNO055_CALIB_STAT_ADDR); |
344 |
if (sys != NULL) { |
345 |
*sys = (calData >> 6) & 0x03; |
346 |
} |
347 |
if (gyro != NULL) { |
348 |
*gyro = (calData >> 4) & 0x03; |
349 |
} |
350 |
if (accel != NULL) { |
351 |
*accel = (calData >> 2) & 0x03; |
352 |
} |
353 |
if (mag != NULL) { |
354 |
*mag = calData & 0x03;
|
355 |
} |
356 |
} |
357 |
|
358 |
/*!
|
359 |
* @brief Gets the temperature in degrees celsius
|
360 |
* @return temperature in degrees celsius
|
361 |
*/
|
362 |
int8_t Adafruit_BNO055::getTemp() { |
363 |
int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR)); |
364 |
return temp;
|
365 |
} |
366 |
|
367 |
/*!
|
368 |
* @brief Gets a vector reading from the specified source
|
369 |
* @param vector_type
|
370 |
* possible vector type values
|
371 |
* [VECTOR_ACCELEROMETER
|
372 |
* VECTOR_MAGNETOMETER
|
373 |
* VECTOR_GYROSCOPE
|
374 |
* VECTOR_EULER
|
375 |
* VECTOR_LINEARACCEL
|
376 |
* VECTOR_GRAVITY]
|
377 |
* @return vector from specified source
|
378 |
*/
|
379 |
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type) {
|
380 |
imu::Vector<3> xyz;
|
381 |
uint8_t buffer[6];
|
382 |
memset(buffer, 0, 6); |
383 |
|
384 |
int16_t x, y, z; |
385 |
x = y = z = 0;
|
386 |
|
387 |
/* Read vector data (6 bytes) */
|
388 |
readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
|
389 |
|
390 |
x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8); |
391 |
y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8); |
392 |
z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8); |
393 |
|
394 |
/*!
|
395 |
* Convert the value to an appropriate range (section 3.6.4)
|
396 |
* and assign the value to the Vector type
|
397 |
*/
|
398 |
switch (vector_type) {
|
399 |
case VECTOR_MAGNETOMETER:
|
400 |
/* 1uT = 16 LSB */
|
401 |
xyz[0] = ((double)x) / 16.0; |
402 |
xyz[1] = ((double)y) / 16.0; |
403 |
xyz[2] = ((double)z) / 16.0; |
404 |
break;
|
405 |
case VECTOR_GYROSCOPE:
|
406 |
/* 1dps = 16 LSB */
|
407 |
xyz[0] = ((double)x) / 16.0; |
408 |
xyz[1] = ((double)y) / 16.0; |
409 |
xyz[2] = ((double)z) / 16.0; |
410 |
break;
|
411 |
case VECTOR_EULER:
|
412 |
/* 1 degree = 16 LSB */
|
413 |
xyz[0] = ((double)x) / 16.0; |
414 |
xyz[1] = ((double)y) / 16.0; |
415 |
xyz[2] = ((double)z) / 16.0; |
416 |
break;
|
417 |
case VECTOR_ACCELEROMETER:
|
418 |
case VECTOR_LINEARACCEL:
|
419 |
case VECTOR_GRAVITY:
|
420 |
/* 1m/s^2 = 100 LSB */
|
421 |
xyz[0] = ((double)x) / 100.0; |
422 |
xyz[1] = ((double)y) / 100.0; |
423 |
xyz[2] = ((double)z) / 100.0; |
424 |
break;
|
425 |
} |
426 |
|
427 |
return xyz;
|
428 |
} |
429 |
|
430 |
/*!
|
431 |
* @brief Gets a quaternion reading from the specified source
|
432 |
* @return quaternion reading
|
433 |
*/
|
434 |
imu::Quaternion Adafruit_BNO055::getQuat() { |
435 |
uint8_t buffer[8];
|
436 |
memset(buffer, 0, 8); |
437 |
|
438 |
int16_t x, y, z, w; |
439 |
x = y = z = w = 0;
|
440 |
|
441 |
/* Read quat data (8 bytes) */
|
442 |
readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
|
443 |
w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]); |
444 |
x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]); |
445 |
y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]); |
446 |
z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]); |
447 |
|
448 |
/*!
|
449 |
* Assign to Quaternion
|
450 |
* See
|
451 |
* http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
|
452 |
* 3.6.5.5 Orientation (Quaternion)
|
453 |
*/
|
454 |
const double scale = (1.0 / (1 << 14)); |
455 |
imu::Quaternion quat(scale * w, scale * x, scale * y, scale * z); |
456 |
return quat;
|
457 |
} |
458 |
|
459 |
/*!
|
460 |
* @brief Provides the sensor_t data for this sensor
|
461 |
* @param sensor
|
462 |
*/
|
463 |
void Adafruit_BNO055::getSensor(sensor_t *sensor) {
|
464 |
/* Clear the sensor_t object */
|
465 |
memset(sensor, 0, sizeof(sensor_t)); |
466 |
|
467 |
/* Insert the sensor name in the fixed length char array */
|
468 |
strncpy(sensor->name, "BNO055", sizeof(sensor->name) - 1); |
469 |
sensor->name[sizeof(sensor->name) - 1] = 0; |
470 |
sensor->version = 1;
|
471 |
sensor->sensor_id = _sensorID; |
472 |
sensor->type = SENSOR_TYPE_ORIENTATION; |
473 |
sensor->min_delay = 0;
|
474 |
sensor->max_value = 0.0F; |
475 |
sensor->min_value = 0.0F; |
476 |
sensor->resolution = 0.01F; |
477 |
} |
478 |
|
479 |
/*!
|
480 |
* @brief Reads the sensor and returns the data as a sensors_event_t
|
481 |
* @param event
|
482 |
* @return always returns true
|
483 |
*/
|
484 |
bool Adafruit_BNO055::getEvent(sensors_event_t *event) {
|
485 |
/* Clear the event */
|
486 |
memset(event, 0, sizeof(sensors_event_t)); |
487 |
|
488 |
event->version = sizeof(sensors_event_t);
|
489 |
event->sensor_id = _sensorID; |
490 |
event->type = SENSOR_TYPE_ORIENTATION; |
491 |
event->timestamp = millis(); |
492 |
|
493 |
/* Get a Euler angle sample for orientation */
|
494 |
imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
|
495 |
event->orientation.x = euler.x(); |
496 |
event->orientation.y = euler.y(); |
497 |
event->orientation.z = euler.z(); |
498 |
|
499 |
return true; |
500 |
} |
501 |
|
502 |
/*!
|
503 |
* @brief Reads the sensor's offset registers into a byte array
|
504 |
* @param calibData
|
505 |
* @return true if read is successful
|
506 |
*/
|
507 |
bool Adafruit_BNO055::getSensorOffsets(uint8_t *calibData) {
|
508 |
if (isFullyCalibrated()) {
|
509 |
adafruit_bno055_opmode_t lastMode = _mode; |
510 |
setMode(OPERATION_MODE_CONFIG); |
511 |
|
512 |
readLen(ACCEL_OFFSET_X_LSB_ADDR, calibData, NUM_BNO055_OFFSET_REGISTERS); |
513 |
|
514 |
setMode(lastMode); |
515 |
return true; |
516 |
} |
517 |
return false; |
518 |
} |
519 |
|
520 |
/*!
|
521 |
* @brief Reads the sensor's offset registers into an offset struct
|
522 |
* @param offsets_type
|
523 |
* type of offsets
|
524 |
* @return true if read is successful
|
525 |
*/
|
526 |
bool Adafruit_BNO055::getSensorOffsets(
|
527 |
adafruit_bno055_offsets_t &offsets_type) { |
528 |
if (isFullyCalibrated()) {
|
529 |
adafruit_bno055_opmode_t lastMode = _mode; |
530 |
setMode(OPERATION_MODE_CONFIG); |
531 |
delay(25);
|
532 |
|
533 |
/* Accel offset range depends on the G-range:
|
534 |
+/-2g = +/- 2000 mg
|
535 |
+/-4g = +/- 4000 mg
|
536 |
+/-8g = +/- 8000 mg
|
537 |
+/-1§g = +/- 16000 mg */
|
538 |
offsets_type.accel_offset_x = (read8(ACCEL_OFFSET_X_MSB_ADDR) << 8) |
|
539 |
(read8(ACCEL_OFFSET_X_LSB_ADDR)); |
540 |
offsets_type.accel_offset_y = (read8(ACCEL_OFFSET_Y_MSB_ADDR) << 8) |
|
541 |
(read8(ACCEL_OFFSET_Y_LSB_ADDR)); |
542 |
offsets_type.accel_offset_z = (read8(ACCEL_OFFSET_Z_MSB_ADDR) << 8) |
|
543 |
(read8(ACCEL_OFFSET_Z_LSB_ADDR)); |
544 |
|
545 |
/* Magnetometer offset range = +/- 6400 LSB where 1uT = 16 LSB */
|
546 |
offsets_type.mag_offset_x = |
547 |
(read8(MAG_OFFSET_X_MSB_ADDR) << 8) | (read8(MAG_OFFSET_X_LSB_ADDR));
|
548 |
offsets_type.mag_offset_y = |
549 |
(read8(MAG_OFFSET_Y_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Y_LSB_ADDR));
|
550 |
offsets_type.mag_offset_z = |
551 |
(read8(MAG_OFFSET_Z_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Z_LSB_ADDR));
|
552 |
|
553 |
/* Gyro offset range depends on the DPS range:
|
554 |
2000 dps = +/- 32000 LSB
|
555 |
1000 dps = +/- 16000 LSB
|
556 |
500 dps = +/- 8000 LSB
|
557 |
250 dps = +/- 4000 LSB
|
558 |
125 dps = +/- 2000 LSB
|
559 |
... where 1 DPS = 16 LSB */
|
560 |
offsets_type.gyro_offset_x = |
561 |
(read8(GYRO_OFFSET_X_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_X_LSB_ADDR));
|
562 |
offsets_type.gyro_offset_y = |
563 |
(read8(GYRO_OFFSET_Y_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Y_LSB_ADDR));
|
564 |
offsets_type.gyro_offset_z = |
565 |
(read8(GYRO_OFFSET_Z_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Z_LSB_ADDR));
|
566 |
|
567 |
/* Accelerometer radius = +/- 1000 LSB */
|
568 |
offsets_type.accel_radius = |
569 |
(read8(ACCEL_RADIUS_MSB_ADDR) << 8) | (read8(ACCEL_RADIUS_LSB_ADDR));
|
570 |
|
571 |
/* Magnetometer radius = +/- 960 LSB */
|
572 |
offsets_type.mag_radius = |
573 |
(read8(MAG_RADIUS_MSB_ADDR) << 8) | (read8(MAG_RADIUS_LSB_ADDR));
|
574 |
|
575 |
setMode(lastMode); |
576 |
return true; |
577 |
} |
578 |
return false; |
579 |
} |
580 |
|
581 |
/*!
|
582 |
* @brief Writes an array of calibration values to the sensor's offset
|
583 |
* @param *calibData
|
584 |
* calibration data
|
585 |
*/
|
586 |
void Adafruit_BNO055::setSensorOffsets(const uint8_t *calibData) { |
587 |
adafruit_bno055_opmode_t lastMode = _mode; |
588 |
setMode(OPERATION_MODE_CONFIG); |
589 |
delay(25);
|
590 |
|
591 |
/* Note: Configuration will take place only when user writes to the last
|
592 |
byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
|
593 |
Therefore the last byte must be written whenever the user wants to
|
594 |
changes the configuration. */
|
595 |
|
596 |
/* A writeLen() would make this much cleaner */
|
597 |
write8(ACCEL_OFFSET_X_LSB_ADDR, calibData[0]);
|
598 |
write8(ACCEL_OFFSET_X_MSB_ADDR, calibData[1]);
|
599 |
write8(ACCEL_OFFSET_Y_LSB_ADDR, calibData[2]);
|
600 |
write8(ACCEL_OFFSET_Y_MSB_ADDR, calibData[3]);
|
601 |
write8(ACCEL_OFFSET_Z_LSB_ADDR, calibData[4]);
|
602 |
write8(ACCEL_OFFSET_Z_MSB_ADDR, calibData[5]);
|
603 |
|
604 |
write8(MAG_OFFSET_X_LSB_ADDR, calibData[6]);
|
605 |
write8(MAG_OFFSET_X_MSB_ADDR, calibData[7]);
|
606 |
write8(MAG_OFFSET_Y_LSB_ADDR, calibData[8]);
|
607 |
write8(MAG_OFFSET_Y_MSB_ADDR, calibData[9]);
|
608 |
write8(MAG_OFFSET_Z_LSB_ADDR, calibData[10]);
|
609 |
write8(MAG_OFFSET_Z_MSB_ADDR, calibData[11]);
|
610 |
|
611 |
write8(GYRO_OFFSET_X_LSB_ADDR, calibData[12]);
|
612 |
write8(GYRO_OFFSET_X_MSB_ADDR, calibData[13]);
|
613 |
write8(GYRO_OFFSET_Y_LSB_ADDR, calibData[14]);
|
614 |
write8(GYRO_OFFSET_Y_MSB_ADDR, calibData[15]);
|
615 |
write8(GYRO_OFFSET_Z_LSB_ADDR, calibData[16]);
|
616 |
write8(GYRO_OFFSET_Z_MSB_ADDR, calibData[17]);
|
617 |
|
618 |
write8(ACCEL_RADIUS_LSB_ADDR, calibData[18]);
|
619 |
write8(ACCEL_RADIUS_MSB_ADDR, calibData[19]);
|
620 |
|
621 |
write8(MAG_RADIUS_LSB_ADDR, calibData[20]);
|
622 |
write8(MAG_RADIUS_MSB_ADDR, calibData[21]);
|
623 |
|
624 |
setMode(lastMode); |
625 |
} |
626 |
|
627 |
/*!
|
628 |
* @brief Writes to the sensor's offset registers from an offset struct
|
629 |
* @param offsets_type
|
630 |
* accel_offset_x = acceleration offset x
|
631 |
* accel_offset_y = acceleration offset y
|
632 |
* accel_offset_z = acceleration offset z
|
633 |
*
|
634 |
* mag_offset_x = magnetometer offset x
|
635 |
* mag_offset_y = magnetometer offset y
|
636 |
* mag_offset_z = magnetometer offset z
|
637 |
*
|
638 |
* gyro_offset_x = gyroscrope offset x
|
639 |
* gyro_offset_y = gyroscrope offset y
|
640 |
* gyro_offset_z = gyroscrope offset z
|
641 |
*/
|
642 |
void Adafruit_BNO055::setSensorOffsets(
|
643 |
const adafruit_bno055_offsets_t &offsets_type) {
|
644 |
adafruit_bno055_opmode_t lastMode = _mode; |
645 |
setMode(OPERATION_MODE_CONFIG); |
646 |
delay(25);
|
647 |
|
648 |
/* Note: Configuration will take place only when user writes to the last
|
649 |
byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
|
650 |
Therefore the last byte must be written whenever the user wants to
|
651 |
changes the configuration. */
|
652 |
|
653 |
write8(ACCEL_OFFSET_X_LSB_ADDR, (offsets_type.accel_offset_x) & 0x0FF);
|
654 |
write8(ACCEL_OFFSET_X_MSB_ADDR, (offsets_type.accel_offset_x >> 8) & 0x0FF); |
655 |
write8(ACCEL_OFFSET_Y_LSB_ADDR, (offsets_type.accel_offset_y) & 0x0FF);
|
656 |
write8(ACCEL_OFFSET_Y_MSB_ADDR, (offsets_type.accel_offset_y >> 8) & 0x0FF); |
657 |
write8(ACCEL_OFFSET_Z_LSB_ADDR, (offsets_type.accel_offset_z) & 0x0FF);
|
658 |
write8(ACCEL_OFFSET_Z_MSB_ADDR, (offsets_type.accel_offset_z >> 8) & 0x0FF); |
659 |
|
660 |
write8(MAG_OFFSET_X_LSB_ADDR, (offsets_type.mag_offset_x) & 0x0FF);
|
661 |
write8(MAG_OFFSET_X_MSB_ADDR, (offsets_type.mag_offset_x >> 8) & 0x0FF); |
662 |
write8(MAG_OFFSET_Y_LSB_ADDR, (offsets_type.mag_offset_y) & 0x0FF);
|
663 |
write8(MAG_OFFSET_Y_MSB_ADDR, (offsets_type.mag_offset_y >> 8) & 0x0FF); |
664 |
write8(MAG_OFFSET_Z_LSB_ADDR, (offsets_type.mag_offset_z) & 0x0FF);
|
665 |
write8(MAG_OFFSET_Z_MSB_ADDR, (offsets_type.mag_offset_z >> 8) & 0x0FF); |
666 |
|
667 |
write8(GYRO_OFFSET_X_LSB_ADDR, (offsets_type.gyro_offset_x) & 0x0FF);
|
668 |
write8(GYRO_OFFSET_X_MSB_ADDR, (offsets_type.gyro_offset_x >> 8) & 0x0FF); |
669 |
write8(GYRO_OFFSET_Y_LSB_ADDR, (offsets_type.gyro_offset_y) & 0x0FF);
|
670 |
write8(GYRO_OFFSET_Y_MSB_ADDR, (offsets_type.gyro_offset_y >> 8) & 0x0FF); |
671 |
write8(GYRO_OFFSET_Z_LSB_ADDR, (offsets_type.gyro_offset_z) & 0x0FF);
|
672 |
write8(GYRO_OFFSET_Z_MSB_ADDR, (offsets_type.gyro_offset_z >> 8) & 0x0FF); |
673 |
|
674 |
write8(ACCEL_RADIUS_LSB_ADDR, (offsets_type.accel_radius) & 0x0FF);
|
675 |
write8(ACCEL_RADIUS_MSB_ADDR, (offsets_type.accel_radius >> 8) & 0x0FF); |
676 |
|
677 |
write8(MAG_RADIUS_LSB_ADDR, (offsets_type.mag_radius) & 0x0FF);
|
678 |
write8(MAG_RADIUS_MSB_ADDR, (offsets_type.mag_radius >> 8) & 0x0FF); |
679 |
|
680 |
setMode(lastMode); |
681 |
} |
682 |
|
683 |
/*!
|
684 |
* @brief Checks of all cal status values are set to 3 (fully calibrated)
|
685 |
* @return status of calibration
|
686 |
*/
|
687 |
bool Adafruit_BNO055::isFullyCalibrated() {
|
688 |
uint8_t system, gyro, accel, mag; |
689 |
getCalibration(&system, &gyro, &accel, &mag); |
690 |
|
691 |
switch (_mode) {
|
692 |
case OPERATION_MODE_ACCONLY:
|
693 |
return (accel == 3); |
694 |
case OPERATION_MODE_MAGONLY:
|
695 |
return (mag == 3); |
696 |
case OPERATION_MODE_GYRONLY:
|
697 |
case OPERATION_MODE_M4G: /* No magnetometer calibration required. */ |
698 |
return (gyro == 3); |
699 |
case OPERATION_MODE_ACCMAG:
|
700 |
case OPERATION_MODE_COMPASS:
|
701 |
return (accel == 3 && mag == 3); |
702 |
case OPERATION_MODE_ACCGYRO:
|
703 |
case OPERATION_MODE_IMUPLUS:
|
704 |
return (accel == 3 && gyro == 3); |
705 |
case OPERATION_MODE_MAGGYRO:
|
706 |
return (mag == 3 && gyro == 3); |
707 |
default:
|
708 |
return (system == 3 && gyro == 3 && accel == 3 && mag == 3); |
709 |
} |
710 |
} |
711 |
|
712 |
/*!
|
713 |
* @brief Writes an 8 bit value over I2C
|
714 |
*/
|
715 |
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value) {
|
716 |
_wire->beginTransmission(_address); |
717 |
#if ARDUINO >= 100 |
718 |
_wire->write((uint8_t)reg); |
719 |
_wire->write((uint8_t)value); |
720 |
#else
|
721 |
_wire->send(reg); |
722 |
_wire->send(value); |
723 |
#endif
|
724 |
_wire->endTransmission(); |
725 |
|
726 |
/* ToDo: Check for error! */
|
727 |
return true; |
728 |
} |
729 |
|
730 |
/*!
|
731 |
* @brief Reads an 8 bit value over I2C
|
732 |
*/
|
733 |
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg) { |
734 |
byte value = 0;
|
735 |
|
736 |
_wire->beginTransmission(_address); |
737 |
#if ARDUINO >= 100 |
738 |
_wire->write((uint8_t)reg); |
739 |
#else
|
740 |
_wire->send(reg); |
741 |
#endif
|
742 |
_wire->endTransmission(); |
743 |
_wire->requestFrom(_address, (byte)1);
|
744 |
#if ARDUINO >= 100 |
745 |
value = _wire->read(); |
746 |
#else
|
747 |
value = _wire->receive(); |
748 |
#endif
|
749 |
|
750 |
return value;
|
751 |
} |
752 |
|
753 |
/*!
|
754 |
* @brief Reads the specified number of bytes over I2C
|
755 |
*/
|
756 |
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte *buffer,
|
757 |
uint8_t len) { |
758 |
_wire->beginTransmission(_address); |
759 |
#if ARDUINO >= 100 |
760 |
_wire->write((uint8_t)reg); |
761 |
#else
|
762 |
_wire->send(reg); |
763 |
#endif
|
764 |
_wire->endTransmission(); |
765 |
_wire->requestFrom(_address, (byte)len); |
766 |
|
767 |
for (uint8_t i = 0; i < len; i++) { |
768 |
#if ARDUINO >= 100 |
769 |
buffer[i] = _wire->read(); |
770 |
#else
|
771 |
buffer[i] = _wire->receive(); |
772 |
#endif
|
773 |
} |
774 |
|
775 |
/* ToDo: Check for errors! */
|
776 |
return true; |
777 |
} |