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"
78 static const char *IdSrc = "$Id$";
79 static const char *IdHdr = ID_FDMEXEC;
81 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 unsigned int FGFDMExec::FDMctr = 0;
86 FGPropertyManager* FGFDMExec::master=0;
88 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92 void checkTied ( FGPropertyManager *node )
94 int N = node->nChildren();
97 for (int i=0; i<N; i++) {
98 if (node->getChild(i)->nChildren() ) {
99 checkTied( (FGPropertyManager*)node->getChild(i) );
100 } else if ( node->getChild(i)->isTied() ) {
101 name = ((FGPropertyManager*)node->getChild(i))->GetFullyQualifiedName();
102 cerr << name << " is tied" << endl;
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 FGFDMExec::FGFDMExec(FGPropertyManager* root)
142 char* num = getenv("JSBSIM_DEBUG");
143 if (!num) debug_lvl = 1;
144 else debug_lvl = atoi(num); // set debug level
145 } catch (...) { // if error set to 1
149 if (root == 0) master= new FGPropertyManager;
152 instance = master->GetNode("/fdm/jsbsim",IdFDM,true);
157 // this is here to catch errors in binding member functions
158 // to the property tree.
161 } catch ( string msg ) {
162 cout << "Caught error: " << msg << endl;
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 FGFDMExec::~FGFDMExec()
173 checkTied( instance );
174 } catch ( string msg ) {
175 cout << "Caught error: " << msg << endl;
178 for (unsigned int i=1; i<SlaveFDMList.size(); i++) delete SlaveFDMList[i]->exec;
179 SlaveFDMList.clear();
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 bool FGFDMExec::Allocate(void)
190 Atmosphere = new FGAtmosphere(this);
191 FCS = new FGFCS(this);
192 Propulsion = new FGPropulsion(this);
193 MassBalance = new FGMassBalance(this);
194 Aerodynamics = new FGAerodynamics (this);
195 Inertial = new FGInertial(this);
196 GroundReactions = new FGGroundReactions(this);
197 Aircraft = new FGAircraft(this);
198 Translation = new FGTranslation(this);
199 Rotation = new FGRotation(this);
200 Position = new FGPosition(this);
201 Auxiliary = new FGAuxiliary(this);
202 Output = new FGOutput(this);
204 State = new FGState(this); // This must be done here, as the FGState
205 // class needs valid pointers to the above
208 // Initialize models so they can communicate with each other
210 if (!Atmosphere->InitModel()) {
211 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
213 if (!FCS->InitModel()) {
214 cerr << fgred << "FCS model init failed" << fgdef << endl;
216 if (!Propulsion->InitModel()) {
217 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
219 if (!MassBalance->InitModel()) {
220 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
222 if (!Aerodynamics->InitModel()) {
223 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
225 if (!Inertial->InitModel()) {
226 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
228 if (!GroundReactions->InitModel()) {
229 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
231 if (!Aircraft->InitModel()) {
232 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
234 if (!Translation->InitModel()) {
235 cerr << fgred << "Translation model init failed" << fgdef << endl;
237 if (!Rotation->InitModel()) {
238 cerr << fgred << "Rotation model init failed" << fgdef << endl;
240 if (!Position->InitModel()) {
241 cerr << fgred << "Position model init failed" << fgdef << endl;
243 if (!Auxiliary->InitModel()) {
244 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
246 if (!Output->InitModel()) {
247 cerr << fgred << "Output model init failed" << fgdef << endl;
250 if (Error > 0) result = false;
252 IC = new FGInitialCondition(this);
254 // Schedule a model. The second arg (the integer) is the pass number. For
255 // instance, the atmosphere model gets executed every fifth pass it is called
256 // by the executive. Everything else here gets executed each pass.
257 // IC and Trim objects are NOT scheduled.
259 Schedule(Atmosphere, 1);
261 Schedule(Propulsion, 1);
262 Schedule(MassBalance, 1);
263 Schedule(Aerodynamics, 1);
264 Schedule(Inertial, 1);
265 Schedule(GroundReactions, 1);
266 Schedule(Aircraft, 1);
267 Schedule(Rotation, 1);
268 Schedule(Translation, 1);
269 Schedule(Position, 1);
270 Schedule(Auxiliary, 1);
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 bool FGFDMExec::DeAllocate(void)
288 delete GroundReactions;
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 int FGFDMExec::Schedule(FGModel* model, int rate)
326 FGModel* model_iterator;
328 model_iterator = FirstModel;
330 if (model_iterator == 0L) { // this is the first model
333 FirstModel->NextModel = 0L;
334 FirstModel->SetRate(rate);
336 } else { // subsequent model
338 while (model_iterator->NextModel != 0L) {
339 model_iterator = model_iterator->NextModel;
341 model_iterator->NextModel = model;
342 model_iterator->NextModel->SetRate(rate);
349 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 bool FGFDMExec::Run(void)
353 FGModel* model_iterator;
355 if (frozen) return true;
357 model_iterator = FirstModel;
358 if (model_iterator == 0L) return false;
362 for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
363 // SlaveFDMList[i]->exec->State->Initialize(); // Transfer state to the slave FDM
364 // SlaveFDMList[i]->exec->Run();
367 while (model_iterator != 0L) {
368 model_iterator->Run();
369 model_iterator = model_iterator->NextModel;
377 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 bool FGFDMExec::RunIC(void)
382 State->Initialize(IC);
389 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 vector <string> FGFDMExec::EnumerateFDMs(void)
393 vector <string> FDMList;
395 FDMList.push_back(Aircraft->GetAircraftName());
397 for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
398 FDMList.push_back(SlaveFDMList[i]->exec->GetAircraft()->GetAircraftName());
404 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 bool FGFDMExec::LoadModel(string AircraftPath, string EnginePath, string model)
408 FGFDMExec::AircraftPath = AircraftPath;
409 FGFDMExec::EnginePath = EnginePath;
411 return LoadModel(model);
414 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 bool FGFDMExec::LoadModel(string model)
420 string aircraftCfgFileName;
422 if( AircraftPath.empty() || EnginePath.empty() ) {
423 cerr << "Error: attempted to load aircraft with undefined ";
424 cerr << "aircraft and engine paths" << endl;
429 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
431 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
434 FGConfigFile AC_cfg(aircraftCfgFileName);
435 if (!AC_cfg.IsOpen()) return false;
444 if (!ReadPrologue(&AC_cfg)) return false;
446 while ((AC_cfg.GetNextConfigLine() != string("EOF")) &&
447 (token = AC_cfg.GetValue()) != string("/FDM_CONFIG")) {
448 if (token == "METRICS") {
449 if (debug_lvl > 0) cout << fgcyan << "\n Reading Metrics" << fgdef << endl;
450 if (!ReadMetrics(&AC_cfg)) result = false;
451 } else if (token == "SLAVE") {
452 if (debug_lvl > 0) cout << fgcyan << "\n Reading Slave flight vehicle: " << fgdef
453 << AC_cfg.GetValue("NAME") << endl;
454 if (!ReadSlave(&AC_cfg)) result = false;
455 } else if (token == "AERODYNAMICS") {
456 if (debug_lvl > 0) cout << fgcyan << "\n Reading Aerodynamics" << fgdef << endl;
457 if (!ReadAerodynamics(&AC_cfg)) result = false;
458 } else if (token == "UNDERCARRIAGE") {
459 if (debug_lvl > 0) cout << fgcyan << "\n Reading Landing Gear" << fgdef << endl;
460 if (!ReadUndercarriage(&AC_cfg)) result = false;
461 } else if (token == "PROPULSION") {
462 if (debug_lvl > 0) cout << fgcyan << "\n Reading Propulsion" << fgdef << endl;
463 if (!ReadPropulsion(&AC_cfg)) result = false;
464 } else if (token == "FLIGHT_CONTROL") {
465 if (debug_lvl > 0) cout << fgcyan << "\n Reading Flight Control" << fgdef << endl;
466 if (!ReadFlightControls(&AC_cfg)) result = false;
467 } else if (token == "AUTOPILOT") {
468 if (debug_lvl > 0) cout << fgcyan << "\n Reading Autopilot" << fgdef << endl;
469 if (!ReadFlightControls(&AC_cfg)) result = false;
470 } else if (token == "OUTPUT") {
471 if (debug_lvl > 0) cout << fgcyan << "\n Reading Output directives" << fgdef << endl;
472 if (!ReadOutput(&AC_cfg)) result = false;
481 << " FGFDMExec: Failed to load aircraft and/or engine model"
488 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
492 string token = AC_cfg->GetValue();
496 AircraftName = AC_cfg->GetValue("NAME");
497 Aircraft->SetAircraftName(AircraftName);
499 if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
500 << underoff << ": " << highint << AircraftName << normint << endl;
501 scratch = AC_cfg->GetValue("VERSION").c_str();
503 CFGVersion = AC_cfg->GetValue("VERSION");
506 cout << " Version: " << highint << CFGVersion
508 if (CFGVersion != needed_cfg_version) {
509 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
510 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
511 cerr << "Current version needed is: " << needed_cfg_version << endl;
512 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
519 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 bool FGFDMExec::ReadSlave(FGConfigFile* AC_cfg)
523 // Add a new slaveData object to the slave FDM list
524 // Populate that slaveData element with a new FDMExec object
525 // Set the IsSlave flag for that FDMExec object
526 // Get the aircraft name
527 // set debug level to print out no additional data for slave objects
528 // Load the model given the aircraft name
529 // reset debug level to prior setting
531 int saved_debug_lvl = debug_lvl;
534 SlaveFDMList.push_back(new slaveData);
535 SlaveFDMList.back()->exec = new FGFDMExec();
536 SlaveFDMList.back()->exec->SetSlave();
538 string AircraftName = AC_cfg->GetValue("FILE");
540 debug_lvl = 0; // turn off debug output for slave vehicle
542 SlaveFDMList.back()->exec->SetAircraftPath( AircraftPath );
543 SlaveFDMList.back()->exec->SetEnginePath( EnginePath );
544 SlaveFDMList.back()->exec->LoadModel(AircraftName);
545 debug_lvl = saved_debug_lvl; // turn debug output back on for master vehicle
547 AC_cfg->GetNextConfigLine();
548 while ((token = AC_cfg->GetValue()) != string("/SLAVE")) {
550 if (token == "XLOC") { *AC_cfg >> SlaveFDMList.back()->x; }
551 else if (token == "YLOC") { *AC_cfg >> SlaveFDMList.back()->y; }
552 else if (token == "ZLOC") { *AC_cfg >> SlaveFDMList.back()->z; }
553 else if (token == "PITCH") { *AC_cfg >> SlaveFDMList.back()->pitch;}
554 else if (token == "YAW") { *AC_cfg >> SlaveFDMList.back()->yaw; }
555 else if (token == "ROLL") { *AC_cfg >> SlaveFDMList.back()->roll; }
556 else cerr << "Unknown identifier: " << token << " in slave vehicle definition" << endl;
560 cout << " X = " << SlaveFDMList.back()->x << endl;
561 cout << " Y = " << SlaveFDMList.back()->y << endl;
562 cout << " Z = " << SlaveFDMList.back()->z << endl;
563 cout << " Pitch = " << SlaveFDMList.back()->pitch << endl;
564 cout << " Yaw = " << SlaveFDMList.back()->yaw << endl;
565 cout << " Roll = " << SlaveFDMList.back()->roll << endl;
571 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
575 if (!Propulsion->Load(AC_cfg)) {
576 cerr << " Propulsion not successfully loaded" << endl;
582 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
586 if (!FCS->Load(AC_cfg)) {
587 cerr << " Flight Controls not successfully loaded" << endl;
593 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
597 if (!Aerodynamics->Load(AC_cfg)) {
598 cerr << " Aerodynamics not successfully loaded" << endl;
604 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
608 if (!GroundReactions->Load(AC_cfg)) {
609 cerr << " Ground Reactions not successfully loaded" << endl;
615 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
619 if (!Aircraft->Load(AC_cfg)) {
620 cerr << " Aircraft metrics not successfully loaded" << endl;
626 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
630 if (!Output->Load(AC_cfg)) {
631 cerr << " Output not successfully loaded" << endl;
637 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 FGPropertyManager* FGFDMExec::GetPropertyManager(void) {
643 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 FGTrim* FGFDMExec::GetTrim(void) {
647 Trim = new FGTrim(this,tNone);
651 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 // The bitmasked value choices are as follows:
653 // unset: In this case (the default) JSBSim would only print
654 // out the normally expected messages, essentially echoing
655 // the config files as they are read. If the environment
656 // variable is not set, debug_lvl is set to 1 internally
657 // 0: This requests JSBSim not to output any messages
659 // 1: This value explicity requests the normal JSBSim
661 // 2: This value asks for a message to be printed out when
662 // a class is instantiated
663 // 4: When this value is set, a message is displayed when a
664 // FGModel object executes its Run() method
665 // 8: When this value is set, various runtime state variables
666 // are printed out periodically
667 // 16: When set various parameters are sanity checked and
668 // a message is printed out when they go out of bounds
670 void FGFDMExec::Debug(int from)
672 if (debug_lvl <= 0) return;
674 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
675 if (from == 0) { // Constructor
676 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
677 << JSBSim_version << underoff << normint << endl;
678 cout << halfint << " [cfg file spec v" << needed_cfg_version << "]\n\n";
679 cout << normint << "JSBSim startup beginning ...\n\n";
680 } else if (from == 3) {
681 cout << "\n\nJSBSim startup complete\n\n";
684 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
685 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
686 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
688 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
690 cout << "================== Frame: " << Frame << " Time: "
691 << State->Getsim_time() << endl;
694 if (debug_lvl & 8 ) { // Runtime state variables
696 if (debug_lvl & 16) { // Sanity checking
698 if (debug_lvl & 64) {
699 if (from == 0) { // Constructor
700 cout << IdSrc << endl;
701 cout << IdHdr << endl;