JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGUDPInputSocket.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGUDPInputSocket.cpp
4 Author: Dave Culp
5 Date started: 02/19/15
6 Purpose: Manage input of data from a UDP socket
7 Called by: FGInput
8
9 ------------- Copyright (C) 2015 Dave Culp --------------
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
14 version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 details.
20
21 You should have received a copy of the GNU Lesser General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be found on
26 the world wide web at http://www.gnu.org.
27
28FUNCTIONAL DESCRIPTION
29--------------------------------------------------------------------------------
30This class establishes a UDP socket and reads data from it.
31
32HISTORY
33--------------------------------------------------------------------------------
3402/19/15 DC Created
35
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37INCLUDES
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40#include "FGUDPInputSocket.h"
41#include "FGFDMExec.h"
42#include "FGXMLElement.h"
43#include "string_utilities.h"
44#include "FGLog.h"
45
46using namespace std;
47
48namespace JSBSim {
49
50/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51CLASS IMPLEMENTATION
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
55 FGInputSocket(fdmex), rate(20), oldTimeStamp(0.0)
56{
57 SockPort = 5139;
58 SockProtocol = FGfdmSocket::ptUDP;
59}
60
61//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
64{
65 if (!FGInputSocket::Load(el))
66 return false;
67
68 rate = atoi(el->GetAttributeValue("rate").c_str());
69 SetRate(0.5 + 1.0/(FDMExec->GetDeltaT()*rate));
70
71 Element *property_element = el->FindElement("property");
72
73 while (property_element) {
74 string property_str = property_element->GetDataLine();
75 SGPropertyNode* node = PropertyManager->GetNode(property_str);
76 if (!node) {
77 FGXMLLogging log(property_element, LogLevel::ERROR);
78 log << LogFormat::RED << LogFormat::BOLD << "\n No property by the name "
79 << property_str << " can be found.\n" << LogFormat::RESET;
80 } else {
81 InputProperties.push_back(node);
82 }
83 property_element = el->FindNextElement("property");
84 }
85
86 return true;
87}
88
89//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91void FGUDPInputSocket::Read(bool Holding)
92{
93 if (socket == 0) return;
94
95 data = socket->Receive();
96
97 if (!data.empty()) {
98
99 vector<string> tokens;
100 stringstream ss(data);
101 string temp;
102 while (getline(ss, temp, ',')) {
103 tokens.push_back(temp);
104 }
105
106 vector<double> values;
107
108 try {
109 for (string& token : tokens)
110 values.push_back(atof_locale_c(token));
111 } catch(InvalidNumber& e) {
112 FGLogging log(LogLevel::ERROR);
113 log << e.what() << "\n";
114 return;
115 }
116
117 if (values[0] < oldTimeStamp) {
118 return;
119 } else {
120 oldTimeStamp = values[0];
121 }
122
123 // the zeroeth value is the time stamp
124 if ((values.size() - 1) != InputProperties.size()) {
125 FGLogging log(LogLevel::ERROR);
126 log << "\nMismatch between UDP input property and value counts.\n";
127 return;
128 }
129
130 for (unsigned int i=1; i<values.size(); i++) {
131 InputProperties[i-1]->setDoubleValue(values[i]);
132 }
133 }
134}
135
136}
Element * FindElement(const std::string &el="")
Searches for a specified element.
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
std::string GetDataLine(unsigned int i=0)
Gets a line of data belonging to an element.
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
Encapsulates the JSBSim simulation executive.
Definition FGFDMExec.h:185
double GetDeltaT(void) const
Returns the simulation delta T.
Definition FGFDMExec.h:553
Implements the input from a socket.
bool Load(Element *el) override
Init the input directives from an XML file.
void SetRate(unsigned int tt)
Set the ouput rate for the model in frames.
Definition FGModel.h:91
bool Load(Element *el) override
Reads the property names from an XML file.
void Read(bool Holding) override
Reads the socket and updates properties accordingly.
FGUDPInputSocket(FGFDMExec *fdmex)
Constructor.
std::string Receive(void)
Receive data from the socket connection.
A node in a property tree.
Definition props.hxx:747
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71