-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkalmen_filter.ino
More file actions
42 lines (31 loc) · 934 Bytes
/
Copy pathkalmen_filter.ino
File metadata and controls
42 lines (31 loc) · 934 Bytes
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
// KasBot V1 - Kalman filter module
float Q_angle = 0.001; //0.001
float Q_gyro = 0.003; //0.003
float R_angle = 0.03; //0.03
float x_bias = 0;
float P_00 = 0, P_01 = 0, P_10 = 0, P_11 = 0;
float y, S;
float K_0, K_1;
// newAngle = angle measured with atan2 using the accelerometer
// newRate = angle measured using the gyro
// looptime = loop time in millis()
float kalmanCalculate(float newAngle, float newRate,int looptime, float previousAngle)
{
float dt = float(looptime)/1000;
previousAngle += dt * (newRate - x_bias);
P_00 += - dt * (P_10 + P_01) + Q_angle * dt;
P_01 += - dt * P_11;
P_10 += - dt * P_11;
P_11 += + Q_gyro * dt;
y = newAngle - previousAngle;
S = P_00 + R_angle;
K_0 = P_00 / S;
K_1 = P_10 / S;
previousAngle += K_0 * y;
x_bias += K_1 * y;
P_00 -= K_0 * P_00;
P_01 -= K_0 * P_01;
P_10 -= K_1 * P_00;
P_11 -= K_1 * P_01;
return previousAngle;
}