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