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/07/19 23:33] – external edit 127.0.0.1 | code:kalman_filter [2008/10/20 13:38] (current) – laurenceb | ||
---|---|---|---|
Line 18: | 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. | ||
+ | |||
+ | |||
+ | |||
+ | |||
Line 26: | Line 30: | ||
- | 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(&New, | + | |
- | 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 44: | 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 60: | 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,& | ||
+ | if(a.t> | ||
+ | { | ||
+ | a.t-=2*PI; | ||
+ | } | ||
+ | if(a.t< | ||
+ | { | ||
+ | a.t+=2*PI; | ||
+ | } | ||
a=matrix_vector(K,& | a=matrix_vector(K,& | ||
- | return | + | 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 103: | 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); |
</ | </ | ||
code/kalman_filter.1216510404.txt.gz · Last modified: 2008/09/10 22:28 (external edit)