INSTINCT Code Coverage Report


Directory: src/
File: Nodes/DataProcessor/Barometer/PressToHgt.cpp
Date: 2025-11-25 23:34:18
Exec Total Coverage
Lines: 13 79 16.5%
Functions: 4 11 36.4%
Branches: 13 116 11.2%

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 "PressToHgt.hpp"
10
11 #include "util/Logger.hpp"
12
13 #include "internal/FlowManager.hpp"
14
15 #include "NodeData/Baro/BaroPressObs.hpp"
16 #include "NodeData/Baro/BaroHgt.hpp"
17
18 #include "Navigation/Gravity/Gravity.hpp"
19
20 #include "internal/gui/widgets/imgui_ex.hpp"
21 #include "internal/gui/widgets/EnumCombo.hpp"
22 #include "internal/gui/widgets/HelpMarker.hpp"
23 #include "internal/gui/NodeEditorApplication.hpp"
24
25 114 NAV::PressToHgt::PressToHgt()
26
3/6
✓ 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.
114 : Node(typeStatic())
27 {
28 LOG_TRACE("{}: called", name);
29 114 _hasConfig = true;
30 114 _guiConfigDefaultWindowSize = { 710, 220 };
31
32
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("BaroPressObs", Pin::Type::Flow, { NAV::BaroPressObs::type() }, &PressToHgt::receiveObs);
33
34
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("BaroHgt", Pin::Type::Flow, { NAV::BaroHgt::type() });
35 342 }
36
37 228 NAV::PressToHgt::~PressToHgt()
38 {
39 LOG_TRACE("{}: called", nameId());
40 228 }
41
42 228 std::string NAV::PressToHgt::typeStatic()
43 {
44
1/2
✓ Branch 1 taken 228 times.
✗ Branch 2 not taken.
456 return "PressToHgt";
45 }
46
47 std::string NAV::PressToHgt::type() const
48 {
49 return typeStatic();
50 }
51
52 114 std::string NAV::PressToHgt::category()
53 {
54
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
228 return "Data Processor";
55 }
56
57 void NAV::PressToHgt::guiConfig()
58 {
59 float columnWidth = 140.0F * gui::NodeEditorApplication::windowFontRatio();
60
61 if (ImGui::InputDouble(fmt::format("Sea-level pressure##{}", size_t(id)).c_str(), &_pressure0, 0.0, 0.0, "%.3f hPa"))
62 {
63 LOG_DATA("{}: pressure0 changed to {}", nameId(), _pressure0);
64 flow::ApplyChanges();
65 }
66 if (ImGui::InputDouble(fmt::format("Sea-level temperature##{}", size_t(id)).c_str(), &_temp0, 0.0, 0.0, "%.3f K"))
67 {
68 LOG_DATA("{}: temp0 changed to {}", nameId(), _temp0);
69 flow::ApplyChanges();
70 }
71 if (ImGui::InputDouble(fmt::format("Temperature lapse rate##{}", size_t(id)).c_str(), &_lapserate, 0.0, 0.0, "%.6f K / m"))
72 {
73 LOG_DATA("{}: lapserate changed to {}", nameId(), _lapserate);
74 flow::ApplyChanges();
75 }
76 if (ImGui::InputDouble(fmt::format("Geoid undulation##{}", size_t(id)).c_str(), &_geoidhgt, 0.0, 0.0, "%.3f m"))
77 {
78 LOG_DATA("{}: geoidhgt changed to {}", nameId(), _geoidhgt);
79 flow::ApplyChanges();
80 }
81 ImGui::SetNextItemWidth(columnWidth);
82 if (gui::widgets::EnumCombo(fmt::format("Gravity input##{}", size_t(id)).c_str(), _gravityInput))
83 {
84 LOG_DATA("{}: gravity input changed to {}", nameId(), fmt::underlying(_gravityInput));
85 flow::ApplyChanges();
86 }
87 ImGui::SameLine();
88 gui::widgets::HelpMarker("The 'position' input calculates the gravity magnitude from EGM96.\n\n");
89 if (_gravityInput == GravityInput::Manual)
90 {
91 if (ImGui::InputDouble(fmt::format("Gravity##{}", size_t(id)).c_str(), &_gravity, 0.0, 0.0, "%.5f m / s²"))
92 {
93 LOG_DATA("{}: gravity changed to {}", nameId(), _gravity);
94 flow::ApplyChanges();
95 }
96 }
97 else // (_gravityInput == GravityInput::Position)
98 {
99 if (ImGui::InputDouble3(fmt::format("Position LLA [deg, deg, m]##{}", size_t(id)).c_str(), _initPos.data()))
100 {
101 flow::ApplyChanges();
102 }
103 }
104 }
105
106 bool NAV::PressToHgt::initialize()
107 {
108 LOG_TRACE("{}: called", nameId());
109 if (_gravityInput == GravityInput::Position)
110 {
111 _gravity = n_calcGravitation_EGM96(_initPos).norm();
112 LOG_DATA("{}: PressToHgt - Local gravity magnitude: {} m/s² at initial position: {} [deg, deg, m]", nameId(), _gravity, _initPos.transpose());
113 }
114 _exponent = _gravity * InsConst::dMtr / InsConst::Rg / _lapserate;
115 return true;
116 }
117
118 void NAV::PressToHgt::deinitialize()
119 {
120 LOG_TRACE("{}: called", nameId());
121 }
122
123 [[nodiscard]] json NAV::PressToHgt::save() const
124 {
125 LOG_TRACE("{}: called", nameId());
126
127 json j;
128
129 j["temp0"] = _temp0;
130 j["pressure0"] = _pressure0;
131 j["lapserate"] = _lapserate;
132 j["geoidhgt"] = _geoidhgt;
133 j["gravity"] = _gravity;
134 j["initPos"] = _initPos;
135 j["gravityInput"] = _gravityInput;
136 return j;
137 }
138
139 void NAV::PressToHgt::restore(json const& j)
140 {
141 LOG_TRACE("{}: called", nameId());
142
143 if (j.contains("temp0"))
144 {
145 j.at("temp0").get_to(_temp0);
146 }
147 if (j.contains("pressure0"))
148 {
149 j.at("pressure0").get_to(_pressure0);
150 }
151 if (j.contains("lapserate"))
152 {
153 j.at("lapserate").get_to(_lapserate);
154 }
155 if (j.contains("geoidhgt"))
156 {
157 j.at("geoidhgt").get_to(_geoidhgt);
158 }
159 if (j.contains("gravity"))
160 {
161 j.at("gravity").get_to(_gravity);
162 }
163 if (j.contains("initPos"))
164 {
165 j.at("initPos").get_to(_initPos);
166 }
167 if (j.contains("gravityInput"))
168 {
169 j.at("gravityInput").get_to(_gravityInput);
170 }
171 }
172
173 void NAV::PressToHgt::receiveObs(NAV::InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
174 {
175 auto pressureObs = std::static_pointer_cast<const BaroPressObs>(queue.extract_front());
176
177 auto baroheight = std::make_shared<BaroHgt>();
178
179 baroheight->insTime = pressureObs->insTime;
180
181 baroheight->baro_height = _temp0 / _lapserate * (1.0 - std::pow(pressureObs->baro_pressure / _pressure0, 1.0 / _exponent)) + _geoidhgt;
182
183 // if uncertainty of pressure value is provided carry out error propagation for barometric height (assuming all other parameters are error free)
184 if (pressureObs->baro_pressureStdev.has_value())
185 {
186 baroheight->baro_heightStdev = std::fabs(_temp0 / (_lapserate * _exponent * pressureObs->baro_pressure) * std::pow(pressureObs->baro_pressure / _pressure0, 1.0 / _exponent - 1.0)) * pressureObs->baro_pressureStdev.value();
187 }
188
189 invokeCallbacks(OUTPUT_PORT_INDEX_BAROHEIGHT, baroheight);
190 }
191