0.4.1
Loading...
Searching...
No Matches
ImuFusion.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
9/// @file ImuFusion.hpp
10/// @brief Combines signals of sensors that provide the same signal-type to one signal
11/// @author M. Maier (marcel.maier@ins.uni-stuttgart.de)
12/// @date 2022-03-24
13
14#pragma once
15
18
20
22
24
25namespace NAV
26{
27/// @brief Combines signals of sensors that provide the same signal-type to one signal
28class ImuFusion : public Imu
29{
30 public:
31 /// @brief Default constructor
32 ImuFusion();
33 /// @brief Destructor
34 ~ImuFusion() override;
35 /// @brief Copy constructor
36 ImuFusion(const ImuFusion&) = delete;
37 /// @brief Move constructor
38 ImuFusion(ImuFusion&&) = delete;
39 /// @brief Copy assignment operator
40 ImuFusion& operator=(const ImuFusion&) = delete;
41 /// @brief Move assignment operator
43
44 /// @brief String representation of the Class Type
45 [[nodiscard]] static std::string typeStatic();
46
47 /// @brief String representation of the Class Type
48 [[nodiscard]] std::string type() const override;
49
50 /// @brief String representation of the Class Category
51 [[nodiscard]] static std::string category();
52
53 /// @brief ImGui config window which is shown on double click
54 /// @attention Don't forget to set _hasConfig to true in the constructor of the node
55 void guiConfig() override;
56
57 /// @brief Saves the node into a json object
58 [[nodiscard]] json save() const override;
59
60 /// @brief Restores the node from a json object
61 /// @param[in] j Json object with the node state
62 void restore(const json& j) override;
63
64 protected:
65 /// Position and rotation information for conversion from platform to body frame
67
68 private:
69 constexpr static size_t OUTPUT_PORT_INDEX_COMBINED_SIGNAL = 0; ///< @brief Flow (ImuObs)
70 constexpr static size_t OUTPUT_PORT_INDEX_BIASES = 1; ///< @brief Flow (ImuBiases)
71
72 /// @brief Initialize the node
73 bool initialize() override;
74
75 /// @brief Deinitialize the node
76 void deinitialize() override;
77
78 /// @brief Adds/Deletes Input Pins depending on the variable _nInputPins
80
81 /// @brief Initializes the rotation matrices used for the mounting angles of the sensors
83
84 /// @brief Receive Function for the signal at the time tₖ
85 /// @param[in] queue Queue with all the received data messages
86 /// @param[in] pinIdx Index of the pin the data is received on
87 void recvSignal(InputPin::NodeDataQueue& queue, size_t pinIdx);
88
89 /// @brief Calculates the adaptive measurement noise matrix R
90 /// @param[in] alpha Forgetting factor (i.e. weight on previous estimates), 0 < alpha < 1
91 /// @param[in] R Measurement noise covariance matrix at the previous epoch
92 /// @param[in] e Vector of residuals
93 /// @param[in] H Design matrix
94 /// @param[in] P Error covariance matrix
95 /// @return Measurement noise matrix R
96 /// @note See https://arxiv.org/pdf/1702.00884.pdf
97 [[nodiscard]] static Eigen::MatrixXd measurementNoiseMatrix_R_adaptive(double alpha,
98 const Eigen::MatrixXd& R,
99 const Eigen::VectorXd& e,
100 const Eigen::MatrixXd& H,
101 const Eigen::MatrixXd& P);
102
103 /// @brief Previous observation (for timestamp)
105
106 /// @brief Calculates the initial measurement noise matrix R
107 /// @param[in] R Measurement noise uncertainty matrix for sensor at 'pinIndex'
108 /// @param[in] pinIndex Index of pin to identify sensor
109 void measurementNoiseMatrix_R(Eigen::MatrixXd& R, size_t pinIndex = 0) const;
110
111 /// @brief Combines the signals
112 /// @param[in] imuObs Imu observation
113 void combineSignals(const std::shared_ptr<const ImuObs>& imuObs);
114
115 // --------------------------------------- Kalman filter config ------------------------------------------
116 /// Number of input pins
117 size_t _nInputPins = 2;
118
119 /// @brief Number of states estimated by the IRW-KF (angular rate, angular acceleration, specific force, jerk, all in 3D: 4*3 = 12)
120 const uint8_t _numStatesEstIRWKF = 12;
121
122 /// @brief Number of states estimated by the B-spline KF (3 stacked B-splines in 3D for angular rate and specific force: 3*3*2 = 18)
123 const uint8_t _numStatesEstBsplineKF = 18;
124
125 /// @brief Number of quadratic B-splines that make up the entire 3D stacked B-spline
126 const uint8_t _numBsplines = 9;
127
128 /// @brief Number of states per pin (biases of accel and gyro)
129 const uint8_t _numStatesPerPin = 6;
130
131 /// @brief Number of measurements overall
132 const uint8_t _numMeasurements = 6;
133
134 /// @brief Number of estimated states (depends on imuFusionType)
135 uint8_t _numStatesEst{};
136
137 /// @brief Number of states overall
138 uint8_t _numStates = 12;
139
140 /// Kalman Filter representation
142
143 // ---------------------------------------- Kalman filter init -------------------------------------------
144 /// @brief Highest IMU sample rate in [Hz] (for time step in KF prediction)
145 double _imuFrequency{ 100 };
146
147 /// @brief Time until averaging ends and filtering starts in [s]
148 double _averageEndTime{ 1 };
149
150 /// @brief Time until averaging ends and filtering starts as 'InsTime'
152
153 /// @brief Auto-initialize the Kalman Filter - GUI setting
154 bool _autoInitKF = true;
155
156 /// @brief If the multiple IMUs have the same characteristics, GUI input cells can be reduced considerably
158
159 /// @brief If the multiple IMUs have the same bias, GUI input cells can be reduced considerably
161
162 /// @brief Container that collects all imuObs for averaging for auto-init of the KF
163 std::vector<std::shared_ptr<const NAV::ImuObs>> _cumulatedImuObs;
164
165 /// @brief Container that collects all pinIds for averaging for auto-init of the KF
166 std::vector<size_t> _cumulatedPinIds;
167
168 /// @brief flag to determine how jerk and angular acceleration states are initialized if '_autoInitKF = true'
169 bool _initJerkAngAcc = true;
170
171 /// @brief flag to check whether KF has been auto-initialized
172 bool _kfInitialized = false;
173
174 // -------------------------------------------- Sensor info ----------------------------------------------
175 /// @brief Stores parameter data for each connected sensor
176 std::vector<PinData> _pinData;
177 /// @brief Stores IRW-KF specific parameter data
179 /// @brief Stores Bspline-KF specific parameter data
181
182 /// @brief Temporary vector for the initial coefficients for angular rate
183 Eigen::Vector3d _initCoeffsAngRateTemp;
184 /// @brief Temporary vector for the initial coefficients for acceleration
185 Eigen::Vector3d _initCoeffsAccelTemp;
186 /// @brief Temporary vector for the initial coefficients' initial covariance for the angular rate
188 /// @brief Temporary vector for the initial coefficients' initial covariance for the acceleration
190 /// @brief Temporary vector for the initial coefficients' process noise for the angular rate
192 /// @brief Temporary vector for the initial coefficients' process noise for the acceleration
194
195 /// @brief Container for the bias covariances
196 std::vector<Eigen::Vector3d> _biasCovariances;
197 /// @brief Container for process noise of each state
198 std::vector<Eigen::VectorXd> _processNoiseVariances;
199 /// @brief Container for measurement noises of each sensor
200 std::vector<Eigen::Vector3d> _measurementNoiseVariances;
201
202 /// @brief Rotations of all connected accelerometers - key: pinIndex, value: Rotation matrix of the accelerometer platform to body frame
203 std::vector<Eigen::Matrix3d> _imuRotations_accel;
204
205 /// @brief Rotations of all connected gyros - key: pinIndex, value: Rotation matrix of the gyro platform to body frame
206 std::vector<Eigen::Matrix3d> _imuRotations_gyro;
207
208 // ----------------------------------------------- Time --------------------------------------------------
209 /// @brief Saves the timestamp of the measurement before in [s]
211
212 /// @brief Saves the first timestamp in [s]
214
215 /// @brief Time since startup in [s]
217
218 /// @brief Latest knot in [s]
219 double _latestKnot{};
220
221 /// @brief Time difference between two quadratic B-splines in the stacked B-spline
222 double _splineSpacing = 1.0;
223
224 // ------------------------------------------- Miscellaneous ---------------------------------------------
225 /// @brief Check the rank of the Kalman matrices every iteration (computationally expensive)
227
228 /// @brief Check whether the combined solution has an '_imuPos' set
229 bool _imuPosSet = false;
230
231 /// Possible KF-types for the IMU fusion
232 enum class ImuFusionType : uint8_t
233 {
234 IRWKF, ///< IRW Kalman filter
235 Bspline, ///< B-spline Kalman filter
236 COUNT, ///< Number of items in the enum
237 };
238 /// @brief Converts the enum to a string
239 /// @param[in] value Enum value to convert into text
240 /// @return String representation of the enum
241 friend constexpr const char* to_string(ImuFusionType value);
242
243 /// KF-type for the IMU fusion, selected in the GUI
245};
246
247constexpr const char* to_string(NAV::ImuFusion::ImuFusionType value)
248{
249 switch (value)
250 {
252 return "IRW KF";
254 return "B-spline KF";
256 return "";
257 }
258 return "";
259}
260
261} // namespace NAV
nlohmann::json json
json namespace
Parent Class for all IMU Observations.
Abstract IMU Class.
Generalized Kalman Filter class.
Information about a sensor which is connected to a certain pin.
Widget to modify time point values.
KalmanFilter _kalmanFilter
Kalman Filter representation.
void updateNumberOfInputPins()
Adds/Deletes Input Pins depending on the variable _nInputPins.
std::vector< Eigen::Matrix3d > _imuRotations_accel
Rotations of all connected accelerometers - key: pinIndex, value: Rotation matrix of the acceleromete...
uint8_t _numStatesEst
Number of estimated states (depends on imuFusionType)
friend constexpr const char * to_string(ImuFusionType value)
Converts the enum to a string.
const uint8_t _numStatesEstIRWKF
Number of states estimated by the IRW-KF (angular rate, angular acceleration, specific force,...
void initializeMountingAngles()
Initializes the rotation matrices used for the mounting angles of the sensors.
std::string type() const override
String representation of the Class Type.
bool initialize() override
Initialize the node.
Eigen::Vector3d _procNoiseCoeffsAccelTemp
Temporary vector for the initial coefficients' process noise for the acceleration.
bool _kfInitialized
flag to check whether KF has been auto-initialized
bool _checkKalmanMatricesRanks
Check the rank of the Kalman matrices every iteration (computationally expensive)
bool _imuCharacteristicsIdentical
If the multiple IMUs have the same characteristics, GUI input cells can be reduced considerably.
std::vector< Eigen::Matrix3d > _imuRotations_gyro
Rotations of all connected gyros - key: pinIndex, value: Rotation matrix of the gyro platform to body...
Eigen::Vector3d _initCovarianceCoeffsAngRateTemp
Temporary vector for the initial coefficients' initial covariance for the angular rate.
std::vector< Eigen::Vector3d > _measurementNoiseVariances
Container for measurement noises of each sensor.
ImuFusion(const ImuFusion &)=delete
Copy constructor.
const uint8_t _numBsplines
Number of quadratic B-splines that make up the entire 3D stacked B-spline.
json save() const override
Saves the node into a json object.
void recvSignal(InputPin::NodeDataQueue &queue, size_t pinIdx)
Receive Function for the signal at the time tₖ
const uint8_t _numMeasurements
Number of measurements overall.
size_t _nInputPins
Number of input pins.
InsTime _lastFiltObs
Previous observation (for timestamp)
ImuFusion & operator=(const ImuFusion &)=delete
Copy assignment operator.
~ImuFusion() override
Destructor.
void measurementNoiseMatrix_R(Eigen::MatrixXd &R, size_t pinIndex=0) const
Calculates the initial measurement noise matrix R.
ImuPos _imuPos
Position and rotation information for conversion from platform to body frame.
Definition ImuFusion.hpp:66
InsTime _firstTimestamp
Saves the first timestamp in [s].
PinDataIRWKF _pinDataIRWKF
Stores IRW-KF specific parameter data.
Eigen::Vector3d _initCoeffsAccelTemp
Temporary vector for the initial coefficients for acceleration.
double _imuFrequency
Highest IMU sample rate in [Hz] (for time step in KF prediction)
std::vector< Eigen::Vector3d > _biasCovariances
Container for the bias covariances.
double _averageEndTime
Time until averaging ends and filtering starts in [s].
static std::string typeStatic()
String representation of the Class Type.
bool _imuPosSet
Check whether the combined solution has an '_imuPos' set.
static Eigen::MatrixXd measurementNoiseMatrix_R_adaptive(double alpha, const Eigen::MatrixXd &R, const Eigen::VectorXd &e, const Eigen::MatrixXd &H, const Eigen::MatrixXd &P)
Calculates the adaptive measurement noise matrix R.
static constexpr size_t OUTPUT_PORT_INDEX_BIASES
Flow (ImuBiases)
Definition ImuFusion.hpp:70
static constexpr size_t OUTPUT_PORT_INDEX_COMBINED_SIGNAL
Flow (ImuObs)
Definition ImuFusion.hpp:69
void guiConfig() override
ImGui config window which is shown on double click.
ImuFusion()
Default constructor.
std::vector< std::shared_ptr< const NAV::ImuObs > > _cumulatedImuObs
Container that collects all imuObs for averaging for auto-init of the KF.
void restore(const json &j) override
Restores the node from a json object.
static std::string category()
String representation of the Class Category.
Eigen::Vector3d _initCovarianceCoeffsAccelTemp
Temporary vector for the initial coefficients' initial covariance for the acceleration.
ImuFusion & operator=(ImuFusion &&)=delete
Move assignment operator.
std::vector< size_t > _cumulatedPinIds
Container that collects all pinIds for averaging for auto-init of the KF.
uint8_t _numStates
Number of states overall.
const uint8_t _numStatesEstBsplineKF
Number of states estimated by the B-spline KF (3 stacked B-splines in 3D for angular rate and specifi...
double _timeSinceStartup
Time since startup in [s].
bool _initJerkAngAcc
flag to determine how jerk and angular acceleration states are initialized if '_autoInitKF = true'
std::vector< Eigen::VectorXd > _processNoiseVariances
Container for process noise of each state.
void deinitialize() override
Deinitialize the node.
double _latestKnot
Latest knot in [s].
const uint8_t _numStatesPerPin
Number of states per pin (biases of accel and gyro)
ImuFusionType _imuFusionType
KF-type for the IMU fusion, selected in the GUI.
bool _autoInitKF
Auto-initialize the Kalman Filter - GUI setting.
ImuFusionType
Possible KF-types for the IMU fusion.
@ Bspline
B-spline Kalman filter.
@ IRWKF
IRW Kalman filter.
@ COUNT
Number of items in the enum.
double _splineSpacing
Time difference between two quadratic B-splines in the stacked B-spline.
std::vector< PinData > _pinData
Stores parameter data for each connected sensor.
Eigen::Vector3d _procNoiseCoeffsAngRateTemp
Temporary vector for the initial coefficients' process noise for the angular rate.
ImuFusion(ImuFusion &&)=delete
Move constructor.
void combineSignals(const std::shared_ptr< const ImuObs > &imuObs)
Combines the signals.
PinDataBsplineKF _pinDataBsplineKF
Stores Bspline-KF specific parameter data.
bool _imuBiasesIdentical
If the multiple IMUs have the same bias, GUI input cells can be reduced considerably.
InsTime _latestTimestamp
Saves the timestamp of the measurement before in [s].
InsTime _avgEndTime
Time until averaging ends and filtering starts as 'InsTime'.
Eigen::Vector3d _initCoeffsAngRateTemp
Temporary vector for the initial coefficients for angular rate.
IMU Position.
Definition ImuPos.hpp:26
Imu(const Imu &)=delete
Copy constructor.
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
Generalized Kalman Filter class.
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
Sensor information specific to the Bspline-KF.
Definition PinData.hpp:179
Sensor information specific to the IRW-KF.
Definition PinData.hpp:105