Statistics
| Branch: | Revision:

adafruit_bno055 / examples / position / position.ino @ 5f6b7a75

History | View | Annotate | Download (2.945 KB)

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