0.4.1
Loading...
Searching...
No Matches
CommonLog.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 "CommonLog.hpp"
10
11#include <imgui.h>
12
15
16#include "util/Logger.hpp"
17
18namespace NAV
19{
20
22{
23 json j;
24 j["useGuiInputs"] = _useGuiInputs;
25 if (_originPosition) { j["originPosition"] = *_originPosition; }
26
27 return j;
28}
29
31{
32 if (j.contains("useGuiInputs")) { j.at("useGuiInputs").get_to(_useGuiInputs); }
33 else { _useGuiInputs = false; }
34 if (j.contains("originPosition")) { _originPosition = j.at("originPosition").get<gui::widgets::PositionWithFrame>(); }
35 else { _originPosition.reset(); }
36}
37
39{
40 std::scoped_lock lk(_mutex);
41 _index = _wantsInit.size(); // NOLINT(cppcoreguidelines-prefer-member-initializer)
42 _wantsInit.push_back(false);
43}
44
46{
47 std::scoped_lock lk(_mutex);
48 _wantsInit.erase(_wantsInit.begin() + static_cast<int64_t>(_index));
49}
50
52{
53 std::scoped_lock lk(_mutex);
54 if (_useGuiInputs) { return; }
55 _wantsInit.at(_index) = true;
56
57 if (std::ranges::all_of(_wantsInit, [](bool val) { return val; }))
58 {
59 LOG_DEBUG("Resetting common log variables.");
60 _startTime.reset();
61 if (!_useGuiInputs)
62 {
63 _originPosition.reset();
64 }
65
66 std::ranges::fill(_wantsInit, false);
67 }
68}
69
70double CommonLog::calcTimeIntoRun(const InsTime& insTime)
71{
72 if (std::scoped_lock lk(_mutex);
73 _startTime.empty())
74 {
75 _startTime = insTime;
76 LOG_DEBUG("Common log setting start time to {} ({}) GPST.", _startTime.toYMDHMS(GPST), _startTime.toGPSweekTow(GPST));
77 }
78 return static_cast<double>((insTime - _startTime).count());
79}
80
81CommonLog::LocalPosition CommonLog::calcLocalPosition(const Eigen::Vector3d& lla_position)
82{
83 {
84 std::scoped_lock lk(_mutex);
85 if (!_originPosition.has_value())
86 {
87 if (lla_position.hasNaN())
88 {
89 LOG_WARN("Not setting common log origin to {}, {}, {} [deg, deg, m]",
90 rad2deg(lla_position.x()), rad2deg(lla_position.y()), lla_position.z());
91 }
92 else
93 {
95 _originPosition->e_position = trafo::lla2ecef_WGS84(lla_position);
96 LOG_DEBUG("Common log setting position of origin to {}, {}, {} [deg, deg, m]",
97 rad2deg(lla_position.x()), rad2deg(lla_position.y()), lla_position.z());
98 }
99 }
100 }
101
102 // North/South deviation [m]
103 double northSouth = calcGeographicalDistance(lla_position.x(), lla_position.y(),
104 _originPosition->latitude(), lla_position.y())
105 * (lla_position.x() > _originPosition->latitude() ? 1 : -1);
106
107 // East/West deviation [m]
108 double eastWest = calcGeographicalDistance(lla_position.x(), lla_position.y(),
109 lla_position.x(), _originPosition->longitude())
110 * (lla_position.y() > _originPosition->longitude() ? 1 : -1);
111
112 return { .northSouth = northSouth, .eastWest = eastWest };
113}
114
115bool CommonLog::ShowOriginInput(const char* id)
116{
117 bool changed = false;
118 if (ImGui::Checkbox(fmt::format("Override position origin (for all common logging)##{}", id).c_str(), &_useGuiInputs))
119 {
120 LOG_DEBUG("{}: useGuiInputs changed to {}", id, _useGuiInputs);
121 changed = true;
122 }
123 if (_useGuiInputs)
124 {
126 ImGui::Indent();
127 std::scoped_lock lk(_mutex);
128 if (gui::widgets::PositionInput(fmt::format("Origin##{}", id).c_str(), _originPosition.value(), gui::widgets::PositionInputLayout::SINGLE_ROW))
129 {
130 changed = true;
131 }
132 ImGui::Unindent();
133 }
134
135 return changed;
136}
137
138} // namespace NAV
Common logging variables like time into run and local positions.
Functions concerning the ellipsoid model.
nlohmann::json json
json namespace
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_WARN
Error occurred, but a fallback option exists and program continues to work normally.
Definition Logger.hpp:71
static bool ShowOriginInput(const char *id)
Shows a GUI to input a origin location.
static void restore(const json &j)
Read info from a json object.
Definition CommonLog.cpp:30
static std::mutex _mutex
Mutex to lock before writing.
Definition CommonLog.hpp:88
CommonLog()
Default constructor.
Definition CommonLog.cpp:38
size_t _index
Index which common log node this is.
Definition CommonLog.hpp:92
void initialize() const
Initialize the common log variables.
Definition CommonLog.cpp:51
static json save()
Returns a json object of the common log.
Definition CommonLog.cpp:21
static std::optional< gui::widgets::PositionWithFrame > _originPosition
Origin for calculation of relative position.
Definition CommonLog.hpp:82
static double calcTimeIntoRun(const InsTime &insTime)
Calculates the relative time into he run.
Definition CommonLog.cpp:70
static bool _useGuiInputs
Use GUI inputs.
Definition CommonLog.hpp:84
static LocalPosition calcLocalPosition(const Eigen::Vector3d &lla_position)
Calculate the local position offset from a reference point.
Definition CommonLog.cpp:81
static InsTime _startTime
Start Time for calculation of relative time.
Definition CommonLog.hpp:80
static std::vector< bool > _wantsInit
Vector on which nodes want to initialize.
Definition CommonLog.hpp:90
virtual ~CommonLog()
Destructor.
Definition CommonLog.cpp:45
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
bool PositionInput(const char *str, PositionWithFrame &position, PositionInputLayout layout=PositionInputLayout::SINGLE_COLUMN, float itemWidth=140.0F)
Inputs to edit an Position object.
@ SINGLE_ROW
All elements in a single row.
Eigen::Vector3< typename Derived::Scalar > lla2ecef_WGS84(const Eigen::MatrixBase< Derived > &lla_position)
Converts latitude, longitude and altitude into Earth-centered-Earth-fixed coordinates using WGS84.
@ GPST
GPS Time.
Scalar calcGeographicalDistance(Scalar lat1, Scalar lon1, Scalar lat2, Scalar lon2)
Measure the distance between two points over an ellipsoidal-surface.
constexpr auto rad2deg(const T &rad)
Convert Radians to Degree.
Definition Units.hpp:39
Local position offset from a reference point.
Definition CommonLog.hpp:54
Position with Reference frame, used for GUI input.