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
FGInput.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGInput.cpp
4 Author: Jon Berndt
5 Date started: 12/02/98
6 Purpose: Manage input of sim parameters from socket
7 Called by: FGSimExec
8
9 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
13 Software Foundation; either version 2 of the License, or (at your option) any
14 later 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
22 with this program; if not, write to the Free Software Foundation, Inc., 59
23 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be
26 found on the world wide web at http://www.gnu.org.
27
28FUNCTIONAL DESCRIPTION
29--------------------------------------------------------------------------------
30This is the place where you create output routines to dump data for perusal
31later.
32
33HISTORY
34--------------------------------------------------------------------------------
3512/02/98 JSB Created
36
37%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include "FGInput.h"
42#include "FGFDMExec.h"
43#include "input_output/FGUDPInputSocket.h"
44#include "input_output/FGXMLFileRead.h"
45#include "input_output/FGModelLoader.h"
46#include "input_output/FGLog.h"
47#include "input_output/string_utilities.h"
48
49using namespace std;
50
51namespace JSBSim {
52
53/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54CLASS IMPLEMENTATION
55%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57FGInput::FGInput(FGFDMExec* fdmex) : FGModel(fdmex)
58{
59 Name = "FGInput";
60 enabled = true;
61
62 Debug(0);
63}
64
65//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
67FGInput::~FGInput()
68{
69 vector<FGInputType*>::iterator it;
70 for (it = InputTypes.begin(); it != InputTypes.end(); ++it)
71 delete (*it);
72
73 Debug(1);
74}
75
76//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78bool FGInput::Load(Element* el)
79{
80 // Unlike the other FGModel classes, properties listed in the <input> section
81 // are not intended to create new properties. For that reason, FGInput
82 // cannot load its XML directives with FGModel::Load().
83 // Instead FGModelLoader::Open() and FGModel::PreLoad() must be explicitely
84 // called.
85 FGModelLoader ModelLoader(this);
86 Element* element = ModelLoader.Open(el);
87
88 if (!element) return false;
89
90 FGModel::PreLoad(element, FDMExec);
91
92 size_t idx = InputTypes.size();
93 string type = element->GetAttributeValue("type");
94 FGInputType* Input = 0;
95
96 if (debug_lvl > 0) {
97 FGLogging log(LogLevel::DEBUG);
98 log << endl << " Input data set: " << idx << " " << endl;
99 }
100
101 type = to_upper(type);
102
103 if (type.empty() || type == "SOCKET") {
104 Input = new FGInputSocket(FDMExec);
105 } else if (type == "QTJSBSIM") {
106 Input = new FGUDPInputSocket(FDMExec);
107 } else if (type != string("NONE")) {
108 FGXMLLogging log(element, LogLevel::ERROR);
109 log << "Unknown type of input specified in config file" << endl;
110 }
111
112 if (!Input) return false;
113
114 Input->SetIdx(idx);
115 Input->Load(element);
116 PostLoad(element, FDMExec);
117
118 InputTypes.push_back(Input);
119
120 Debug(2);
121 return true;
122}
123
124//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126bool FGInput::InitModel(void)
127{
128 bool ret = false;
129
130 if (!FGModel::InitModel()) return false;
131
132 vector<FGInputType*>::iterator it;
133 for (it = InputTypes.begin(); it != InputTypes.end(); ++it)
134 ret &= (*it)->InitModel();
135
136 return ret;
137}
138
139//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141bool FGInput::Run(bool Holding)
142{
143 if (FDMExec->GetTrimStatus()) return true;
144 if (FGModel::Run(Holding)) return true;
145 if (!enabled) return true;
146
147 vector<FGInputType*>::iterator it;
148 for (it = InputTypes.begin(); it != InputTypes.end(); ++it)
149 (*it)->Run(Holding);
150
151 return false;
152}
153
154//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156bool FGInput::SetDirectivesFile(const SGPath& fname)
157{
159 Element* document = XMLFile.LoadXMLDocument(fname);
160 if (!document) {
161 LogException err;
162 err << "Could not read directive file: " << fname << endl;
163 throw err;
164 }
165 bool result = Load(document);
166
167 if (!result) {
168 FGLogging log(LogLevel::ERROR);
169 log << endl << "Aircraft input element has problems in file " << fname << endl;
170 }
171
172 return result;
173}
174
175//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176
177bool FGInput::Toggle(int idx)
178{
179 if (idx >= (int)0 && idx < (int)InputTypes.size())
180 return InputTypes[idx]->Toggle();
181
182 return false;
183}
184
185//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186
187bool FGInput::SetInputName(unsigned int idx, const std::string& name)
188{
189 if (idx >= InputTypes.size()) return false;
190
191 InputTypes[idx]->SetInputName(name);
192 return true;
193}
194
195//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196
197string FGInput::GetInputName(unsigned int idx) const
198{
199 string name;
200
201 if (idx < InputTypes.size())
202 name = InputTypes[idx]->GetInputName();
203 return name;
204}
205
206
207//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208// The bitmasked value choices are as follows:
209// unset: In this case (the default) JSBSim would only print
210// out the normally expected messages, essentially echoing
211// the config files as they are read. If the environment
212// variable is not set, debug_lvl is set to 1 internally
213// 0: This requests JSBSim not to output any messages
214// whatsoever.
215// 1: This value explicity requests the normal JSBSim
216// startup messages
217// 2: This value asks for a message to be printed out when
218// a class is instantiated
219// 4: When this value is set, a message is displayed when a
220// FGModel object executes its Run() method
221// 8: When this value is set, various runtime state variables
222// are printed out periodically
223// 16: When set various parameters are sanity checked and
224// a message is printed out when they go out of bounds
225
226void FGInput::Debug(int from)
227{
228 string scratch="";
229
230 if (debug_lvl <= 0) return;
231
232 if (debug_lvl & 1) { // Standard console startup message output
233 if (from == 0) { // Constructor
234 }
235 if (from == 2) {
236 }
237 }
238 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
239 FGLogging log(LogLevel::DEBUG);
240 if (from == 0) log << "Instantiated: FGInput" << endl;
241 if (from == 1) log << "Destroyed: FGInput" << endl;
242 }
243 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
244 }
245 if (debug_lvl & 8 ) { // Runtime state variables
246 }
247 if (debug_lvl & 16) { // Sanity checking
248 }
249 if (debug_lvl & 64) {
250 if (from == 0) { // Constructor
251 }
252 }
253}
254}
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Implements the input from a socket.
Abstract class to provide functions generic to all the input directives.
Definition FGInputType.h:74
bool Load(Element *el) override
Init the input directives from an XML file (implement the FGModel interface).
void SetIdx(unsigned int idx)
Set the idx for this input instance.
Implements a UDP input socket.
This class is solely for the purpose of determining what type of file is given on the command line.
Definition JSBSim.cpp:155
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71