0.5.0
Loading...
Searching...
No Matches
ImuIntegrator.cpp
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#include "ImuIntegrator.hpp"
10
11#include <memory>
12#include <algorithm>
13#include <cstddef>
14
15#include <imgui.h>
16#include <imgui_internal.h>
17
20
21#include "NodeRegistry.hpp"
23#include <fmt/format.h>
24namespace nm = NAV::NodeManager;
26
29
30#include "util/Logger.hpp"
31#include "util/Eigen.hpp"
32
34 : Node(typeStatic())
35{
36 LOG_TRACE("{}: called", name);
37
38 _hasConfig = true;
39 _guiConfigDefaultWindowSize = { 422, 146 };
40
42 [](const Node* node, const InputPin& inputPin) {
43 const auto* imuIntegrator = static_cast<const ImuIntegrator*>(node); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
44 return !inputPin.queue.empty() && imuIntegrator->_inertialIntegrator.hasInitialPosition();
45 });
47
49}
50
55
57{
58 return "ImuIntegrator";
59}
60
61std::string NAV::ImuIntegrator::type() const
62{
63 return typeStatic();
64}
65
67{
68 return "Data Processor";
69}
70
72{
73 if (ImGui::Checkbox(fmt::format("IMU Preintegration##{}", size_t(id)).c_str(), &_imuPreintegration))
74 {
76 }
77
79 {
80 if (ImGui::Checkbox(fmt::format("Reset every epoch##{}", size_t(id)).c_str(), &_resetPreintegratorEveryEpoch))
81 {
83 }
84 ImGui::SameLine();
85 gui::widgets::HelpMarker("Resetting the preintegrator every epoch makes it behave like a normal integrator");
86
87 if (InertialPreIntegratorGui(std::to_string(size_t(id)).c_str(), _inertialPreintegrator))
88 {
90 }
91 }
92 else
93 {
95 {
97 }
98 }
99}
100
101[[nodiscard]] json NAV::ImuIntegrator::save() const
102{
103 LOG_TRACE("{}: called", nameId());
104
105 return {
106 { "imuPreintegration", _imuPreintegration },
107 { "resetPreintegratorEveryEpoch", _resetPreintegratorEveryEpoch },
108 { "inertialIntegrator", _inertialIntegrator },
109 { "inertialPreintegrator", _inertialPreintegrator },
110 { "preferAccelerationOverDeltaMeasurements", _preferAccelerationOverDeltaMeasurements },
111 };
112}
113
115{
116 LOG_TRACE("{}: called", nameId());
117
118 if (j.contains("imuPreintegration")) { j.at("imuPreintegration").get_to(_imuPreintegration); }
119 if (j.contains("resetPreintegratorEveryEpoch")) { j.at("resetPreintegratorEveryEpoch").get_to(_resetPreintegratorEveryEpoch); }
120 if (j.contains("inertialIntegrator")) { j.at("inertialIntegrator").get_to(_inertialIntegrator); }
121 if (j.contains("inertialPreintegrator")) { j.at("inertialPreintegrator").get_to(_inertialPreintegrator); }
122 if (j.contains("preferAccelerationOverDeltaMeasurements")) { j.at("preferAccelerationOverDeltaMeasurements").get_to(_preferAccelerationOverDeltaMeasurements); }
123}
124
126{
127 LOG_TRACE("{}: called", nameId());
128
129 _inertialIntegrator.reset();
131 _lastImuObs.reset();
132 _lastPosVelAtt.reset();
133
134 LOG_DEBUG("ImuIntegrator initialized");
135
136 return true;
137}
138
140{
141 LOG_TRACE("{}: called", nameId());
142}
143
145{
146 auto nodeData = queue.extract_front();
147 if (nodeData->insTime.empty())
148 {
149 LOG_ERROR("{}: Can't set new imuObs__t0 because the observation has no time tag (insTime)", nameId());
150 return;
151 }
152
153 std::shared_ptr<NAV::PosVelAtt> integratedPosVelAtt = nullptr;
154
156 {
157 auto obs = std::static_pointer_cast<const ImuObs>(nodeData);
158 LOG_DATA("{}: recvImuObs at time [{}] (preintegration)", nameId(), obs->insTime.toYMDHMS(GPST));
159 // LOG_DATA("{}: p_accel = {}", nameId(), obs->p_acceleration.transpose());
160 // LOG_DATA("{}: p_gyro = {}", nameId(), obs->p_angularRate.transpose());
161
162 if (!_lastImuObs)
163 {
164 _lastImuObs = obs;
165 LOG_DATA("{}: Skipping as first epoch", nameId());
166 return;
167 }
168
169 auto dt = static_cast<double>((obs->insTime - _lastImuObs->insTime).count());
170 // LOG_DATA("{}: dt(imu) = {}", nameId(), dt);
171 if (dt <= 1e-9)
172 {
173 LOG_DATA("{}: Skipping as dt is negative or zero", nameId());
174 return;
175 }
178 .p_acceleration = _lastImuObs->p_acceleration,
179 .p_angularRate = _lastImuObs->p_angularRate },
181 };
183 .position = _lastPosVelAtt->e_position(),
184 .velocity = _lastPosVelAtt->e_velocity(),
185 .attitude = _lastPosVelAtt->e_Quat_b()
186 };
187
188 _inertialPreintegrator.addInertialMeasurement(meas, obs->imuPos, nameId());
189
190 auto pvaNew = _inertialPreintegrator.calcIntegratedState(pvaOld, InertialPreIntegrator::ImuState<double>{}, obs->imuPos, nameId());
191
192 integratedPosVelAtt = std::make_shared<PosVelAtt>();
193 integratedPosVelAtt->insTime = obs->insTime;
194 integratedPosVelAtt->setPosVelAtt_e(pvaNew.position, pvaNew.velocity, pvaNew.attitude);
195
196 _lastImuObs = obs;
197
199 {
200 _lastPosVelAtt = integratedPosVelAtt;
202 }
203 }
204 else
205 {
207 && NAV::NodeRegistry::NodeDataTypeAnyIsChildOf(inputPins.at(INPUT_PORT_INDEX_IMU_OBS).link.getConnectedPin()->dataIdentifier, { ImuObsWDelta::type() }))
208 {
209 auto obs = std::static_pointer_cast<const ImuObsWDelta>(nodeData);
210 LOG_DATA("{}: recvImuObsWDelta at time [{}]", nameId(), obs->insTime.toYMDHMS(GPST));
211
212 integratedPosVelAtt = _inertialIntegrator.calcInertialSolutionDelta(obs->insTime, obs->dtime, obs->dvel, obs->dtheta, obs->imuPos, nameId().c_str());
213 }
214 else
215 {
216 auto obs = std::static_pointer_cast<const ImuObs>(nodeData);
217 LOG_DATA("{}: recvImuObs at time [{}]", nameId(), obs->insTime.toYMDHMS(GPST));
218
219 integratedPosVelAtt = _inertialIntegrator.calcInertialSolution(obs->insTime, obs->p_acceleration, obs->p_angularRate, obs->imuPos, nameId().c_str());
220 }
221 }
222
223 if (integratedPosVelAtt)
224 {
225 LOG_DATA("{}: e_position = {}", nameId(), integratedPosVelAtt->e_position().transpose());
226 LOG_DATA("{}: e_velocity = {}", nameId(), integratedPosVelAtt->e_velocity().transpose());
227 LOG_DATA("{}: rollPitchYaw = {}", nameId(), rad2deg(integratedPosVelAtt->rollPitchYaw()).transpose());
229 }
230}
231
233{
234 auto posVelAtt = std::static_pointer_cast<const PosVelAtt>(queue.extract_front());
235 inputPins[INPUT_PORT_INDEX_POS_VEL_ATT_INIT].queueBlocked = true;
237
238 LOG_DATA("{}: recvPosVelAttInit at time [{}]", nameId(), posVelAtt->insTime.toYMDHMS(GPST));
239
240 _lastPosVelAtt = posVelAtt;
241 _inertialIntegrator.setInitialState(*posVelAtt, nameId().c_str());
242 LOG_DATA("{}: e_position = {}", nameId(), posVelAtt->e_position().transpose());
243 LOG_DATA("{}: e_velocity = {}", nameId(), posVelAtt->e_velocity().transpose());
244 LOG_DATA("{}: rollPitchYaw = {}", nameId(), rad2deg(posVelAtt->rollPitchYaw()).transpose());
245
247}
Holds all Constants.
Vector space operations.
Save/Load the Nodes.
nlohmann::json json
json namespace
Text Help Marker (?) with Tooltip.
Integrates ImuObs Data.
Inertial Measurement Preintegrator.
Utility class for logging to console and file.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
#define LOG_ERROR
Error occurred, which stops part of the program to work, but not everything.
Definition Logger.hpp:73
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Simple Math functions.
Manages all Nodes.
Utility class which specifies available nodes.
static constexpr size_t INPUT_PORT_INDEX_POS_VEL_ATT_INIT
Flow (PosVelAtt)
std::shared_ptr< const PosVelAtt > _lastPosVelAtt
Last position, velocity and attitude.
void deinitialize() override
Deinitialize the node.
void recvObservation(InputPin::NodeDataQueue &queue, size_t pinIdx)
Receive Function.
std::shared_ptr< const ImuObs > _lastImuObs
Last IMU measuremnt.
bool _preferAccelerationOverDeltaMeasurements
Prefer the raw acceleration measurements over the deltaVel & deltaTheta values.
static std::string category()
String representation of the Class Category.
static std::string typeStatic()
String representation of the Class Type.
void restore(const json &j) override
Restores the node from a json object.
bool _imuPreintegration
Wether IMU preintegration should be used.
std::string type() const override
String representation of the Class Type.
ImuIntegrator()
Default constructor.
void guiConfig() override
ImGui config window which is shown on double click.
bool initialize() override
Initialize the node.
~ImuIntegrator() override
Destructor.
static constexpr size_t OUTPUT_PORT_INDEX_INERTIAL_NAV_SOL
Flow (InertialNavSol)
InertialPreIntegrator _inertialPreintegrator
Inertial Preintegrator.
void recvPosVelAttInit(InputPin::NodeDataQueue &queue, size_t pinIdx)
Receive Function for the PosVelAtt initial values.
json save() const override
Saves the node into a json object.
bool _resetPreintegratorEveryEpoch
Resetting the preintegrator every epoch makes it behave like a normal integrator.
InertialIntegrator _inertialIntegrator
Inertial Integrator.
static constexpr size_t INPUT_PORT_INDEX_IMU_OBS
Flow (ImuObs)
static std::string type()
Returns the type of the data class.
static std::string type()
Returns the type of the data class.
Definition ImuObs.hpp:33
GenericMeasurement< double > Measurement
Inertial measurements.
Input pins of nodes.
Definition Pin.hpp:491
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
Node(std::string name)
Constructor.
Definition Node.cpp:30
std::vector< InputPin > inputPins
List of input pins.
Definition Node.hpp:397
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:180
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
static std::string type()
Returns the type of the data class.
Definition PosVelAtt.hpp:29
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
OutputPin * CreateOutputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
InputPin * CreateInputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier={}, InputPin::Callback callback=static_cast< InputPin::FlowFirableCallbackFunc >(nullptr), InputPin::FlowFirableCheckFunc firable=nullptr, int priority=0, int idx=-1)
Create an Input Pin object.
bool NodeDataTypeAnyIsChildOf(const std::vector< std::string > &childTypes, const std::vector< std::string > &parentTypes)
Checks if any of the provided child types is a child of any of the provided parent types.
void ApplyChanges()
Signals that there have been changes to the flow.
void HelpMarker(const char *desc, const char *symbol="(?)")
Text Help Marker, e.g. '(?)', with Tooltip.
@ GPST
GPS Time.
bool InertialPreIntegratorGui(const char *label, InertialPreIntegrator &integrator, float width)
Shows a GUI for advanced configuration of the InertialPreIntegrator.
bool InertialIntegratorGui(const char *label, InertialIntegrator &integrator, bool &preferAccelerationOverDeltaMeasurements, float width)
Shows a GUI for advanced configuration of the InertialIntegrator.
constexpr auto rad2deg(const T &rad)
Convert Radians to Degree.
Definition Units.hpp:39
Position, velocity and attitude state.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52