0.5.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
16
19
21
23 : Imu(typeStatic())
24{
25 LOG_TRACE("{}: called", name);
26
27 _onlyRealTime = true;
28 _hasConfig = true;
29 _guiConfigDefaultWindowSize = { 360, 70 };
30
31 // TODO: Update the library to handle different baudrates
33
35}
36
38{
39 LOG_TRACE("{}: called", nameId());
40}
41
43{
44 return "KvhSensor";
45}
46
47std::string NAV::KvhSensor::type() const
48{
49 return typeStatic();
50}
51
53{
54 return "Data Provider";
55}
56
58{
59 if (ImGui::InputTextWithHint("SensorPort", "/dev/ttyUSB0", &_sensorPort))
60 {
61 LOG_DEBUG("{}: SensorPort changed to {}", nameId(), _sensorPort);
64 }
65 ImGui::SameLine();
66 gui::widgets::HelpMarker("COM port where the sensor is attached to\n"
67 "- \"COM1\" (Windows format for physical and virtual (USB) serial port)\n"
68 "- \"/dev/ttyS1\" (Linux format for physical serial port)\n"
69 "- \"/dev/ttyUSB0\" (Linux format for virtual (USB) serial port)\n"
70 "- \"/dev/tty.usbserial-FTXXXXXX\" (Mac OS X format for virtual (USB) serial port)\n"
71 "- \"/dev/ttyS0\" (CYGWIN format. Usually the Windows COM port number minus 1. This would connect to COM1)");
72
74}
75
76[[nodiscard]] json NAV::KvhSensor::save() const
77{
78 LOG_TRACE("{}: called", nameId());
79
80 json j;
81
82 j["UartSensor"] = UartSensor::save();
83 j["Imu"] = Imu::save();
84
85 return j;
86}
87
89{
90 LOG_TRACE("{}: called", nameId());
91
92 if (j.contains("UartSensor"))
93 {
94 UartSensor::restore(j.at("UartSensor"));
95 }
96 if (j.contains("Imu"))
97 {
98 Imu::restore(j.at("Imu"));
99 }
100}
101
103{
104 return true;
105}
106
108{
109 LOG_TRACE("{}: called", nameId());
110
111 // connect to the sensor
112 try
113 {
115
116 LOG_DEBUG("{} connected on port {} with baudrate {}", nameId(), _sensorPort, sensorBaudrate());
117 }
118 catch (...)
119 {
120 LOG_ERROR("{} could not connect", nameId());
121 return false;
122 }
123
124 _sensor->registerAsyncPacketReceivedHandler(this, asciiOrBinaryAsyncMessageReceived);
125
126 return true;
127}
128
130{
131 LOG_TRACE("{}: called", nameId());
132
133 if (!isInitialized())
134 {
135 return;
136 }
137
138 if (_sensor->isConnected())
139 {
140 try
141 {
142 _sensor->unregisterAsyncPacketReceivedHandler();
143 }
144 catch (...) // NOLINT(bugprone-empty-catch)
145 {}
146 _sensor->disconnect();
147 }
148}
149
150void NAV::KvhSensor::asciiOrBinaryAsyncMessageReceived(void* userData, uart::protocol::Packet& p, [[maybe_unused]] size_t index)
151{
152 auto* kvhSensor = static_cast<KvhSensor*>(userData);
153
154 if (p.type() == uart::protocol::Packet::Type::TYPE_BINARY)
155 {
156 auto obs = std::make_shared<KvhObs>(kvhSensor->_imuPos, p);
157
159
160 LOG_DATA("DATA({}): {}, {}, {}",
161 kvhSensor->name, obs->sequenceNumber, obs->temperature.value(), fmt::streamed(obs->status));
162
163 // Check if a packet was skipped
164 if (kvhSensor->_prevSequenceNumber == UINT8_MAX)
165 {
166 kvhSensor->_prevSequenceNumber = obs->sequenceNumber;
167 }
168 if (obs->sequenceNumber != 0 && (obs->sequenceNumber < kvhSensor->_prevSequenceNumber || obs->sequenceNumber > kvhSensor->_prevSequenceNumber + 2))
169 {
170 LOG_WARN("{}: Sequence Number changed from {} to {}", kvhSensor->name, kvhSensor->_prevSequenceNumber, obs->sequenceNumber);
171 }
172 kvhSensor->_prevSequenceNumber = obs->sequenceNumber;
173
174 // Calls all the callbacks
175 if (InsTime currentTime = util::time::GetCurrentInsTime();
176 !currentTime.empty())
177 {
178 obs->insTime = currentTime;
179 }
180 kvhSensor->invokeCallbacks(OUTPUT_PORT_INDEX_KVH_OBS, obs);
181 }
182 else if (p.type() == uart::protocol::Packet::Type::TYPE_ASCII)
183 {
184 LOG_WARN("{}: Received an ASCII Async message: {}", kvhSensor->name, p.datastr());
185 }
186}
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
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:57
static std::string category()
String representation of the Class Category.
Definition KvhSensor.cpp:52
void restore(const json &j) override
Restores the node from a json object.
Definition KvhSensor.cpp:88
KvhSensor()
Default constructor.
Definition KvhSensor.cpp:22
json save() const override
Saves the node into a json object.
Definition KvhSensor.cpp:76
std::string type() const override
String representation of the Class Type.
Definition KvhSensor.cpp:47
vendor::kvh::KvhUartSensor _sensor
Sensor Object.
Definition KvhSensor.hpp:78
static std::string typeStatic()
String representation of the Class Type.
Definition KvhSensor.cpp:42
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:37
bool isInitialized() const
Checks if the node is initialized.
Definition Node.cpp:574
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
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.
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