INSTINCT Code Coverage Report


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