Skip to content

Commit 9244135

Browse files
authored
Merge pull request #134 from jbrodovsky/jbrodovsky/issue133
Fix UKF negative variance issue (#133)
2 parents c27fa87 + d9ea0c9 commit 9244135

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

core/src/kalman.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,13 @@ impl NavigationFilter for UnscentedKalmanFilter {
300300
self.mean_state[7] = wrap_to_2pi(self.mean_state[7]);
301301
self.mean_state[8] = wrap_to_2pi(self.mean_state[8]);
302302
self.covariance -= &k * &s * &k.transpose();
303-
self.covariance = 0.5 * (&self.covariance + self.covariance.transpose());
303+
// Ensure covariance remains positive semi-definite with gentle regularization
304+
self.covariance = symmetrize(&self.covariance);
305+
// Add small diagonal regularization to prevent negative eigenvalues
306+
let eps = 1e-9;
307+
for i in 0..self.state_size {
308+
self.covariance[(i, i)] += eps;
309+
}
304310
}
305311
fn get_estimate(&self) -> DVector<f64> {
306312
self.mean_state.clone()

0 commit comments

Comments
 (0)