Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/src/kalman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,13 @@ impl NavigationFilter for UnscentedKalmanFilter {
self.mean_state[7] = wrap_to_2pi(self.mean_state[7]);
self.mean_state[8] = wrap_to_2pi(self.mean_state[8]);
self.covariance -= &k * &s * &k.transpose();
self.covariance = 0.5 * (&self.covariance + self.covariance.transpose());
// Ensure covariance remains positive semi-definite with gentle regularization
self.covariance = symmetrize(&self.covariance);
// Add small diagonal regularization to prevent negative eigenvalues
let eps = 1e-9;
for i in 0..self.state_size {
self.covariance[(i, i)] += eps;
}
Comment on lines +306 to +309

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The value 1e-9 is a magic number. It's better to define it as a constant with a descriptive name (e.g., COVARIANCE_REGULARIZATION) to improve readability and maintainability. This makes the purpose of the value explicit and easier to change if needed.

Additionally, the loop for adding the regularization term to the diagonal can be expressed more idiomatically and potentially more efficiently using nalgebra's diagonal_mut() and add_scalar_mut() methods.

        const COVARIANCE_REGULARIZATION: f64 = 1e-9;
        self.covariance.diagonal_mut().add_scalar_mut(COVARIANCE_REGULARIZATION);

}
fn get_estimate(&self) -> DVector<f64> {
self.mean_state.clone()
Expand Down
Loading