-
Notifications
You must be signed in to change notification settings - Fork 22
/
BNO055+BMP280_Basic_AHRS_t3.ino
1342 lines (1169 loc) · 55.9 KB
/
BNO055+BMP280_Basic_AHRS_t3.ino
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BNO055_BMP280_t3 Basic Example Code
by: Kris Winer
date: April 25, 2015
license: Beerware - Use this code however you'd like. If you
find it useful you can buy me a beer some time.
Demonstrates basic BNO055 functionality including parameterizing the register addresses,
initializing the sensor, communicating with pressure sensor BMP280,
getting properly scaled accelerometer, gyroscope, and magnetometer data out.
Added display functions to allow display to on breadboard monitor.
Addition of 9 DoF sensor fusion using open source Madgwick and Mahony filter algorithms.
Can compare results to hardware 9 DoF sensor fusion carried out on the BNO055.
Sketch runs on the 3.3 V 8 MHz Pro Mini and the Teensy 3.1.
This sketch is intended specifically for the BNO055+BMP280 Add-On Shield for the Teensy 3.1.
It uses SDA/SCL on pins 17/16, respectively, and it uses the Teensy 3.1-specific Wire library i2c_t3.h.
The Add-on shield can also be used as a stand-alone breakout board for any Arduino, Teensy, or
other microcontroller by closing the solder jumpers on the back of the board.
The BMP280 is a simple but high resolution (20-bit) pressure sensor, which can be used in its high resolution
mode but with power consumption of 20 microAmp, or in a lower resolution mode with power consumption of
only 1 microAmp. The choice will depend on the application.
All sensors communicate via I2C at 400 Hz or higher.
SDA and SCL should have external pull-up resistors (to 3.3V).
4K7 resistors are on the BNO055_BMP280 breakout board.
Hardware setup:
Breakout Board --------- Arduino/Teensy
3V3 ---------------------- 3.3V
SDA -----------------------A4/17
SCL -----------------------A5/16
GND ---------------------- GND
Note: The BNO055_BMP280 breakout board is an I2C sensor and uses the Arduino Wire or Teensy i2c_t3.h library.
Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1.
We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file.
We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
The Teensy has no internal pullups and we are using the Wire.begin function of the i2c_t3.h library
to select 400 Hz i2c speed.
*/
//#include <Wire.h>
#include <i2c_t3.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Using NOKIA 5110 monochrome 84 x 48 pixel display
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 3 - LCD chip select (SCE)
// pin 4 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 3, 4);
// BMP280 registers
#define BMP280_TEMP_XLSB 0xFC
#define BMP280_TEMP_LSB 0xFB
#define BMP280_TEMP_MSB 0xFA
#define BMP280_PRESS_XLSB 0xF9
#define BMP280_PRESS_LSB 0xF8
#define BMP280_PRESS_MSB 0xF7
#define BMP280_CONFIG 0xF5
#define BMP280_CTRL_MEAS 0xF4
#define BMP280_STATUS 0xF3
#define BMP280_RESET 0xE0
#define BMP280_ID 0xD0 // should be 0x58
#define BMP280_CALIB00 0x88
// BNO055 Register Map
// http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_10_Release.pdf
//
// BNO055 Page 0
#define BNO055_CHIP_ID 0x00 // should be 0xA0
#define BNO055_ACC_ID 0x01 // should be 0xFB
#define BNO055_MAG_ID 0x02 // should be 0x32
#define BNO055_GYRO_ID 0x03 // should be 0x0F
#define BNO055_SW_REV_ID_LSB 0x04
#define BNO055_SW_REV_ID_MSB 0x05
#define BNO055_BL_REV_ID 0x06
#define BNO055_PAGE_ID 0x07
#define BNO055_ACC_DATA_X_LSB 0x08
#define BNO055_ACC_DATA_X_MSB 0x09
#define BNO055_ACC_DATA_Y_LSB 0x0A
#define BNO055_ACC_DATA_Y_MSB 0x0B
#define BNO055_ACC_DATA_Z_LSB 0x0C
#define BNO055_ACC_DATA_Z_MSB 0x0D
#define BNO055_MAG_DATA_X_LSB 0x0E
#define BNO055_MAG_DATA_X_MSB 0x0F
#define BNO055_MAG_DATA_Y_LSB 0x10
#define BNO055_MAG_DATA_Y_MSB 0x11
#define BNO055_MAG_DATA_Z_LSB 0x12
#define BNO055_MAG_DATA_Z_MSB 0x13
#define BNO055_GYR_DATA_X_LSB 0x14
#define BNO055_GYR_DATA_X_MSB 0x15
#define BNO055_GYR_DATA_Y_LSB 0x16
#define BNO055_GYR_DATA_Y_MSB 0x17
#define BNO055_GYR_DATA_Z_LSB 0x18
#define BNO055_GYR_DATA_Z_MSB 0x19
#define BNO055_EUL_HEADING_LSB 0x1A
#define BNO055_EUL_HEADING_MSB 0x1B
#define BNO055_EUL_ROLL_LSB 0x1C
#define BNO055_EUL_ROLL_MSB 0x1D
#define BNO055_EUL_PITCH_LSB 0x1E
#define BNO055_EUL_PITCH_MSB 0x1F
#define BNO055_QUA_DATA_W_LSB 0x20
#define BNO055_QUA_DATA_W_MSB 0x21
#define BNO055_QUA_DATA_X_LSB 0x22
#define BNO055_QUA_DATA_X_MSB 0x23
#define BNO055_QUA_DATA_Y_LSB 0x24
#define BNO055_QUA_DATA_Y_MSB 0x25
#define BNO055_QUA_DATA_Z_LSB 0x26
#define BNO055_QUA_DATA_Z_MSB 0x27
#define BNO055_LIA_DATA_X_LSB 0x28
#define BNO055_LIA_DATA_X_MSB 0x29
#define BNO055_LIA_DATA_Y_LSB 0x2A
#define BNO055_LIA_DATA_Y_MSB 0x2B
#define BNO055_LIA_DATA_Z_LSB 0x2C
#define BNO055_LIA_DATA_Z_MSB 0x2D
#define BNO055_GRV_DATA_X_LSB 0x2E
#define BNO055_GRV_DATA_X_MSB 0x2F
#define BNO055_GRV_DATA_Y_LSB 0x30
#define BNO055_GRV_DATA_Y_MSB 0x31
#define BNO055_GRV_DATA_Z_LSB 0x32
#define BNO055_GRV_DATA_Z_MSB 0x33
#define BNO055_TEMP 0x34
#define BNO055_CALIB_STAT 0x35
#define BNO055_ST_RESULT 0x36
#define BNO055_INT_STATUS 0x37
#define BNO055_SYS_CLK_STATUS 0x38
#define BNO055_SYS_STATUS 0x39
#define BNO055_SYS_ERR 0x3A
#define BNO055_UNIT_SEL 0x3B
#define BNO055_OPR_MODE 0x3D
#define BNO055_PWR_MODE 0x3E
#define BNO055_SYS_TRIGGER 0x3F
#define BNO055_TEMP_SOURCE 0x40
#define BNO055_AXIS_MAP_CONFIG 0x41
#define BNO055_AXIS_MAP_SIGN 0x42
#define BNO055_ACC_OFFSET_X_LSB 0x55
#define BNO055_ACC_OFFSET_X_MSB 0x56
#define BNO055_ACC_OFFSET_Y_LSB 0x57
#define BNO055_ACC_OFFSET_Y_MSB 0x58
#define BNO055_ACC_OFFSET_Z_LSB 0x59
#define BNO055_ACC_OFFSET_Z_MSB 0x5A
#define BNO055_MAG_OFFSET_X_LSB 0x5B
#define BNO055_MAG_OFFSET_X_MSB 0x5C
#define BNO055_MAG_OFFSET_Y_LSB 0x5D
#define BNO055_MAG_OFFSET_Y_MSB 0x5E
#define BNO055_MAG_OFFSET_Z_LSB 0x5F
#define BNO055_MAG_OFFSET_Z_MSB 0x60
#define BNO055_GYR_OFFSET_X_LSB 0x61
#define BNO055_GYR_OFFSET_X_MSB 0x62
#define BNO055_GYR_OFFSET_Y_LSB 0x63
#define BNO055_GYR_OFFSET_Y_MSB 0x64
#define BNO055_GYR_OFFSET_Z_LSB 0x65
#define BNO055_GYR_OFFSET_Z_MSB 0x66
#define BNO055_ACC_RADIUS_LSB 0x67
#define BNO055_ACC_RADIUS_MSB 0x68
#define BNO055_MAG_RADIUS_LSB 0x69
#define BNO055_MAG_RADIUS_MSB 0x6A
//
// BNO055 Page 1
#define BNO055_PAGE_ID 0x07
#define BNO055_ACC_CONFIG 0x08
#define BNO055_MAG_CONFIG 0x09
#define BNO055_GYRO_CONFIG_0 0x0A
#define BNO055_GYRO_CONFIG_1 0x0B
#define BNO055_ACC_SLEEP_CONFIG 0x0C
#define BNO055_GYR_SLEEP_CONFIG 0x0D
#define BNO055_INT_MSK 0x0F
#define BNO055_INT_EN 0x10
#define BNO055_ACC_AM_THRES 0x11
#define BNO055_ACC_INT_SETTINGS 0x12
#define BNO055_ACC_HG_DURATION 0x13
#define BNO055_ACC_HG_THRESH 0x14
#define BNO055_ACC_NM_THRESH 0x15
#define BNO055_ACC_NM_SET 0x16
#define BNO055_GYR_INT_SETTINGS 0x17
#define BNO055_GYR_HR_X_SET 0x18
#define BNO055_GYR_DUR_X 0x19
#define BNO055_GYR_HR_Y_SET 0x1A
#define BNO055_GYR_DUR_Y 0x1B
#define BNO055_GYR_HR_Z_SET 0x1C
#define BNO055_GYR_DUR_Z 0x1D
#define BNO055_GYR_AM_THRESH 0x1E
#define BNO055_GYR_AM_SET 0x1F
// Using the BNO055_BMP280 breakout board/Teensy 3.1 Add-On Shield, ADO is set to 1 by default
#define ADO 1
#if ADO
#define BNO055_ADDRESS 0x29 // Device address of BNO055 when ADO = 1
#define BMP280_ADDRESS 0x77 // Address of BMP280
#else
#define BNO055_ADDRESS 0x28 // Device address of BNO055 when ADO = 0
#define BMP280_ADDRESS 0x77 // Address of BMP280
#endif
#define SerialDebug true // set to true to get Serial output for debugging
// Set initial input parameters
enum Ascale { // ACC Full Scale
AFS_2G = 0,
AFS_4G,
AFS_8G,
AFS_18G
};
enum Abw { // ACC Bandwidth
ABW_7_81Hz = 0,
ABW_15_63Hz,
ABW_31_25Hz,
ABW_62_5Hz,
ABW_125Hz,
ABW_250Hz,
ABW_500Hz,
ABW_1000Hz, //0x07
};
enum APwrMode { // ACC Pwr Mode
NormalA = 0,
SuspendA,
LowPower1A,
StandbyA,
LowPower2A,
DeepSuspendA
};
enum Gscale { // gyro full scale
GFS_2000DPS = 0,
GFS_1000DPS,
GFS_500DPS,
GFS_250DPS,
GFS_125DPS // 0x04
};
enum GPwrMode { // GYR Pwr Mode
NormalG = 0,
FastPowerUpG,
DeepSuspendedG,
SuspendG,
AdvancedPowerSaveG
};
enum Gbw { // gyro bandwidth
GBW_523Hz = 0,
GBW_230Hz,
GBW_116Hz,
GBW_47Hz,
GBW_23Hz,
GBW_12Hz,
GBW_64Hz,
GBW_32Hz
};
enum OPRMode { // BNO-55 operation modes
CONFIGMODE = 0x00,
// Sensor Mode
ACCONLY,
MAGONLY,
GYROONLY,
ACCMAG,
ACCGYRO,
MAGGYRO,
AMG, // 0x07
// Fusion Mode
IMU,
COMPASS,
M4G,
NDOF_FMC_OFF,
NDOF // 0x0C
};
enum PWRMode {
Normalpwr = 0,
Lowpower,
Suspendpwr
};
enum Modr { // magnetometer output data rate
MODR_2Hz = 0,
MODR_6Hz,
MODR_8Hz,
MODR_10Hz,
MODR_15Hz,
MODR_20Hz,
MODR_25Hz,
MODR_30Hz
};
enum MOpMode { // MAG Op Mode
LowPower = 0,
Regular,
EnhancedRegular,
HighAccuracy
};
enum MPwrMode { // MAG power mode
Normal = 0,
Sleep,
Suspend,
ForceMode
};
enum Posr {
P_OSR_00 = 0, // no op
P_OSR_01,
P_OSR_02,
P_OSR_04,
P_OSR_08,
P_OSR_16
};
enum Tosr {
T_OSR_00 = 0, // no op
T_OSR_01,
T_OSR_02,
T_OSR_04,
T_OSR_08,
T_OSR_16
};
enum IIRFilter {
full = 0, // bandwidth at full sample rate
BW0_223ODR,
BW0_092ODR,
BW0_042ODR,
BW0_021ODR // bandwidth at 0.021 x sample rate
};
enum Mode {
BMP280Sleep = 0,
forced,
forced2,
normal
};
enum SBy {
t_00_5ms = 0,
t_62_5ms,
t_125ms,
t_250ms,
t_500ms,
t_1000ms,
t_2000ms,
t_4000ms,
};
// Specify BMP280 configuration
uint8_t Posr = P_OSR_16, Tosr = T_OSR_02, Mode = normal, IIRFilter = BW0_042ODR, SBy = t_62_5ms; // set pressure amd temperature output data rate
// t_fine carries fine temperature as global value for BMP280
int32_t t_fine;
//
uint8_t GPwrMode = NormalG; // Gyro power mode
uint8_t Gscale = GFS_250DPS; // Gyro full scale
//uint8_t Godr = GODR_250Hz; // Gyro sample rate
uint8_t Gbw = GBW_23Hz; // Gyro bandwidth
//
uint8_t Ascale = AFS_2G; // Accel full scale
//uint8_t Aodr = AODR_250Hz; // Accel sample rate
uint8_t APwrMode = NormalA; // Accel power mode
uint8_t Abw = ABW_31_25Hz; // Accel bandwidth, accel sample rate divided by ABW_divx
//
//uint8_t Mscale = MFS_4Gauss; // Select magnetometer full-scale resolution
uint8_t MOpMode = Regular; // Select magnetometer perfomance mode
uint8_t MPwrMode = Normal; // Select magnetometer power mode
uint8_t Modr = MODR_10Hz; // Select magnetometer ODR when in BNO055 bypass mode
uint8_t PWRMode = Normalpwr; // Select BNO055 power mode
uint8_t OPRMode = NDOF; // specify operation mode for sensors
uint8_t status; // BNO055 data status register
float aRes, gRes, mRes; // scale resolutions per LSB for the sensors
// Pin definitions
int intPin = 8; // These can be changed, 2 and 3 are the Arduinos ext int pins
int myLed = 13;
uint16_t Pcal[8]; // calibration constants from BMP280 PROM registers
unsigned char nCRC; // calculated check sum to ensure PROM integrity
uint32_t D1 = 0, D2 = 0; // raw BMP280 pressure and temperature data
double dT, OFFSET, SENS, T2, OFFSET2, SENS2; // First order and second order corrections for raw S5637 temperature and pressure data
int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output
int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output
int16_t magCount[3]; // Stores the 16-bit signed magnetometer sensor output
int16_t quatCount[4]; // Stores the 16-bit signed quaternion output
int16_t EulCount[3]; // Stores the 16-bit signed Euler angle output
int16_t LIACount[3]; // Stores the 16-bit signed linear acceleration output
int16_t GRVCount[3]; // Stores the 16-bit signed gravity vector output
float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}, magBias[3] = {0, 0, 0}; // Bias corrections for gyro, accelerometer, and magnetometer
int16_t tempGCount, tempMCount; // temperature raw count output of mag and gyro
float Gtemperature, Mtemperature; // Stores the BNO055 gyro and LIS3MDL mag internal chip temperatures in degrees Celsius
double Temperature, Pressure; // stores BMP280 pressures sensor pressure and temperature
int32_t rawPress, rawTemp; // pressure and temperature raw count output for BMP280
// global constants for 9 DoF fusion and AHRS (Attitude and Heading Reference System)
float GyroMeasError = PI * (40.0f / 180.0f); // gyroscope measurement error in rads/s (start at 40 deg/s)
float GyroMeasDrift = PI * (0.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
// There is a tradeoff in the beta parameter between accuracy and response speed.
// In the original Madgwick study, beta of 0.041 (corresponding to GyroMeasError of 2.7 degrees/s) was found to give optimal accuracy.
// However, with this value, the LSM9SD0 response time is about 10 seconds to a stable initial quaternion.
// Subsequent changes also require a longish lag time to a stable output, not fast enough for a quadcopter or robot car!
// By increasing beta (GyroMeasError) by about a factor of fifteen, the response time constant is reduced to ~2 sec
// I haven't noticed any reduction in solution accuracy. This is essentially the I coefficient in a PID control sense;
// the bigger the feedback coefficient, the faster the solution converges, usually at the expense of accuracy.
// In any case, this is the free parameter in the Madgwick filtering and fusion scheme.
float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta
float zeta = sqrt(3.0f / 4.0f) * GyroMeasDrift; // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
#define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
#define Ki 0.0f
uint32_t delt_t = 0, count = 0, sumCount = 0; // used to control display output rate
float pitch, yaw, roll;
float Pitch, Yaw, Roll;
float LIAx, LIAy, LIAz, GRVx, GRVy, GRVz;
float deltat = 0.0f, sum = 0.0f; // integration interval for both filter schemes
uint32_t lastUpdate = 0, firstUpdate = 0; // used to calculate integration interval
uint32_t Now = 0; // used to calculate integration interval
float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values
float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
float quat[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
float eInt[3] = {0.0f, 0.0f, 0.0f}; // vector to hold integral error for Mahony method
// BMP280 compensation parameters
uint16_t dig_T1, dig_P1;
int16_t dig_T2, dig_T3, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
void setup()
{
// Wire.begin();
// TWBR = 12; // 400 kbit/sec I2C speed for Pro Mini
// Setup for Master mode, pins 16/17, external pullups, 400kHz for Teensy 3.1
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_16_17, I2C_PULLUP_EXT, I2C_RATE_400);
delay(4000);
Serial.begin(38400);
// Set up the interrupt pin, its set as active high, push-pull
pinMode(intPin, INPUT);
pinMode(myLed, OUTPUT);
digitalWrite(myLed, HIGH);
display.begin(); // Initialize the display
display.setContrast(58); // Set the contrast
// Start device display with ID of sensor
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0); display.print("BNO055");
display.setTextSize(1);
display.setCursor(0, 20); display.print("9-DOF 16-bit");
display.setCursor(0, 30); display.print("motion sensor");
display.setCursor(20,40); display.print("60 ug LSB");
display.display();
delay(1000);
// Set up for data display
display.setTextSize(1); // Set text size to normal, 2 is twice normal etc.
display.setTextColor(BLACK); // Set pixel color; 1 on the monochrome screen
display.clearDisplay(); // clears the screen and buffer
I2Cscan(); // check for I2C devices on the bus8
// Read the WHO_AM_I register, this is a good test of communication
Serial.println("BNO055 9-axis motion sensor...");
byte c = readByte(BNO055_ADDRESS, BNO055_CHIP_ID); // Read WHO_AM_I register for BNO055
Serial.print("BNO055 Address = 0x"); Serial.println(BNO055_ADDRESS, HEX);
Serial.print("BNO055 WHO_AM_I = 0x"); Serial.println(BNO055_CHIP_ID, HEX);
Serial.print("BNO055 "); Serial.print("I AM "); Serial.print(c, HEX); Serial.println(" I should be 0xA0");
display.setCursor(20,0); display.print("BNO055");
display.setCursor(0,10); display.print("I AM");
display.setCursor(0,20); display.print(c, HEX);
display.setCursor(0,30); display.print("I Should Be");
display.setCursor(0,40); display.print(0xA0, HEX);
display.display();
delay(1000);
// Read the WHO_AM_I register of the accelerometer, this is a good test of communication
byte d = readByte(BNO055_ADDRESS, BNO055_ACC_ID); // Read WHO_AM_I register for accelerometer
Serial.print("BNO055 ACC "); Serial.print("I AM "); Serial.print(d, HEX); Serial.println(" I should be 0xFB");
display.clearDisplay();
display.setCursor(20,0); display.print("BNO055 ACC");
display.setCursor(0,10); display.print("I AM");
display.setCursor(0,20); display.print(d, HEX);
display.setCursor(0,30); display.print("I Should Be");
display.setCursor(0,40); display.print(0xFB, HEX);
display.display();
delay(1000);
// Read the WHO_AM_I register of the magnetometer, this is a good test of communication
byte e = readByte(BNO055_ADDRESS, BNO055_MAG_ID); // Read WHO_AM_I register for magnetometer
Serial.print("BNO055 MAG "); Serial.print("I AM "); Serial.print(e, HEX); Serial.println(" I should be 0x32");
display.clearDisplay();
display.setCursor(20,0); display.print("BNO055 MAG");
display.setCursor(0,10); display.print("I AM");
display.setCursor(0,20); display.print(e, HEX);
display.setCursor(0,30); display.print("I Should Be");
display.setCursor(0,40); display.print(0x32, HEX);
display.display();
delay(1000);
// Read the WHO_AM_I register of the gyroscope, this is a good test of communication
byte f = readByte(BNO055_ADDRESS, BNO055_GYRO_ID); // Read WHO_AM_I register for LIS3MDL
Serial.print("BNO055 GYRO "); Serial.print("I AM "); Serial.print(f, HEX); Serial.println(" I should be 0x0F");
display.clearDisplay();
display.setCursor(20,0); display.print("BNO055 GYRO");
display.setCursor(0,10); display.print("I AM");
display.setCursor(0,20); display.print(f, HEX);
display.setCursor(0,30); display.print("I Should Be");
display.setCursor(0,40); display.print(0x0F, HEX);
display.display();
delay(1000);
if (c == 0xA0) // BNO055 WHO_AM_I should always be 0xA0
{
Serial.println("BNO055 is online...");
// Check software revision ID
byte swlsb = readByte(BNO055_ADDRESS, BNO055_SW_REV_ID_LSB);
byte swmsb = readByte(BNO055_ADDRESS, BNO055_SW_REV_ID_MSB);
Serial.print("BNO055 SW Revision ID: "); Serial.print(swmsb, HEX); Serial.print("."); Serial.println(swlsb, HEX);
Serial.println("Should be 03.04");
// Check bootloader version
byte blid = readByte(BNO055_ADDRESS, BNO055_BL_REV_ID);
Serial.print("BNO055 bootloader Version: "); Serial.println(blid);
// Check self-test results
byte selftest = readByte(BNO055_ADDRESS, BNO055_ST_RESULT);
if(selftest & 0x01) {
Serial.println("accelerometer passed selftest");
} else {
Serial.println("accelerometer failed selftest");
}
if(selftest & 0x02) {
Serial.println("magnetometer passed selftest");
} else {
Serial.println("magnetometer failed selftest");
}
if(selftest & 0x04) {
Serial.println("gyroscope passed selftest");
} else {
Serial.println("gyroscope failed selftest");
}
if(selftest & 0x08) {
Serial.println("MCU passed selftest");
} else {
Serial.println("MCU failed selftest");
}
delay(1000);
// Read the WHO_AM_I register of the BMP280 this is a good test of communication
byte f = readByte(BMP280_ADDRESS, BMP280_ID); // Read WHO_AM_I register for BMP280
Serial.print("BMP280 ");
Serial.print("I AM ");
Serial.print(f, HEX);
Serial.print(" I should be ");
Serial.println(0x58, HEX);
Serial.println(" ");
display.clearDisplay();
display.setCursor(20,0);
display.print("BMP280");
display.setCursor(0,10);
display.print("I AM");
display.setCursor(0,20);
display.print(e, HEX);
display.setCursor(0,30);
display.print("I Should Be");
display.setCursor(0,40);
display.print(0x58, HEX);
display.display();
delay(1000);
writeByte(BMP280_ADDRESS, BMP280_RESET, 0xB6); // reset BMP280 before initilization
delay(100);
BMP280Init(); // Initialize BMP280 altimeter
Serial.println("Calibration coeficients:");
Serial.print("dig_T1 =");
Serial.println(dig_T1);
Serial.print("dig_T2 =");
Serial.println(dig_T2);
Serial.print("dig_T3 =");
Serial.println(dig_T3);
Serial.print("dig_P1 =");
Serial.println(dig_P1);
Serial.print("dig_P2 =");
Serial.println(dig_P2);
Serial.print("dig_P3 =");
Serial.println(dig_P3);
Serial.print("dig_P4 =");
Serial.println(dig_P4);
Serial.print("dig_P5 =");
Serial.println(dig_P5);
Serial.print("dig_P6 =");
Serial.println(dig_P6);
Serial.print("dig_P7 =");
Serial.println(dig_P7);
Serial.print("dig_P8 =");
Serial.println(dig_P8);
Serial.print("dig_P9 =");
Serial.println(dig_P9);
accelgyroCalBNO055(accelBias, gyroBias);
Serial.println("Average accelerometer bias (mg) = "); Serial.println(accelBias[0]); Serial.println(accelBias[1]); Serial.println(accelBias[2]);
Serial.println("Average gyro bias (dps) = "); Serial.println(gyroBias[0]); Serial.println(gyroBias[1]); Serial.println(gyroBias[2]);
display.clearDisplay();
display.setCursor(0, 0); display.print("BNO055 bias");
display.setCursor(0, 8); display.print(" x y z ");
display.setCursor(0, 16); display.print((int)(accelBias[0]));
display.setCursor(24, 16); display.print((int)(accelBias[1]));
display.setCursor(48, 16); display.print((int)(accelBias[2]));
display.setCursor(72, 16); display.print("mg");
display.setCursor(0, 24); display.print(gyroBias[0], 1);
display.setCursor(24, 24); display.print(gyroBias[1], 1);
display.setCursor(48, 24); display.print(gyroBias[2], 1);
display.setCursor(72, 24); display.print("dps");
display.display();
delay(1000);
magCalBNO055(magBias);
Serial.println("Average magnetometer bias (mG) = "); Serial.println(magBias[0]); Serial.println(magBias[1]); Serial.println(magBias[2]);
display.clearDisplay();
display.setCursor(0, 0); display.print("BNO055 bias");
display.setCursor(0, 8); display.print(" x y z ");
display.setCursor(0, 16); display.print((int)(magBias[0]));
display.setCursor(24, 16); display.print((int)(magBias[1]));
display.setCursor(48, 16); display.print((int)(magBias[2]));
display.setCursor(72, 16); display.print("mG");
display.display();
delay(1000);
// Check calibration status of the sensors
uint8_t calstat = readByte(BNO055_ADDRESS, BNO055_CALIB_STAT);
Serial.println("Not calibrated = 0, fully calibrated = 3");
Serial.print("System calibration status "); Serial.println( (0xC0 & calstat) >> 6);
Serial.print("Gyro calibration status "); Serial.println( (0x30 & calstat) >> 4);
Serial.print("Accel calibration status "); Serial.println( (0x0C & calstat) >> 2);
Serial.print("Mag calibration status "); Serial.println( (0x03 & calstat) >> 0);
initBNO055(); // Initialize the BNO055
Serial.println("BNO055 initialized for sensor mode...."); // Initialize BNO055 for sensor read
}
else
{
Serial.print("Could not connect to BNO055: 0x");
Serial.println(c, HEX);
while(1) ; // Loop forever if communication doesn't happen
}
}
void loop()
{
readAccelData(accelCount); // Read the x/y/z adc values
// Now we'll calculate the accleration value into actual mg's
ax = (float)accelCount[0]; // - accelBias[0]; // subtract off calculated accel bias
ay = (float)accelCount[1]; // - accelBias[1];
az = (float)accelCount[2]; // - accelBias[2];
readGyroData(gyroCount); // Read the x/y/z adc values
// Calculate the gyro value into actual degrees per second
gx = (float)gyroCount[0]/16.; // - gyroBias[0]; // subtract off calculated gyro bias
gy = (float)gyroCount[1]/16.; // - gyroBias[1];
gz = (float)gyroCount[2]/16.; // - gyroBias[2];
readMagData(magCount); // Read the x/y/z adc values
// Calculate the magnetometer values in milliGauss
mx = (float)magCount[0]/1.6; // - magBias[0]; // get actual magnetometer value in mGauss
my = (float)magCount[1]/1.6; // - magBias[1];
mz = (float)magCount[2]/1.6; // - magBias[2];
readQuatData(quatCount); // Read the x/y/z adc values
// Calculate the quaternion values
quat[0] = (float)(quatCount[0])/16384.;
quat[1] = (float)(quatCount[1])/16384.;
quat[2] = (float)(quatCount[2])/16384.;
quat[3] = (float)(quatCount[3])/16384.;
readEulData(EulCount); // Read the x/y/z adc values
// Calculate the Euler angles values in degrees
Yaw = (float)EulCount[0]/16.;
Roll = (float)EulCount[1]/16.;
Pitch = (float)EulCount[2]/16.;
readLIAData(LIACount); // Read the x/y/z adc values
// Calculate the linear acceleration (sans gravity) values in mg
LIAx = (float)LIACount[0];
LIAy = (float)LIACount[1];
LIAz = (float)LIACount[2];
readGRVData(GRVCount); // Read the x/y/z adc values
// Calculate the linear acceleration (sans gravity) values in mg
GRVx = (float)GRVCount[0];
GRVy = (float)GRVCount[1];
GRVz = (float)GRVCount[2];
Now = micros();
deltat = ((Now - lastUpdate)/1000000.0f); // set integration time by time elapsed since last filter update
lastUpdate = Now;
sum += deltat; // sum for averaging filter update rate
sumCount++;
// Sensors x, y, and z-axes for the three sensor: accel, gyro, and magnetometer are all aligned, so
// no allowance for any orientation mismatch in feeding the output to the quaternion filter is required.
// For the BNO055, the sensor forward is along the x-axis just like
// in the LSM9DS0 and MPU9250 sensors. This rotation can be modified to allow any convenient orientation convention.
// This is ok by aircraft orientation standards!
// Pass gyro rate as rad/s
MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, mx, my, mz);
// MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, mx, my, mz);
// Serial print and/or display at 0.5 s rate independent of data rates
delt_t = millis() - count;
if (delt_t > 500) { // update LCD once per half-second independent of read rate
// check BNO-055 error status at 2 Hz rate
uint8_t sysstat = readByte(BNO055_ADDRESS, BNO055_SYS_STATUS); // check system status
Serial.print("System Status = 0x"); Serial.println(sysstat, HEX);
if(sysstat == 0x05) Serial.println("Sensor fusion algorithm running");
if(sysstat == 0x06) Serial.println("Sensor fusion not algorithm running");
if(sysstat == 0x01) {
uint8_t syserr = readByte(BNO055_ADDRESS, BNO055_SYS_ERR);
if(syserr == 0x01) Serial.println("Peripheral initialization error");
if(syserr == 0x02) Serial.println("System initialization error");
if(syserr == 0x03) Serial.println("Self test result failed");
if(syserr == 0x04) Serial.println("Register map value out of range");
if(syserr == 0x05) Serial.println("Register map address out of range");
if(syserr == 0x06) Serial.println("Register map write error");
if(syserr == 0x07) Serial.println("BNO low power mode no available for selected operation mode");
if(syserr == 0x08) Serial.println("Accelerometer power mode not available");
if(syserr == 0x09) Serial.println("Fusion algorithm configuration error");
if(syserr == 0x0A) Serial.println("Sensor configuration error");
}
if(SerialDebug) {
Serial.print("ax = "); Serial.print((int)ax);
Serial.print(" ay = "); Serial.print((int)ay);
Serial.print(" az = "); Serial.print((int)az); Serial.println(" mg");
Serial.print("gx = "); Serial.print( gx, 2);
Serial.print(" gy = "); Serial.print( gy, 2);
Serial.print(" gz = "); Serial.print( gz, 2); Serial.println(" deg/s");
Serial.print("mx = "); Serial.print( (int)mx );
Serial.print(" my = "); Serial.print( (int)my );
Serial.print(" mz = "); Serial.print( (int)mz ); Serial.println(" mG");
Serial.print("qx = "); Serial.print(q[0]);
Serial.print(" qy = "); Serial.print(q[1]);
Serial.print(" qz = "); Serial.print(q[2]);
Serial.print(" qw = "); Serial.println(q[3]);
Serial.print("quatw = "); Serial.print(quat[0]);
Serial.print(" quatx = "); Serial.print(quat[1]);
Serial.print(" quaty = "); Serial.print(quat[2]);
Serial.print(" quatz = "); Serial.println(quat[3]);
}
tempGCount = readGyroTempData(); // Read the gyro adc values
Gtemperature = (float) tempGCount; // Gyro chip temperature in degrees Centigrade
// Print gyro die temperature in degrees Centigrade
Serial.print("Gyro temperature is "); Serial.print(Gtemperature, 1); Serial.println(" degrees C"); // Print T values to tenths of a degree C
rawPress = readBMP280Pressure();
Pressure = (float) bmp280_compensate_P(rawPress)/25600.; // Pressure in mbar
rawTemp = readBMP280Temperature();
Temperature = (float) bmp280_compensate_T(rawTemp)/100.;
float altitude = 145366.45f*(1.0f - pow((Pressure/1013.25f), 0.190284f));
if(SerialDebug) {
Serial.println("BMP280:");
Serial.print("Altimeter temperature = ");
Serial.print( Temperature, 2);
Serial.println(" C"); // temperature in degrees Celsius
Serial.print("Altimeter temperature = ");
Serial.print(9.*Temperature/5. + 32., 2);
Serial.println(" F"); // temperature in degrees Fahrenheit
Serial.print("Altimeter pressure = ");
Serial.print(Pressure, 2);
Serial.println(" mbar");// pressure in millibar
Serial.print("Altitude = ");
Serial.print(altitude, 2);
Serial.println(" feet");
Serial.println(" ");
}
// Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
// In this coordinate system, the positive z-axis is down toward Earth.
// Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
// Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
// Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
// These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
// Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
// applied in the correct order which for this configuration is yaw, pitch, and then roll.
// For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
pitch *= 180.0f / PI;
yaw *= 180.0f / PI;
// yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
roll *= 180.0f / PI;
if(SerialDebug) {
Serial.print("Software Yaw, Pitch, Roll: ");
Serial.print(yaw, 2);
Serial.print(", ");
Serial.print(pitch, 2);
Serial.print(", ");
Serial.println(roll, 2);
Serial.print("Hardware Yaw, Pitch, Roll: ");
Serial.print(Yaw, 2);
Serial.print(", ");
Serial.print(Pitch, 2);
Serial.print(", ");
Serial.println(Roll, 2);
Serial.print("Hardware x, y, z linear acceleration: ");
Serial.print(LIAx, 2);
Serial.print(", ");
Serial.print(LIAy, 2);
Serial.print(", ");
Serial.println(LIAz, 2);
Serial.print("Hardware x, y, z gravity vector: ");
Serial.print(GRVx, 2);
Serial.print(", ");
Serial.print(GRVy, 2);
Serial.print(", ");
Serial.println(GRVz, 2);
Serial.print("rate = "); Serial.print((float)sumCount/sum, 2); Serial.println(" Hz");
}
/* display.clearDisplay();
display.setCursor(0, 0); display.print(" x y z ");
display.setCursor(0, 8); display.print((int)(1000*ax));
display.setCursor(24, 8); display.print((int)(1000*ay));
display.setCursor(48, 8); display.print((int)(1000*az));
display.setCursor(72, 8); display.print("mg");
display.setCursor(0, 16); display.print((int)(gx));
display.setCursor(24, 16); display.print((int)(gy));
display.setCursor(48, 16); display.print((int)(gz));
display.setCursor(66, 16); display.print("o/s");
display.setCursor(0, 24); display.print((int)(mx));
display.setCursor(24, 24); display.print((int)(my));
display.setCursor(48, 24); display.print((int)(mz));
display.setCursor(72, 24); display.print("mG");
display.setCursor(0, 32); display.print((int)(yaw));
display.setCursor(24, 32); display.print((int)(pitch));
display.setCursor(48, 32); display.print((int)(roll));
display.setCursor(66, 32); display.print("ypr");
// With these settings the filter is updating at a ~145 Hz rate using the Madgwick scheme and
// >200 Hz using the Mahony scheme even though the display refreshes at only 2 Hz.
// The filter update rate is determined mostly by the mathematical steps in the respective algorithms,
// the processor speed (8 MHz for the 3.3V Pro Mini), and the magnetometer ODR:
// an ODR of 10 Hz for the magnetometer produce the above rates, maximum magnetometer ODR of 100 Hz produces
// filter update rates of 36 - 145 and ~38 Hz for the Madgwick and Mahony schemes, respectively.
// This is presumably because the magnetometer read takes longer than the gyro or accelerometer reads.
// This filter update rate should be fast enough to maintain accurate platform orientation for
// stabilization control of a fast-moving robot or quadcopter. Compare to the update rate of 200 Hz
// produced by the on-board Digital Motion Processor of Invensense's MPU6050 6 DoF and MPU9150 9DoF sensors.
// The 3.3 V 8 MHz Pro Mini is doing pretty well!
display.setCursor(0, 40); display.print(altitude, 0); display.print("ft");
display.setCursor(68, 0); display.print(9.*Temperature/5. + 32., 0);
display.setCursor(42, 40); display.print((float) sumCount / (1000.*sum), 2); display.print("kHz");
display.display();
*/
digitalWrite(myLed, !digitalRead(myLed));
count = millis();
sumCount = 0;
sum = 0;
}
}
//===================================================================================================================
//====== Set of useful function to access acceleration. gyroscope, magnetometer, and temperature data
//===================================================================================================================
void readAccelData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z accel register data stored here
readBytes(BNO055_ADDRESS, BNO055_ACC_DATA_X_LSB, 6, &rawData[0]); // Read the six raw data registers into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
void readGyroData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_GYR_DATA_X_LSB, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
int8_t readGyroTempData()
{
return readByte(BNO055_ADDRESS, BNO055_TEMP); // Read the two raw data registers sequentially into data array
}
void readMagData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_MAG_DATA_X_LSB, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
void readQuatData(int16_t * destination)
{
uint8_t rawData[8]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
destination[3] = ((int16_t)rawData[7] << 8) | rawData[6] ;
}
void readEulData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_EUL_HEADING_LSB, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
void readLIAData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_LIA_DATA_X_LSB, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
void readGRVData(int16_t * destination)
{
uint8_t rawData[6]; // x/y/z gyro register data stored here
readBytes(BNO055_ADDRESS, BNO055_GRV_DATA_X_LSB, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
}
void initBNO055() {
// Select BNO055 config mode
writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, CONFIGMODE );
delay(25);
// Select page 1 to configure sensors
writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x01);
// Configure ACC
writeByte(BNO055_ADDRESS, BNO055_ACC_CONFIG, APwrMode << 5 | Abw << 2 | Ascale );
// Configure GYR
writeByte(BNO055_ADDRESS, BNO055_GYRO_CONFIG_0, Gbw << 3 | Gscale );
writeByte(BNO055_ADDRESS, BNO055_GYRO_CONFIG_1, GPwrMode);
// Configure MAG
writeByte(BNO055_ADDRESS, BNO055_MAG_CONFIG, MPwrMode << 5 | MOpMode << 3 | Modr );
// Select page 0 to read sensors
writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x00);
// Select BNO055 gyro temperature source
writeByte(BNO055_ADDRESS, BNO055_TEMP_SOURCE, 0x01 );