-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyro.c
executable file
·103 lines (77 loc) · 2.38 KB
/
gyro.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
task gyroDrift() {
int gyroError = 0;
float lastGyro;
while(true) {
if ( abs(lastGyro - SensorValue[gyro]) < 2)
gyroError += lastGyro - SensorValue[gyro];
lastGyro = SensorValue[gyro];
currentValueGyro = SensorValue[gyro] + gyroError;
wait1Msec(100);
}
}
task gyroTurn() {
while(true) {
errorGyro = targetGyro - currentValueGyro;
derivativeGyro = errorGyro - lastErrorGyro;
integralGyro = integralGyro + lastErrorGyro;
lastErrorGyro = errorGyro;
if(abs(integralGyro) > 380)
{
integralGyro = 0;
}
if(errorGyro == 0)
{
integralGyro = 0;
}
speedGyro = kpGyro * errorGyro + kiGyro * integralGyro + kdGyro * derivativeGyro;
driveL(-speedGyro);
driveR(speedGyro);
wait1Msec(25);
}
}
task kFilter() {
float kG; //kalman gain
float Eest = 10; // error in esitmate
float Emea = 20; //error in measurement
float lastEstimate; //last estimate
float measurement;
float GkG; //kalman gain
float GEest = 10; // error in esitmate
float GEmea = 20; //error in measurement
float GlastEstimate; //last estimate
float Gmeasurement;
float G2kG; //kalman gain
float G2Eest = 10; // error in esitmate
float G2Emea = 20; //error in measurement
float G2lastEstimate; //last estimate
float G2measurement;
float G3kG; //kalman gain
float G3Eest = 10; // error in esitmate
float G3Emea = 20; //error in measurement
float G3lastEstimate; //last estimate
float G3measurement;
while(true) {
lastEstimate = estimate;
measurement = FlyRPM;
kG = Eest / (Eest + Emea);
estimate = lastEstimate + kG * (measurement - lastEstimate);
Eest = (1 - kG) * (lastEstimate);
GlastEstimate = Gestimate;
Gmeasurement = currentValueGyro;
GkG = GEest / (GEest + GEmea);
Gestimate = GlastEstimate + GkG * (Gmeasurement - GlastEstimate);
GEest = (1 - GkG) * (GlastEstimate);
G2lastEstimate = G2estimate;
G2measurement = currentValueGyro2;
G2kG = G2Eest / (G2Eest + G2Emea);
G2estimate = G2lastEstimate + G2kG * (G2measurement - G2lastEstimate);
G2Eest = (1 - G2kG) * (G2lastEstimate);
gyroAvg = (Gestimate + G2estimate) / 2;
G3lastEstimate = G3estimate;
G3measurement = gyroAvg;
G3kG = G3Eest / (G3Eest + G3Emea);
G3estimate = G3lastEstimate + G3kG * (G3measurement - G3lastEstimate);
G3Eest = (1 - G3kG) * (G3lastEstimate);
wait1Msec(25);
}
}