pub struct KalmanFilter { /* private fields */ }Expand description
Discrete linear Kalman filter over fixed-size state, measurement, and control vectors.
Construct with KalmanFilter::new, assign the model matrices with the
set_* methods, seed KalmanFilter::set_state_post and
KalmanFilter::set_error_cov_post, then alternate predict/correct
calls.
Implementations§
Source§impl KalmanFilter
impl KalmanFilter
Sourcepub fn new(
dynam_params: usize,
measure_params: usize,
control_params: usize,
) -> Result<Self>
pub fn new( dynam_params: usize, measure_params: usize, control_params: usize, ) -> Result<Self>
Creates a filter with zero-filled model matrices and covariances,
matching cv::KalmanFilter::KalmanFilter.
§Errors
Returns an error unless dynam_params and measure_params are
nonzero; control_params may be zero to disable the control input.
Sourcepub fn set_transition_matrix(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_transition_matrix(&mut self, matrix: &Matrix) -> Result<()>
Assigns the dynam x dynam state-transition matrix A.
§Errors
Returns an error unless matrix is [dynam, dynam] F32.
Sourcepub fn set_control_matrix(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_control_matrix(&mut self, matrix: &Matrix) -> Result<()>
Assigns the dynam x control control-input matrix B.
§Errors
Returns an error unless matrix is [dynam, control] F32.
Sourcepub fn set_measurement_matrix(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_measurement_matrix(&mut self, matrix: &Matrix) -> Result<()>
Assigns the measure x dynam measurement matrix H.
§Errors
Returns an error unless matrix is [measure, dynam] F32.
Sourcepub fn set_process_noise_cov(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_process_noise_cov(&mut self, matrix: &Matrix) -> Result<()>
Assigns the dynam x dynam process-noise covariance Q.
§Errors
Returns an error unless matrix is [dynam, dynam] F32.
Sourcepub fn set_measurement_noise_cov(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_measurement_noise_cov(&mut self, matrix: &Matrix) -> Result<()>
Assigns the measure x measure measurement-noise covariance R.
§Errors
Returns an error unless matrix is [measure, measure] F32.
Sourcepub fn set_state_post(&mut self, state: &Matrix) -> Result<()>
pub fn set_state_post(&mut self, state: &Matrix) -> Result<()>
Seeds the corrected state estimate consumed by the next predict
call.
§Errors
Returns an error unless state holds exactly dynam F32 values.
Sourcepub fn set_error_cov_post(&mut self, matrix: &Matrix) -> Result<()>
pub fn set_error_cov_post(&mut self, matrix: &Matrix) -> Result<()>
Seeds the corrected dynam x dynam error covariance P.
§Errors
Returns an error unless matrix is [dynam, dynam] F32.
Sourcepub fn state_pre(&self) -> Result<Matrix>
pub fn state_pre(&self) -> Result<Matrix>
Current predicted state [dynam, 1] (state_pre).
§Errors
Returns an error when the F32 output cannot be materialized.
Sourcepub fn state_post(&self) -> Result<Matrix>
pub fn state_post(&self) -> Result<Matrix>
Current corrected state [dynam, 1] (state_post).
§Errors
Returns an error when the F32 output cannot be materialized.
Sourcepub fn predict(&mut self, control: Option<&Matrix>) -> Result<Matrix>
pub fn predict(&mut self, control: Option<&Matrix>) -> Result<Matrix>
Advances the model one step: state_pre = A * state_post + B * u,
error_cov_pre = A * P * A' + Q. control must be [control, 1]
when the filter was built with a nonzero control dimension. Like
cv::KalmanFilter::predict, only the *_pre fields advance.
Returns state_pre as [dynam, 1].
§Errors
Returns an error for a wrongly sized or missing control input.
Sourcepub fn correct(&mut self, measurement: &Matrix) -> Result<Matrix>
pub fn correct(&mut self, measurement: &Matrix) -> Result<Matrix>
Fuses a measure-dimensional measurement: gain
K = P_pre * H' * (H * P_pre * H' + R)^-1, then
state_post = state_pre + K * (z - H * state_pre) and
error_cov_post = (I - K * H) * P_pre.
Returns state_post as [dynam, 1].
§Errors
Returns an error for a wrongly sized measurement or a singular
innovation covariance H * P_pre * H' + R.