adafruit_bno055 / examples / sensorapi / sensorapi.ino @ master
History | View | Annotate | Download (5.574 KB)
1 | 4bc1c0c1 | Kevin Townsend | #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 | 8ac2d6f6 | Kevin Townsend | |
9 | 4bc1c0c1 | Kevin Townsend | 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 | 8ac2d6f6 | Kevin Townsend | |
18 | 4bc1c0c1 | Kevin Townsend | Connections |
19 | =========== |
||
20 | a64dd7d6 | Melissa LeBlanc-Williams | Connect SCL to SCL pin (analog 5 on Arduino UNO) |
21 | Connect SDA to SDA pin (analog 4 on Arduino UNO) |
||
22 | Connect VDD to 3-5V DC (depending on your board's logic level) |
||
23 | 4bc1c0c1 | Kevin Townsend | Connect GROUND to common ground |
24 | 8ac2d6f6 | Kevin Townsend | |
25 | 4bc1c0c1 | Kevin Townsend | History |
26 | ======= |
||
27 | 2015/MAR/03 - First release (KTOWN) |
||
28 | 3237e6e5 | Kevin Townsend | 2015/AUG/27 - Added calibration and system status helpers |
29 | 4bc1c0c1 | Kevin Townsend | */ |
30 | |||
31 | /* Set the delay between fresh samples */ |
||
32 | c4f272e1 | ladyada | #define BNO055_SAMPLERATE_DELAY_MS (100) |
33 | 8ac2d6f6 | Kevin Townsend | |
34 | e5a77cdb | Gintaras | // Check I2C device address and correct line below (by default address is 0x29 or 0x28) |
35 | // id, address |
||
36 | Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28); |
||
37 | 4bc1c0c1 | Kevin Townsend | |
38 | /**************************************************************************/ |
||
39 | /* |
||
40 | Displays some basic information on this sensor from the unified |
||
41 | sensor API sensor_t type (see Adafruit_Sensor for more information) |
||
42 | */ |
||
43 | /**************************************************************************/ |
||
44 | void displaySensorDetails(void) |
||
45 | { |
||
46 | sensor_t sensor; |
||
47 | bno.getSensor(&sensor); |
||
48 | Serial.println("------------------------------------"); |
||
49 | Serial.print ("Sensor: "); Serial.println(sensor.name); |
||
50 | Serial.print ("Driver Ver: "); Serial.println(sensor.version); |
||
51 | Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); |
||
52 | Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx"); |
||
53 | Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx"); |
||
54 | 8ac2d6f6 | Kevin Townsend | Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx"); |
55 | 4bc1c0c1 | Kevin Townsend | Serial.println("------------------------------------"); |
56 | Serial.println(""); |
||
57 | delay(500); |
||
58 | } |
||
59 | |||
60 | /**************************************************************************/ |
||
61 | /* |
||
62 | 8ac2d6f6 | Kevin Townsend | Display some basic info about the sensor status |
63 | */ |
||
64 | /**************************************************************************/ |
||
65 | void displaySensorStatus(void) |
||
66 | { |
||
67 | /* Get the system status values (mostly for debugging purposes) */ |
||
68 | uint8_t system_status, self_test_results, system_error; |
||
69 | system_status = self_test_results = system_error = 0; |
||
70 | bno.getSystemStatus(&system_status, &self_test_results, &system_error); |
||
71 | |||
72 | /* Display the results in the Serial Monitor */ |
||
73 | 32ddee6e | Kevin Townsend | Serial.println(""); |
74 | 8ac2d6f6 | Kevin Townsend | Serial.print("System Status: 0x"); |
75 | Serial.println(system_status, HEX); |
||
76 | Serial.print("Self Test: 0x"); |
||
77 | Serial.println(self_test_results, HEX); |
||
78 | Serial.print("System Error: 0x"); |
||
79 | Serial.println(system_error, HEX); |
||
80 | Serial.println(""); |
||
81 | delay(500); |
||
82 | } |
||
83 | |||
84 | /**************************************************************************/ |
||
85 | /* |
||
86 | 3237e6e5 | Kevin Townsend | Display sensor calibration status |
87 | */ |
||
88 | /**************************************************************************/ |
||
89 | void displayCalStatus(void) |
||
90 | { |
||
91 | /* Get the four calibration values (0..3) */ |
||
92 | /* Any sensor data reporting 0 should be ignored, */ |
||
93 | /* 3 means 'fully calibrated" */ |
||
94 | uint8_t system, gyro, accel, mag; |
||
95 | system = gyro = accel = mag = 0; |
||
96 | bno.getCalibration(&system, &gyro, &accel, &mag); |
||
97 | |||
98 | /* The data should be ignored until the system calibration is > 0 */ |
||
99 | Serial.print("\t"); |
||
100 | if (!system) |
||
101 | { |
||
102 | Serial.print("! "); |
||
103 | } |
||
104 | |||
105 | /* Display the individual values */ |
||
106 | Serial.print("Sys:"); |
||
107 | Serial.print(system, DEC); |
||
108 | Serial.print(" G:"); |
||
109 | Serial.print(gyro, DEC); |
||
110 | Serial.print(" A:"); |
||
111 | Serial.print(accel, DEC); |
||
112 | Serial.print(" M:"); |
||
113 | 32ddee6e | Kevin Townsend | Serial.print(mag, DEC); |
114 | 3237e6e5 | Kevin Townsend | } |
115 | |||
116 | /**************************************************************************/ |
||
117 | /* |
||
118 | 4bc1c0c1 | Kevin Townsend | Arduino setup function (automatically called at startup) |
119 | */ |
||
120 | /**************************************************************************/ |
||
121 | 8ac2d6f6 | Kevin Townsend | void setup(void) |
122 | 4bc1c0c1 | Kevin Townsend | { |
123 | e5a77cdb | Gintaras | Serial.begin(115200); |
124 | 4bc1c0c1 | Kevin Townsend | Serial.println("Orientation Sensor Test"); Serial.println(""); |
125 | 8ac2d6f6 | Kevin Townsend | |
126 | 4bc1c0c1 | Kevin Townsend | /* Initialise the sensor */ |
127 | if(!bno.begin()) |
||
128 | { |
||
129 | /* There was a problem detecting the BNO055 ... check your connections */ |
||
130 | Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); |
||
131 | while(1); |
||
132 | } |
||
133 | 8ac2d6f6 | Kevin Townsend | |
134 | 4bc1c0c1 | Kevin Townsend | delay(1000); |
135 | 8ac2d6f6 | Kevin Townsend | |
136 | 4bc1c0c1 | Kevin Townsend | /* Display some basic information on this sensor */ |
137 | displaySensorDetails(); |
||
138 | 67f3cff5 | Kevin Townsend | |
139 | 8ac2d6f6 | Kevin Townsend | /* Optional: Display current status */ |
140 | 32ddee6e | Kevin Townsend | displaySensorStatus(); |
141 | 8ac2d6f6 | Kevin Townsend | |
142 | c4f272e1 | ladyada | bno.setExtCrystalUse(true); |
143 | 4bc1c0c1 | Kevin Townsend | } |
144 | |||
145 | /**************************************************************************/ |
||
146 | /* |
||
147 | Arduino loop function, called once 'setup' is complete (your own code |
||
148 | should go here) |
||
149 | */ |
||
150 | /**************************************************************************/ |
||
151 | 8ac2d6f6 | Kevin Townsend | void loop(void) |
152 | 4bc1c0c1 | Kevin Townsend | { |
153 | 8ac2d6f6 | Kevin Townsend | /* Get a new sensor event */ |
154 | sensors_event_t event; |
||
155 | fcd68623 | Kevin Townsend | bno.getEvent(&event); |
156 | 8ac2d6f6 | Kevin Townsend | |
157 | fcd68623 | Kevin Townsend | /* Display the floating point data */ |
158 | 4bc1c0c1 | Kevin Townsend | Serial.print("X: "); |
159 | c4f272e1 | ladyada | Serial.print(event.orientation.x, 4); |
160 | Serial.print("\tY: "); |
||
161 | Serial.print(event.orientation.y, 4); |
||
162 | Serial.print("\tZ: "); |
||
163 | Serial.print(event.orientation.z, 4); |
||
164 | 3237e6e5 | Kevin Townsend | |
165 | /* Optional: Display calibration status */ |
||
166 | 32ddee6e | Kevin Townsend | displayCalStatus(); |
167 | |||
168 | /* Optional: Display sensor status (debug only) */ |
||
169 | //displaySensorStatus(); |
||
170 | 3237e6e5 | Kevin Townsend | |
171 | /* New line for the next sample */ |
||
172 | 48741e1f | Kevin Townsend | Serial.println(""); |
173 | 8ac2d6f6 | Kevin Townsend | |
174 | 3237e6e5 | Kevin Townsend | /* Wait the specified delay before requesting nex data */ |
175 | 4bc1c0c1 | Kevin Townsend | delay(BNO055_SAMPLERATE_DELAY_MS); |
176 | 48741e1f | Kevin Townsend | } |