0.4.1
Loading...
Searching...
No Matches
KvhSensor.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 "KvhSensor.hpp"
10
11#include "util/Logger.hpp"
12
14
16namespace nm = NAV::NodeManager;
18
21
23
25 : Imu(typeStatic())
26{
27 LOG_TRACE("{}: called", name);
28
29 _onlyRealTime = true;
30 _hasConfig = true;
31 _guiConfigDefaultWindowSize = { 360, 70 };
32
33 // TODO: Update the library to handle different baudrates
35
37}
38
40{
41 LOG_TRACE("{}: called", nameId());
42}
43
45{
46 return "KvhSensor";
47}
48
49std::string NAV::KvhSensor::type() const
50{
51 return typeStatic();
52}
53
55{
56 return "Data Provider";
57}
58
60{
61 if (ImGui::InputTextWithHint("SensorPort", "/dev/ttyUSB0", &_sensorPort))
62 {
63 LOG_DEBUG("{}: SensorPort changed to {}", nameId(), _sensorPort);
66 }
67 ImGui::SameLine();
68 gui::widgets::HelpMarker("COM port where the sensor is attached to\n"
69 "- \"COM1\" (Windows format for physical and virtual (USB) serial port)\n"
70 "- \"/dev/ttyS1\" (Linux format for physical serial port)\n"
71 "- \"/dev/ttyUSB0\" (Linux format for virtual (USB) serial port)\n"
72 "- \"/dev/tty.usbserial-FTXXXXXX\" (Mac OS X format for virtual (USB) serial port)\n"
73 "- \"/dev/ttyS0\" (CYGWIN format. Usually the Windows COM port number minus 1. This would connect to COM1)");
74
76}
77
78[[nodiscard]] json NAV::KvhSensor::save() const
79{
80 LOG_TRACE("{}: called", nameId());
81
82 json j;
83
84 j["UartSensor"] = UartSensor::save();
85 j["Imu"] = Imu::save();
86
87 return j;
88}
89
91{
92 LOG_TRACE("{}: called", nameId());
93
94 if (j.contains("UartSensor"))
95 {
96 UartSensor::restore(j.at("UartSensor"));
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());
112
113 // connect to the sensor
114 try
115 {
117
118 LOG_DEBUG("{} connected on port {} with baudrate {}", nameId(), _sensorPort, sensorBaudrate());
119 }
120 catch (...)
121 {
122 LOG_ERROR("{} could not connect", nameId());
123 return false;
124 }
125
126 _sensor->registerAsyncPacketReceivedHandler(this, asciiOrBinaryAsyncMessageReceived);
127
128 return true;
129}
130
132{
133 LOG_TRACE("{}: called", nameId());
134
135 if (!isInitialized())
136 {
137 return;
138 }
139
140 if (_sensor->isConnected())
141 {
142 try
143 {
144 _sensor->unregisterAsyncPacketReceivedHandler();
145 }
146 catch (...) // NOLINT(bugprone-empty-catch)
147 {}
148 _sensor->disconnect();
149 }
150}
151
152void NAV::KvhSensor::asciiOrBinaryAsyncMessageReceived(void* userData, uart::protocol::Packet& p, [[maybe_unused]] size_t index)
153{
154 auto* kvhSensor = static_cast<KvhSensor*>(userData);
155
156 if (p.type() == uart::protocol::Packet::Type::TYPE_BINARY)
157 {
158 auto obs = std::make_shared<KvhObs>(kvhSensor->_imuPos, p);
159
161
162 LOG_DATA("DATA({}): {}, {}, {}",
163 kvhSensor->name, obs->sequenceNumber, obs->temperature.value(), fmt::streamed(obs->status));
164
165 // Check if a packet was skipped
166 if (kvhSensor->_prevSequenceNumber == UINT8_MAX)
167 {
168 kvhSensor->_prevSequenceNumber = obs->sequenceNumber;
169 }
170 if (obs->sequenceNumber != 0 && (obs->sequenceNumber < kvhSensor->_prevSequenceNumber || obs->sequenceNumber > kvhSensor->_prevSequenceNumber + 2))
171 {
172 LOG_WARN("{}: Sequence Number changed from {} to {}", kvhSensor->name, kvhSensor->_prevSequenceNumber, obs->sequenceNumber);
173 }
174 kvhSensor->_prevSequenceNumber = obs->sequenceNumber;
175
176 // Calls all the callbacks
177 if (InsTime currentTime = util::time::GetCurrentInsTime();
178 !currentTime.empty())
179 {
180 obs->insTime = currentTime;
181 }
182 kvhSensor->invokeCallbacks(OUTPUT_PORT_INDEX_KVH_OBS, obs);
183 }
184 else if (p.type() == uart::protocol::Packet::Type::TYPE_ASCII)
185 {
186 LOG_WARN("{}: Received an ASCII Async message: {}", kvhSensor->name, p.datastr());
187 }
188}
Save/Load the Nodes.
nlohmann::json json
json namespace
Text Help Marker (?) with Tooltip.
Data storage class for one KVH Imu observation.
KVH Sensors.
Helper Functions to work with Kvh Sensors.
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_WARN
Error occurred, but a fallback option exists and program continues to work normally.
Definition Logger.hpp:71
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Manages all Nodes.
Keeps track of the current real/simulation time.
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
static std::string type()
Returns the type of the data class.
Definition KvhObs.hpp:39
bool initialize() override
Initialize the node.
void deinitialize() override
Deinitialize the node.
void guiConfig() override
ImGui config window which is shown on double click.
Definition KvhSensor.cpp:59
static std::string category()
String representation of the Class Category.
Definition KvhSensor.cpp:54
void restore(const json &j) override
Restores the node from a json object.
Definition KvhSensor.cpp:90
KvhSensor()
Default constructor.
Definition KvhSensor.cpp:24
json save() const override
Saves the node into a json object.
Definition KvhSensor.cpp:78
std::string type() const override
String representation of the Class Type.
Definition KvhSensor.cpp:49
vendor::kvh::KvhUartSensor _sensor
Sensor Object.
Definition KvhSensor.hpp:78
static std::string typeStatic()
String representation of the Class Type.
Definition KvhSensor.cpp:44
static constexpr size_t OUTPUT_PORT_INDEX_KVH_OBS
Flow (KvhObs)
Definition KvhSensor.hpp:63
bool resetNode() override
Resets the node. It is guaranteed that the node is initialized when this is called.
static void asciiOrBinaryAsyncMessageReceived(void *userData, uart::protocol::Packet &p, size_t index)
Callback handler for notifications of new asynchronous data packets received.
~KvhSensor() override
Destructor.
Definition KvhSensor.cpp:39
bool isInitialized() const
Checks if the node is initialized.
Definition Node.cpp:504
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
static int baudrate2Selection(Baudrate baud)
Returns the guiSelection for the given baudrate.
int _selectedBaudrate
Baudrate for the sensor.
@ BAUDRATE_921600
Baudrate with 921600 symbols per second [Baud].
std::string _sensorPort
void restore(const json &j)
Restores the node from a json object.
Baudrate sensorBaudrate() const
Returns the Baudrate for the element Selected by the GUI.
json save() const
Saves the node into a json object.
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.
void HelpMarker(const char *desc, const char *symbol="(?)")
Text Help Marker, e.g. '(?)', with Tooltip.
void decryptKvhObs(const std::shared_ptr< NAV::KvhObs > &obs)
Decrypts the provided Kvh observation.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52