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/09/10 22:28] – laurenceb | 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 28: | Line 31: | ||
void predict_and_update(kalman_state * Prev, kalman_model * model, vector * u, vector * y) | void predict_and_update(kalman_state * Prev, kalman_model * model, vector * u, vector * y) | ||
- | |||
{ | { | ||
- | |||
predict_state(model, | predict_state(model, | ||
predict_P(model, | predict_P(model, | ||
- | |||
matrix K=optimal_gain(model, | matrix K=optimal_gain(model, | ||
state_update(Prev, | state_update(Prev, | ||
- | |||
P_update(& | P_update(& | ||
} | } | ||
- | |||
- | |||
void predict_state(kalman_model * model, kalman_state * state, vector * u) //x is system state, u control input(s), B input model | 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; | ||
- | |||
vector b; | vector b; | ||
- | |||
a=matrix_vector(& | a=matrix_vector(& | ||
- | |||
b=matrix_vector(& | b=matrix_vector(& | ||
- | |||
state-> | state-> | ||
- | |||
} | } | ||
- | |||
- | |||
void predict_P(kalman_model * model, kalman_state * state) //F is propagation model, Q model noise | 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(& | ||
- | |||
state-> | state-> | ||
- | |||
} | } | ||
- | |||
- | |||
matrix optimal_gain(kalman_model * model, kalman_state * state) //H is measurement model, R measurement noise | matrix optimal_gain(kalman_model * model, kalman_state * state) //H is measurement model, R measurement noise | ||
- | |||
{ | { | ||
- | |||
matrix T=transpose(& | matrix T=transpose(& | ||
- | |||
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(& | ||
- | |||
} | } | ||
- | |||
- | |||
void state_update(kalman_state * state, kalman_model * model, matrix * K, vector * y) //y is the measurements | 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,& | ||
- | |||
state-> | state-> | ||
- | |||
} | } | ||
- | |||
- | |||
void P_update(matrix *K, kalman_model * model, kalman_state * state) //K is optimal gain, H measurement model | 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.0-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.0-S.br; | S.br=1.0-S.br; | ||
- | |||
state-> | state-> | ||
- | |||
} | } | ||
+ | </ | ||
- | </ | ||
===== Kalman.h ===== | ===== Kalman.h ===== | ||
Line 152: | 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.1221085731.txt.gz · Last modified: 2008/09/10 22:28 by laurenceb