From 79f81bfe39ef1bd45cef4fdf13ebd9fea55d9e16 Mon Sep 17 00:00:00 2001 From: Andy Little Date: Wed, 11 Mar 2020 18:27:12 +0000 Subject: [PATCH] example, kalman filter : use constexpr std::array rather than std::vector for measurements and track, to make it harder for compiler to complain about existence of first element. --- ...kalman_filter-alpha_beta_filter_example2.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/example/kalman_filter-alpha_beta_filter_example2.cpp b/example/kalman_filter-alpha_beta_filter_example2.cpp index c45286ce..aab6c65b 100644 --- a/example/kalman_filter-alpha_beta_filter_example2.cpp +++ b/example/kalman_filter-alpha_beta_filter_example2.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include /* kalman filter tutorial @@ -54,19 +54,16 @@ int main() std::cout << "\n\n1d aircraft α-β filter example2 from https://www.kalmanfilter.net/alphabeta.html#ex2"; std::cout << "\n\n"; - std::vector> measurements{0.0q_m, // N.B measurement[0] is unknown and unused + constexpr auto measurements = std::array{0.0q_m, // N.B measurement[0] is unknown and unused 30110.0q_m, 30265.0q_m, 30740.0q_m, 30750.0q_m, 31135.0q_m, 31015.0q_m, 31180.0q_m, 31610.0q_m, 31960.0q_m, 31865.0q_m}; - const auto num_measurements = measurements.size(); + constexpr auto num_measurements = measurements.size(); - std::vector track(num_measurements); - - if(track.size() > 0) { - // We need an initial estimate of track[0] as there is no previous state to get a prediction from - track[0].range.estimated_current_state = 30'000q_m; - track[0].speed.estimated_current_state = 40.0q_m_per_s; - } + std::array track; + // We need an initial estimate of track[0] as there is no previous state to get a prediction from + track[0].range.estimated_current_state = 30'000q_m; + track[0].speed.estimated_current_state = 40.0q_m_per_s; for (auto n = 0U; n < num_measurements; ++n) { if (n > 0) {