]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/JSBSim.cpp
Syncing with latest JSBSim code with retractable landing gear support.
[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 #  ifdef SG_HAVE_STD_INCLUDES
58 #    include <ctime>
59 #  else
60 #    include <time.h>
61 #  endif
62 #else
63 #  if defined(sgi) && !defined(__GNUC__)
64 #    include <iostream.h>
65 #    include <time.h>
66 #  else
67 #    include <iostream>
68 #    include <ctime>
69 #  endif
70 #endif
71
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 DEFINITIONS
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75
76 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 GLOBAL DATA
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
79
80 static const char *IdSrc = "$Id$";
81
82 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 DOCUMENTATION
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89
90 /** Standalone JSBSim main program
91     This is the wrapper program used to instantiate the JSBSim system and control
92     it. Use this program to build a version of JSBSim that can be run from the
93     command line. To get any use out of this, you will have to create a script
94     to run a test case and specify what kind of output you would like.
95     @author Jon S. Berndt
96     @version $Id$
97     @see -
98 */
99
100 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 IMPLEMENTATION
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
103
104 int main(int argc, char** argv)
105 {
106   FGFDMExec* FDMExec;
107   float cmd = 0.0;
108   bool result = false;
109   bool scripted = false;
110
111   if (argc == 2) {
112     FGConfigFile testFile(argv[1]);
113
114     if (!testFile.IsOpen()) {
115       cout << "Script file not opened" << endl;
116       exit(-1); 
117     }
118
119     testFile.GetNextConfigLine();
120     if (testFile.GetValue("runscript").length() <= 0) {
121       cout << "File: " << argv[1] << " is not a script file" << endl;
122       exit(-1); 
123     }
124     scripted = true;
125   } else if (argc != 3) {
126     cout << endl
127          << "  You must enter the name of a registered aircraft and reset point:"
128          << endl << endl << "  FDM <aircraft name> <reset file>" << endl;
129     cout << endl << "  Alternatively, you may specify only the name of a script file:"
130          << endl << endl << "  FDM <script file>" << endl << endl;
131     exit(0);
132   }
133
134   FDMExec = new FGFDMExec();
135
136   if (scripted) { // form jsbsim <scriptfile>
137     result = FDMExec->LoadScript(argv[1]);
138     if (!result) {
139       cerr << "Script file " << argv[1] << " was not successfully loaded" << endl;
140       exit(-1);
141     }
142   } else {        // form jsbsim <acname> <resetfile>
143     if ( ! FDMExec->LoadModel("aircraft", "engine", string(argv[1]))) {
144         cerr << "  JSBSim could not be started" << endl << endl;
145       exit(-1);
146     }                   
147
148     FGInitialCondition IC(FDMExec);
149     if ( ! IC.Load("aircraft",string(argv[1]),string(argv[2]))) {
150         cerr << "Initialization unsuccessful" << endl;
151       exit(-1);
152     }
153   }
154
155   FGJSBBase::Message* msg;
156   while (FDMExec->Run()) {
157     while (FDMExec->ReadMessage()) {
158       msg = FDMExec->ProcessMessage();
159       switch (msg->type) {
160       case FGJSBBase::Message::eText:
161         cout << msg->messageId << ": " << msg->text << endl;
162         break;
163       case FGJSBBase::Message::eBool:
164         cout << msg->messageId << ": " << msg->text << " " << msg->bVal << endl;
165         break;
166       case FGJSBBase::Message::eInteger:
167         cout << msg->messageId << ": " << msg->text << " " << msg->iVal << endl;
168         break;
169       case FGJSBBase::Message::eDouble:
170         cout << msg->messageId << ": " << msg->text << " " << msg->dVal << endl;
171         break;
172       default:
173         cerr << "Unrecognized message type." << endl;
174               break;
175       }
176     }
177   }
178
179   delete FDMExec;
180
181   return 0;
182 }