0.3.0
Loading...
Searching...
No Matches
MotionModel.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <array>
17#include <cmath>
19#include <Eigen/Core>
20#include <cstdint>
21#include <imgui.h>
22
28#include "Units.hpp"
31
32namespace NAV
33{
34
35namespace Keys
36{
37
53
55template<typename StateKeyType>
56constexpr std::array<StateKeyType, 3> Pos = { Keys::PosX, Keys::PosY, Keys::PosZ };
58template<typename StateKeyType>
59constexpr std::array<StateKeyType, 3> Vel = { Keys::VelX, Keys::VelY, Keys::VelZ };
61template<typename StateKeyType>
62constexpr std::array<StateKeyType, 4> Att = { Keys::AttQ1, Keys::AttQ2, Keys::AttQ3, Keys::AttQ0 };
63
65template<typename StateKeyType>
66constexpr std::array<StateKeyType, 6> PosVel = { Keys::PosX, Keys::PosY, Keys::PosZ,
68
69template<typename StateKeyType>
73
74} // namespace Keys
75
77template<typename StateKeyType>
79{
80 public:
83 {
84 for (size_t i = 0; i < _gui_covarianceAccel.size(); i++)
85 {
86 // Covariance of the acceleration 𝜎_a due to user motion in horizontal and vertical component [m²/s³]
88 }
89 }
90
94 template<typename Scalar, int Size>
97 {
98 initialize();
99
100 F.template block<3>(Pos, Vel) = Eigen::Matrix3d::Identity();
101 W.template block<3>(Vel, Vel) = Eigen::DiagonalMatrix<double, 3>(_covarianceAccel[0], _covarianceAccel[0], _covarianceAccel[1]);
102 }
103
114 template<typename Scalar, int Size>
120 double dt,
121 const double& latitude,
122 const double& longitude,
123 SystemModelCalcAlgorithm algorithm)
124 {
125 if (algorithm == SystemModelCalcAlgorithm::VanLoan)
126 {
127 G.template block<3>(Vel, Vel) = trafo::e_Quat_n(latitude, longitude).toRotationMatrix();
128 auto [PhiMot, QMot] = NAV::calcPhiAndQWithVanLoanMethod(
129 F.template block<6>(PosVel, PosVel),
130 G.template block<6>(PosVel, PosVel),
131 W.template block<6>(PosVel, PosVel),
132 dt);
133 Phi.template block<6>(PosVel, PosVel) = PhiMot;
134 Q.template block<6>(PosVel, PosVel) = QMot;
135 }
136 else // QCalculationAlgorithm::Taylor1
137 {
138 Phi.template block<6>(PosVel, PosVel) = transitionMatrix_Phi_Taylor(F.template block<6>(PosVel, PosVel), dt, 1);
139 Q.template block<6>(PosVel, PosVel) = calcProcessNoiseMatrixTaylor(dt, latitude, longitude)(all, all);
140 }
141 }
142
149 [[nodiscard]] std::pair<KeyedMatrix6d<StateKeyType>, KeyedMatrix6d<StateKeyType>>
150 calcPhiAndQ(double dt, const double& latitude, const double& longitude, SystemModelCalcAlgorithm algorithm)
151 {
152 KeyedMatrix6d<StateKeyType> F(Eigen::Matrix6d::Zero(), PosVel, PosVel);
153 KeyedMatrix6d<StateKeyType> G(Eigen::Matrix6d::Zero(), PosVel, PosVel);
154 KeyedMatrix6d<StateKeyType> W(Eigen::Matrix6d::Zero(), PosVel, PosVel);
155 initialize(F, W);
156
157 KeyedMatrix6d<StateKeyType> Phi(Eigen::Matrix6d::Zero(), PosVel, PosVel);
158 KeyedMatrix6d<StateKeyType> Q(Eigen::Matrix6d::Zero(), PosVel, PosVel);
159 updatePhiAndQ(Phi, Q, G, F, W, dt, latitude, longitude, algorithm);
160
161 return { Phi, Q };
162 }
163
169 bool ShowGui(float itemWidth, float unitWidth, const char* id)
170 {
171 bool changed = false;
172
173 if (gui::widgets::InputDouble2WithUnit(fmt::format("{} of the acceleration due to user motion (Hor/Ver)##{}",
175 ? "StdDev"
176 : "Variance",
177 id)
178 .c_str(),
179 itemWidth, unitWidth, _gui_covarianceAccel.data(), _gui_covarianceAccelUnit,
180 MakeComboItems<Units::CovarianceAccelUnits>().c_str(), //"m/√(s^3)\0m^2/s^3\0\0",
181 "%.2e", ImGuiInputTextFlags_CharsScientific))
182 {
183 LOG_DEBUG("{}: _gui_covarianceAccel changed to {}", id, fmt::join(_gui_covarianceAccel.begin(), _gui_covarianceAccel.end(), ", "));
184 LOG_DEBUG("{}: _gui_covarianceAccelUnit changed to {}", id, to_string(_gui_covarianceAccelUnit));
185 changed = true;
186 }
187 ImGui::SameLine();
188 gui::widgets::HelpMarker(fmt::format("Suitable values for the horizontal acceleration are around\n"
189 "- 1 {} for a pedestrian or ship,\n"
190 "- {} {} for a car, and\n"
191 "- {} {} for a military aircraft.\n"
192 "The vertical acceleration PSD is usually smaller.",
198 .c_str());
199
200 return changed;
201 }
202
203 private:
205 const std::array<StateKeyType, 3>& Pos = Keys::Pos<StateKeyType>;
207 const std::array<StateKeyType, 3>& Vel = Keys::Vel<StateKeyType>;
209 const std::array<StateKeyType, 6>& PosVel = Keys::PosVel<StateKeyType>;
210
216 [[nodiscard]] KeyedMatrix6d<StateKeyType>
217 calcProcessNoiseMatrixTaylor(double dt, double latitude, double longitude) const
218 {
219 // Scaling matrix in n-frame
220 Eigen::Matrix3d a_S_n = Eigen::DiagonalMatrix<double, 3>(_covarianceAccel[0],
223 // Scaling matrix in e-frame
224 Eigen::Matrix3d a_S_e = trafo::e_Quat_n(latitude, longitude) * a_S_n * trafo::n_Quat_e(latitude, longitude);
225
226 double dt2 = std::pow(dt, 2);
227 double dt3 = dt2 * dt;
228
229 KeyedMatrix6d<StateKeyType> Q(Eigen::Matrix6d::Zero(), PosVel, PosVel);
230
231 // Groves ch. 9.4.2.2, eq. 9.152, p. 417
232 Q.template block<3>(Pos, Pos) = dt3 / 3.0 * a_S_e;
233 Q.template block<3>(Pos, Vel) = dt2 / 2.0 * a_S_e;
234 Q.template block<3>(Vel, Pos) = Q.template block<3>(Pos, Vel).transpose();
235 Q.template block<3>(Vel, Vel) = dt * a_S_e;
236
237 return Q;
238 }
239
244 std::array<double, 2> _gui_covarianceAccel = { 3.0, 1.5 } /* [ m / √(s^3) ] */;
245
247 std::array<double, 2> _covarianceAccel = { 3.0, 1.5 };
248
252 friend void to_json(json& j, const MotionModel& data)
253 {
254 j = {
255 { "covarianceAccelUnit", data._gui_covarianceAccelUnit },
256 { "covarianceAccel", data._gui_covarianceAccel },
257 };
258 }
259
262 friend void from_json(const json& j, MotionModel& data)
263 {
264 if (j.contains("covarianceAccelUnit")) { j.at("covarianceAccelUnit").get_to(data._gui_covarianceAccelUnit); }
265 if (j.contains("covarianceAccel")) { j.at("covarianceAccel").get_to(data._gui_covarianceAccel); }
266 }
267};
268
269} // namespace NAV
270
275std::ostream& operator<<(std::ostream& os, const NAV::Keys::MotionModelKey& obj);
276
277#ifndef DOXYGEN_IGNORE
278
280template<>
281struct fmt::formatter<NAV::Keys::MotionModelKey> : fmt::formatter<const char*>
282{
287 template<typename FormatContext>
288 auto format(const NAV::Keys::MotionModelKey& state, FormatContext& ctx) const
289 {
290 using namespace NAV::Keys; // NOLINT(google-build-using-namespace)
291
292 switch (state)
293 {
294 case PosX:
295 return fmt::formatter<const char*>::format("PosX", ctx);
296 case PosY:
297 return fmt::formatter<const char*>::format("PosY", ctx);
298 case PosZ:
299 return fmt::formatter<const char*>::format("PosZ", ctx);
300 case VelX:
301 return fmt::formatter<const char*>::format("VelX", ctx);
302 case VelY:
303 return fmt::formatter<const char*>::format("VelY", ctx);
304 case VelZ:
305 return fmt::formatter<const char*>::format("VelZ", ctx);
306 case AttQ1:
307 return fmt::formatter<const char*>::format("AttQ1", ctx);
308 case AttQ2:
309 return fmt::formatter<const char*>::format("AttQ2", ctx);
310 case AttQ3:
311 return fmt::formatter<const char*>::format("AttQ3", ctx);
312 case AttQ0:
313 return fmt::formatter<const char*>::format("AttQ0", ctx);
315 return fmt::formatter<const char*>::format("MotionModelKey_COUNT", ctx);
316 }
317
318 return fmt::formatter<const char*>::format("ERROR", ctx);
319 }
320};
321
322#endif
Transformation collection.
Eigen::Quaternion< Scalar > e_Quat_n(const Scalar &latitude, const Scalar &longitude)
Quaternion for rotations from navigation to Earth-fixed frame.
Definition CoordinateFrames.hpp:305
Eigen::Quaternion< Scalar > n_Quat_e(const Scalar &latitude, const Scalar &longitude)
Quaternion for rotations from Earth-fixed to navigation frame.
Definition CoordinateFrames.hpp:320
nlohmann::json json
json namespace
Definition FlowManager.hpp:21
double convertUnit(const double &value, Units::PositionUncertaintyUnits unit)
Converts the value depending on the unit provided.
Units used by the system model parameters.
CovarianceAccelUnits
Possible Units for the Standard deviation of the acceleration due to user motion.
Definition Units.hpp:29
@ m_sqrts3
[ m / √(s^3) ]
Definition Units.hpp:31
Text Help Marker (?) with Tooltip.
void HelpMarker(const char *desc, const char *symbol="(?)")
Text Help Marker, e.g. '(?)', with Tooltip.
Defines Widgets which allow the input of values and the selection of the unit.
InputWithUnitChange InputDouble2WithUnit(const char *label, float itemWidth, float unitWidth, double v[2], U &combo_current_item, const char *combo_items_separated_by_zeros, const char *format="%.6f", ImGuiInputTextFlags flags=0, int combo_popup_max_height_in_items=-1)
Shows an InputText GUI element to modify the provided value and also set its unit.
Definition InputWithUnit.hpp:305
std::string MakeComboItems()
Units separated by '\0' and terminated by double '\0'.
Definition InputWithUnit.hpp:28
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
Generalized Kalman Filter class.
Derived::PlainObject transitionMatrix_Phi_Taylor(const Eigen::MatrixBase< Derived > &F, double tau_s, size_t order)
Calculates the state transition matrix 𝚽 limited to specified order in 𝐅𝜏ₛ
Definition KalmanFilter.hpp:195
std::ostream & operator<<(std::ostream &os, const NAV::Keys::MotionModelKey &obj)
Stream insertion operator overload.
constexpr std::array< StateKeyType, 4 > Att
All attitude keys.
Definition MotionModel.hpp:62
constexpr std::array< StateKeyType, 3 > Pos
All position keys.
Definition MotionModel.hpp:56
MotionModelKey
Keys used in the model.
Definition MotionModel.hpp:40
@ PosY
Position ECEF_Y [m].
Definition MotionModel.hpp:42
@ VelZ
Velocity ECEF_Z [m/s].
Definition MotionModel.hpp:46
@ VelY
Velocity ECEF_Y [m/s].
Definition MotionModel.hpp:45
@ VelX
Velocity ECEF_X [m/s].
Definition MotionModel.hpp:44
@ AttQ0
w: Real (scalar) part of the Quaternion
Definition MotionModel.hpp:50
@ PosX
Position ECEF_X [m].
Definition MotionModel.hpp:41
@ AttQ3
z: Coefficient of k
Definition MotionModel.hpp:49
@ AttQ1
x: Coefficient of i
Definition MotionModel.hpp:47
@ AttQ2
y: Coefficient of j
Definition MotionModel.hpp:48
@ PosZ
Position ECEF_Z [m].
Definition MotionModel.hpp:43
@ MotionModelKey_COUNT
Count.
Definition MotionModel.hpp:51
constexpr std::array< StateKeyType, 10 > PosVelAtt
Vector with all position velocity and attitude keys.
Definition MotionModel.hpp:70
constexpr std::array< StateKeyType, 6 > PosVel
Vector with all position and velocity keys.
Definition MotionModel.hpp:66
constexpr std::array< StateKeyType, 3 > Vel
All velocity keys.
Definition MotionModel.hpp:59
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
System Model.
SystemModelCalcAlgorithm
Algorithms to calculate the system model with.
Definition SystemModel.hpp:23
@ VanLoan
Van-Loan.
Definition SystemModel.hpp:24
Van Loan Method loan1978.
std::pair< typename DerivedF::PlainObject, typename DerivedF::PlainObject > calcPhiAndQWithVanLoanMethod(const Eigen::MatrixBase< DerivedF > &F, const Eigen::MatrixBase< DerivedG > &G, const Eigen::MatrixBase< DerivedW > &W, double dt)
Numerical Method to calculate the State transition matrix 𝚽 and System/Process noise covariance matri...
Definition VanLoan.hpp:65
Static sized KeyedMatrix.
Definition KeyedMatrix.hpp:1921
Motion System Model.
Definition MotionModel.hpp:79
friend void to_json(json &j, const MotionModel &data)
Converts the provided data into a json object.
Definition MotionModel.hpp:252
const std::array< StateKeyType, 6 > & PosVel
All position and velocity keys.
Definition MotionModel.hpp:209
void updatePhiAndQ(KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &Phi, KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &Q, KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &G, const KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &F, const KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &W, double dt, const double &latitude, const double &longitude, SystemModelCalcAlgorithm algorithm)
Updates the provided Phi, Q and G matrix.
Definition MotionModel.hpp:115
std::array< double, 2 > _covarianceAccel
Covariance of the acceleration 𝜎_a due to user motion in horizontal and vertical component [m²/s³].
Definition MotionModel.hpp:247
std::array< double, 2 > _gui_covarianceAccel
GUI selection for the Standard deviation of the acceleration 𝜎_a due to user motion in horizontal and...
Definition MotionModel.hpp:244
void initialize()
Initializes the motion model.
Definition MotionModel.hpp:82
const std::array< StateKeyType, 3 > & Vel
All velocity keys.
Definition MotionModel.hpp:207
void initialize(KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &F, KeyedMatrix< Scalar, StateKeyType, StateKeyType, Size, Size > &W)
Initializes the motion model.
Definition MotionModel.hpp:95
bool ShowGui(float itemWidth, float unitWidth, const char *id)
Shows a GUI.
Definition MotionModel.hpp:169
Units::CovarianceAccelUnits _gui_covarianceAccelUnit
Gui selection for the Unit of the input covarianceAccel parameter for the StDev due to acceleration d...
Definition MotionModel.hpp:241
const std::array< StateKeyType, 3 > & Pos
All position keys.
Definition MotionModel.hpp:205
KeyedMatrix6d< StateKeyType > calcProcessNoiseMatrixTaylor(double dt, double latitude, double longitude) const
Calculates the process noise matrix Q.
Definition MotionModel.hpp:217
friend void from_json(const json &j, MotionModel &data)
Converts the provided json object into the data object.
Definition MotionModel.hpp:262
std::pair< KeyedMatrix6d< StateKeyType >, KeyedMatrix6d< StateKeyType > > calcPhiAndQ(double dt, const double &latitude, const double &longitude, SystemModelCalcAlgorithm algorithm)
Calculates the state transition matrix (𝚽) and the process noise covariance matrix (𝐐)
Definition MotionModel.hpp:150
Matrix which can be accessed by keys.
static const internal::all_t all
Used to request all rows or columns in KeyedMatrices.
Definition KeyedMatrix.hpp:1457
KeyedMatrix< double, RowKeyType, ColKeyType, 6, 6 > KeyedMatrix6d
Static 6x6 squared size KeyedMatrix with double types.
Definition KeyedMatrix.hpp:2338