Skip to content

Commit

Permalink
Merge 6498b08 into 36b60df
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed May 2, 2022
2 parents 36b60df + 6498b08 commit 0c4b627
Show file tree
Hide file tree
Showing 12 changed files with 951 additions and 283 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/verify_code_static_analysis_cppcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Update
run: sudo apt update
- name: Install
run: sudo apt install cppcheck
run: |
( cd /tmp ; mkdir cppcheck ;
git clone --depth 1 https://github.com/danmar/cppcheck.git ;
( cd cppcheck ;
mkdir build ) ;
( cd cppcheck/build ;
cmake .. ;
cmake --build . ;
sudo make install ) )
- name: Version
run: cppcheck --version
- name: Check
run: cppcheck --verbose --suppressions-list=.cppcheck --error-exitcode=1 --enable=all -I include .
113 changes: 92 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ using kalman = fcarouge::kalman<double>;

kalman k;

k.state_x = 60;
k.estimate_uncertainty_p = 225;
k.transition_observation_h = [] { return kalman::observation{ 1 }; };
k.noise_observation_r = [] {
return kalman::observation_noise_uncertainty{ 25 };
};
k.x(60.);
k.p(225.);
k.r(25.);

k.observe(48.54);
```
Expand All @@ -76,35 +73,50 @@ const double gravitational_acceleration{ -9.8 }; // m.s^-2
const std::chrono::milliseconds delta_time{ 250 };
kalman k;

k.state_x = { 0, 0 };
k.estimate_uncertainty_p =
kalman::estimate_uncertainty{ { 500, 0 }, { 0, 500 } };
k.transition_state_f = [](const std::chrono::milliseconds &delta_time) {
k.x(0, 0);
k.p(kalman::estimate_uncertainty{ { 500, 0 }, { 0, 500 } });
k.f([](const std::chrono::milliseconds &delta_time) {
const auto dt{ std::chrono::duration<double>(delta_time).count() };
return kalman::state_transition{ { 1, dt }, { 0, 1 } };
};
k.noise_process_q = [](const std::chrono::milliseconds &delta_time) {
});
k.q([](const std::chrono::milliseconds &delta_time) {
const auto dt{ std::chrono::duration<double>(delta_time).count() };
return kalman::process_noise_uncertainty{
return kalman::process_uncertainty{
{ 0.1 * 0.1 * dt * dt * dt * dt / 4, 0.1 * 0.1 * dt * dt * dt / 2 },
{ 0.1 * 0.1 * dt * dt * dt / 2, 0.1 * 0.1 * dt * dt }
};
};
k.transition_control_g = [](const std::chrono::milliseconds &delta_time) {
});
k.g([](const std::chrono::milliseconds &delta_time) {
const auto dt{ std::chrono::duration<double>(delta_time).count() };
return kalman::control{ 0.0313, dt };
};
k.transition_observation_h = [] { return kalman::observation{ { 1, 0 } }; };
k.noise_observation_r = [] {
return kalman::observation_noise_uncertainty{ 400 };
};
return kalman::input_control{ 0.0313, dt };
});
k.h(1, 0);
k.r(400);

k.predict(delta_time, gravitational_acceleration);
k.observe(-32.40 );
k.predict(delta_time, 39.72);
k.observe(-11.1);
```

# Library

- [Kalman Filter for C++](#kalman-filter-for-c)
- [Continuous Integration & Deployment Actions](#continuous-integration--deployment-actions)
- [Examples](#examples)
- [One-Dimensional](#one-dimensional)
- [Multi-Dimensional](#multi-dimensional)
- [Library](#library)
- [Motivation](#motivation)
- [Resources](#resources)
- [Class fcarouge::kalman](#class-fcarougekalman)
- [Template Parameters](#template-parameters)
- [Member Types](#member-types)
- [Member Functions](#member-functions)
- [Characteristics](#characteristics)
- [Modifiers](#modifiers)
- [License](#license)

# Motivation

Kalman filters can be difficult to learn, use, and implement. Users often need fair algebra, domain, and software knowledge. Inadequacy leads to incorrectness, underperformance, and a big ball of mud.
Expand All @@ -121,6 +133,65 @@ Awesome resources to learn about Kalman filters:
- [KalmanFilter.NET](https://www.kalmanfilter.net) by Alex Becker.
- [Kalman and Bayesian Filters in Python](https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python) by Roger Labbe.

# Class fcarouge::kalman

Defined in header [fcarouge/kalman.hpp](include/fcarouge/kalman.hpp)

```cpp
template <typename State, typename Output = State, typename Input = State,
template <typename> typename Transpose = internal::transpose,
template <typename> typename Symmetrize = internal::symmetrize,
template <typename, typename> typename Divide = internal::divide,
template <typename> typename Identity = internal::identity,
typename... PredictionArguments>
class kalman
```
## Template Parameters
## Member Types
| Member Type | Definition |
| --- | --- |
| `state` | Type of the state estimate vector X. |
| `output` | Type of the observation vector Z, also known as Y. |
| `input` | Type of the control vector U. |
| `estimate_uncertainty` | Type of the estimated covariance matrix P, also known as Σ. |
| `process_uncertainty` | Type of the process noise covariance matrix Q. |
| `output_uncertainty` | Type of the observation, measurement noise covariance matrix R. |
| `state_transition` | Type of the state transition matrix F, also known as Φ or A. |
| `output_model` | Type of the observation transition matrix H, also known as C. |
| `input_control` | Type of the control transition matrix G, also known as B. |
## Member Functions
| Member Function | Definition |
| --- | --- |
| `(constructor)` | Constructs the filter. |
| `(destructor)` | Destructs the filter. |
| `operator=` | Assigns values to the filter. |
### Characteristics
| Modifier | Definition |
| --- | --- |
| `x` | Manages the state estimate vector. |
| `z` | Manages the observation vector. |
| `u` | Manages the control vector. |
| `p` | Manages the estimated covariance matrix. |
| `q` | Manages the process noise covariance matrix. |
| `r` | Manages the observation, measurement noise covariance matrix. |
| `f` | Manages the state transition matrix. |
| `h` | Manages the observation transition matrix. |
| `g` | Manages the control transition matrix. |
### Modifiers
| Modifier | Definition |
| --- | --- |
| `observe` | Updates the estimates with the outcome of a measurement. |
| `predict` | Produces estimates of the state variables and uncertainties. |
# License
<img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
Expand Down
223 changes: 223 additions & 0 deletions include/fcarouge/internal/kalman.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*_ __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter for C++
Version 0.1.0
https://github.com/FrancoisCarouge/Kalman
SPDX-License-Identifier: Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> */

#ifndef FCAROUGE_INTERNAL_KALMAN_HPP
#define FCAROUGE_INTERNAL_KALMAN_HPP

//! @file
//! @brief The main Kalman filter class.

#include "kalman_equation.hpp"
#include "kalman_operator.hpp"

#include <functional>
#include <type_traits>

namespace fcarouge::internal
{
template <typename State, typename Output = State, typename Input = State,
template <typename> typename Transpose = transpose,
template <typename> typename Symmetrize = symmetrize,
template <typename, typename> typename Divide = divide,
template <typename> typename Identity = identity,
typename... PredictionArguments>
struct kalman {
//! @name Public Member Types
//! @{

//! @brief Type of the state estimate vector X.
using state = State;

//! @brief Type of the observation vector Z.
//!
//! @details Also known as Y.
using output = Output;

//! @brief Type of the control vector U.
using input = Input;

//! @brief Type of the estimated covariance matrix P.
//!
//! @details Also known as Σ.
using estimate_uncertainty =
std::invoke_result_t<Divide<State, State>, State, State>;

//! @brief Type of the process noise covariance matrix Q.
using process_uncertainty =
std::invoke_result_t<Divide<State, State>, State, State>;

//! @brief Type of the observation, measurement noise covariance matrix R.
using output_uncertainty =
std::invoke_result_t<Divide<Output, Output>, Output, Output>;

//! @brief Type of the state transition matrix F.
//!
//! @details Also known as Φ or A.
using state_transition =
std::invoke_result_t<Divide<State, State>, State, State>;

//! @brief Type of the observation transition matrix H.
//!
//! @details Also known as C.
using output_model =
std::invoke_result_t<Divide<Output, State>, Output, State>;

//! @brief Type of the control transition matrix G.
//!
//! @details Also known as B.
using input_control =
std::invoke_result_t<Divide<State, Input>, State, Input>;

//! @}

//! @name Public Member Variables
//! @{

//! @brief The state estimate vector x.
state x{};

//! @brief The estimate uncertainty, covariance matrix P.
//!
//! @details The estimate uncertainty, covariance is also known as Σ.
estimate_uncertainty p{ Identity<estimate_uncertainty>()() };

output_model h{ Identity<output_model>()() };

output_uncertainty r{ Identity<output_uncertainty>()() };

state_transition f{ Identity<state_transition>()() };

process_uncertainty q{};

input_control g{};

//! @}

//! @name Public Member Function Objects
//! @{

//! @brief Compute observation transition H matrix.
//!
//! @details The observation transition H is also known as C.
std::function<output_model()> transition_observation_h{ [this] {
return h;
} };

//! @brief Compute observation noise R matrix.
std::function<output_uncertainty()> noise_observation_r{ [this] {
return r;
} };

//! @brief Compute state transition F matrix.
//!
//! @details The state transition F matrix is also known as Φ or A.
//! For non-linear system, or extended filter, F is the Jacobian of the state
//! transition function. F = ∂fj/∂xi that is each row i contains the the
//! derivatives of the state transition function for every element j in the
//! state vector x.
std::function<state_transition(const PredictionArguments &...)>
transition_state_f{ [this](const PredictionArguments &...arguments) {
static_cast<void>((arguments, ...));
return f;
} };

//! @brief Compute process noise Q matrix.
std::function<process_uncertainty(const PredictionArguments &...)>
noise_process_q{ [this](const PredictionArguments &...arguments) {
static_cast<void>((arguments, ...));
return q;
} };

//! @brief Compute control transition G matrix.
std::function<input_control(const PredictionArguments &...)>
transition_control_g{ [this](const PredictionArguments &...arguments) {
static_cast<void>((arguments, ...));
return g;
} };

//! @brief State transition function.
//!
//! @details
// Add prediction arguments?
std::function<state(const state &, const state_transition &)> predict_state =
[](const state &x, const state_transition &f) { return state{ f * x }; };

//! @}

//! @name Public Member Functions
//! @{

inline constexpr void observe(const auto &...output_z)
{
h = transition_observation_h();
r = noise_observation_r();
const auto z{ output{ output_z... } };
internal::observe<Transpose, Symmetrize, Divide, Identity>(x, p, h, r, z);
}

inline constexpr void
predict(const PredictionArguments &...arguments,
const auto &...input_u) requires(sizeof...(PredictionArguments) > 0)
{
// use member variables
const auto ff{ predict_state };
f = transition_state_f(arguments...);
q = noise_process_q(arguments...);
g = transition_control_g(arguments...);
const auto u{ input{ input_u... } };
internal::predict<Transpose, Symmetrize>(x, p, ff, f, q, g, u);
}

inline constexpr void
predict(const auto &...input_u) requires(sizeof...(PredictionArguments) == 0)
{
// use member variables
const auto ff{ predict_state };
f = transition_state_f();
q = noise_process_q();
g = transition_control_g();
const auto u{ input{ input_u... } };
internal::predict<Transpose, Symmetrize>(x, p, ff, f, q, g, u);
}

//! @}
};

} // namespace fcarouge::internal

#endif // FCAROUGE_INTERNAL_KALMAN_HPP
Loading

0 comments on commit 0c4b627

Please sign in to comment.