strawlab / adskalman-rs

Kalman filter implementation in Rust
Apache License 2.0
56 stars 9 forks source link

Question on how to implement simple kalman filter #20

Open Hoeze opened 1 month ago

Hoeze commented 1 month ago

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

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

astraw commented 1 month ago

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

Hoeze commented 1 month ago

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

I assume the relevant lines are these: https://github.com/strawlab/adskalman-rs/blob/f7c20fc7b3fa7b487180e8759db7055950231989/examples/src/bin/online_tracking.rs#L17-L27 However, I am not sure what exactly I need to adjust here:

Please excuse me for my lack of understanding :sweat_smile:

astraw commented 1 month ago

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 commented 3 weeks ago

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 commented 3 weeks ago

@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.