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