Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
kalman_filter_vision.c
Go to the documentation of this file.
1 /*
2  * kalman_filter_vision.c
3  *
4  * Created on: Sep 12, 2016
5  * Author: knmcguire
6  */
7 #include "kalman_filter_vision.h"
9 
10 //TODO: implement kalman filter for larger states
11 //TODO: implement extended kalman filter
12 
13 
14 
26 void kalman_filter_linear_2D_float(float *model, float *measurements, float *covariance, float *state
27  , float *process_noise, float *measurement_noise)
28 {
29  //......................Preparation kalman filter ..................... //
30 
31  // process model (linear)
32  float _G[2][2];
33  MAKE_MATRIX_PTR(G, _G, 2);
34  G[0][0] = model[0];
35  G[0][1] = model[1];
36  G[1][0] = model[2];
37  G[1][1] = model[3];
38 
39  // transpose of G
40  float _Gtrans[2][2];
41  MAKE_MATRIX_PTR(Gtrans, _Gtrans, 2);
42  float_mat_copy(Gtrans, G, 2, 2);
43  float_mat_transpose(Gtrans, 2);
44 
45  // Observation model (linear)
46  // note: right now both velocity and acceleration are observed
47  float _H[2][2];
48  MAKE_MATRIX_PTR(H, _H, 2);
49  H[0][0] = 1.0f;
50  H[0][1] = 0.0f;
51  H[1][0] = 0.0f;
52  H[1][1] = 1.0f;
53 
54  //transpose of H
55  float _Htrans[2][2];
56  MAKE_MATRIX_PTR(Htrans, _Htrans, 2);
57  float_mat_copy(Htrans, H, 2, 2);
58  float_mat_transpose(Htrans, 2);
59 
60  //Previous state
61  float _Xprev[2][1];
62  MAKE_MATRIX_PTR(Xprev, _Xprev, 2);
63  Xprev[0][0] = state[0];
64  Xprev[1][0] = state[1]; //state[1];
65 
66  //Previous covariance
67  float _Pprevious[2][2];
68  MAKE_MATRIX_PTR(Pprevious, _Pprevious, 2);
69  Pprevious[0][0] = covariance[0];
70  Pprevious[0][1] = covariance[1];
71  Pprevious[1][0] = covariance[2];
72  Pprevious[1][1] = covariance[3];
73 
74  //measurements;
75  float _Z[2][1];
76  MAKE_MATRIX_PTR(Z, _Z, 2);
77  Z[0][0] = measurements[0];
78  Z[1][0] = measurements[1];
79 
80  //Process noise model
81  float _Q[2][2];
82  MAKE_MATRIX_PTR(Q, _Q, 2);
83  Q[0][0] = process_noise[0];
84  Q[0][1] = 0.0f;
85  Q[1][0] = 0.0f;
86  Q[1][1] = process_noise[1];
87 
88  //measurement nosie model
89  float _R[2][2];
90  MAKE_MATRIX_PTR(R, _R, 2);
91  R[0][0] = measurement_noise[0];
92  R[0][1] = 0.0f;
93  R[1][0] = 0.0f;
94  R[1][1] = measurement_noise[1];
95 
96  //Variables during kalman computation:
97  float _Xpredict[2][1];
98  MAKE_MATRIX_PTR(Xpredict, _Xpredict, 2);
99  float _Xnext[2][1];
100  MAKE_MATRIX_PTR(Xnext, _Xnext, 2);
101 
102  float _Ppredict[2][2];
103  MAKE_MATRIX_PTR(Ppredict, _Ppredict, 2);
104  float _Pnext[2][2];
105  MAKE_MATRIX_PTR(Pnext, _Pnext, 2);
106 
107  float _K[2][2];
108  MAKE_MATRIX_PTR(K, _K, 2);
109 
110  float _eye[2][2];
111  MAKE_MATRIX_PTR(eye, _eye, 2);
112  eye[0][0] = 1.0f;
113  eye[0][1] = 0.0f;
114  eye[1][0] = 0.0f;
115  eye[1][1] = 1.0f;
116 
117  float _temp_mat[2][2];
118  MAKE_MATRIX_PTR(temp_mat, _temp_mat, 2);
119  float _temp_mat2[2][2];
120  MAKE_MATRIX_PTR(temp_mat2, _temp_mat2, 2)
121  float _temp_mat3[2][2];
122  MAKE_MATRIX_PTR(temp_mat3, _temp_mat3, 2)
123 
124  float _temp_vec[2][1];
125  MAKE_MATRIX_PTR(temp_vec, _temp_vec, 2);
126  float _temp_vec2[2][1];
127  MAKE_MATRIX_PTR(temp_vec2, _temp_vec2, 2);
128 
129 
130  //......................KALMAN FILTER ..................... //
131 
132 
133  // 1. calculate state predict
134 
135  //Xpredict = G* Xprev;
136  float_mat_mul(Xpredict, G, Xprev, 2, 2, 1);
137  //......................KALMAN FILTER ..................... //
138 
139  // 2. calculate covariance predict
140 
141  // Ppredict = G*Pprevious*Gtrans + Q
142  //...Pprevious*Gtrans...
143  float_mat_mul(temp_mat, Pprevious, Gtrans, 2, 2, 2);
144  //G*Pprevious*Gtrans...
145  float_mat_mul(temp_mat2, G, temp_mat, 2, 2, 2);
146  //G*Pprevious*Gtrans+Q
147  float_mat_sum(Ppredict, temp_mat2, Q, 2, 2);
148 
149  // 3. Calculate Kalman gain
150 
151  // K = Ppredict * Htrans /( H * Ppredict * Htrans + R)
152  // ... Ppredict * Htrans ...
153  float_mat_mul(temp_mat, Ppredict, Htrans, 2, 2, 2);
154  //... H * Predict * Htrans
155  float_mat_mul(temp_mat2, H, temp_mat, 2, 2, 2);
156  //..( H * Ppredict * Htrans + R)
157  float_mat_sum(temp_mat3, temp_mat2, R, 2, 2);
158  //...inv( H * Ppredict * Htrans + R)
159  //TODO: Make a matrix inverse function for more than 2x2 matrix!
160 
161  float det_temp2 = 1 / (temp_mat3[0][0] * temp_mat3[1][1] - temp_mat3[0][1] * temp_mat3[1][0]);
162  temp_mat2[0][0] = det_temp2 * (temp_mat3[1][1]);
163  temp_mat2[0][1] = det_temp2 * (-1 * temp_mat3[1][0]);
164  temp_mat2[1][0] = det_temp2 * (-1 * temp_mat3[0][1]);
165  temp_mat2[1][1] = det_temp2 * (temp_mat3[0][0]);
166  // K = Ppredict * Htrans / *inv( H * Ppredict * Htrans + R)
167  float_mat_mul(K, temp_mat, temp_mat2, 2, 2, 2);
168 
169  // 4. Update state estimate
170 
171  //Xnext = Xpredict + K *(Z - Htrans * Xpredict)
172  // ... Htrans * Xpredict)
173  float_mat_mul(temp_vec, Htrans, Xpredict, 2, 2, 1);
174 
175  //... (Z - Htrans * Xpredict)
176  float_mat_diff(temp_vec2, Z, temp_vec, 2, 1);
177 
178  // ... K *(Z - Htrans * Xpredict)
179  float_mat_mul(temp_vec, K, temp_vec2, 2, 2, 1);
180 
181 
182  //Xnext = Xpredict + K *(Z - Htrans * Xpredict)
183  float_mat_sum(Xnext, Xpredict, temp_vec, 2, 1);
184 
185  // 5. Update covariance matrix
186 
187  // Pnext = (eye(2) - K*H)*P_predict
188  // ...K*H...
189  float_mat_mul(temp_mat, K, H, 2, 2, 2);
190  //(eye(2) - K*H)
191  float_mat_diff(temp_mat2, eye, temp_mat, 2, 2);
192  // Pnext = (eye(2) - K*H)*P_predict
193  float_mat_mul(Pnext, temp_mat2, Ppredict, 2, 2, 2);
194 
195 
196  //save values for next state
197  covariance[0] = Pnext[0][0];
198  covariance[1] = Pnext[0][1];;
199  covariance[2] = Pnext[1][0];;
200  covariance[3] = Pnext[1][1];;
201 
202 
203  state[0] = Xnext[0][0];
204  state[1] = Xnext[1][0];
205 }
#define Q
Definition: hf_float.c:74
static void float_mat_transpose(float **a, int n)
transpose square matrix
#define MAKE_MATRIX_PTR(_ptr, _mat, _rows)
Make a pointer to a matrix of _rows lines.
static gazebo::physics::ModelPtr model
void kalman_filter_linear_2D_float(float *model, float *measurements, float *covariance, float *state, float *process_noise, float *measurement_noise)
A simple linear 2D kalman filter, computed using floats and matrices.
static void float_mat_copy(float **a, float **b, int m, int n)
a = b
static void float_mat_diff(float **o, float **a, float **b, int m, int n)
o = a - b
Paparazzi floating point algebra.
static struct FloatVect3 H
static void float_mat_sum(float **o, float **a, float **b, int m, int n)
o = a + b
static void float_mat_mul(float **o, float **a, float **b, int m, int n, int l)
o = a * b
struct State state
Definition: state.c:36