-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkalman.cpp
59 lines (47 loc) · 1.6 KB
/
kalman.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "kalman.h"
//Kalman Variables
float f_1=1.00000; //cast as float
float kalman_x;
float kalman_x_last;
float kalman_p;
float kalman_p_last;
float kalman_k;
float kalman_q;
float kalman_r;
float kalman_x_temp;
float kalman_p_temp;
//end of Kalman Variables
//================================================================
// Kalman functions in your code
//================================================================
//Call KalmanInit() once.
//KalmanInit() - Call before any iterations of KalmanCalc()
void KalmanInit()
{
kalman_q=4.0001; //filter parameters, you can play around with them
kalman_r=.20001; // but these values appear to be fairly optimal
kalman_x = 0;
kalman_p = 0;
kalman_x_temp = 0;
kalman_p_temp = 0;
kalman_x_last = 0;
kalman_p_last = 0;
}
//KalmanCalc() - Calculates new Kalman values from float value "altitude"
// This will be the ASL altitude during the flight, and the AGL altitude during dumps
float KalmanCalc (float altitude)
{
//Predict kalman_x_temp, kalman_p_temp
kalman_x_temp = kalman_x_last;
kalman_p_temp = kalman_p_last + kalman_r;
//Update kalman values
kalman_k = (f_1/(kalman_p_temp + kalman_q)) * kalman_p_temp;
kalman_x = kalman_x_temp + (kalman_k * (altitude - kalman_x_temp));
kalman_p = (f_1 - kalman_k) * kalman_p_temp;
//Save this state for next time
kalman_x_last = kalman_x;
kalman_p_last = kalman_p;
//Assign current Kalman filtered altitude to working variables
//KAlt = kalman_x; //FLOAT Kalman-filtered altitude value
return kalman_x;
}