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"
74 #include "FGPropertyManager.h"
76 static const char *IdSrc = "$Id$";
77 static const char *IdHdr = ID_FDMEXEC;
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 unsigned int FGFDMExec::FDMctr = 0;
85 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
91 FGFDMExec::FGFDMExec(FGPropertyManager* root)
120 char* num = getenv("JSBSIM_DEBUG");
121 if (!num) debug_lvl = 1;
122 else debug_lvl = atoi(num); // set debug level
123 } catch (...) { // if error set to 1
129 master= new FGPropertyManager;
132 instance = master->GetNode("/fdm/jsbsim",IdFDM,true);
133 instance->SetDouble("zero",0);
137 // this is here to catch errors in binding member functions
138 // to the property tree.
141 } catch ( string msg ) {
142 cout << "Caught error: " << msg << endl;
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 FGFDMExec::~FGFDMExec()
153 } catch ( string msg ) {
154 cout << "Caught error: " << msg << endl;
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 bool FGFDMExec::Allocate(void)
166 Atmosphere = new FGAtmosphere(this);
167 FCS = new FGFCS(this);
168 Propulsion = new FGPropulsion(this);
169 MassBalance = new FGMassBalance(this);
170 Aerodynamics = new FGAerodynamics (this);
171 Inertial = new FGInertial(this);
172 GroundReactions = new FGGroundReactions(this);
173 Aircraft = new FGAircraft(this);
174 Translation = new FGTranslation(this);
175 Rotation = new FGRotation(this);
176 Position = new FGPosition(this);
177 Auxiliary = new FGAuxiliary(this);
178 Output = new FGOutput(this);
180 State = new FGState(this); // This must be done here, as the FGState
181 // class needs valid pointers to the above
184 // Initialize models so they can communicate with each other
186 if (!Atmosphere->InitModel()) {
187 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
189 if (!FCS->InitModel()) {
190 cerr << fgred << "FCS model init failed" << fgdef << endl;
192 if (!Propulsion->InitModel()) {
193 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
195 if (!MassBalance->InitModel()) {
196 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
198 if (!Aerodynamics->InitModel()) {
199 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
201 if (!Inertial->InitModel()) {
202 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
204 if (!GroundReactions->InitModel()) {
205 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
207 if (!Aircraft->InitModel()) {
208 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
210 if (!Translation->InitModel()){
211 cerr << fgred << "Translation model init failed" << fgdef << endl;
213 if (!Rotation->InitModel()) {
214 cerr << fgred << "Rotation model init failed" << fgdef << endl;
216 if (!Position->InitModel()) {
217 cerr << fgred << "Position model init failed" << fgdef << endl;
219 if (!Auxiliary->InitModel()) {
220 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
222 if (!Output->InitModel()) {
223 cerr << fgred << "Output model init failed" << fgdef << endl;
226 if (Error > 0) result = false;
228 // Schedule a model. The second arg (the integer) is the pass number. For
229 // instance, the atmosphere model gets executed every fifth pass it is called
230 // by the executive. Everything else here gets executed each pass.
232 Schedule(Atmosphere, 1);
234 Schedule(Propulsion, 1);
235 Schedule(MassBalance, 1);
236 Schedule(Aerodynamics, 1);
237 Schedule(Inertial, 1);
238 Schedule(GroundReactions, 1);
239 Schedule(Aircraft, 1);
240 Schedule(Rotation, 1);
241 Schedule(Translation, 1);
242 Schedule(Position, 1);
243 Schedule(Auxiliary, 1);
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 bool FGFDMExec::DeAllocate(void) {
255 if ( Atmosphere != 0 ) delete Atmosphere;
256 if ( FCS != 0 ) delete FCS;
257 if ( Propulsion != 0) delete Propulsion;
258 if ( MassBalance != 0) delete MassBalance;
259 if ( Aerodynamics != 0) delete Aerodynamics;
260 if ( Inertial != 0) delete Inertial;
261 if ( GroundReactions != 0) delete GroundReactions;
262 if ( Aircraft != 0 ) delete Aircraft;
263 if ( Translation != 0 ) delete Translation;
264 if ( Rotation != 0 ) delete Rotation;
265 if ( Position != 0 ) delete Position;
266 if ( Auxiliary != 0 ) delete Auxiliary;
267 if ( Output != 0 ) delete Output;
268 if ( State != 0 ) delete State;
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 int FGFDMExec::Schedule(FGModel* model, int rate)
296 FGModel* model_iterator;
298 model_iterator = FirstModel;
300 if (model_iterator == 0L) { // this is the first model
303 FirstModel->NextModel = 0L;
304 FirstModel->SetRate(rate);
306 } else { // subsequent model
308 while (model_iterator->NextModel != 0L) {
309 model_iterator = model_iterator->NextModel;
311 model_iterator->NextModel = model;
312 model_iterator->NextModel->SetRate(rate);
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 bool FGFDMExec::Run(void)
323 FGModel* model_iterator;
325 if (frozen) return true;
327 model_iterator = FirstModel;
328 if (model_iterator == 0L) return false;
332 while (!model_iterator->Run()) {
333 model_iterator = model_iterator->NextModel;
334 if (model_iterator == 0L) break;
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
348 State->Initialize(fgic);
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
360 string aircraftCfgFileName;
362 AircraftPath = APath;
366 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
368 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
371 FGConfigFile AC_cfg(aircraftCfgFileName);
372 if (!AC_cfg.IsOpen()) return false;
379 if (!ReadPrologue(&AC_cfg)) return false;
381 while ((AC_cfg.GetNextConfigLine() != string("EOF")) &&
382 (token = AC_cfg.GetValue()) != string("/FDM_CONFIG")) {
383 if (token == "METRICS") {
384 if (debug_lvl > 0) cout << fgcyan << "\n Reading Metrics" << fgdef << endl;
385 if (!ReadMetrics(&AC_cfg)) result = false;
386 } else if (token == "AERODYNAMICS") {
387 if (debug_lvl > 0) cout << fgcyan << "\n Reading Aerodynamics" << fgdef << endl;
388 if (!ReadAerodynamics(&AC_cfg)) result = false;
389 } else if (token == "UNDERCARRIAGE") {
390 if (debug_lvl > 0) cout << fgcyan << "\n Reading Landing Gear" << fgdef << endl;
391 if (!ReadUndercarriage(&AC_cfg)) result = false;
392 } else if (token == "PROPULSION") {
393 if (debug_lvl > 0) cout << fgcyan << "\n Reading Propulsion" << fgdef << endl;
394 if (!ReadPropulsion(&AC_cfg)) result = false;
395 } else if (token == "FLIGHT_CONTROL") {
396 if (debug_lvl > 0) cout << fgcyan << "\n Reading Flight Control" << fgdef << endl;
397 if (!ReadFlightControls(&AC_cfg)) result = false;
398 } else if (token == "OUTPUT") {
399 if (debug_lvl > 0) cout << fgcyan << "\n Reading Output directives" << fgdef << endl;
400 if (!ReadOutput(&AC_cfg)) result = false;
409 << " FGFDMExec: Failed to load aircraft and/or engine model"
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
420 string token = AC_cfg->GetValue();
424 AircraftName = AC_cfg->GetValue("NAME");
425 Aircraft->SetAircraftName(AircraftName);
427 if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
428 << underoff << ": " << highint << AircraftName << normint << endl;
429 scratch = AC_cfg->GetValue("VERSION").c_str();
431 CFGVersion = AC_cfg->GetValue("VERSION");
434 cout << " Version: " << highint << CFGVersion
436 if (CFGVersion != needed_cfg_version) {
437 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
438 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
439 cerr << "Current version needed is: " << needed_cfg_version << endl;
440 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
450 if (!Propulsion->Load(AC_cfg)) {
451 cerr << " Propulsion not successfully loaded" << endl;
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
461 if (!FCS->Load(AC_cfg)) {
462 cerr << " Flight Controls not successfully loaded" << endl;
468 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
472 if (!Aerodynamics->Load(AC_cfg)) {
473 cerr << " Aerodynamics not successfully loaded" << endl;
479 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
483 if (!GroundReactions->Load(AC_cfg)) {
484 cerr << " Ground Reactions not successfully loaded" << endl;
490 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
494 if (!Aircraft->Load(AC_cfg)) {
495 cerr << " Aircraft metrics not successfully loaded" << endl;
501 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
505 if (!Output->Load(AC_cfg)) {
506 cerr << " Output not successfully loaded" << endl;
512 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 FGPropertyManager* FGFDMExec::GetPropertyManager(void) {
518 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 // The bitmasked value choices are as follows:
520 // unset: In this case (the default) JSBSim would only print
521 // out the normally expected messages, essentially echoing
522 // the config files as they are read. If the environment
523 // variable is not set, debug_lvl is set to 1 internally
524 // 0: This requests JSBSim not to output any messages
526 // 1: This value explicity requests the normal JSBSim
528 // 2: This value asks for a message to be printed out when
529 // a class is instantiated
530 // 4: When this value is set, a message is displayed when a
531 // FGModel object executes its Run() method
532 // 8: When this value is set, various runtime state variables
533 // are printed out periodically
534 // 16: When set various parameters are sanity checked and
535 // a message is printed out when they go out of bounds
537 void FGFDMExec::Debug(int from)
539 if (debug_lvl <= 0) return;
541 if (debug_lvl & 1) { // Standard console startup message output
542 if (from == 0) { // Constructor
543 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
544 << JSBSim_version << underoff << normint << endl;
545 cout << halfint << " [cfg file spec v" << needed_cfg_version << "]\n\n";
546 cout << normint << "JSBSim startup beginning ...\n\n";
547 } else if (from == 3) {
548 cout << "\n\nJSBSim startup complete\n\n";
551 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
552 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
553 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
555 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
557 cout << "================== Frame: " << Frame << " Time: "
558 << State->Getsim_time() << endl;
561 if (debug_lvl & 8 ) { // Runtime state variables
563 if (debug_lvl & 16) { // Sanity checking
565 if (debug_lvl & 64) {
566 if (from == 0) { // Constructor
567 cout << IdSrc << endl;
568 cout << IdHdr << endl;