mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 20:34:26 +02:00
refactor(example): remaining kalman filter examples refactored for V2
This commit is contained in:
@@ -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)
|
||||
|
@@ -33,8 +33,7 @@
|
||||
namespace kalman {
|
||||
|
||||
template<typename T>
|
||||
concept QuantityOrQuantityPoint =
|
||||
units::Quantity<T> || units::QuantityPoint<T>; // TODO Should it also account for `kinds`?
|
||||
concept QuantityOrQuantityPoint = units::Quantity<T> || units::QuantityPoint<T>;
|
||||
|
||||
template<units::Dimension auto... Ds>
|
||||
inline constexpr bool are_time_derivatives = false;
|
||||
|
@@ -22,32 +22,13 @@
|
||||
|
||||
#include "kalman.h"
|
||||
#include <units/format.h>
|
||||
#include <units/isq/si/thermodynamic_temperature.h>
|
||||
#include <units/isq/thermodynamics.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_point.h>
|
||||
#include <units/unit.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};
|
||||
|
||||
} // 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<QP> initial)
|
||||
"Next Estimate");
|
||||
}
|
||||
|
||||
template<QuantityPoint QP, Dimensionless K>
|
||||
template<QuantityPoint QP, quantity_of<dimensionless> K>
|
||||
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> 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<QP> 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 = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement, Dimensionless auto gain) {
|
||||
auto update = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement,
|
||||
quantity_of<dimensionless> auto gain) {
|
||||
return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)};
|
||||
};
|
||||
|
||||
|
@@ -22,32 +22,13 @@
|
||||
|
||||
#include "kalman.h"
|
||||
#include <units/format.h>
|
||||
#include <units/isq/si/thermodynamic_temperature.h>
|
||||
#include <units/isq/thermodynamics.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_point.h>
|
||||
#include <units/unit.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};
|
||||
|
||||
} // 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<QP> initial)
|
||||
"Next Estimate");
|
||||
}
|
||||
|
||||
template<QuantityPoint QP, Dimensionless K>
|
||||
template<QuantityPoint QP, quantity_of<dimensionless> K>
|
||||
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> 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<QP> 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 = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement, Dimensionless auto gain) {
|
||||
auto update = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement,
|
||||
quantity_of<dimensionless> auto gain) {
|
||||
return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)};
|
||||
};
|
||||
|
||||
|
@@ -22,32 +22,13 @@
|
||||
|
||||
#include "kalman.h"
|
||||
#include <units/format.h>
|
||||
#include <units/isq/si/thermodynamic_temperature.h>
|
||||
#include <units/isq/thermodynamics.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_point.h>
|
||||
#include <units/unit.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};
|
||||
|
||||
} // 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<QP> initial)
|
||||
"Next Estimate");
|
||||
}
|
||||
|
||||
template<QuantityPoint QP, Dimensionless K>
|
||||
template<QuantityPoint QP, quantity_of<dimensionless> K>
|
||||
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> 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<QP> 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 = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement, Dimensionless auto gain) {
|
||||
auto update = [=]<QuantityPoint QP>(const estimation<QP>& previous, const QP& meassurement, quantity_of<dimensionless> auto gain) {
|
||||
return estimation{state_update(previous.state, meassurement, gain), covariance_update(previous.uncertainty, gain)};
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user