adafruit_bno055 / examples / sensorapi / sensorapi.ino @ 3cae40b9
History | View | Annotate | Download (5.345 KB)
1 |
#include <Wire.h> |
---|---|
2 |
#include <Adafruit_Sensor.h> |
3 |
#include <Adafruit_BNO055.h> |
4 |
#include <utility/imumaths.h> |
5 |
|
6 |
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), |
7 |
which provides a common 'type' for sensor data and some helper functions. |
8 |
|
9 |
To use this driver you will also need to download the Adafruit_Sensor |
10 |
library and include it in your libraries folder. |
11 |
|
12 |
You should also assign a unique ID to this sensor for use with |
13 |
the Adafruit Sensor API so that you can identify this particular |
14 |
sensor in any data logs, etc. To assign a unique ID, simply |
15 |
provide an appropriate value in the constructor below (12345 |
16 |
is used by default in this example). |
17 |
|
18 |
Connections |
19 |
=========== |
20 |
Connect SCL to analog 5 |
21 |
Connect SDA to analog 4 |
22 |
Connect VDD to 3-5V DC |
23 |
Connect GROUND to common ground |
24 |
|
25 |
History |
26 |
======= |
27 |
2015/MAR/03 - First release (KTOWN) |
28 |
2015/AUG/27 - Added calibration and system status helpers |
29 |
*/ |
30 |
|
31 |
/* Set the delay between fresh samples */ |
32 |
#define BNO055_SAMPLERATE_DELAY_MS (100) |
33 |
|
34 |
Adafruit_BNO055 bno = Adafruit_BNO055(55); |
35 |
|
36 |
/**************************************************************************/ |
37 |
/* |
38 |
Displays some basic information on this sensor from the unified |
39 |
sensor API sensor_t type (see Adafruit_Sensor for more information) |
40 |
*/ |
41 |
/**************************************************************************/ |
42 |
void displaySensorDetails(void) |
43 |
{ |
44 |
sensor_t sensor; |
45 |
bno.getSensor(&sensor); |
46 |
Serial.println("------------------------------------"); |
47 |
Serial.print ("Sensor: "); Serial.println(sensor.name); |
48 |
Serial.print ("Driver Ver: "); Serial.println(sensor.version); |
49 |
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); |
50 |
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx"); |
51 |
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx"); |
52 |
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx"); |
53 |
Serial.println("------------------------------------"); |
54 |
Serial.println(""); |
55 |
delay(500); |
56 |
} |
57 |
|
58 |
/**************************************************************************/ |
59 |
/* |
60 |
Display some basic info about the sensor status |
61 |
*/ |
62 |
/**************************************************************************/ |
63 |
void displaySensorStatus(void) |
64 |
{ |
65 |
/* Get the system status values (mostly for debugging purposes) */ |
66 |
uint8_t system_status, self_test_results, system_error; |
67 |
system_status = self_test_results = system_error = 0; |
68 |
bno.getSystemStatus(&system_status, &self_test_results, &system_error); |
69 |
|
70 |
/* Display the results in the Serial Monitor */ |
71 |
Serial.println(""); |
72 |
Serial.print("System Status: 0x"); |
73 |
Serial.println(system_status, HEX); |
74 |
Serial.print("Self Test: 0x"); |
75 |
Serial.println(self_test_results, HEX); |
76 |
Serial.print("System Error: 0x"); |
77 |
Serial.println(system_error, HEX); |
78 |
Serial.println(""); |
79 |
delay(500); |
80 |
} |
81 |
|
82 |
/**************************************************************************/ |
83 |
/* |
84 |
Display sensor calibration status |
85 |
*/ |
86 |
/**************************************************************************/ |
87 |
void displayCalStatus(void) |
88 |
{ |
89 |
/* Get the four calibration values (0..3) */ |
90 |
/* Any sensor data reporting 0 should be ignored, */ |
91 |
/* 3 means 'fully calibrated" */ |
92 |
uint8_t system, gyro, accel, mag; |
93 |
system = gyro = accel = mag = 0; |
94 |
bno.getCalibration(&system, &gyro, &accel, &mag); |
95 |
|
96 |
/* The data should be ignored until the system calibration is > 0 */ |
97 |
Serial.print("\t"); |
98 |
if (!system) |
99 |
{ |
100 |
Serial.print("! "); |
101 |
} |
102 |
|
103 |
/* Display the individual values */ |
104 |
Serial.print("Sys:"); |
105 |
Serial.print(system, DEC); |
106 |
Serial.print(" G:"); |
107 |
Serial.print(gyro, DEC); |
108 |
Serial.print(" A:"); |
109 |
Serial.print(accel, DEC); |
110 |
Serial.print(" M:"); |
111 |
Serial.print(mag, DEC); |
112 |
} |
113 |
|
114 |
/**************************************************************************/ |
115 |
/* |
116 |
Arduino setup function (automatically called at startup) |
117 |
*/ |
118 |
/**************************************************************************/ |
119 |
void setup(void) |
120 |
{ |
121 |
Serial.begin(9600); |
122 |
Serial.println("Orientation Sensor Test"); Serial.println(""); |
123 |
|
124 |
/* Initialise the sensor */ |
125 |
if(!bno.begin()) |
126 |
{ |
127 |
/* There was a problem detecting the BNO055 ... check your connections */ |
128 |
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); |
129 |
while(1); |
130 |
} |
131 |
|
132 |
delay(1000); |
133 |
|
134 |
/* Display some basic information on this sensor */ |
135 |
displaySensorDetails(); |
136 |
|
137 |
/* Optional: Display current status */ |
138 |
displaySensorStatus(); |
139 |
|
140 |
bno.setExtCrystalUse(true); |
141 |
} |
142 |
|
143 |
/**************************************************************************/ |
144 |
/* |
145 |
Arduino loop function, called once 'setup' is complete (your own code |
146 |
should go here) |
147 |
*/ |
148 |
/**************************************************************************/ |
149 |
void loop(void) |
150 |
{ |
151 |
/* Get a new sensor event */ |
152 |
sensors_event_t event; |
153 |
bno.getEvent(&event); |
154 |
|
155 |
/* Display the floating point data */ |
156 |
Serial.print("X: "); |
157 |
Serial.print(event.orientation.x, 4); |
158 |
Serial.print("\tY: "); |
159 |
Serial.print(event.orientation.y, 4); |
160 |
Serial.print("\tZ: "); |
161 |
Serial.print(event.orientation.z, 4); |
162 |
|
163 |
/* Optional: Display calibration status */ |
164 |
displayCalStatus(); |
165 |
|
166 |
/* Optional: Display sensor status (debug only) */ |
167 |
//displaySensorStatus(); |
168 |
|
169 |
/* New line for the next sample */ |
170 |
Serial.println(""); |
171 |
|
172 |
/* Wait the specified delay before requesting nex data */ |
173 |
delay(BNO055_SAMPLERATE_DELAY_MS); |
174 |
} |