-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccelerometer.cpp
45 lines (31 loc) · 929 Bytes
/
Accelerometer.cpp
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
#include "Accelerometer.hpp"
Accelerometer::Accelerometer(unsigned short ain_x, unsigned short ain_y, unsigned short ain_z)
:adc_x(ain_x), adc_y(ain_y), adc_z(ain_z)
{
}
Accelerometer::~Accelerometer()
{
}
float Accelerometer::roll()
{
float x, y, z, roll;
//Get 3-axis gravity
x = adc_x.GetValue() * scaling + offset;
y = adc_y.GetValue() * scaling + offset + 0.15;
z = (adc_z.GetValue() + biasCorrection) * scaling + offset;
//Get roll
roll = atan(y / sqrt(x*x + z*z)) * (180.0 / PI);
// printf("%f\t%f\t%f\t==>\t%f\n", x, y, z, roll);
return roll;
}
float Accelerometer::pitch()
{
float x, y, z, pitch;
//Get 3-axis gravity
x = adc_x.GetValue() * scaling + offset;
y = adc_y.GetValue() * scaling + offset;
z = (adc_z.GetValue() + biasCorrection) * scaling + offset;
//Get pitch
pitch = atan(x / sqrt(y*y + z*z)) * (180.0 / PI);
return pitch;
}