-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccel.ino
72 lines (59 loc) · 1.64 KB
/
accel.ino
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
#define NUM_READINGS 10
#include "accel.h"
#include "quaternion.h"
struct AccelAxis {
int readings[NUM_READINGS];
int axisPin;
int rawMin, rawMax;
int currentReading;
AccelAxis(int axisPin, int rawMin, int rawMax) :
axisPin(axisPin),
rawMin(rawMin),
rawMax(rawMax),
currentReading(0)
{
for (size_t i = 0; i < NUM_READINGS; i++) {
readSample();
delay(5);
}
}
void readSample() {
readings[currentReading] = analogRead(axisPin);
currentReading = (currentReading + 1) % NUM_READINGS;
}
int getRawValue() {
long value = 0;
for (int i = 0; i < NUM_READINGS; i++) {
value += readings[i];
}
return value / NUM_READINGS;
}
int getScaledValue() {
return map(getRawValue(), rawMin, rawMax, -1000, 1000);
}
};
static AccelAxis xAxis(A3, 419, 619);
static AccelAxis yAxis(A2, 403, 612);
static AccelAxis zAxis(A1, 405, 609);
static Quaternion correction;
void initAccelerometer() {
Vector3f initialDirection = uncorrectedDirection();
correction = getRotationFromTo(normalize(initialDirection), -Vector3f::UnitY);
}
void sampleAccelerometer() {
xAxis.readSample();
yAxis.readSample();
zAxis.readSample();
}
static Vector3f uncorrectedDirection() {
return (Vector3f(
xAxis.getScaledValue(),
yAxis.getScaledValue(),
zAxis.getScaledValue()
));
}
Vector3f accelerometerDirection() {
Vector3f dir = uncorrectedDirection();
return normalize(correction*dir);
}