]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/JSBSim.cpp
Latest JSBSim changes, including SGI patch.
[flightgear.git] / src / FDM / JSBSim / JSBSim.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
2 \r
3  Module:       JSBSim.cpp\r
4  Author:       Jon S. Berndt\r
5  Date started: 08/17/99\r
6  Purpose:      Standalone version of JSBSim.\r
7  Called by:    The USER.\r
8 \r
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------\r
10 \r
11  This program is free software; you can redistribute it and/or modify it under\r
12  the terms of the GNU General Public License as published by the Free Software\r
13  Foundation; either version 2 of the License, or (at your option) any later\r
14  version.\r
15 \r
16  This program is distributed in the hope that it will be useful, but WITHOUT\r
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
19  details.\r
20 \r
21  You should have received a copy of the GNU General Public License along with\r
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple\r
23  Place - Suite 330, Boston, MA  02111-1307, USA.\r
24 \r
25  Further information about the GNU General Public License can also be found on\r
26  the world wide web at http://www.gnu.org.\r
27 \r
28 FUNCTIONAL DESCRIPTION\r
29 --------------------------------------------------------------------------------\r
30 \r
31 This class Handles calling JSBSim standalone. It is set up for compilation under\r
32 Borland C+Builder or other compiler.\r
33 \r
34 HISTORY\r
35 --------------------------------------------------------------------------------\r
36 08/17/99   JSB   Created\r
37 \r
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
39 INCLUDES\r
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
41 \r
42 #include "FGFDMExec.h"\r
43 #include "FGRotation.h"\r
44 #include "FGAtmosphere.h"\r
45 #include "FGState.h"\r
46 #include "FGFCS.h"\r
47 #include "FGAircraft.h"\r
48 #include "FGTranslation.h"\r
49 #include "FGPosition.h"\r
50 #include "FGAuxiliary.h"\r
51 #include "FGOutput.h"\r
52 #include "FGConfigFile.h"\r
53 #include "FGScript.h"\r
54 #include "FGJSBBase.h"\r
55 \r
56 #ifdef FGFS\r
57 #include <simgear/compiler.h>\r
58 #include STL_IOSTREAM\r
59 #else\r
60 #  if defined(sgi) && !defined(__GNUC__)\r
61 #    include <iostream.h>\r
62 #  else\r
63 #    include <iostream>\r
64 #  endif\r
65 #endif\r
66 \r
67 #if __BCPLUSPLUS__ == 0x540\r
68 #pragma hdrstop\r
69 #include <condefs.h>\r
70 //---------------------------------------------------------------------------\r
71 USERES("JSBSim.res");\r
72 USEUNIT("FGUtility.cpp");\r
73 USEUNIT("FGAircraft.cpp");\r
74 USEUNIT("FGAtmosphere.cpp");\r
75 USEUNIT("FGAuxiliary.cpp");\r
76 USEUNIT("FGCoefficient.cpp");\r
77 USEUNIT("FGColumnVector3.cpp");\r
78 USEUNIT("FGColumnVector4.cpp");\r
79 USEUNIT("FGConfigFile.cpp");\r
80 USEUNIT("FGEngine.cpp");\r
81 USEUNIT("FGFactorGroup.cpp");\r
82 USEUNIT("FGFCS.cpp");\r
83 USEUNIT("FGFDMExec.cpp");\r
84 USEUNIT("FGfdmSocket.cpp");\r
85 USEUNIT("FGForce.cpp");\r
86 USEUNIT("FGGroundReactions.cpp");\r
87 USEUNIT("FGInertial.cpp");\r
88 USEUNIT("FGInitialCondition.cpp");\r
89 USEUNIT("FGJSBBase.cpp");\r
90 USEUNIT("FGLGear.cpp");\r
91 USEUNIT("FGMassBalance.cpp");\r
92 USEUNIT("FGMatrix33.cpp");\r
93 USEUNIT("FGModel.cpp");\r
94 USEUNIT("FGNozzle.cpp");\r
95 USEUNIT("FGOutput.cpp");\r
96 USEUNIT("FGPiston.cpp");\r
97 USEUNIT("FGPosition.cpp");\r
98 USEUNIT("FGPropeller.cpp");\r
99 USEUNIT("FGPropulsion.cpp");\r
100 USEUNIT("FGRocket.cpp");\r
101 USEUNIT("FGRotation.cpp");\r
102 USEUNIT("FGRotor.cpp");\r
103 USEUNIT("FGScript.cpp");\r
104 USEUNIT("FGState.cpp");\r
105 USEUNIT("FGTable.cpp");\r
106 USEUNIT("FGTank.cpp");\r
107 USEUNIT("FGThruster.cpp");\r
108 USEUNIT("FGTranslation.cpp");\r
109 USEUNIT("FGTrim.cpp");\r
110 USEUNIT("FGTrimAxis.cpp");\r
111 USEUNIT("FGTurboJet.cpp");\r
112 USEUNIT("FGTurboProp.cpp");\r
113 USEUNIT("FGTurboShaft.cpp");\r
114 USEUNIT("FGAerodynamics.cpp");\r
115 USEUNIT("filtersjb\FGSwitch.cpp");\r
116 USEUNIT("filtersjb\FGFCSComponent.cpp");\r
117 USEUNIT("filtersjb\FGFilter.cpp");\r
118 USEUNIT("filtersjb\FGGain.cpp");\r
119 USEUNIT("filtersjb\FGGradient.cpp");\r
120 USEUNIT("filtersjb\FGKinemat.cpp");\r
121 USEUNIT("filtersjb\FGSummer.cpp");\r
122 USEUNIT("filtersjb\FGDeadBand.cpp");\r
123 //---------------------------------------------------------------------------\r
124 #pragma argsused\r
125 #endif\r
126 \r
127 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
128 DEFINITIONS\r
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
130 \r
131 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
132 GLOBAL DATA\r
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
134 \r
135 static const char *IdSrc = "$Id$";\r
136 \r
137 string ScriptName;\r
138 string AircraftName;\r
139 string ResetName;\r
140 string LogOutputName;\r
141 string LogDirectiveName;\r
142 FGFDMExec* FDMExec;\r
143 \r
144 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
145 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]\r
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
147 \r
148 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
149 FORWARD DECLARATIONS\r
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
151 \r
152 void options(int, char**);\r
153 \r
154 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
155 DOCUMENTATION\r
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
157 \r
158 /** Standalone JSBSim main program\r
159     This is the wrapper program used to instantiate the JSBSim system and control\r
160     it. Use this program to build a version of JSBSim that can be run from the\r
161     command line. To get any use out of this, you will have to create a script\r
162     to run a test case and specify what kind of output you would like.\r
163     @author Jon S. Berndt\r
164     @version $Id$\r
165     @see -\r
166 */\r
167 \r
168 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
169 IMPLEMENTATION\r
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
171 \r
172 int main(int argc, char* argv[])\r
173 {\r
174   ScriptName = "";\r
175   AircraftName = "";\r
176   ResetName = "";\r
177   LogOutputName = "";\r
178   LogDirectiveName = "";\r
179   bool result = false;\r
180   bool Scripted = false;\r
181   FGScript* Script;\r
182 \r
183   options(argc, argv);\r
184 \r
185   FDMExec = new FGFDMExec();\r
186 \r
187   if (!ScriptName.empty()) { // SCRIPTED CASE\r
188 \r
189     Script = new FGScript(FDMExec);\r
190     result = Script->LoadScript(ScriptName);\r
191 \r
192     if (!result) {\r
193       cerr << "Script file " << ScriptName << " was not successfully loaded" << endl;\r
194       exit(-1);\r
195     }\r
196 \r
197     Scripted = true;\r
198 \r
199   } else if (!AircraftName.empty() || !ResetName.empty()) {        // form jsbsim <acname> <resetfile>\r
200 \r
201     if ( ! FDMExec->LoadModel("aircraft", "engine", AircraftName)) {\r
202         cerr << "  JSBSim could not be started" << endl << endl;\r
203       exit(-1);\r
204     }\r
205 \r
206     FGInitialCondition IC(FDMExec);\r
207     if ( ! IC.Load("aircraft",AircraftName,ResetName)) {\r
208         cerr << "Initialization unsuccessful" << endl;\r
209       exit(-1);\r
210     }\r
211   } else {\r
212     cout << "  No Aircraft, Script, or Reset information given" << endl << endl;\r
213     exit(-1);\r
214   }\r
215 \r
216 //\r
217 // RUN loop. MESSAGES are read inside the Run() loop and output as necessary.\r
218 //\r
219 \r
220   FGJSBBase::Message* msg;\r
221   result = FDMExec->Run();\r
222   while (result) {\r
223     while (FDMExec->ReadMessage()) {\r
224       msg = FDMExec->ProcessMessage();\r
225       switch (msg->type) {\r
226       case FGJSBBase::Message::eText:\r
227         cout << msg->messageId << ": " << msg->text << endl;\r
228         break;\r
229       case FGJSBBase::Message::eBool:\r
230         cout << msg->messageId << ": " << msg->text << " " << msg->bVal << endl;\r
231         break;\r
232       case FGJSBBase::Message::eInteger:\r
233         cout << msg->messageId << ": " << msg->text << " " << msg->iVal << endl;\r
234         break;\r
235       case FGJSBBase::Message::eDouble:\r
236         cout << msg->messageId << ": " << msg->text << " " << msg->dVal << endl;\r
237         break;\r
238       default:\r
239         cerr << "Unrecognized message type." << endl;\r
240               break;\r
241       }\r
242     }\r
243 \r
244     if (Scripted) {\r
245       if (!Script->RunScript()) break;\r
246     }\r
247 \r
248     result = FDMExec->Run();\r
249   }\r
250 \r
251   delete FDMExec;\r
252 \r
253   return 0;\r
254 }\r
255 \r
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
257 \r
258 void options(int count, char **arg)\r
259 {\r
260   int i;\r
261 \r
262   if (count == 1) {\r
263     cout << endl << "  JSBSim version " << FDMExec->GetVersion() << endl << endl;\r
264     cout << "  Usage: jsbsim <options>" << endl << endl;\r
265     cout << "  options:" << endl;\r
266       cout << "    --help  returns this message" << endl;\r
267       cout << "    --version  returns the version number" << endl;\r
268       cout << "    --outputlogfile=<filename>  sets the name of the data output file" << endl;\r
269       cout << "    --logdirectivefile=<filename>  specifies the name of the data logging directives file" << endl;\r
270       cout << "    --aircraft=<filename>  specifies the name of the aircraft to be modeled" << endl;\r
271       cout << "    --script=<filename>  specifies a script to run" << endl;\r
272       cout << "    --initfile=<filename>  specifies an initilization file" << endl << endl;\r
273     cout << "  NOTE: There can be no spaces around the = sign when" << endl;\r
274     cout << "        an option is followed by a filename" << endl << endl;\r
275   }\r
276 \r
277   for (i=1; i<count; i++) {\r
278     string argument = string(arg[i]);\r
279     int n=0;\r
280     if (argument.find("--help") != string::npos) {\r
281       cout << endl << "  JSBSim version " << FDMExec->GetVersion() << endl << endl;\r
282       cout << "  Usage: jsbsim <options>" << endl << endl;\r
283       cout << "  options:" << endl;\r
284       cout << "    --help  returns this message" << endl;\r
285       cout << "    --version  returns the version number" << endl;\r
286       cout << "    --outputlogfile=<filename>  sets the name of the data output file" << endl;\r
287       cout << "    --logdirectivefile=<filename>  specifies the name of the data logging directives file" << endl;\r
288       cout << "    --aircraft=<filename>  specifies the name of the aircraft to be modeled" << endl;\r
289       cout << "    --script=<filename>  specifies a script to run" << endl;\r
290       cout << "    --initfile=<filename>  specifies an initilization file" << endl << endl;\r
291       cout << "  NOTE: There can be no spaces around the = sign when" << endl;\r
292       cout << "        an option is followed by a filename" << endl << endl;\r
293       exit(0);\r
294     } else if (argument.find("--version") != string::npos) {\r
295       cout << endl << "  JSBSim Version: " << FDMExec->GetVersion() << endl << endl;\r
296       exit (0);\r
297     } else if (argument.find("--outputlogfile") != string::npos) {\r
298       n = argument.find("=")+1;\r
299       if (n > 0) {\r
300         LogOutputName = argument.substr(argument.find("=")+1);\r
301       } else {\r
302         LogOutputName = "JSBout.csv";\r
303         cerr << "  Output log file name not valid or not understood. Using JSBout.csv as default";\r
304       }\r
305     } else if (argument.find("--logdirectivefile") != string::npos) {\r
306       n = argument.find("=")+1;\r
307       if (n > 0) {\r
308         LogDirectiveName = argument.substr(argument.find("=")+1);\r
309       } else {\r
310         cerr << "  Log directives file not valid or not understood." << endl << endl;\r
311         exit(1);\r
312       }\r
313     } else if (argument.find("--aircraft") != string::npos) {\r
314       n = argument.find("=")+1;\r
315       if (n > 0) {\r
316         AircraftName = argument.substr(argument.find("=")+1);\r
317       } else {\r
318         cerr << "  Aircraft name not valid or not understood." << endl << endl;\r
319         exit(1);\r
320       }\r
321     } else if (argument.find("--script") != string::npos) {\r
322       n = argument.find("=")+1;\r
323       if (n > 0) {\r
324         ScriptName = argument.substr(argument.find("=")+1);\r
325       } else {\r
326         cerr << "  Script name not valid or not understood." << endl << endl;\r
327         exit(1);\r
328       }\r
329     } else if (argument.find("--initfile") != string::npos) {\r
330       n = argument.find("=")+1;\r
331       if (n > 0) {\r
332         ResetName = argument.substr(argument.find("=")+1);\r
333       } else {\r
334         cerr << "  Reset name not valid or not understood." << endl << endl;\r
335         exit(1);\r
336       }\r
337     } else {\r
338       cerr << endl << "  Parameter: " << argument << " not understood" << endl;\r
339     }\r
340   }\r
341 \r
342 }\r
343 \r