code:kalman_filter
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:kalman_filter [2008/04/15 20:31] – laurenceb | code:kalman_filter [2008/10/20 13:38] (current) – laurenceb | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Testing ===== | ===== Testing ===== | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | Test with data for an oscillating parafoil (hopefully won't happen). Green is the filter heading output, pink GPS heading, red gyro output, and blue filtered rate output. Time is in units of 20ms, so ticks are at 20 second intervals. | ||
{{code: | {{code: | ||
Line 12: | Line 18: | ||
The simulated gyro output in red, and filter turn rate output in green. | The simulated gyro output in red, and filter turn rate output in green. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
===== Kalman.c ===== | ===== Kalman.c ===== | ||
- | <code c> | + | <code c>#include " |
- | #include "main.h" | + | #include "matrix.h" |
- | kalman_state | + | void predict_and_update(kalman_state * Prev, kalman_model * model, vector * u, vector * y) |
{ | { | ||
- | kalman_state New; | + | predict_state(model, |
- | matrix K; | + | predict_P(model, |
- | New.state=predict_state(model, | + | matrix |
- | New.P=predict_P(model, | + | state_update(Prev, |
- | K=optimal_gain(model, | + | P_update(& |
- | New.state=state_update(Prev, | + | |
- | New.P=P_update(& | + | |
- | return New; | + | |
} | } | ||
- | vector | + | void predict_state(kalman_model * model, kalman_state * state, vector * u) //x is system state, u control input(s), B input model |
{ | { | ||
vector a; | vector a; | ||
Line 37: | Line 45: | ||
a=matrix_vector(& | a=matrix_vector(& | ||
b=matrix_vector(& | b=matrix_vector(& | ||
- | return | + | state-> |
} | } | ||
- | matrix | + | void predict_P(kalman_model * model, kalman_state * state) //F is propagation model, Q model noise |
{ | { | ||
matrix S=transpose(& | matrix S=transpose(& | ||
S=multiply(& | S=multiply(& | ||
S=multiply(& | S=multiply(& | ||
- | return | + | state->P = add(& |
} | } | ||
Line 53: | Line 61: | ||
matrix S=multiply(& | matrix S=multiply(& | ||
S=multiply(& | S=multiply(& | ||
- | S=add(& | + | S=add(& |
S=inverse(& | S=inverse(& | ||
S=multiply(& | S=multiply(& | ||
- | return multiply(& | + | return multiply(& |
} | } | ||
- | vector | + | void state_update(kalman_state * state, kalman_model * model, matrix * K, vector * y) //y is the measurements |
{ | { | ||
vector a=matrix_vector(& | vector a=matrix_vector(& | ||
- | a=vector_subtract(y,& | + | a=vector_subtract(y,& |
- | matrix_vector(K,& | + | if(a.t> |
- | return | + | { |
+ | a.t-=2*PI; | ||
+ | } | ||
+ | if(a.t< | ||
+ | { | ||
+ | a.t+=2*PI; | ||
+ | } | ||
+ | a=matrix_vector(K,& | ||
+ | state-> | ||
} | } | ||
- | matrix | + | void P_update(matrix *K, kalman_model * model, kalman_state * state) //K is optimal gain, H measurement model |
{ | { | ||
matrix S=multiply(K,& | matrix S=multiply(K,& | ||
- | S.tl=1-S.tl; | + | S.tl=1.0-S.tl; |
S.tr=-S.tr; | S.tr=-S.tr; | ||
S.bl=-S.bl; | S.bl=-S.bl; | ||
- | S.br=1-S.br; | + | S.br=1.0-S.br; |
- | return | + | state->P = multiply(& |
} | } | ||
</ | </ | ||
+ | |||
===== Kalman.h ===== | ===== Kalman.h ===== | ||
Line 84: | Line 101: | ||
{ | { | ||
vector state; | vector state; | ||
- | matrix P; //estimate covariance | + | matrix P; |
} kalman_state; | } kalman_state; | ||
Line 96: | Line 113: | ||
} kalman_model; | } kalman_model; | ||
- | kalman_state | + | void predict_and_update(kalman_state * Prev, kalman_model * model, vector * u, vector * y); |
- | vector | + | void predict_state(kalman_model * model, kalman_state * state, vector * u); |
- | matrix | + | void predict_P(kalman_model * model, kalman_state * state); |
matrix optimal_gain(kalman_model * model, kalman_state * state); | matrix optimal_gain(kalman_model * model, kalman_state * state); | ||
- | vector | + | void state_update(kalman_state * state, matrix * K, vector * y); |
- | matrix | + | void P_update(matrix *K, kalman_model * model, kalman_state * state); |
</ | </ | ||
===== Matrix.c ===== | ===== Matrix.c ===== | ||
- | <code c> | + | <code c># |
- | #include "main.h" | + | |
//matrix operations | //matrix operations | ||
Line 140: | Line 156: | ||
return ans; | return ans; | ||
} | } | ||
+ | /* | ||
matrix subtract(matrix * a, matrix * b) | matrix subtract(matrix * a, matrix * b) | ||
{ | { | ||
Line 150: | Line 166: | ||
return ans; | return ans; | ||
} | } | ||
+ | */ | ||
matrix transpose(matrix * a) | matrix transpose(matrix * a) | ||
{ | { | ||
Line 184: | Line 200: | ||
return ans; | return ans; | ||
} | } | ||
+ | |||
</ | </ | ||
code/kalman_filter.1208291469.txt.gz · Last modified: 2008/07/19 23:31 (external edit)