0.5.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
20
22
24
26 : Imu(typeStatic())
27{
28 LOG_TRACE("{}: called", name);
29
30 _onlyRealTime = true;
31 _hasConfig = true;
32 _guiConfigDefaultWindowSize = { 295, 92 };
33
35}
36
38{
39 LOG_TRACE("{}: called", nameId());
40}
41
43{
44 return "Navio2Sensor";
45}
46
47std::string NAV::Navio2Sensor::type() const
48{
49 return typeStatic();
50}
51
53{
54 return "Data Provider";
55}
56
58{
59 if (auto imuType = static_cast<int>(_imuType);
60 ImGui::Combo("IMU", &imuType, "MPU9250\0LSM9DS1\0\0"))
61 {
62 _imuType = static_cast<decltype(_imuType)>(imuType);
63 LOG_DEBUG("{}: IMU changed to {}", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
66 }
67
68 if (ImGui::SliderInt("Frequency", &_outputFrequency, 1, 200, "%d Hz"))
69 {
70 LOG_DEBUG("{}: Frequency changed to {}", nameId(), _outputFrequency);
73 }
74
76}
77
78[[nodiscard]] json NAV::Navio2Sensor::save() const
79{
80 LOG_TRACE("{}: called", nameId());
81
82 json j;
83
84 j["Frequency"] = _outputFrequency;
85 j["Imu"] = Imu::save();
86
87 return j;
88}
89
91{
92 LOG_TRACE("{}: called", nameId());
93
94 if (j.contains("Frequency"))
95 {
96 j.at("Frequency").get_to(_outputFrequency);
97 }
98 if (j.contains("Imu"))
99 {
100 Imu::restore(j.at("Imu"));
101 }
102}
103
105{
106 return true;
107}
108
110{
111 LOG_TRACE("{} ({}): called", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
112
113#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
114 if (_imuType == ImuType::MPU)
115 {
116 _sensor = std::make_unique<MPU9250>();
117 }
118 else // ImuType::LSM
119 {
120 _sensor = std::make_unique<LSM9DS1>();
121 }
122
123 if (!_sensor->probe())
124 {
125 LOG_ERROR("{} ({}): Sensor not enabled", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
126 return false;
127 }
128 _sensor->initialize();
129#else
130 LOG_ERROR("{} ({}): MacOS is not supported by the Navio2 Node", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
131 return false;
132#endif
133
134 int outputInterval = static_cast<int>(1.0 / static_cast<double>(_outputFrequency) * 1000.0);
135 _startTime = std::chrono::steady_clock::now();
136 _timer.start(outputInterval, readImuThread, this);
137
138 return true;
139}
140
142{
143 LOG_TRACE("{} ({}): called", nameId(), _imuType ? "LSM9DS1" : "MPU9250");
144
145 if (_timer.is_running())
146 {
147 _timer.stop();
148 }
149
150#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
151 _sensor.reset();
152#endif
153}
154
155// void NAV::Navio2Sensor::readImuThread()
157{
158 auto* navio = static_cast<Navio2Sensor*>(userData);
159 auto obs = std::make_shared<ImuObs>(navio->_imuPos);
160
161#if !__APPLE__ && !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
162 navio->_sensor->update();
163
164 navio->_sensor->read_accelerometer(&navio->_ax, &navio->_ay, &navio->_az);
165 navio->_sensor->read_gyroscope(&navio->_gx, &navio->_gy, &navio->_gz);
166 navio->_sensor->read_magnetometer(&navio->_mx, &navio->_my, &navio->_mz);
167
168 obs->temperature = navio->_sensor->read_temperature();
169#endif
170
171 obs->p_acceleration = { navio->_ax, navio->_ay, navio->_az };
172 obs->p_angularRate = { navio->_gx, navio->_gy, navio->_gz };
173
174 if (navio->_imuType == ImuType::LSM)
175 {
176 obs->p_magneticField.emplace(navio->_mx, navio->_my, navio->_mz);
177 // constexpr double uT2Gauss = 1.0 / 100.0;
178 // obs->p_magneticField.value() *= uT2Gauss;
179 }
180
181 LOG_DATA("DATA({}): {}°C, a=({}, {}, {})", navio->name, obs->temperature.value(),
182 navio->_ax, navio->_ay, navio->_az);
183
184 if (InsTime currentTime = util::time::GetCurrentInsTime();
185 !currentTime.empty())
186 {
187 obs->insTime = currentTime;
188 }
189 navio->invokeCallbacks(OUTPUT_PORT_INDEX_IMU_OBS, obs);
190}
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.
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:465
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:522
OutputPin * CreateOutputPin(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.
Definition Node.cpp:278
std::string nameId() const
Node name and id.
Definition Node.cpp:323
std::string name
Name of the Node.
Definition Node.hpp:507
bool _onlyRealTime
Whether the node can run in post-processing or only real-time.
Definition Node.hpp:531
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:525
void ApplyChanges()
Signals that there have been changes to the flow.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52