0.4.1
Loading...
Searching...
No Matches
Navio2Sensor.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 "Navio2Sensor.hpp"
10
11#include "util/Logger.hpp"
12
13#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
14 #include "Navio/Common/MPU9250.h"
15 #include "Navio/Navio2/LSM9DS1.h"
16 #include "Navio/Common/Util.h"
17#endif
18
20namespace nm = NAV::NodeManager;
22
24
26
28 : Imu(typeStatic())
29{
30 LOG_TRACE("{}: called", name);
31
32 _onlyRealTime = true;
33 _hasConfig = true;
34 _guiConfigDefaultWindowSize = { 295, 92 };
35
37}
38
40{
41 LOG_TRACE("{}: called", nameId());
42}
43
45{
46 return "Navio2Sensor";
47}
48
49std::string NAV::Navio2Sensor::type() const
50{
51 return typeStatic();
52}
53
55{
56 return "Data Provider";
57}
58
60{
61 if (auto imuType = static_cast<int>(_imuType);
62 ImGui::Combo("IMU", &imuType, "MPU9250\0LSM9DS1\0\0"))
63 {
64 _imuType = static_cast<decltype(_imuType)>(imuType);
65 LOG_DEBUG("{}: IMU changed to {}", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
68 }
69
70 if (ImGui::SliderInt("Frequency", &_outputFrequency, 1, 200, "%d Hz"))
71 {
72 LOG_DEBUG("{}: Frequency changed to {}", nameId(), _outputFrequency);
75 }
76
78}
79
80[[nodiscard]] json NAV::Navio2Sensor::save() const
81{
82 LOG_TRACE("{}: called", nameId());
83
84 json j;
85
86 j["Frequency"] = _outputFrequency;
87 j["Imu"] = Imu::save();
88
89 return j;
90}
91
93{
94 LOG_TRACE("{}: called", nameId());
95
96 if (j.contains("Frequency"))
97 {
98 j.at("Frequency").get_to(_outputFrequency);
99 }
100 if (j.contains("Imu"))
101 {
102 Imu::restore(j.at("Imu"));
103 }
104}
105
107{
108 return true;
109}
110
112{
113 LOG_TRACE("{} ({}): called", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
114
115#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
116 if (_imuType == ImuType::MPU)
117 {
118 _sensor = std::make_unique<MPU9250>();
119 }
120 else // ImuType::LSM
121 {
122 _sensor = std::make_unique<LSM9DS1>();
123 }
124
125 if (!_sensor->probe())
126 {
127 LOG_ERROR("{} ({}): Sensor not enabled", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
128 return false;
129 }
130 _sensor->initialize();
131#else
132 LOG_ERROR("{} ({}): MacOS is not supported by the Navio2 Node", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
133 return false;
134#endif
135
136 int outputInterval = static_cast<int>(1.0 / static_cast<double>(_outputFrequency) * 1000.0);
137 _startTime = std::chrono::steady_clock::now();
138 _timer.start(outputInterval, readImuThread, this);
139
140 return true;
141}
142
144{
145 LOG_TRACE("{} ({}): called", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
146
147 if (_timer.is_running())
148 {
149 _timer.stop();
150 }
151
152#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
153 _sensor.reset();
154#endif
155}
156
157// void NAV::Navio2Sensor::readImuThread()
159{
160 auto* navio = static_cast<Navio2Sensor*>(userData);
161 auto obs = std::make_shared<ImuObs>(navio->_imuPos);
162
163#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
164 navio->_sensor->update();
165
166 navio->_sensor->read_accelerometer(&navio->_ax, &navio->_ay, &navio->_az);
167 navio->_sensor->read_gyroscope(&navio->_gx, &navio->_gy, &navio->_gz);
168 navio->_sensor->read_magnetometer(&navio->_mx, &navio->_my, &navio->_mz);
169
170 obs->temperature = navio->_sensor->read_temperature();
171#endif
172
173 obs->p_acceleration = { navio->_ax, navio->_ay, navio->_az };
174 obs->p_angularRate = { navio->_gx, navio->_gy, navio->_gz };
175
176 if (navio->_imuType == ImuType::LSM)
177 {
178 obs->p_magneticField.emplace(navio->_mx, navio->_my, navio->_mz);
179 // constexpr double uT2Gauss = 1.0 / 100.0;
180 // obs->p_magneticField.value() *= uT2Gauss;
181 }
182
183 LOG_DATA("DATA({}): {}°C, a=({}, {}, {})", navio->name, obs->temperature.value(),
184 navio->_ax, navio->_ay, navio->_az);
185
186 if (InsTime currentTime = util::time::GetCurrentInsTime();
187 !currentTime.empty())
188 {
189 obs->insTime = currentTime;
190 }
191 navio->invokeCallbacks(OUTPUT_PORT_INDEX_IMU_OBS, obs);
192}
Save/Load the Nodes.
nlohmann::json json
json namespace
Parent Class for all IMU Observations.
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
Navio2 Sensors.
Manages all Nodes.
Keeps track of the current real/simulation time.
static std::string type()
Returns the type of the data class.
Definition ImuObs.hpp:33
json save() const override
Saves the node into a json object.
Definition Imu.cpp:93
void restore(const json &j) override
Restores the node from a json object.
Definition Imu.cpp:104
void guiConfig() override
ImGui config window which is shown on double click.
Definition Imu.cpp:55
Imu(const Imu &)=delete
Copy constructor.
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
constexpr bool empty() const
Checks if the Time object has a value.
Definition InsTime.hpp:1089
CallbackTimer _timer
Timer object to handle async data requests.
bool initialize() override
Initialize the node.
static void readImuThread(void *userData)
Function which performs the async data reading.
std::string type() const override
String representation of the Class Type.
static std::string typeStatic()
String representation of the Class Type.
Navio2Sensor()
Default constructor.
void guiConfig() override
ImGui config window which is shown on double click.
bool resetNode() override
Resets the node. It is guaranteed that the node is initialized when this is called.
json save() const override
Saves the node into a json object.
~Navio2Sensor() override
Destructor.
static constexpr size_t OUTPUT_PORT_INDEX_IMU_OBS
Flow (ImuObs)
void restore(const json &j) override
Restores the node from a json object.
int _outputFrequency
OutputFrequency to calculate rateDivisor field.
std::chrono::time_point< std::chrono::steady_clock > _startTime
Start Time to calculate the TimeSinceStartup.
static std::string category()
String representation of the Class Category.
ImuType _imuType
The Imu type.
void deinitialize() override
Deinitialize the node.
std::unique_ptr< InertialSensor > _sensor
Sensor object.
bool doDeinitialize(bool wait=false)
Asks the node worker to deinitialize the node.
Definition Node.cpp:395
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
bool _onlyRealTime
Whether the node can run in post-processing or only real-time.
Definition Node.hpp:419
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
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.
void ApplyChanges()
Signals that there have been changes to the flow.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52