INSTINCT Code Coverage Report


Directory: src/
File: Nodes/DataProvider/Barometer/Simulators/BaroSimulator.cpp
Date: 2025-11-25 23:34:18
Exec Total Coverage
Lines: 16 109 14.7%
Functions: 4 12 33.3%
Branches: 17 202 8.4%

Line Branch Exec Source
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 "BaroSimulator.hpp"
10
11 #include <limits>
12
13 #include "util/Logger.hpp"
14
15 #include "internal/FlowManager.hpp"
16
17 #include "NodeData/State/Pos.hpp"
18 #include "NodeData/Baro/BaroPressObs.hpp"
19
20 #include "Navigation/Gravity/Gravity.hpp"
21
22 #include "internal/gui/widgets/imgui_ex.hpp"
23 #include "internal/gui/widgets/EnumCombo.hpp"
24 #include "internal/gui/widgets/HelpMarker.hpp"
25 #include "internal/gui/NodeEditorApplication.hpp"
26
27 114 NAV::BaroSimulator::BaroSimulator()
28
4/8
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 114 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 114 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 114 times.
✗ Branch 12 not taken.
114 : Node(typeStatic())
29 {
30 LOG_TRACE("{}: called", name);
31 114 _hasConfig = true;
32 114 _guiConfigDefaultWindowSize = { 710, 220 };
33
34
1/2
✓ Branch 4 taken 114 times.
✗ Branch 5 not taken.
114 std::mt19937_64 gen(static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count()));
35
1/2
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
114 std::uniform_int_distribution<uint64_t> dist(0, std::numeric_limits<uint64_t>::max() / 2);
36
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
114 _pressureRng.seed = dist(gen);
37
38
4/8
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 114 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 114 times.
✓ Branch 10 taken 114 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
456 CreateOutputPin("BaroPressObs", Pin::Type::Flow, { NAV::BaroPressObs::type() });
39
40
4/8
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 114 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 114 times.
✓ Branch 9 taken 114 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
342 CreateInputPin("Pos", Pin::Type::Flow, { NAV::Pos::type() }, &BaroSimulator::receiveObs);
41 342 }
42
43 228 NAV::BaroSimulator::~BaroSimulator()
44 {
45 LOG_TRACE("{}: called", nameId());
46 228 }
47
48 228 std::string NAV::BaroSimulator::typeStatic()
49 {
50
1/2
✓ Branch 1 taken 228 times.
✗ Branch 2 not taken.
456 return "BaroSimulator";
51 }
52
53 std::string NAV::BaroSimulator::type() const
54 {
55 return typeStatic();
56 }
57
58 114 std::string NAV::BaroSimulator::category()
59 {
60
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
228 return "Data Simulator";
61 }
62
63 void NAV::BaroSimulator::guiConfig()
64 {
65 float columnWidth = 140.0F * gui::NodeEditorApplication::windowFontRatio();
66
67 if (ImGui::InputDouble(fmt::format("Sea-level pressure##{}", size_t(id)).c_str(), &_pressure0, 0.0, 0.0, "%.3f hPa"))
68 {
69 LOG_DATA("{}: pressure0 changed to {}", nameId(), _pressure0);
70 flow::ApplyChanges();
71 }
72 if (ImGui::InputDouble(fmt::format("Sea-level temperature##{}", size_t(id)).c_str(), &_temp0, 0.0, 0.0, "%.3f K"))
73 {
74 LOG_DATA("{}: temo0 changed to {}", nameId(), _temp0);
75 flow::ApplyChanges();
76 }
77 if (ImGui::InputDouble(fmt::format("Temperature lapse rate##{}", size_t(id)).c_str(), &_lapserate, 0.0, 0.0, "%.6f K / m"))
78 {
79 LOG_DATA("{}: lapserate changed to {}", nameId(), _lapserate);
80 flow::ApplyChanges();
81 }
82 if (ImGui::InputDouble(fmt::format("Geoid undulation##{}", size_t(id)).c_str(), &_geoidhgt, 0.0, 0.0, "%.3f m"))
83 {
84 LOG_DATA("{}: geoidhgt changed to {}", nameId(), _geoidhgt);
85 flow::ApplyChanges();
86 }
87 ImGui::SetNextItemWidth(columnWidth);
88 if (gui::widgets::EnumCombo(fmt::format("Gravity input##{}", size_t(id)).c_str(), _gravityInput))
89 {
90 LOG_DATA("{}: gravity input changed to {}", nameId(), fmt::underlying(_gravityInput));
91 flow::ApplyChanges();
92 }
93 ImGui::SameLine();
94 gui::widgets::HelpMarker("The 'position' input calculates the gravity magnitude from EGM96.\n\n");
95 if (_gravityInput == GravityInput::Manual)
96 {
97 if (ImGui::InputDouble(fmt::format("Gravity##{}", size_t(id)).c_str(), &_gravity, 0.0, 0.0, "%.5f m / s²"))
98 {
99 LOG_DATA("{}: gravity changed to {}", nameId(), _gravity);
100 flow::ApplyChanges();
101 }
102 }
103 else // (_gravityInput == GravityInput::position)
104 {
105 if (ImGui::InputDouble3(fmt::format("Position LLA [deg, deg, m]##{}", size_t(id)).c_str(), _initPos.data()))
106 {
107 flow::ApplyChanges();
108 }
109 }
110 if (ImGui::InputDouble(fmt::format("Std. Dev. of Pressure noise##{}", size_t(id)).c_str(), &_pressurenoise, 0.0, 0.0, "%.3f hPa"))
111 {
112 LOG_DEBUG("{}: pressurenoise changed to {}", nameId(), _pressurenoise);
113 flow::ApplyChanges();
114 }
115 float itemWidth = 470 * gui::NodeEditorApplication::windowFontRatio();
116 auto rngInput = [&](const char* title, RandomNumberGenerator& rng) {
117 float currentCursorX = ImGui::GetCursorPosX();
118 if (ImGui::Checkbox(fmt::format("##rng.useSeed {} {}", title, size_t(id)).c_str(), &rng.useSeed))
119 {
120 LOG_DEBUG("{}: {} rng.useSeed changed to {}", nameId(), title, rng.useSeed);
121 flow::ApplyChanges();
122 }
123 if (ImGui::IsItemHovered()) { ImGui::SetTooltip("Use seed?"); }
124 ImGui::SameLine();
125 if (!rng.useSeed)
126 {
127 ImGui::BeginDisabled();
128 }
129 ImGui::SetNextItemWidth(itemWidth - (ImGui::GetCursorPosX() - currentCursorX));
130 if (ImGui::SliderULong(fmt::format("{} Seed##{}", title, size_t(id)).c_str(), &rng.seed, 0, std::numeric_limits<uint64_t>::max() / 2, "%lu"))
131 {
132 LOG_DEBUG("{}: {} rng.seed changed to {}", nameId(), title, rng.seed);
133 flow::ApplyChanges();
134 }
135 if (!rng.useSeed)
136 {
137 ImGui::EndDisabled();
138 }
139 };
140 rngInput("Pressure Noise seed", _pressureRng);
141 }
142
143 bool NAV::BaroSimulator::initialize()
144 {
145 LOG_TRACE("{}: called", nameId());
146 if (_gravityInput == GravityInput::Position)
147 {
148 _gravity = n_calcGravitation_EGM96(_initPos).norm();
149 LOG_DATA("{}: PressToHgt - Local gravity magnitude: {} m/s² at initial position: {} [deg, deg, m]", nameId(), _gravity, _initPos.transpose());
150 }
151 _exponent = _gravity * InsConst::dMtr / InsConst::Rg / _lapserate;
152 _pressureRng.resetSeed();
153 return true;
154 }
155
156 void NAV::BaroSimulator::deinitialize()
157 {
158 LOG_TRACE("{}: called", nameId());
159 }
160
161 [[nodiscard]] json NAV::BaroSimulator::save() const
162 {
163 LOG_TRACE("{}: called", nameId());
164
165 json j;
166
167 j["temp0"] = _temp0;
168 j["pressure0"] = _pressure0;
169 j["lapserate"] = _lapserate;
170 j["geoidhgt"] = _geoidhgt;
171 j["gravity"] = _gravity;
172 j["initPos"] = _initPos;
173 j["gravityInput"] = _gravityInput;
174 j["pressurenoise"] = _pressurenoise;
175 j["pressureRng"] = _pressureRng;
176 return j;
177 }
178
179 void NAV::BaroSimulator::restore(json const& j)
180 {
181 LOG_TRACE("{}: called", nameId());
182
183 if (j.contains("temp0"))
184 {
185 j.at("temp0").get_to(_temp0);
186 }
187 if (j.contains("pressure0"))
188 {
189 j.at("pressure0").get_to(_pressure0);
190 }
191 if (j.contains("lapserate"))
192 {
193 j.at("lapserate").get_to(_lapserate);
194 }
195 if (j.contains("geoidhgt"))
196 {
197 j.at("geoidhgt").get_to(_geoidhgt);
198 }
199 if (j.contains("gravity"))
200 {
201 j.at("gravity").get_to(_gravity);
202 }
203 if (j.contains("initPos"))
204 {
205 j.at("initPos").get_to(_initPos);
206 }
207 if (j.contains("gravityInput"))
208 {
209 j.at("gravityInput").get_to(_gravityInput);
210 }
211 if (j.contains("pressurenoise"))
212 {
213 j.at("pressurenoise").get_to(_pressurenoise);
214 }
215 if (j.contains("pressureRng"))
216 {
217 j.at("pressureRng").get_to(_pressureRng);
218 }
219 }
220
221 void NAV::BaroSimulator::receiveObs(NAV::InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
222 {
223 auto PosObs = std::static_pointer_cast<const Pos>(queue.extract_front());
224
225 auto pressureObs = std::make_shared<BaroPressObs>();
226
227 pressureObs->insTime = PosObs->insTime;
228
229 pressureObs->baro_pressure = _pressure0 * std::pow(1 - _lapserate * (PosObs->altitude() - _geoidhgt) / _temp0, _exponent) + _pressureRng.getRand_normalDist(0.0, _pressurenoise);
230
231 pressureObs->baro_pressureStdev = _pressurenoise;
232
233 invokeCallbacks(OUTPUT_PORT_INDEX_BAROPRESSURE, pressureObs);
234 }
235