0.4.1
Loading...
Searching...
No Matches
udpRecv.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 "udpRecv.hpp"
10
12namespace nm = NAV::NodeManager;
14
18
20
21#include "util/Logger.hpp"
22
25{
26 LOG_TRACE("{}: called", name);
27
28 _onlyRealTime = true;
29 _hasConfig = true;
30 _guiConfigDefaultWindowSize = { 202, 66 };
31
33}
34
36{
37 LOG_TRACE("{}: called", nameId());
38}
39
41{
42 return "UdpRecv";
43}
44
45std::string NAV::UdpRecv::type() const
46{
47 return typeStatic();
48}
49
51{
52 return "Data Link";
53}
54
56{
57 ImGui::SetNextItemWidth(150 * gui::NodeEditorApplication::windowFontRatio());
58 if (ImGui::InputIntL(fmt::format("Port##{}", size_t(id)).c_str(), &_port, PORT_LIMITS[0], PORT_LIMITS[1]))
59 {
61 }
62}
63
65{
66 return true;
67}
68
70{
71 LOG_TRACE("{}: called", nameId());
72
73 json j;
74 j["port"] = _port;
75
76 return j;
77}
78
80{
81 LOG_TRACE("{}: called", nameId());
82 if (j.contains("port"))
83 {
84 j.at("port").get_to(_port);
85 }
86}
87
89{
90 LOG_TRACE("{}: called", nameId());
91
92 try
93 {
94 _socket = boost::asio::ip::udp::socket(_io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), static_cast<uint16_t>(_port)));
95 }
96 catch (const std::exception& /* e */)
97 {
98 LOG_ERROR("{}: Port {} is already in use. Choose a different port for this instance.", nameId(), _port);
99 return false;
100 }
101
102 _running = true;
103
104 asyncReceive();
105
106 if (_isStartup)
107 {
108 _recvThread = std::thread([this]() {
109 _io_context.run();
110 });
111 }
112 else
113 {
114 _recvThread = std::thread([this]() {
115 _io_context.restart();
116 _io_context.run();
117 });
118 }
119
120 _isStartup = false;
121
122 return true;
123}
124
126{
127 _running = false;
128 _io_context.stop();
129 _recvThread.join();
130 _socket.close();
131
132 LOG_TRACE("{}: called", nameId());
133}
134
136{
137 _socket.async_receive_from(
138 boost::asio::buffer(_data, _maxBytes), _sender_endpoint,
139 [this](boost::system::error_code errorRcvd, std::size_t bytesRcvd) {
140 if ((!errorRcvd) && (bytesRcvd > 0))
141 {
142 auto obs = std::make_shared<PosVelAtt>();
143
144 // Position in LLA coordinates
145 Eigen::Vector3d posLLA{ _data.at(0), _data.at(1), _data.at(2) };
146
147 // Velocity in local frame
148 Eigen::Vector3d vel_n{ _data.at(3), _data.at(4), _data.at(5) };
149
150 // Attitude
151 Eigen::Quaterniond n_Quat_b{};
152 n_Quat_b.x() = _data.at(6);
153 n_Quat_b.y() = _data.at(7);
154 n_Quat_b.z() = _data.at(8);
155 n_Quat_b.w() = _data.at(9);
156
157 // Time
158 auto gpsC = static_cast<int32_t>(_data.at(10));
159 auto gpsW = static_cast<int32_t>(_data.at(11));
160 auto gpsT = static_cast<long double>(_data.at(12));
161
162 obs->setPosVelAtt_n(posLLA, vel_n, n_Quat_b);
163 obs->insTime = InsTime(gpsC, gpsW, gpsT);
164
166 }
167 else
168 {
169 LOG_ERROR("Error receiving the UDP network stream.");
170 }
171
172 if (_running)
173 {
174 asyncReceive();
175 }
176 });
177}
Save/Load the Nodes.
nlohmann::json json
json namespace
Text Help Marker (?) with Tooltip.
Utility class for logging to console and file.
#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
Manages all Nodes.
Keeps track of the current real/simulation time.
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
Node(std::string name)
Constructor.
Definition Node.cpp:30
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
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
static std::string type()
Returns the type of the data class.
Definition PosVelAtt.hpp:29
static std::string category()
String representation of the Class Category.
Definition udpRecv.cpp:50
UdpRecv()
Default constructor.
Definition udpRecv.cpp:23
void asyncReceive()
Polls the next data.
Definition udpRecv.cpp:135
void guiConfig() override
ImGui config window which is shown on double click.
Definition udpRecv.cpp:55
bool initialize() override
Initialize the node.
Definition udpRecv.cpp:88
static constexpr std::array< int, 2 > PORT_LIMITS
Range a port can be in [0, 2^16-1].
Definition udpRecv.hpp:88
~UdpRecv() override
Destructor.
Definition udpRecv.cpp:35
boost::asio::io_context _io_context
Asynchronous receive fct.
Definition udpRecv.hpp:91
bool _isStartup
Startup handler: used in 'initialize()' to differentiate between startup and re-initialization.
Definition udpRecv.hpp:103
bool resetNode() override
Resets the node. Moves the read cursor to the start.
Definition udpRecv.cpp:64
std::array< double, 13 > _data
Network data stream array.
Definition udpRecv.hpp:112
std::thread _recvThread
Receiver thread.
Definition udpRecv.hpp:98
bool _running
Flag that indicates the running data link.
Definition udpRecv.hpp:101
void restore(const json &j) override
Restores the node from a json object.
Definition udpRecv.cpp:79
static constexpr size_t OUTPUT_PORT_INDEX_NODE_DATA
Object (NodeData)
Definition udpRecv.hpp:73
json save() const override
Saves the node into a json object.
Definition udpRecv.cpp:69
std::string type() const override
String representation of the Class Type.
Definition udpRecv.cpp:45
boost::asio::ip::udp::socket _socket
Boost udp socket.
Definition udpRecv.hpp:93
static std::string typeStatic()
String representation of the Class Type.
Definition udpRecv.cpp:40
static constexpr unsigned int _maxBytes
Network data stream maximum buffer size in [bytes].
Definition udpRecv.hpp:109
int _port
UDP port number.
Definition udpRecv.hpp:85
void deinitialize() override
Deinitialize the node.
Definition udpRecv.cpp:125
boost::asio::ip::udp::endpoint _sender_endpoint
Boost udp endpoint.
Definition udpRecv.hpp:95
static float windowFontRatio()
Ratio to multiply for GUI window elements.
ImGui extensions.
bool InputIntL(const char *label, int *v, int v_min, int v_max, int step, int step_fast, ImGuiInputTextFlags flags)
Shows a value limited InputText GUI element for 'int'.
Definition imgui_ex.cpp:242
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
Asynchronous data link - receiver node.