0.5.0
Loading...
Searching...
No Matches
udpSend.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 "udpSend.hpp"
10
13#include "internal/Node/Pin.hpp"
15namespace nm = NAV::NodeManager;
17#include "NodeRegistry.hpp"
18
19#include "util/Logger.hpp"
22
23#include <cstring>
24#include <string>
25#include <vector>
26
27// ---------------------------------------------------------- Private variabels ------------------------------------------------------------
28
29namespace NAV
30{
31/// List of supported data identifiers
32const std::vector<std::string> supportedDataIdentifier{
35 Pos::type(),
37};
38
39} // namespace NAV
40
41// ---------------------------------------------------------- Member functions -------------------------------------------------------------
42
44 : Node(typeStatic()), _socket(_io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)), _resolver(_io_context)
45{
46 LOG_TRACE("{}: called", name);
47
48 _hasConfig = true;
49 _guiConfigDefaultWindowSize = { 202, 96 };
50
52}
53
55{
56 LOG_TRACE("{}: called", nameId());
57}
58
60{
61 return "UdpSend";
62}
63
64std::string NAV::UdpSend::type() const
65{
66 return typeStatic();
67}
68
70{
71 return "Data Link";
72}
73
75{
76 ImGui::SetNextItemWidth(150 * gui::NodeEditorApplication::windowFontRatio());
77 if (ImGui::InputInt4L(fmt::format("IPv4##{}", size_t(id)).c_str(), _ip.data(), IP_LIMITS[0], IP_LIMITS[1]))
78 {
80 }
81 ImGui::SetNextItemWidth(150 * gui::NodeEditorApplication::windowFontRatio());
82 if (ImGui::InputIntL(fmt::format("Port##{}", size_t(id)).c_str(), &_port, UdpUtil::PORT_LIMITS[0], UdpUtil::PORT_LIMITS[1]))
83 {
85 }
86}
87
89{
90 return true;
91}
92
94{
95 LOG_TRACE("{}: called", nameId());
96
97 json j;
98
99 j["ip"] = _ip;
100 j["port"] = _port;
101
102 return j;
103}
104
106{
107 LOG_TRACE("{}: called", nameId());
108 if (j.contains("ip"))
109 {
110 j.at("ip").get_to(_ip);
111 }
112 if (j.contains("port"))
113 {
114 j.at("port").get_to(_port);
115 }
116}
117
119{
120 LOG_TRACE("{}: called", nameId());
121
122 std::string ipString{};
123 for (size_t i = 0; i < 4; i++)
124 {
125 ipString.append(std::to_string(_ip.at(i)));
126 i < 3 ? ipString.append(".") : ipString.append("");
127 }
128
129 _endpoints = _resolver.resolve(boost::asio::ip::udp::v4(), ipString, std::to_string(_port));
130
131 return true;
132}
133
135{
136 _io_context.stop();
137
138 LOG_TRACE("{}: called", nameId());
139}
140
142{
143 auto data = queue.extract_front();
144
145 std::vector<char> data2send{};
146
147 // Identify message type
148 if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { PosVelAtt::type() }))
149 {
151 data2send.resize(UdpUtil::Size::TOTAL_POSVELATT);
152 setMsgTypeAndTime(data2send, data->insTime);
153 }
154 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { PosVel::type() }))
155 {
157 data2send.resize(UdpUtil::Size::TOTAL_POSVEL);
158 setMsgTypeAndTime(data2send, data->insTime);
159 }
160 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { Pos::type() }))
161 {
163 data2send.resize(UdpUtil::Size::TOTAL_POS);
164 setMsgTypeAndTime(data2send, data->insTime);
165 }
166 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { GnssObs::type() }))
167 {
169 }
170
171 // Copy data
173 {
174 auto pos = std::static_pointer_cast<const Pos>(data);
175 std::memcpy(data2send.data() + UdpUtil::Offset::POS, pos->lla_position().data(), UdpUtil::Size::POS);
176 }
178 {
179 auto posVel = std::static_pointer_cast<const PosVel>(data);
180 std::memcpy(data2send.data() + UdpUtil::Offset::POS, posVel->lla_position().data(), UdpUtil::Size::POS);
181 std::memcpy(data2send.data() + UdpUtil::Offset::VEL, posVel->n_velocity().data(), UdpUtil::Size::VEL);
182 }
184 {
185 auto posVelAtt = std::static_pointer_cast<const PosVelAtt>(data);
186 std::memcpy(data2send.data() + UdpUtil::Offset::POS, posVelAtt->lla_position().data(), UdpUtil::Size::POS);
187 std::memcpy(data2send.data() + UdpUtil::Offset::VEL, posVelAtt->n_velocity().data(), UdpUtil::Size::VEL);
188 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT, &posVelAtt->n_Quat_b().x(), UdpUtil::Size::QUAT);
189 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().y(), UdpUtil::Size::QUAT);
190 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + 2 * UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().z(), UdpUtil::Size::QUAT);
191 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + 3 * UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().w(), UdpUtil::Size::QUAT);
192 }
194 {
195 auto gnssObs = std::static_pointer_cast<const GnssObs>(data);
196 const size_t sizeGnssData = UdpUtil::Size::SINGLE_OBSERVATION_DATA * gnssObs->data.size();
197
199 if (sizeTotal > UdpUtil::MAXIMUM_BYTES)
200 {
201 LOG_ERROR("{}: gnssObs msg is bigger than the maximum size of a single UDP package: {} bytes.", nameId(), sizeTotal);
202 }
203
204 data2send.resize(sizeTotal);
205
206 setMsgTypeAndTime(data2send, data->insTime);
207
208 std::memcpy(data2send.data() + UdpUtil::Offset::SIZE, &sizeGnssData, UdpUtil::Size::SIZE);
209 std::memcpy(data2send.data() + UdpUtil::Offset::GNSSDATA, gnssObs->data.data(), sizeGnssData);
210 }
211 _socket.send_to(boost::asio::buffer(data2send), *_endpoints.begin());
212}
213
214void NAV::UdpSend::setMsgTypeAndTime(std::vector<char>& data2send, const InsTime& insTime)
215{
216 auto gpsCycle = insTime.toGPSweekTow().gpsCycle;
217 auto gpsWeek = insTime.toGPSweekTow().gpsWeek;
218 auto gpsTow = static_cast<double>(insTime.toGPSweekTow().tow);
219
220 std::memcpy(data2send.data(), &_msgType, UdpUtil::Size::MSGTYPE);
221
222 std::memcpy(data2send.data() + UdpUtil::Offset::GPSCYCLE, &gpsCycle, UdpUtil::Size::GPSCYCLE);
223 std::memcpy(data2send.data() + UdpUtil::Offset::GPSWEEK, &gpsWeek, UdpUtil::Size::GPSWEEK);
224 std::memcpy(data2send.data() + UdpUtil::Offset::GPSTOW, &gpsTow, UdpUtil::Size::GPSTOW);
225}
Save/Load the Nodes.
nlohmann::json json
json namespace
GNSS Observation messages.
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.
Utility class which specifies available nodes.
Pin class.
Position, Velocity and Attitude Storage Class.
static std::string type()
Returns the type of the data class.
Definition GnssObs.hpp:150
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
constexpr InsTime_GPSweekTow toGPSweekTow(TimeSystem timesys=GPST) const
Converts this time object into a different format.
Definition InsTime.hpp:854
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 _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 type()
Returns the type of the data class.
Definition PosVel.hpp:27
static std::string type()
Returns the type of the data class.
Definition Pos.hpp:36
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
boost::asio::ip::udp::resolver::results_type _endpoints
Boost udp endpoint.
Definition udpSend.hpp:114
UdpUtil::MessageType _msgType
Message Type: 0 = posVelAtt, 1 = gnssObs.
Definition udpSend.hpp:102
bool initialize() override
Initialize the node.
Definition udpSend.cpp:118
UdpSend()
Default constructor.
Definition udpSend.cpp:43
static constexpr std::array< int, 2 > IP_LIMITS
Range an IPv4 address can be in [0, 2^8-1].
Definition udpSend.hpp:105
boost::asio::ip::udp::resolver _resolver
Boost udp resolver.
Definition udpSend.hpp:112
bool resetNode() override
Resets the node. Moves the read cursor to the start.
Definition udpSend.cpp:88
void deinitialize() override
Deinitialize the node.
Definition udpSend.cpp:134
static std::string category()
String representation of the Class Category.
Definition udpSend.cpp:69
std::string type() const override
String representation of the Class Type.
Definition udpSend.cpp:64
boost::asio::io_context _io_context
Asynchronous receive fct.
Definition udpSend.hpp:108
void receiveData(InputPin::NodeDataQueue &queue, size_t pinIdx)
Callback when receiving data on a port.
Definition udpSend.cpp:141
void restore(const json &j) override
Restores the node from a json object.
Definition udpSend.cpp:105
void guiConfig() override
ImGui config window which is shown on double click.
Definition udpSend.cpp:74
void setMsgTypeAndTime(std::vector< char > &data2send, const InsTime &insTime)
Set the Msg Type And Time object.
Definition udpSend.cpp:214
static std::string typeStatic()
String representation of the Class Type.
Definition udpSend.cpp:59
int _port
UDP port number.
Definition udpSend.hpp:99
boost::asio::ip::udp::socket _socket
Boost udp socket.
Definition udpSend.hpp:110
std::array< int, 4 > _ip
IPv4 address.
Definition udpSend.hpp:96
json save() const override
Saves the node into a json object.
Definition udpSend.cpp:93
~UdpSend() override
Destructor.
Definition udpSend.cpp:54
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
bool InputInt4L(const char *label, int v[4], int v_min, int v_max, ImGuiInputTextFlags flags)
Shows a value limited InputText GUI element for an array of 'int[4]'.
Definition imgui_ex.cpp:280
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.
bool NodeDataTypeAnyIsChildOf(const std::vector< std::string > &childTypes, const std::vector< std::string > &parentTypes)
Checks if any of the provided child types is a child of any of the provided parent types.
static constexpr size_t POS
Offset of the position.
Definition UdpUtil.hpp:75
static constexpr size_t GPSCYCLE
Offset of the GPS cycle.
Definition UdpUtil.hpp:69
static constexpr size_t GPSTOW
Offset of the GPS tow.
Definition UdpUtil.hpp:73
static constexpr size_t SIZE
Offset of the GNSS data size.
Definition UdpUtil.hpp:82
static constexpr size_t QUAT
Offset of the quaternion.
Definition UdpUtil.hpp:79
static constexpr size_t GNSSDATA
Offset of the GNSS data.
Definition UdpUtil.hpp:84
static constexpr size_t VEL
Offset of the velocity.
Definition UdpUtil.hpp:77
static constexpr size_t GPSWEEK
Offset of the GPS week.
Definition UdpUtil.hpp:71
static constexpr size_t TOTAL_POSVELATT
Size of a total 'PosVelAtt' message.
Definition UdpUtil.hpp:58
static constexpr size_t GPSCYCLE
Size of a GPS cycle.
Definition UdpUtil.hpp:41
static constexpr size_t SIZE
Size of the size of a GNSS observation.
Definition UdpUtil.hpp:61
static constexpr size_t QUAT
Size of a Quaternion element.
Definition UdpUtil.hpp:51
static constexpr size_t SINGLE_OBSERVATION_DATA
Size of a single GNSS observation.
Definition UdpUtil.hpp:63
static constexpr size_t MSGTYPE
Size of the message type.
Definition UdpUtil.hpp:39
static constexpr size_t POS
Size of a Pos (LLA)
Definition UdpUtil.hpp:47
static constexpr size_t GPSTOW
Size of a GPS TOW.
Definition UdpUtil.hpp:45
static constexpr size_t GPSWEEK
Size of a GPS week.
Definition UdpUtil.hpp:43
static constexpr size_t TOTAL_POSVEL
Size of a total 'PosVel' message.
Definition UdpUtil.hpp:56
static constexpr size_t VEL
Size of a Vel (NED)
Definition UdpUtil.hpp:49
static constexpr size_t TOTAL_POS
Size of a total 'Pos' message.
Definition UdpUtil.hpp:54
static constexpr unsigned int MAXIMUM_BYTES
Network data stream maximum buffer size in [bytes] (Maximum payload size of a UDP package)
Definition UdpUtil.hpp:22
static constexpr std::array< int, 2 > PORT_LIMITS
Range a port can be in [0, 2^16-1].
Definition UdpUtil.hpp:24
@ PosVel
Extract PosVel data.
Definition UdpUtil.hpp:30
@ GnssObs
Extract GnssObs data.
Definition UdpUtil.hpp:32
@ PosVelAtt
Extract PosVelAtt data.
Definition UdpUtil.hpp:29
@ Pos
Extract Pos data.
Definition UdpUtil.hpp:31
void ApplyChanges()
Signals that there have been changes to the flow.
const std::vector< std::string > supportedDataIdentifier
List of supported data identifiers.
Definition udpSend.cpp:32
int32_t gpsCycle
Contains GPS cycle in GPS standard time [GPST].
Definition InsTime.hpp:370
int32_t gpsWeek
Contains GPS week in GPS standard time [GPST].
Definition InsTime.hpp:371
@ Flow
NodeData Trigger.
Definition Pin.hpp:52
Asynchronous data link - sender node.