Revision 7faf77c3
examples/position/position.ino | ||
---|---|---|
1 |
#include <Wire.h> |
|
2 |
#include <Adafruit_Sensor.h> |
|
3 |
#include <Adafruit_BNO055.h> |
|
4 |
|
|
5 |
double xPos = 0, yPos = 0, headingVel = 0; |
|
6 |
uint16_t BNO055_SAMPLERATE_DELAY_MS = 10; //how often to read data from the board |
|
7 |
uint16_t PRINT_DELAY_MS = 500; // how often to print the data |
|
8 |
uint16_t printCount = 0; //counter to avoid printing every 10MS sample |
|
9 |
|
|
10 |
//velocity = accel*dt (dt in seconds) |
|
11 |
//position = 0.5*accel*dt^2 |
|
12 |
double ACCEL_VEL_TRANSITION = (double)(BNO055_SAMPLERATE_DELAY_MS) / 1000.0; |
|
13 |
double ACCEL_POS_TRANSITION = 0.5 * ACCEL_VEL_TRANSITION * ACCEL_VEL_TRANSITION; |
|
14 |
double DEG_2_RAD = 0.01745329251; //trig functions require radians, BNO055 outputs degrees |
|
15 |
|
|
16 |
Adafruit_BNO055 bno = Adafruit_BNO055(55); |
|
17 |
|
|
18 |
void setup(void) |
|
19 |
{ |
|
20 |
Serial.begin(115200); |
|
21 |
if (!bno.begin()) |
|
22 |
{ |
|
23 |
Serial.print("No BNO055 detected"); |
|
24 |
while (1); |
|
25 |
} |
|
26 |
|
|
27 |
|
|
28 |
delay(1000); |
|
29 |
} |
|
30 |
|
|
31 |
void loop(void) |
|
32 |
{ |
|
33 |
// |
|
34 |
unsigned long tStart = micros(); |
|
35 |
sensors_event_t orientationData , linearAccelData; |
|
36 |
bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER); |
|
37 |
// bno.getEvent(&angVelData, Adafruit_BNO055::VECTOR_GYROSCOPE); |
|
38 |
bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); |
|
39 |
|
|
40 |
xPos = xPos + ACCEL_POS_TRANSITION * linearAccelData.acceleration.x; |
|
41 |
yPos = yPos + ACCEL_POS_TRANSITION * linearAccelData.acceleration.y; |
|
42 |
|
|
43 |
// velocity of sensor in the direction it's facing |
|
44 |
headingVel = ACCEL_VEL_TRANSITION * linearAccelData.acceleration.x / cos(DEG_2_RAD * orientationData.orientation.x); |
|
45 |
|
|
46 |
if (printCount * BNO055_SAMPLERATE_DELAY_MS >= PRINT_DELAY_MS) { |
|
47 |
//enough iterations have passed that we can print the latest data |
|
48 |
Serial.print("Heading: "); |
|
49 |
Serial.println(orientationData.orientation.x); |
|
50 |
Serial.print("Position: "); |
|
51 |
Serial.print(xPos); |
|
52 |
Serial.print(" , "); |
|
53 |
Serial.println(yPos); |
|
54 |
Serial.print("Speed: "); |
|
55 |
Serial.println(headingVel); |
|
56 |
Serial.println("-------"); |
|
57 |
|
|
58 |
printCount = 0; |
|
59 |
} |
|
60 |
else { |
|
61 |
printCount = printCount + 1; |
|
62 |
} |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
while ((micros() - tStart) < (BNO055_SAMPLERATE_DELAY_MS * 1000)) |
|
67 |
{ |
|
68 |
//poll until the next sample is ready |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
void printEvent(sensors_event_t* event) { |
|
73 |
Serial.println(); |
|
74 |
Serial.print(event->type); |
|
75 |
double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem |
|
76 |
if (event->type == SENSOR_TYPE_ACCELEROMETER) { |
|
77 |
x = event->acceleration.x; |
|
78 |
y = event->acceleration.y; |
|
79 |
z = event->acceleration.z; |
|
80 |
} |
|
81 |
else if (event->type == SENSOR_TYPE_ORIENTATION) { |
|
82 |
x = event->orientation.x; |
|
83 |
y = event->orientation.y; |
|
84 |
z = event->orientation.z; |
|
85 |
} |
|
86 |
else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { |
|
87 |
x = event->magnetic.x; |
|
88 |
y = event->magnetic.y; |
|
89 |
z = event->magnetic.z; |
|
90 |
} |
|
91 |
else if ((event->type == SENSOR_TYPE_GYROSCOPE) || (event->type == SENSOR_TYPE_ROTATION_VECTOR)) { |
|
92 |
x = event->gyro.x; |
|
93 |
y = event->gyro.y; |
|
94 |
z = event->gyro.z; |
|
95 |
} |
|
96 |
|
|
97 |
Serial.print(": x= "); |
|
98 |
Serial.print(x); |
|
99 |
Serial.print(" | y= "); |
|
100 |
Serial.print(y); |
|
101 |
Serial.print(" | z= "); |
|
102 |
Serial.println(z); |
|
103 |
} |
|
104 |
|
examples/read_all_data/read_all_data.ino | ||
---|---|---|
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.3-5V DC |
|
23 |
Connect GROUND to common ground |
|
24 |
|
|
25 |
History |
|
26 |
======= |
|
27 |
2015/MAR/03 - First release (KTOWN) |
|
28 |
*/ |
|
29 |
|
|
30 |
/* Set the delay between fresh samples */ |
|
31 |
uint16_t BNO055_SAMPLERATE_DELAY_MS = 100; |
|
32 |
|
|
33 |
Adafruit_BNO055 bno = Adafruit_BNO055(55); |
|
34 |
|
|
35 |
void setup(void) |
|
36 |
{ |
|
37 |
Serial.begin(115200); |
|
38 |
Serial.println("Orientation Sensor Test"); Serial.println(""); |
|
39 |
|
|
40 |
/* Initialise the sensor */ |
|
41 |
if (!bno.begin()) |
|
42 |
{ |
|
43 |
/* There was a problem detecting the BNO055 ... check your connections */ |
|
44 |
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); |
|
45 |
while (1); |
|
46 |
} |
|
47 |
|
|
48 |
delay(1000); |
|
49 |
} |
|
50 |
|
|
51 |
void loop(void) |
|
52 |
{ |
|
53 |
//could add VECTOR_ACCELEROMETER, VECTOR_MAGNETOMETER,VECTOR_GRAVITY... |
|
54 |
sensors_event_t orientationData , angVelocityData , linearAccelData; |
|
55 |
bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER); |
|
56 |
bno.getEvent(&angVelocityData, Adafruit_BNO055::VECTOR_GYROSCOPE); |
|
57 |
bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); |
|
58 |
|
|
59 |
printEvent(&orientationData); |
|
60 |
printEvent(&angVelocityData); |
|
61 |
printEvent(&linearAccelData); |
|
62 |
|
|
63 |
int8_t boardTemp = bno.getTemp(); |
|
64 |
Serial.print(F("temperature: ")); |
|
65 |
Serial.println(boardTemp); |
|
66 |
|
|
67 |
|
|
68 |
delay(BNO055_SAMPLERATE_DELAY_MS); |
|
69 |
} |
|
70 |
|
|
71 |
void printEvent(sensors_event_t* event) { |
|
72 |
Serial.println(); |
|
73 |
Serial.print(event->type); |
|
74 |
double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem |
|
75 |
if (event->type == SENSOR_TYPE_ACCELEROMETER) { |
|
76 |
x = event->acceleration.x; |
|
77 |
y = event->acceleration.y; |
|
78 |
z = event->acceleration.z; |
|
79 |
} |
|
80 |
else if (event->type == SENSOR_TYPE_ORIENTATION) { |
|
81 |
x = event->orientation.x; |
|
82 |
y = event->orientation.y; |
|
83 |
z = event->orientation.z; |
|
84 |
} |
|
85 |
else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { |
|
86 |
x = event->magnetic.x; |
|
87 |
y = event->magnetic.y; |
|
88 |
z = event->magnetic.z; |
|
89 |
} |
|
90 |
else if ((event->type == SENSOR_TYPE_GYROSCOPE) || (event->type == SENSOR_TYPE_ROTATION_VECTOR)) { |
|
91 |
x = event->gyro.x; |
|
92 |
y = event->gyro.y; |
|
93 |
z = event->gyro.z; |
|
94 |
} |
|
95 |
|
|
96 |
Serial.print(": x= "); |
|
97 |
Serial.print(x); |
|
98 |
Serial.print(" | y= "); |
|
99 |
Serial.print(y); |
|
100 |
Serial.print(" | z= "); |
|
101 |
Serial.println(z); |
|
102 |
} |
|
103 |
|
|
104 |
|
Also available in: Unified diff