]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/JSBSim.cpp
Initial revision.
[flightgear.git] / src / FDM / JSBSim / JSBSim.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       JSBSim.cpp
4  Author:       Jon S. Berndt
5  Date started: 08/17/99
6  Purpose:      Standalone version of JSBSim.
7  Called by:    The USER.
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU 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 General Public License for more
19  details.
20
21  You should have received a copy of the GNU 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 General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 This class Handles calling JSBSim standalone. It is set up for compilation under
32 Borland C+Builder or other compiler.
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 08/17/99   JSB   Created
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include "FGFDMExec.h"
43 #include "FGRotation.h"
44 #include "FGAtmosphere.h"
45 #include "FGState.h"
46 #include "FGFCS.h"
47 #include "FGAircraft.h"
48 #include "FGTranslation.h"
49 #include "FGPosition.h"
50 #include "FGAuxiliary.h"
51 #include "FGOutput.h"
52 #include "FGConfigFile.h"
53
54 #ifdef FGFS
55 #include <simgear/compiler.h>
56 #include STL_IOSTREAM
57 #else
58 #  if defined(sgi) && !defined(__GNUC__)
59 #    include <iostream.h>
60 #  else
61 #    include <iostream>
62 #  endif
63 #endif
64
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 DEFINITIONS
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68
69 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 GLOBAL DATA
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72
73 static const char *IdSrc = "$Id$";
74
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
78
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 DOCUMENTATION
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82
83 /** Standalone JSBSim main program
84     This is the wrapper program used to instantiate the JSBSim system and control
85     it. Use this program to build a version of JSBSim that can be run from the
86     command line. To get any use out of this, you will have to create a script
87     to run a test case and specify what kind of output you would like.
88     @author Jon S. Berndt
89     @version $Id$
90     @see -
91 */
92
93 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 IMPLEMENTATION
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
96
97 int main(int argc, char** argv)
98 {
99   FGFDMExec* FDMExec;
100   float cmd = 0.0;
101   bool result = false;
102   bool scripted = false;
103
104   if (argc == 2) {
105     FGConfigFile testFile(argv[1]);
106
107     if (!testFile.IsOpen()) {
108       cout << "Script file not opened" << endl;
109       exit(-1); 
110     }
111
112     testFile.GetNextConfigLine();
113     if (testFile.GetValue("runscript").length() <= 0) {
114       cout << "File: " << argv[1] << " is not a script file" << endl;
115       exit(-1); 
116     }
117     scripted = true;
118   } else if (argc != 3) {
119     cout << endl
120          << "  You must enter the name of a registered aircraft and reset point:"
121          << endl << endl << "  FDM <aircraft name> <reset file>" << endl;
122     cout << endl << "  Alternatively, you may specify only the name of a script file:"
123          << endl << endl << "  FDM <script file>" << endl << endl;
124     exit(0);
125   }
126
127   FDMExec = new FGFDMExec();
128
129   if (scripted) { // form jsbsim <scriptfile>
130     result = FDMExec->LoadScript(argv[1]);
131     if (!result) {
132       cerr << "Script file " << argv[1] << " was not successfully loaded" << endl;
133       exit(-1);
134     }
135   } else {        // form jsbsim <acname> <resetfile>
136     if ( ! FDMExec->LoadModel("aircraft", "engine", string(argv[1]))) {
137         cerr << "  JSBSim could not be started" << endl << endl;
138       exit(-1);
139     }                   
140
141     FGInitialCondition IC(FDMExec);
142     if ( ! IC.Load("aircraft",string(argv[1]),string(argv[2]))) {
143         cerr << "Initialization unsuccessful" << endl;
144       exit(-1);
145     }
146   }
147
148 //
149 // RUN loop. MESSAGES are read inside the Run() loop and output as necessary.
150 //
151
152   FGJSBBase::Message* msg;
153   while (FDMExec->Run()) {
154     while (FDMExec->ReadMessage()) {
155       msg = FDMExec->ProcessMessage();
156       switch (msg->type) {
157       case FGJSBBase::Message::eText:
158         cout << msg->messageId << ": " << msg->text << endl;
159         break;
160       case FGJSBBase::Message::eBool:
161         cout << msg->messageId << ": " << msg->text << " " << msg->bVal << endl;
162         break;
163       case FGJSBBase::Message::eInteger:
164         cout << msg->messageId << ": " << msg->text << " " << msg->iVal << endl;
165         break;
166       case FGJSBBase::Message::eDouble:
167         cout << msg->messageId << ": " << msg->text << " " << msg->dVal << endl;
168         break;
169       default:
170         cerr << "Unrecognized message type." << endl;
171               break;
172       }
173     }
174   }
175
176   delete FDMExec;
177
178   return 0;
179 }