Skip to content

Commit

Permalink
[filter] declarative paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Nov 2, 2023
1 parent 033285c commit b43f286
Show file tree
Hide file tree
Showing 6 changed files with 539 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/fcarouge/kalman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ class kalman final {
//! @complexity Constant.
inline constexpr kalman() = default;

// wip
inline constexpr kalman([[maybe_unused]] const auto &...wip_sink) {}

//! @brief Copy constructs a filter.
//!
//! @details Constructs the filter with the copy of the contents of the
Expand Down Expand Up @@ -628,6 +631,42 @@ class kalman final {
template <std::size_t Position> inline constexpr auto update() const;
//! @}
};

//! @name Deduction Guides
//! @{

// Reference:
// template <typename State = double, typename Output = double,
// typename Input = void, typename UpdateTypes = empty_pack,
// typename PredictionTypes = empty_pack>
// class kalman final {

// Needed?
// kalman() -> kalman<double, double, void, empty_pack, empty_pack>;

// If ordering and inputs get confusing, we can use tagged types, ifconstexpr.

// assert p is x by x ?
template <typename State, typename EstimateUncertainty,
arithmetic OutputUncertainty>
kalman(const State &x, const EstimateUncertainty &p, const OutputUncertainty &r)
-> kalman<State, OutputUncertainty, void, empty_pack, empty_pack>;

// assert p is x by x ?
template <typename State, typename EstimateUncertainty,
arithmetic ProcessUncertainty, arithmetic OutputUncertainty>
kalman(const State &x, const EstimateUncertainty &p,
const ProcessUncertainty &q, const OutputUncertainty &r)
-> kalman<State, OutputUncertainty, void, empty_pack, empty_pack>;

template <typename State, typename EstimateUncertainty,
arithmetic ProcessUncertainty, arithmetic OutputUncertainty,
typename Input>
kalman(const State &x, const EstimateUncertainty &p,
const ProcessUncertainty &q, const OutputUncertainty &r, const Input &u)
-> kalman<State, OutputUncertainty, Input, empty_pack, empty_pack>;

//! @}
} // namespace fcarouge

#include "internal/kalman.tpp"
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ foreach(
TEST
"kalman_constructor_default_float_1x1x1.cpp"
"kalman_constructor_default.cpp"
"kalman_declarative_1x1x0_building_height.cpp"
"kalman_declarative_1x1x0_liquid_temperature.cpp"
"kalman_declarative_1x1x1_dog_position.cpp"
"kalman_f.cpp"
"kalman_format_arguments.cpp"
"kalman_format_float_1x1x1.cpp"
Expand Down Expand Up @@ -111,6 +114,7 @@ foreach(BACKEND IN ITEMS "eigen")
"kalman_constructor_default_5x4x1.cpp"
"kalman_constructor_default_5x4x3.cpp"
"kalman_constructor_move_5x4x3.cpp"
"kalman_declarative_4x1x0_soaring.cpp"
"kalman_f_5x4x3.cpp"
"kalman_h_5x4x3.cpp")
get_filename_component(NAME ${TEST} NAME_WE)
Expand Down
73 changes: 73 additions & 0 deletions test/kalman_declarative_1x1x0_building_height.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.3.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> */

#include "fcarouge/kalman.hpp"

#include <cassert>
#include <cmath>

namespace fcarouge::test {
namespace {
//! @test ...
[[maybe_unused]] auto test{[] {
using state = double;
using estimate_uncertainty = double;
using output_uncertainty = double;

auto filter{
kalman{state{60.}, estimate_uncertainty{225.}, output_uncertainty{25.}}};

filter.update(48.54);
filter.update(47.11);
filter.update(55.01);
filter.update(55.15);
filter.update(49.89);
filter.update(40.85);
filter.update(46.72);
filter.update(50.05);
filter.update(51.27);
filter.update(49.95);

assert(std::abs(1 - filter.x() / 49.57) < 0.001 &&
"After 10 measurement and update iterations, the building estimated "
"height is: 49.57m.");

return 0;
}()};
} // namespace
} // namespace fcarouge::test
86 changes: 86 additions & 0 deletions test/kalman_declarative_1x1x0_liquid_temperature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.3.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> */

#include "fcarouge/kalman.hpp"

#include <cassert>
#include <cmath>

namespace fcarouge::test {
namespace {
//! @test ...
[[maybe_unused]] auto test{[] {
using state = double;
using estimate_uncertainty = double;
using process_uncertainty = double;
using output_uncertainty = double;

auto filter{kalman{state{10.}, estimate_uncertainty{100 * 100.},
process_uncertainty{0.0001},
output_uncertainty{0.1 * 0.1}}};

const auto step{[&filter](double temperature) {
filter.predict();
filter.update(temperature);
}};

step(49.95);
step(49.967);
step(50.1);
step(50.106);
step(49.992);
step(49.819);
step(49.933);
step(50.007);
step(50.023);
step(49.99);

assert(std::abs(1 - filter.p() / 0.0013) < 0.05 &&
"The estimate uncertainty expected at 5% accuracy."
"The estimate uncertainty is 0.0013, i.e. the estimate error standard "
"deviation is: 0.036°C.");
assert(std::abs(1 - filter.x() / 49.988) < 0.001 &&
"The state estimates expected at 0.1% accuracy."
"The filter estimates the liquid temperature at 49.988°C.");
assert(std::abs(1 - filter.k() / 0.1265) < 0.001 &&
"The gain expected at 0.1% accuracy.");

return 0;
}()};
} // namespace
} // namespace fcarouge::test
88 changes: 88 additions & 0 deletions test/kalman_declarative_1x1x1_dog_position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.3.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> */

#include "fcarouge/kalman.hpp"

#include <cassert>
#include <cmath>

namespace fcarouge::test {
namespace {
//! @test ...
[[maybe_unused]] auto test{[] {
using state = double;
using estimate_uncertainty = double;
using process_uncertainty = double;
using output_uncertainty = double;
using input = double;

auto filter{kalman{state{1.}, estimate_uncertainty{20 * 20.},
process_uncertainty{2.}, output_uncertainty{1.}, input{}}};

filter.predict(1.);
filter.update(1.354);
filter.predict(1.);
filter.update(1.882);
filter.predict(1.);
filter.update(4.341);
filter.predict(1.);
filter.update(7.156);
filter.predict(1.);
filter.update(6.939);
filter.predict(1.);
filter.update(6.844);
filter.predict(1.);
filter.update(9.847);
filter.predict(1.);
filter.update(12.553);
filter.predict(1.);
filter.update(16.273);
filter.predict(1.);
filter.update(14.8);

assert(
std::abs(1 - filter.x() / 15.053) < 0.001 &&
"The state estimates expected at 0.1% accuracy."
"Here we can see that the variance converges to 2.1623 in 9 steps. This "
"means that we have become very confident in our position estimate. It "
"is equal to meters. Contrast this to the sensor's meters.");

return 0;
}()};
} // namespace
} // namespace fcarouge::test
Loading

0 comments on commit b43f286

Please sign in to comment.