Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on how to implement simple kalman filter #20

Open
Hoeze opened this issue May 24, 2024 · 5 comments
Open

Question on how to implement simple kalman filter #20

Hoeze opened this issue May 24, 2024 · 5 comments

Comments

@Hoeze
Copy link

Hoeze commented May 24, 2024

Hi, I am trying to build an ESP32-based scale, but I am struggling to use this library for improving the measurement estimates.
Basically, I try to implement a simple kalman filter as described in this C++ library:
https://github.com/denyssene/SimpleKalmanFilter

  • e_mea: Measurement Uncertainty - How much do we expect to our measurement vary
  • e_est: Estimation Uncertainty - Can be initilized with the same value as e_mea since the kalman filter will adjust its value.
  • q: Process Variance - usually a small number between 0.001 and 1 - how fast your measurement moves. Recommended 0.01.
 SimpleKalmanFilter kf = SimpleKalmanFilter(e_mea, e_est, q);

 while (1) {
  float x = analogRead(A0);
  float estimated_x = kf.updateEstimate(x);
  
  // ...
 } 

Could you maybe give me some guidance on how to translate this to your library?
Your help would be highly appreciated :)

@astraw
Copy link
Member

astraw commented May 24, 2024

Have you seen the examples, e.g. https://github.com/strawlab/adskalman-rs/blob/main/examples/src/bin/online_tracking.rs ?

@Hoeze
Copy link
Author

Hoeze commented May 24, 2024

Thank you for your answer! Indeed, i did not see this example yet as I missed looking into the bin/ subfolder 😅

I assume the relevant lines are these:

let dt = 0.01;
let true_initial_state = Vector4::<MyType>::new(0.0, 0.0, 10.0, -5.0);
#[rustfmt::skip]
let initial_covariance = Matrix4::<MyType>::new(0.1, 0.0, 0.0, 0.0,
0.0, 0.1, 0.0, 0.0,
0.0, 0.0, 0.1, 0.0,
0.0, 0.0, 0.0, 0.1);
let motion_model = motion_model::ConstantVelocity2DModel::new(dt, 100.0);
let observation_model = linear_observation_model::PositionObservationModel::new(0.01);
let kf = KalmanFilterNoControl::new(&motion_model, &observation_model);

However, I am not sure what exactly I need to adjust here:

  • motion_model and observation_model come from the examples, can I just reuse these exactly as they are?
  • Since I only have weight_measurement as variable, I assume that my true_initial_state has only one element?
  • What do I need to set the initial_covariance matrix to? [[1]]?

Please excuse me for my lack of understanding 😅

@astraw
Copy link
Member

astraw commented May 24, 2024

That model has a 4D state (2D XY position, 2D XY velocity) and 2D observation (XY position only). If that suits your case, yes, you can use this directly. Otherwise you'll have to implement your own transition model and observation model. You can use the rest of the example code as a basis, I hope.

@Hoeze
Copy link
Author

Hoeze commented Jun 16, 2024

Dear @astraw, I managed to implement my own Kalman filter for a 2d state (1D position, 1D velocity).
Thank you for your help.

Now I would like to use it with actual time measurements but dt is a constant which gets defined when creating the motion_model.
Is it also possible to specify dt in each step, based on actual time measurements?

@astraw
Copy link
Member

astraw commented Jun 17, 2024

@Hoeze Glad to hear it.

Yes, you can simply rebuild the motion model each time you have a new dt.

You can check your math by ensuring that the covariance grows by the same amount if you have either A) two timesteps of half the duration vs B) a single timestep at normal duration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants