0.5.1
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"
15#include "NodeRegistry.hpp"
16
17#include "util/Logger.hpp"
20
21#include <cstring>
22#include <string>
23#include <vector>
24
25// ---------------------------------------------------------- Private variabels ------------------------------------------------------------
26
27namespace NAV
28{
29/// List of supported data identifiers
30const std::vector<std::string> supportedDataIdentifier{
33 Pos::type(),
35};
36
37} // namespace NAV
38
39// ---------------------------------------------------------- Member functions -------------------------------------------------------------
40
42 : Node(typeStatic()), _socket(_io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)), _resolver(_io_context)
43{
44 LOG_TRACE("{}: called", name);
45
46 _hasConfig = true;
47 _guiConfigDefaultWindowSize = { 202, 96 };
48
50}
51
53{
54 LOG_TRACE("{}: called", nameId());
55}
56
58{
59 return "UdpSend";
60}
61
62std::string NAV::UdpSend::type() const
63{
64 return typeStatic();
65}
66
68{
69 return "Data Link";
70}
71
73{
74 ImGui::SetNextItemWidth(150 * gui::NodeEditorApplication::windowFontRatio());
75 if (ImGui::InputInt4L(fmt::format("IPv4##{}", size_t(id)).c_str(), _ip.data(), IP_LIMITS[0], IP_LIMITS[1]))
76 {
78 }
79 ImGui::SetNextItemWidth(150 * gui::NodeEditorApplication::windowFontRatio());
80 if (ImGui::InputIntL(fmt::format("Port##{}", size_t(id)).c_str(), &_port, UdpUtil::PORT_LIMITS[0], UdpUtil::PORT_LIMITS[1]))
81 {
83 }
84}
85
87{
88 return true;
89}
90
92{
93 LOG_TRACE("{}: called", nameId());
94
95 json j;
96
97 j["ip"] = _ip;
98 j["port"] = _port;
99
100 return j;
101}
102
104{
105 LOG_TRACE("{}: called", nameId());
106 if (j.contains("ip"))
107 {
108 j.at("ip").get_to(_ip);
109 }
110 if (j.contains("port"))
111 {
112 j.at("port").get_to(_port);
113 }
114}
115
117{
118 LOG_TRACE("{}: called", nameId());
119
120 std::string ipString{};
121 for (size_t i = 0; i < 4; i++)
122 {
123 ipString.append(std::to_string(_ip.at(i)));
124 i < 3 ? ipString.append(".") : ipString.append("");
125 }
126
127 _endpoints = _resolver.resolve(boost::asio::ip::udp::v4(), ipString, std::to_string(_port));
128
129 return true;
130}
131
133{
134 _io_context.stop();
135
136 LOG_TRACE("{}: called", nameId());
137}
138
140{
141 auto data = queue.extract_front();
142
143 std::vector<char> data2send{};
144
145 // Identify message type
146 if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { PosVelAtt::type() }))
147 {
149 data2send.resize(UdpUtil::Size::TOTAL_POSVELATT);
150 setMsgTypeAndTime(data2send, data->insTime);
151 }
152 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { PosVel::type() }))
153 {
155 data2send.resize(UdpUtil::Size::TOTAL_POSVEL);
156 setMsgTypeAndTime(data2send, data->insTime);
157 }
158 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { Pos::type() }))
159 {
161 data2send.resize(UdpUtil::Size::TOTAL_POS);
162 setMsgTypeAndTime(data2send, data->insTime);
163 }
164 else if (NAV::NodeRegistry::NodeDataTypeAnyIsChildOf({ data->getType() }, { GnssObs::type() }))
165 {
167 }
168
169 // Copy data
171 {
172 auto pos = std::static_pointer_cast<const Pos>(data);
173 std::memcpy(data2send.data() + UdpUtil::Offset::POS, pos->lla_position().data(), UdpUtil::Size::POS);
174 }
176 {
177 auto posVel = std::static_pointer_cast<const PosVel>(data);
178 std::memcpy(data2send.data() + UdpUtil::Offset::POS, posVel->lla_position().data(), UdpUtil::Size::POS);
179 std::memcpy(data2send.data() + UdpUtil::Offset::VEL, posVel->n_velocity().data(), UdpUtil::Size::VEL);
180 }
182 {
183 auto posVelAtt = std::static_pointer_cast<const PosVelAtt>(data);
184 std::memcpy(data2send.data() + UdpUtil::Offset::POS, posVelAtt->lla_position().data(), UdpUtil::Size::POS);
185 std::memcpy(data2send.data() + UdpUtil::Offset::VEL, posVelAtt->n_velocity().data(), UdpUtil::Size::VEL);
186 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT, &posVelAtt->n_Quat_b().x(), UdpUtil::Size::QUAT);
187 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().y(), UdpUtil::Size::QUAT);
188 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + 2 * UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().z(), UdpUtil::Size::QUAT);
189 std::memcpy(data2send.data() + UdpUtil::Offset::QUAT + 3 * UdpUtil::Size::QUAT, &posVelAtt->n_Quat_b().w(), UdpUtil::Size::QUAT);
190 }
192 {
193 auto gnssObs = std::static_pointer_cast<const GnssObs>(data);
194 const size_t sizeGnssData = UdpUtil::Size::SINGLE_OBSERVATION_DATA * gnssObs->data.size();
195
197 if (sizeTotal > UdpUtil::MAXIMUM_BYTES)
198 {
199 LOG_ERROR("{}: gnssObs msg is bigger than the maximum size of a single UDP package: {} bytes.", nameId(), sizeTotal);
200 }
201
202 data2send.resize(sizeTotal);
203
204 setMsgTypeAndTime(data2send, data->insTime);
205
206 std::memcpy(data2send.data() + UdpUtil::Offset::SIZE, &sizeGnssData, UdpUtil::Size::SIZE);
207 std::memcpy(data2send.data() + UdpUtil::Offset::GNSSDATA, gnssObs->data.data(), sizeGnssData);
208 }
209 _socket.send_to(boost::asio::buffer(data2send), *_endpoints.begin());
210}
211
212void NAV::UdpSend::setMsgTypeAndTime(std::vector<char>& data2send, const InsTime& insTime)
213{
214 auto gpsCycle = insTime.toGPSweekTow().gpsCycle;
215 auto gpsWeek = insTime.toGPSweekTow().gpsWeek;
216 auto gpsTow = static_cast<double>(insTime.toGPSweekTow().tow);
217
218 std::memcpy(data2send.data(), &_msgType, UdpUtil::Size::MSGTYPE);
219
220 std::memcpy(data2send.data() + UdpUtil::Offset::GPSCYCLE, &gpsCycle, UdpUtil::Size::GPSCYCLE);
221 std::memcpy(data2send.data() + UdpUtil::Offset::GPSWEEK, &gpsWeek, UdpUtil::Size::GPSWEEK);
222 std::memcpy(data2send.data() + UdpUtil::Offset::GPSTOW, &gpsTow, UdpUtil::Size::GPSTOW);
223}
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
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:151
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:522
Node(std::string name)
Constructor.
Definition Node.cpp:29
std::string nameId() const
Node name and id.
Definition Node.cpp:323
std::string name
Name of the Node.
Definition Node.hpp:507
InputPin * CreateInputPin(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.
Definition Node.cpp:252
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:525
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:116
UdpSend()
Default constructor.
Definition udpSend.cpp:41
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:86
void deinitialize() override
Deinitialize the node.
Definition udpSend.cpp:132
static std::string category()
String representation of the Class Category.
Definition udpSend.cpp:67
std::string type() const override
String representation of the Class Type.
Definition udpSend.cpp:62
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:139
void restore(const json &j) override
Restores the node from a json object.
Definition udpSend.cpp:103
void guiConfig() override
ImGui config window which is shown on double click.
Definition udpSend.cpp:72
void setMsgTypeAndTime(std::vector< char > &data2send, const InsTime &insTime)
Set the Msg Type And Time object.
Definition udpSend.cpp:212
static std::string typeStatic()
String representation of the Class Type.
Definition udpSend.cpp:57
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:91
~UdpSend() override
Destructor.
Definition udpSend.cpp:52
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
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:30
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.