0.4.1
Loading...
Searching...
No Matches
UartPacketConverter.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
10
11#include <memory>
12
13#include "util/Logger.hpp"
15
17namespace nm = NAV::NodeManager;
19
23
25
30
42
47
49{
50 return "UartPacketConverter";
51}
52
54{
55 return typeStatic();
56}
57
59{
60 return "Converter";
61}
62
64{
65 if (auto outputType = static_cast<int>(_outputType);
66 ImGui::Combo(fmt::format("Output Type##{}", size_t(id)).c_str(), &outputType, "UbloxObs\0EmlidObs\0WiFiObs\0\0"))
67 {
68 _outputType = static_cast<decltype(_outputType)>(outputType);
69 std::string outputTypeString;
70 switch (_outputType)
71 {
73 outputTypeString = "UbloxObs";
74 break;
76 outputTypeString = "EmlidObs";
77 break;
78 default:
79 outputTypeString = "WiFiObs";
80 }
81
82 LOG_DEBUG("{}: Output Type changed to {}", nameId(), outputTypeString);
83
85 {
88 }
90 {
93 }
95 {
98 }
99
100 for (auto& link : outputPins.front().links)
101 {
102 if (auto* connectedPin = link.getConnectedPin())
103 {
104 outputPins.front().recreateLink(*connectedPin);
105 }
106 }
108 }
109
111 {
112 if (ImGui::Checkbox(fmt::format("Show SyncIn Pin##{}", size_t(id)).c_str(), &_syncInPin))
113 {
114 LOG_DEBUG("{}: syncInPin changed to {}", nameId(), _syncInPin);
116 if (_syncInPin && (inputPins.size() <= 1))
117 {
118 nm::CreateInputPin(this, "SyncIn", Pin::Type::Object, { "TimeSync" });
119 }
120 else if (!_syncInPin)
121 {
123 }
124 }
125 // Remove the extra flow::ApplyChanges() statement here
126 }
127 else if (inputPins.size() > 1)
128 {
130 }
131}
132
134{
135 LOG_TRACE("{}: called", nameId());
136
137 json j;
138
139 j["outputType"] = _outputType;
140 j["syncInPin"] = _syncInPin;
141
142 return j;
143}
144
146{
147 LOG_TRACE("{}: called", nameId());
148
149 if (j.contains("outputType"))
150 {
151 j.at("outputType").get_to(_outputType);
152
153 if (!outputPins.empty())
154 {
156 {
159 }
161 {
164 }
166 {
169 }
170 }
171 }
172 if (j.contains("syncInPin") && _outputType == OutputType_WiFiObs)
173 {
174 j.at("syncInPin").get_to(_syncInPin);
175 if (_syncInPin && inputPins.size() <= 1)
176 {
177 nm::CreateInputPin(this, "SyncIn", Pin::Type::Object, { "TimeSync" });
178 }
179 }
180}
181
183{
184 LOG_TRACE("{}: called", nameId());
185
186 _lastSyncInCnt = 0;
187 _lastSyncOutCnt = 0;
188 _syncOutCntCorr = 0;
189 _syncInCntCorr = 0;
190
191 return true;
192}
193
195{
196 auto uartPacket = std::static_pointer_cast<const UartPacket>(queue.extract_front());
197
198 std::shared_ptr<NodeData> convertedData = nullptr;
200 {
201 auto obs = std::make_shared<UbloxObs>();
202 auto packet = uartPacket->raw;
203 if (!vendor::ublox::decryptUbloxObs(obs, packet, nameId())) { return; };
204 convertedData = obs;
205 }
207 {
208 auto obs = std::make_shared<WiFiObs>();
209 auto packet = uartPacket->raw;
210
211 if (!vendor::espressif::decryptWiFiObs(obs, packet, nameId()))
212 {
213 LOG_WARN("{}: Decoding WiFi Obs failed", nameId());
214 return;
215 };
216 if (_syncInPin && inputPins.at(INPUT_PORT_INDEX_SYNC_IN).isPinLinked())
217 {
219 timeSyncMaster && !timeSyncMaster->v->ppsTime.empty())
220 {
221 if (_lastSyncInCnt > obs->timeOutputs.syncInCnt) // reset of the slave
222 {
223 LOG_DEBUG("{}: Slave reset during time sync", nameId());
224 _syncOutCntCorr = 0;
225 _syncInCntCorr = timeSyncMaster->v->syncOutCnt;
226 }
227 else if (_lastSyncOutCnt > timeSyncMaster->v->syncOutCnt) // reset of the master
228 {
229 LOG_DEBUG("{}: Master reset during time sync", nameId());
230 _syncInCntCorr = 0;
231 _syncOutCntCorr = obs->timeOutputs.syncInCnt;
232 }
233 else if (_lastSyncOutCnt == 0 && timeSyncMaster->v->syncOutCnt > 1) // slave counter started later
234 {
235 LOG_DEBUG("{}: Slave counter started later", nameId());
236 _syncInCntCorr = timeSyncMaster->v->syncOutCnt;
237 }
238 else if (_lastSyncOutCnt == 0 && obs->timeOutputs.syncInCnt > 1) // master counter started later
239 {
240 LOG_DEBUG("{}: Master counter started later", nameId());
241 _syncOutCntCorr = obs->timeOutputs.syncInCnt;
242 }
243 _lastSyncOutCnt = timeSyncMaster->v->syncOutCnt;
244 _lastSyncInCnt = obs->timeOutputs.syncInCnt;
245 int64_t syncCntDiff = obs->timeOutputs.syncInCnt + _syncInCntCorr - timeSyncMaster->v->syncOutCnt - _syncOutCntCorr;
246 obs->insTime = timeSyncMaster->v->ppsTime + std::chrono::microseconds(obs->timeOutputs.timeSyncIn)
247 + std::chrono::seconds(syncCntDiff);
248 // LOG_DATA("{}: Syncing time {}, pps {}, syncOutCnt {}, syncInCnt {}, syncCntDiff {}, syncInCntCorr {}, syncOutCntCorr {}",
249 // nameId(), obs->insTime.toGPSweekTow(), timeSyncMaster->ppsTime.toGPSweekTow(),
250 // timeSyncMaster->syncOutCnt, obs->timeOutputs.syncInCnt, syncCntDiff, _syncInCntCorr, _syncOutCntCorr);
251 }
252 else
253 {
254 return;
255 }
256 }
257 convertedData = obs;
258 }
259 else /* if (_outputType == OutputType_EmlidObs) */
260 {
261 auto obs = std::make_shared<EmlidObs>();
262 auto packet = uartPacket->raw;
264 convertedData = obs;
265 }
266
267 if (auto currentTime = util::time::GetCurrentInsTime();
268 !currentTime.empty())
269 {
270 convertedData->insTime = currentTime;
271 }
272
273 if (convertedData)
274 {
276 }
277}
Emlid Observation Class.
Helper Functions to work with Emlid Sensors.
Helper Functions to work with Espressif Sensors.
Save/Load the Nodes.
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
#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.
Decrypts Uart packets.
UART Packet storage class.
ublox Observation Class
Helper Functions to work with Ublox Sensors.
Vector Nav Sensors.
Espressif Observation Class.
static std::string type()
Returns the type of the data class.
Definition EmlidObs.hpp:30
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:399
Node(std::string name)
Constructor.
Definition Node.cpp:30
std::optional< InputPin::IncomingLink::ValueWrapper< T > > getInputValue(size_t portIndex) const
Get Input Value connected on the pin. Only const data types.
Definition Node.hpp:290
std::vector< InputPin > inputPins
List of input pins.
Definition Node.hpp:397
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:180
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
static std::string category()
String representation of the Class Category.
json save() const override
Saves the node into a json object.
bool _syncInPin
Show the SyncIn Pin.
void restore(const json &j) override
Restores the node from a json object.
UartPacketConverter()
Default constructor.
OutputType _outputType
The selected output type in the GUI.
int64_t _syncOutCntCorr
Corrected SyncOut counter in case of a reset (initiator)
bool initialize() override
Initialize the node.
static constexpr size_t OUTPUT_PORT_INDEX_CONVERTED
Flow.
int64_t _lastSyncOutCnt
Last received syncOutCnt.
void guiConfig() override
ImGui config window which is shown on double click.
static std::string typeStatic()
String representation of the Class Type.
~UartPacketConverter() override
Destructor.
@ OutputType_UbloxObs
Extract UbloxObs data.
@ OutputType_EmlidObs
Extract EmlidObs data.
@ OutputType_WiFiObs
Extract WiFiObs data.
static constexpr size_t INPUT_PORT_INDEX_SYNC_IN
Flow (SyncIn)
std::string type() const override
String representation of the Class Type.
void receiveObs(InputPin::NodeDataQueue &queue, size_t pinIdx)
Converts the UartPacket to the selected message type.
int64_t _lastSyncInCnt
Last received syncInCnt.
int64_t _syncInCntCorr
Corrected SyncIn counter in case of a reset (target)
static std::string type()
Returns the type of the data class.
static std::string type()
Returns the type of the data class.
Definition UbloxObs.hpp:30
static std::string type()
Returns the type of the data class.
Definition WiFiObs.hpp:27
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.
bool DeleteInputPin(InputPin &pin)
Deletes the input pin. Invalidates the pin reference given.
InputPin * CreateInputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier={}, InputPin::Callback callback=static_cast< InputPin::FlowFirableCallbackFunc >(nullptr), InputPin::FlowFirableCheckFunc firable=nullptr, int priority=0, int idx=-1)
Create an Input Pin object.
void ApplyChanges()
Signals that there have been changes to the flow.
void decryptEmlidObs(const std::shared_ptr< NAV::EmlidObs > &obs, uart::protocol::Packet &packet)
Decrypts the provided Emlid observation.
bool decryptWiFiObs(const std::shared_ptr< NAV::WiFiObs > &obs, uart::protocol::Packet &packet, const std::string &nameId)
Decrypts the provided Espressif observation.
bool decryptUbloxObs(const std::shared_ptr< NAV::UbloxObs > &obs, uart::protocol::Packet &packet, const std::string &nameId)
Decrypts the provided Ublox observation.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52
@ Object
Generic Object.
Definition Pin.hpp:57