From 278dbace981044b955606c6ab27bc52609a2cab5 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 23 Dec 2022 19:23:00 +0100 Subject: [PATCH] refactor(example): remaining kalman filter examples refactored for V2 --- example/kalman_filter/CMakeLists.txt | 10 ++--- example/kalman_filter/kalman.h | 3 +- .../kalman_filter/kalman_filter-example_6.cpp | 44 ++++++------------- .../kalman_filter/kalman_filter-example_7.cpp | 44 ++++++------------- .../kalman_filter/kalman_filter-example_8.cpp | 43 +++++------------- 5 files changed, 42 insertions(+), 102 deletions(-) diff --git a/example/kalman_filter/CMakeLists.txt b/example/kalman_filter/CMakeLists.txt index d2f62243..79bd7cfc 100644 --- a/example/kalman_filter/CMakeLists.txt +++ b/example/kalman_filter/CMakeLists.txt @@ -28,7 +28,6 @@ cmake_minimum_required(VERSION 3.2) function(add_example target) add_executable(${target} ${target}.cpp) target_link_libraries(${target} PRIVATE ${ARGN}) - target_compile_definitions(${target} PRIVATE ${projectPrefix}NO_LITERALS ${projectPrefix}NO_ALIASES) endfunction() add_example(kalman_filter-example_1 mp-units::core-fmt mp-units::si) @@ -36,9 +35,6 @@ add_example(kalman_filter-example_2 mp-units::core-fmt mp-units::si) add_example(kalman_filter-example_3 mp-units::core-fmt mp-units::si) add_example(kalman_filter-example_4 mp-units::core-fmt mp-units::si) add_example(kalman_filter-example_5 mp-units::core-fmt mp-units::si) - -if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") - add_example(kalman_filter-example_6 mp-units::core-fmt mp-units::si) - add_example(kalman_filter-example_7 mp-units::core-fmt mp-units::si) - add_example(kalman_filter-example_8 mp-units::core-fmt mp-units::si) -endif() +add_example(kalman_filter-example_6 mp-units::core-fmt mp-units::si) +add_example(kalman_filter-example_7 mp-units::core-fmt mp-units::si) +add_example(kalman_filter-example_8 mp-units::core-fmt mp-units::si) diff --git a/example/kalman_filter/kalman.h b/example/kalman_filter/kalman.h index eb2a23ce..e9341d43 100644 --- a/example/kalman_filter/kalman.h +++ b/example/kalman_filter/kalman.h @@ -33,8 +33,7 @@ namespace kalman { template -concept QuantityOrQuantityPoint = - units::Quantity || units::QuantityPoint; // TODO Should it also account for `kinds`? +concept QuantityOrQuantityPoint = units::Quantity || units::QuantityPoint; template inline constexpr bool are_time_derivatives = false; diff --git a/example/kalman_filter/kalman_filter-example_6.cpp b/example/kalman_filter/kalman_filter-example_6.cpp index b77d3d62..507a1b16 100644 --- a/example/kalman_filter/kalman_filter-example_6.cpp +++ b/example/kalman_filter/kalman_filter-example_6.cpp @@ -22,32 +22,13 @@ #include "kalman.h" #include -#include +#include #include #include -#include +#include #include #include -// TODO Fix when Celsius is properly supported (#232) -namespace units::isq::si { - -struct degree_celsius : alias_unit {}; - -namespace thermodynamic_temperature_references { - -inline constexpr auto deg_C = reference{}; - -} // namespace thermodynamic_temperature_references - -namespace references { - -using namespace thermodynamic_temperature_references; - -} // namespace references - -} // namespace units::isq::si - // Based on: https://www.kalmanfilter.net/kalman1d.html#ex6 using namespace units; @@ -60,7 +41,7 @@ void print_header(kalman::estimation initial) "Next Estimate"); } -template +template K> void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { std::cout << STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, @@ -69,20 +50,21 @@ void print(auto iteration, K gain, QP measured, kalman::estimation current, int main() { + constexpr auto deg_C = isq::Celsius_temperature[si::degree_Celsius]; + using namespace kalman; - using namespace units::isq; - using namespace units::isq::si::references; const auto process_noise_variance = 0.0001 * (deg_C * deg_C); - const estimation initial = {state{quantity_point(10. * deg_C)}, pow<2>(100. * deg_C)}; - const std::array measurements = {quantity_point(49.95 * deg_C), quantity_point(49.967 * deg_C), - quantity_point(50.1 * deg_C), quantity_point(50.106 * deg_C), - quantity_point(49.992 * deg_C), quantity_point(49.819 * deg_C), - quantity_point(49.933 * deg_C), quantity_point(50.007 * deg_C), - quantity_point(50.023 * deg_C), quantity_point(49.99 * deg_C)}; + const estimation initial = {state{quantity_point{10. * deg_C}}, pow<2>(100. * deg_C)}; + const std::array measurements = {quantity_point{49.95 * deg_C}, quantity_point{49.967 * deg_C}, + quantity_point{50.1 * deg_C}, quantity_point{50.106 * deg_C}, + quantity_point{49.992 * deg_C}, quantity_point{49.819 * deg_C}, + quantity_point{49.933 * deg_C}, quantity_point{50.007 * deg_C}, + quantity_point{50.023 * deg_C}, quantity_point{49.99 * deg_C}}; const auto measurement_uncertainty = pow<2>(0.1 * deg_C); - auto update = [=](const estimation& previous, const QP& meassurement, Dimensionless auto gain) { + auto update = [=](const estimation& previous, const QP& meassurement, + quantity_of auto gain) { return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)}; }; diff --git a/example/kalman_filter/kalman_filter-example_7.cpp b/example/kalman_filter/kalman_filter-example_7.cpp index 7387ab56..d0f53585 100644 --- a/example/kalman_filter/kalman_filter-example_7.cpp +++ b/example/kalman_filter/kalman_filter-example_7.cpp @@ -22,32 +22,13 @@ #include "kalman.h" #include -#include +#include #include #include -#include +#include #include #include -// TODO Fix when Celsius is properly supported (#232) -namespace units::isq::si { - -struct degree_celsius : alias_unit {}; - -namespace thermodynamic_temperature_references { - -inline constexpr auto deg_C = reference{}; - -} // namespace thermodynamic_temperature_references - -namespace references { - -using namespace thermodynamic_temperature_references; - -} // namespace references - -} // namespace units::isq::si - // Based on: https://www.kalmanfilter.net/kalman1d.html#ex7 using namespace units; @@ -60,7 +41,7 @@ void print_header(kalman::estimation initial) "Next Estimate"); } -template +template K> void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { std::cout << STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, @@ -69,20 +50,21 @@ void print(auto iteration, K gain, QP measured, kalman::estimation current, int main() { + constexpr auto deg_C = isq::Celsius_temperature[si::degree_Celsius]; + using namespace kalman; - using namespace units::isq; - using namespace units::isq::si::references; const auto process_noise_variance = 0.0001 * (deg_C * deg_C); - const estimation initial = {state{quantity_point(10. * deg_C)}, pow<2>(100. * deg_C)}; - const std::array measurements = {quantity_point(50.45 * deg_C), quantity_point(50.967 * deg_C), - quantity_point(51.6 * deg_C), quantity_point(52.106 * deg_C), - quantity_point(52.492 * deg_C), quantity_point(52.819 * deg_C), - quantity_point(53.433 * deg_C), quantity_point(54.007 * deg_C), - quantity_point(54.523 * deg_C), quantity_point(54.99 * deg_C)}; + const estimation initial = {state{quantity_point{10. * deg_C}}, pow<2>(100. * deg_C)}; + const std::array measurements = {quantity_point{50.45 * deg_C}, quantity_point{50.967 * deg_C}, + quantity_point{51.6 * deg_C}, quantity_point{52.106 * deg_C}, + quantity_point{52.492 * deg_C}, quantity_point{52.819 * deg_C}, + quantity_point{53.433 * deg_C}, quantity_point{54.007 * deg_C}, + quantity_point{54.523 * deg_C}, quantity_point{54.99 * deg_C}}; const auto measurement_uncertainty = pow<2>(0.1 * deg_C); - auto update = [=](const estimation& previous, const QP& meassurement, Dimensionless auto gain) { + auto update = [=](const estimation& previous, const QP& meassurement, + quantity_of auto gain) { return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)}; }; diff --git a/example/kalman_filter/kalman_filter-example_8.cpp b/example/kalman_filter/kalman_filter-example_8.cpp index c2e0e1f6..5b7df927 100644 --- a/example/kalman_filter/kalman_filter-example_8.cpp +++ b/example/kalman_filter/kalman_filter-example_8.cpp @@ -22,32 +22,13 @@ #include "kalman.h" #include -#include +#include #include #include -#include +#include #include #include -// TODO Fix when Celsius is properly supported (#232) -namespace units::isq::si { - -struct degree_celsius : alias_unit {}; - -namespace thermodynamic_temperature_references { - -inline constexpr auto deg_C = reference{}; - -} // namespace thermodynamic_temperature_references - -namespace references { - -using namespace thermodynamic_temperature_references; - -} // namespace references - -} // namespace units::isq::si - // Based on: https://www.kalmanfilter.net/kalman1d.html#ex8 using namespace units; @@ -60,7 +41,7 @@ void print_header(kalman::estimation initial) "Next Estimate"); } -template +template K> void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { std::cout << STD_FMT::format("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain, @@ -69,20 +50,20 @@ void print(auto iteration, K gain, QP measured, kalman::estimation current, int main() { + constexpr auto deg_C = isq::Celsius_temperature[si::degree_Celsius]; + using namespace kalman; - using namespace units::isq; - using namespace units::isq::si::references; const auto process_noise_variance = 0.15 * (deg_C * deg_C); - const estimation initial = {state{quantity_point(10. * deg_C)}, pow<2>(100. * deg_C)}; - const std::array measurements = {quantity_point(50.45 * deg_C), quantity_point(50.967 * deg_C), - quantity_point(51.6 * deg_C), quantity_point(52.106 * deg_C), - quantity_point(52.492 * deg_C), quantity_point(52.819 * deg_C), - quantity_point(53.433 * deg_C), quantity_point(54.007 * deg_C), - quantity_point(54.523 * deg_C), quantity_point(54.99 * deg_C)}; + const estimation initial = {state{quantity_point{10. * deg_C}}, pow<2>(100. * deg_C)}; + const std::array measurements = {quantity_point{50.45 * deg_C}, quantity_point{50.967 * deg_C}, + quantity_point{51.6 * deg_C}, quantity_point{52.106 * deg_C}, + quantity_point{52.492 * deg_C}, quantity_point{52.819 * deg_C}, + quantity_point{53.433 * deg_C}, quantity_point{54.007 * deg_C}, + quantity_point{54.523 * deg_C}, quantity_point{54.99 * deg_C}}; const auto measurement_uncertainty = pow<2>(0.1 * deg_C); - auto update = [=](const estimation& previous, const QP& meassurement, Dimensionless auto gain) { + auto update = [=](const estimation& previous, const QP& meassurement, quantity_of auto gain) { return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)}; };