Robot Control Library
|
Kalman filter implementation.
This may be used to implement a discrete time linear or extended kalman filter.
For the linear case, initialize the filter with rc_kalman_alloc_lin() which takes in the linear state matrices. These are then saved and used by rc_kalman_update_lin to calculate the predicted state x_pre and predicted sensor measurements h internally each step.
Basic loop structure for Linear case:
For the non-linear EKF case, the user should initialize the filter with rc_kalman_alloc_ekf() which does NOT take in the state transition matrices F,G,H. Instead it's up to the user to calculate the new predicted state x_pre and predicted sensor measurement h for their own non-linear system model each step. Those user-calculated predictions are then given to the non-linear rc_kalman_update_ekf() function.
Basic loop structure for non-linear EKF case:
Data Structures | |
struct | rc_kalman_t |
Macros | |
#define | RC_KALMAN_INITIALIZER |
Typedefs | |
typedef struct rc_kalman_t | rc_kalman_t |
Functions | |
rc_kalman_t | rc_kalman_empty (void) |
Critical function for initializing rc_kalman_t structs. More... | |
int | rc_kalman_alloc_lin (rc_kalman_t *kf, rc_matrix_t F, rc_matrix_t G, rc_matrix_t H, rc_matrix_t Q, rc_matrix_t R, rc_matrix_t Pi) |
Allocates memory for a Kalman filter of given dimensions. More... | |
int | rc_kalman_alloc_ekf (rc_kalman_t *kf, rc_matrix_t Q, rc_matrix_t R, rc_matrix_t Pi) |
Allocates memory for a Kalman filter of given dimensions. More... | |
int | rc_kalman_free (rc_kalman_t *kf) |
Frees the memory allocated by a kalman filter's matrices and vectors. Also resets all values to 0 like rc_kalman_empty(). More... | |
int | rc_kalman_reset (rc_kalman_t *kf) |
reset state and dynamics of the filter to 0 More... | |
int | rc_kalman_update_lin (rc_kalman_t *kf, rc_vector_t u, rc_vector_t y) |
Kalman Filter state prediction step based on physical model. More... | |
int | rc_kalman_update_ekf (rc_kalman_t *kf, rc_matrix_t F, rc_matrix_t H, rc_vector_t x_pre, rc_vector_t y, rc_vector_t h) |
Kalman Filter measurement update step. More... | |
#define RC_KALMAN_INITIALIZER |
rc_kalman_t rc_kalman_empty | ( | void | ) |
Critical function for initializing rc_kalman_t structs.
This is a very important function. If your rc_kalman_t struct is not a global variable, then its initial contents cannot be guaranteed to be anything in particular. Therefore it could contain problematic contents which could interfere with functions in this library. Therefore, you should always initialize your filters with rc_kalman_empty() before using with any other function in this library such as rc_kalman_alloc. This serves the same purpose as rc_matrix_empty, rc_vector_empty, rc_filter_empty, and rc_ringbuf_empty.
int rc_kalman_alloc_lin | ( | rc_kalman_t * | kf, |
rc_matrix_t | F, | ||
rc_matrix_t | G, | ||
rc_matrix_t | H, | ||
rc_matrix_t | Q, | ||
rc_matrix_t | R, | ||
rc_matrix_t | Pi | ||
) |
Allocates memory for a Kalman filter of given dimensions.
kf | pointer to struct to be allocated | |
[in] | F | undriven state-transition model |
[in] | G | control input model |
[in] | H | observation model |
[in] | Q | Process noise covariance, can be updated later |
[in] | R | Measurement noise covariance, can be updated later |
[in] | Pi | Initial P matrix |
int rc_kalman_alloc_ekf | ( | rc_kalman_t * | kf, |
rc_matrix_t | Q, | ||
rc_matrix_t | R, | ||
rc_matrix_t | Pi | ||
) |
Allocates memory for a Kalman filter of given dimensions.
kf | pointer to struct to be allocated | |
[in] | Q | Process noise covariance, can be updated later |
[in] | R | Measurement noise covariance, can be updated later |
[in] | Pi | Initial P matrix |
int rc_kalman_free | ( | rc_kalman_t * | kf | ) |
Frees the memory allocated by a kalman filter's matrices and vectors. Also resets all values to 0 like rc_kalman_empty().
kf | pointer to user's rc_kalman_t struct |
int rc_kalman_reset | ( | rc_kalman_t * | kf | ) |
reset state and dynamics of the filter to 0
Q and R are constant, and therefore not reset. P is set to identity*P_init.
kf | pointer to struct to be reset |
int rc_kalman_update_lin | ( | rc_kalman_t * | kf, |
rc_vector_t | u, | ||
rc_vector_t | y | ||
) |
Kalman Filter state prediction step based on physical model.
Uses the state estimate and control input from the previous timestep to produce an estimate of the state at the current timestep. This step pdates P and the estimated state x. Assume that you have calculated f(x[k|k],u[k]) and F(x[k|k],u[k]) before calling this function.
kf | pointer to struct to be updated | |
u | control input | |
[in] | y | sensor measurement |
int rc_kalman_update_ekf | ( | rc_kalman_t * | kf, |
rc_matrix_t | F, | ||
rc_matrix_t | H, | ||
rc_vector_t | x_pre, | ||
rc_vector_t | y, | ||
rc_vector_t | h | ||
) |
Kalman Filter measurement update step.
Updates L, P, & x_est. Assumes that you have done the non-linear prediction step in your own function which should calculate the Jacobians F(x[k|k-1]) & H(x[k|k-1]), the predicted sensor value h(x[k|k-1]), and of course the predicted state x_pre[k|k-1]
-Kalman measurement Update:
Also updates the step counter in the rc_kalman_t struct
kf | pointer to struct to be updated | |
[in] | F | Jacobian of state transition matrix linearized at x_pre |
[in] | H | Jacobian of observation matrix linearized at x_pre |
[in] | x_pre | predicted state |
[in] | y | new sensor data |
[in] | h | Ideal estimate of y, usually h=H*x_pre. |