1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Schedules and runs the model routines.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class wraps up the simulation scheduling routines.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 # include <simgear/compiler.h>
46 # include STL_IOSTREAM
47 # include STL_ITERATOR
49 # if defined(sgi) && !defined(__GNUC__)
50 # include <iostream.h>
57 #include "FGFDMExec.h"
59 #include "FGAtmosphere.h"
61 #include "FGPropulsion.h"
62 #include "FGMassBalance.h"
63 #include "FGGroundReactions.h"
64 #include "FGAerodynamics.h"
65 #include "FGInertial.h"
66 #include "FGAircraft.h"
67 #include "FGTranslation.h"
68 #include "FGRotation.h"
69 #include "FGPosition.h"
70 #include "FGAuxiliary.h"
72 #include "FGConfigFile.h"
73 #include "FGInitialCondition.h"
75 static const char *IdSrc = "$Id$";
76 static const char *IdHdr = ID_FDMEXEC;
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82 unsigned int FGFDMExec::FDMctr = 0;
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90 FGFDMExec::FGFDMExec(void)
118 char* num = getenv("JSBSIM_DEBUG");
119 if (!num) debug_lvl = 1;
120 else debug_lvl = atoi(num); // set debug level
121 } catch (...) { // if error set to 1
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 FGFDMExec::~FGFDMExec()
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 bool FGFDMExec::Allocate(void)
145 Atmosphere = new FGAtmosphere(this);
146 FCS = new FGFCS(this);
147 Propulsion = new FGPropulsion(this);
148 MassBalance = new FGMassBalance(this);
149 Aerodynamics = new FGAerodynamics (this);
150 Inertial = new FGInertial(this);
151 GroundReactions = new FGGroundReactions(this);
152 Aircraft = new FGAircraft(this);
153 Translation = new FGTranslation(this);
154 Rotation = new FGRotation(this);
155 Position = new FGPosition(this);
156 Auxiliary = new FGAuxiliary(this);
157 Output = new FGOutput(this);
159 State = new FGState(this); // This must be done here, as the FGState
160 // class needs valid pointers to the above
163 // Initialize models so they can communicate with each other
165 if (!Atmosphere->InitModel()) {
166 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
168 if (!FCS->InitModel()) {
169 cerr << fgred << "FCS model init failed" << fgdef << endl;
171 if (!Propulsion->InitModel()) {
172 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
174 if (!MassBalance->InitModel()) {
175 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
177 if (!Aerodynamics->InitModel()) {
178 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
180 if (!Inertial->InitModel()) {
181 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
183 if (!GroundReactions->InitModel()) {
184 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
186 if (!Aircraft->InitModel()) {
187 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
189 if (!Translation->InitModel()){
190 cerr << fgred << "Translation model init failed" << fgdef << endl;
192 if (!Rotation->InitModel()) {
193 cerr << fgred << "Rotation model init failed" << fgdef << endl;
195 if (!Position->InitModel()) {
196 cerr << fgred << "Position model init failed" << fgdef << endl;
198 if (!Auxiliary->InitModel()) {
199 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
201 if (!Output->InitModel()) {
202 cerr << fgred << "Output model init failed" << fgdef << endl;
205 if (Error > 0) result = false;
207 // Schedule a model. The second arg (the integer) is the pass number. For
208 // instance, the atmosphere model gets executed every fifth pass it is called
209 // by the executive. Everything else here gets executed each pass.
211 Schedule(Atmosphere, 1);
213 Schedule(Propulsion, 1);
214 Schedule(MassBalance, 1);
215 Schedule(Aerodynamics, 1);
216 Schedule(Inertial, 1);
217 Schedule(GroundReactions, 1);
218 Schedule(Aircraft, 1);
219 Schedule(Rotation, 1);
220 Schedule(Translation, 1);
221 Schedule(Position, 1);
222 Schedule(Auxiliary, 1);
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 bool FGFDMExec::DeAllocate(void) {
234 if ( Atmosphere != 0 ) delete Atmosphere;
235 if ( FCS != 0 ) delete FCS;
236 if ( Propulsion != 0) delete Propulsion;
237 if ( MassBalance != 0) delete MassBalance;
238 if ( Aerodynamics != 0) delete Aerodynamics;
239 if ( Inertial != 0) delete Inertial;
240 if ( GroundReactions != 0) delete GroundReactions;
241 if ( Aircraft != 0 ) delete Aircraft;
242 if ( Translation != 0 ) delete Translation;
243 if ( Rotation != 0 ) delete Rotation;
244 if ( Position != 0 ) delete Position;
245 if ( Auxiliary != 0 ) delete Auxiliary;
246 if ( Output != 0 ) delete Output;
247 if ( State != 0 ) delete State;
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 int FGFDMExec::Schedule(FGModel* model, int rate)
275 FGModel* model_iterator;
277 model_iterator = FirstModel;
279 if (model_iterator == 0L) { // this is the first model
282 FirstModel->NextModel = 0L;
283 FirstModel->SetRate(rate);
285 } else { // subsequent model
287 while (model_iterator->NextModel != 0L) {
288 model_iterator = model_iterator->NextModel;
290 model_iterator->NextModel = model;
291 model_iterator->NextModel->SetRate(rate);
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 bool FGFDMExec::Run(void)
302 FGModel* model_iterator;
304 if (frozen) return true;
306 model_iterator = FirstModel;
307 if (model_iterator == 0L) return false;
311 while (!model_iterator->Run()) {
312 model_iterator = model_iterator->NextModel;
313 if (model_iterator == 0L) break;
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
327 State->Initialize(fgic);
333 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
339 string aircraftCfgFileName;
341 AircraftPath = APath;
345 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
347 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
350 FGConfigFile AC_cfg(aircraftCfgFileName);
351 if (!AC_cfg.IsOpen()) return false;
358 if (!ReadPrologue(&AC_cfg)) return false;
360 while ((AC_cfg.GetNextConfigLine() != string("EOF")) &&
361 (token = AC_cfg.GetValue()) != string("/FDM_CONFIG")) {
362 if (token == "METRICS") {
363 if (debug_lvl > 0) cout << fgcyan << "\n Reading Metrics" << fgdef << endl;
364 if (!ReadMetrics(&AC_cfg)) result = false;
365 } else if (token == "AERODYNAMICS") {
366 if (debug_lvl > 0) cout << fgcyan << "\n Reading Aerodynamics" << fgdef << endl;
367 if (!ReadAerodynamics(&AC_cfg)) result = false;
368 } else if (token == "UNDERCARRIAGE") {
369 if (debug_lvl > 0) cout << fgcyan << "\n Reading Landing Gear" << fgdef << endl;
370 if (!ReadUndercarriage(&AC_cfg)) result = false;
371 } else if (token == "PROPULSION") {
372 if (debug_lvl > 0) cout << fgcyan << "\n Reading Propulsion" << fgdef << endl;
373 if (!ReadPropulsion(&AC_cfg)) result = false;
374 } else if (token == "FLIGHT_CONTROL") {
375 if (debug_lvl > 0) cout << fgcyan << "\n Reading Flight Control" << fgdef << endl;
376 if (!ReadFlightControls(&AC_cfg)) result = false;
377 } else if (token == "OUTPUT") {
378 if (debug_lvl > 0) cout << fgcyan << "\n Reading Output directives" << fgdef << endl;
379 if (!ReadOutput(&AC_cfg)) result = false;
388 << " FGFDMExec: Failed to load aircraft and/or engine model"
395 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
399 string token = AC_cfg->GetValue();
403 AircraftName = AC_cfg->GetValue("NAME");
404 Aircraft->SetAircraftName(AircraftName);
406 if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
407 << underoff << ": " << highint << AircraftName << normint << endl;
408 scratch = AC_cfg->GetValue("VERSION").c_str();
410 CFGVersion = AC_cfg->GetValue("VERSION");
413 cout << " Version: " << highint << CFGVersion
415 if (CFGVersion != needed_cfg_version) {
416 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
417 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
418 cerr << "Current version needed is: " << needed_cfg_version << endl;
419 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
429 if (!Propulsion->Load(AC_cfg)) {
430 cerr << " Propulsion not successfully loaded" << endl;
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
440 if (!FCS->Load(AC_cfg)) {
441 cerr << " Flight Controls not successfully loaded" << endl;
447 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
451 if (!Aerodynamics->Load(AC_cfg)) {
452 cerr << " Aerodynamics not successfully loaded" << endl;
458 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
462 if (!GroundReactions->Load(AC_cfg)) {
463 cerr << " Ground Reactions not successfully loaded" << endl;
469 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
473 if (!Aircraft->Load(AC_cfg)) {
474 cerr << " Aircraft metrics not successfully loaded" << endl;
480 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
484 if (!Output->Load(AC_cfg)) {
485 cerr << " Output not successfully loaded" << endl;
491 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 // The bitmasked value choices are as follows:
493 // unset: In this case (the default) JSBSim would only print
494 // out the normally expected messages, essentially echoing
495 // the config files as they are read. If the environment
496 // variable is not set, debug_lvl is set to 1 internally
497 // 0: This requests JSBSim not to output any messages
499 // 1: This value explicity requests the normal JSBSim
501 // 2: This value asks for a message to be printed out when
502 // a class is instantiated
503 // 4: When this value is set, a message is displayed when a
504 // FGModel object executes its Run() method
505 // 8: When this value is set, various runtime state variables
506 // are printed out periodically
507 // 16: When set various parameters are sanity checked and
508 // a message is printed out when they go out of bounds
510 void FGFDMExec::Debug(int from)
512 if (debug_lvl <= 0) return;
514 if (debug_lvl & 1) { // Standard console startup message output
515 if (from == 0) { // Constructor
516 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
517 << JSBSim_version << underoff << normint << endl;
518 cout << halfint << " [cfg file spec v" << needed_cfg_version << "]\n\n";
519 cout << normint << "JSBSim startup beginning ...\n\n";
520 } else if (from == 3) {
521 cout << "\n\nJSBSim startup complete\n\n";
524 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
525 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
526 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
528 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
530 cout << "================== Frame: " << Frame << " Time: "
531 << State->Getsim_time() << endl;
534 if (debug_lvl & 8 ) { // Runtime state variables
536 if (debug_lvl & 16) { // Sanity checking
538 if (debug_lvl & 64) {
539 if (from == 0) { // Constructor
540 cout << IdSrc << endl;
541 cout << IdHdr << endl;