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__) && (_COMPILER_VERSION < 740)
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 "FGPropagate.h"
68 #include "FGAuxiliary.h"
70 #include "FGConfigFile.h"
71 #include "FGInitialCondition.h"
72 #include "FGPropertyManager.h"
76 static const char *IdSrc = "$Id$";
77 static const char *IdHdr = ID_FDMEXEC;
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 unsigned int FGFDMExec::FDMctr = 0;
84 FGPropertyManager* FGFDMExec::master=0;
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90 void checkTied ( FGPropertyManager *node )
92 int N = node->nChildren();
95 for (int i=0; i<N; i++) {
96 if (node->getChild(i)->nChildren() ) {
97 checkTied( (FGPropertyManager*)node->getChild(i) );
98 } else if ( node->getChild(i)->isTied() ) {
99 name = ((FGPropertyManager*)node->getChild(i))->GetFullyQualifiedName();
100 cerr << name << " is tied" << endl;
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 FGFDMExec::FGFDMExec(FGPropertyManager* root)
138 char* num = getenv("JSBSIM_DEBUG");
139 if (num) debug_lvl = atoi(num); // set debug level
140 } catch (...) { // if error set to 1
144 if (root == 0) master= new FGPropertyManager;
147 instance = master->GetNode("/fdm/jsbsim",IdFDM,true);
152 // this is here to catch errors in binding member functions
153 // to the property tree.
156 } catch ( string msg ) {
157 cout << "Caught error: " << msg << endl;
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 FGFDMExec::~FGFDMExec()
168 checkTied( instance );
169 } catch ( string msg ) {
170 cout << "Caught error: " << msg << endl;
173 for (unsigned int i=1; i<SlaveFDMList.size(); i++) delete SlaveFDMList[i]->exec;
174 SlaveFDMList.clear();
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 bool FGFDMExec::Allocate(void)
185 Atmosphere = new FGAtmosphere(this);
186 FCS = new FGFCS(this);
187 Propulsion = new FGPropulsion(this);
188 MassBalance = new FGMassBalance(this);
189 Aerodynamics = new FGAerodynamics (this);
190 Inertial = new FGInertial(this);
191 GroundReactions = new FGGroundReactions(this);
192 Aircraft = new FGAircraft(this);
193 Propagate = new FGPropagate(this);
194 Auxiliary = new FGAuxiliary(this);
195 Output = new FGOutput(this);
197 State = new FGState(this); // This must be done here, as the FGState
198 // class needs valid pointers to the above
201 // Initialize models so they can communicate with each other
203 if (!Atmosphere->InitModel()) {
204 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
206 if (!FCS->InitModel()) {
207 cerr << fgred << "FCS model init failed" << fgdef << endl;
209 if (!Propulsion->InitModel()) {
210 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
212 if (!MassBalance->InitModel()) {
213 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
215 if (!Aerodynamics->InitModel()) {
216 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
218 if (!Inertial->InitModel()) {
219 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
221 if (!GroundReactions->InitModel()) {
222 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
224 if (!Aircraft->InitModel()) {
225 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
227 if (!Propagate->InitModel()) {
228 cerr << fgred << "Propagate model init failed" << fgdef << endl;
230 if (!Auxiliary->InitModel()) {
231 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
233 if (!Output->InitModel()) {
234 cerr << fgred << "Output model init failed" << fgdef << endl;
237 if (Error > 0) result = false;
239 IC = new FGInitialCondition(this);
241 // Schedule a model. The second arg (the integer) is the pass number. For
242 // instance, the atmosphere model gets executed every fifth pass it is called
243 // by the executive. Everything else here gets executed each pass.
244 // IC and Trim objects are NOT scheduled.
246 Schedule(Atmosphere, 1);
248 Schedule(Propulsion, 1);
249 Schedule(MassBalance, 1);
250 Schedule(Aerodynamics, 1);
251 Schedule(Inertial, 1);
252 Schedule(GroundReactions, 1);
253 Schedule(Aircraft, 1);
254 Schedule(Propagate, 1);
255 Schedule(Auxiliary, 1);
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 bool FGFDMExec::DeAllocate(void)
273 delete GroundReactions;
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 int FGFDMExec::Schedule(FGModel* model, int rate)
307 FGModel* model_iterator;
309 model_iterator = FirstModel;
311 if (model_iterator == 0L) { // this is the first model
314 FirstModel->NextModel = 0L;
315 FirstModel->SetRate(rate);
317 } else { // subsequent model
319 while (model_iterator->NextModel != 0L) {
320 model_iterator = model_iterator->NextModel;
322 model_iterator->NextModel = model;
323 model_iterator->NextModel->SetRate(rate);
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 bool FGFDMExec::Run(void)
334 FGModel* model_iterator;
336 if (frozen) return true;
338 model_iterator = FirstModel;
339 if (model_iterator == 0L) return false;
343 for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
344 // SlaveFDMList[i]->exec->State->Initialize(); // Transfer state to the slave FDM
345 // SlaveFDMList[i]->exec->Run();
348 while (model_iterator != 0L) {
349 model_iterator->Run();
350 model_iterator = model_iterator->NextModel;
358 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 bool FGFDMExec::RunIC(void)
363 State->Initialize(IC);
370 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 vector <string> FGFDMExec::EnumerateFDMs(void)
374 vector <string> FDMList;
376 FDMList.push_back(Aircraft->GetAircraftName());
378 for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
379 FDMList.push_back(SlaveFDMList[i]->exec->GetAircraft()->GetAircraftName());
385 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 bool FGFDMExec::LoadModel(string AircraftPath, string EnginePath, string model,
391 FGFDMExec::AircraftPath = AircraftPath;
392 FGFDMExec::EnginePath = EnginePath;
394 return LoadModel(model, addModelToPath);
397 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 bool FGFDMExec::LoadModel(string model, bool addModelToPath)
405 string aircraftCfgFileName;
407 if( AircraftPath.empty() || EnginePath.empty() ) {
408 cerr << "Error: attempted to load aircraft with undefined ";
409 cerr << "aircraft and engine paths" << endl;
413 aircraftCfgFileName = AircraftPath;
415 if (addModelToPath) aircraftCfgFileName += "/" + model;
416 aircraftCfgFileName += "/" + model + ".xml";
418 if (addModelToPath) aircraftCfgFileName += ";" + model;
419 aircraftCfgFileName += ";" + model + ".xml";
422 FGConfigFile AC_cfg(aircraftCfgFileName);
423 if (!AC_cfg.IsOpen()) return false;
432 if (!ReadPrologue(&AC_cfg)) return false;
434 while ((AC_cfg.GetNextConfigLine() != string("EOF")) &&
435 (token = AC_cfg.GetValue()) != string("/FDM_CONFIG")) {
436 if (token == "METRICS") {
437 if (debug_lvl > 0) cout << fgcyan << "\n Reading Metrics" << fgdef << endl;
438 if (!ReadMetrics(&AC_cfg)) result = false;
439 } else if (token == "SLAVE") {
440 if (debug_lvl > 0) cout << fgcyan << "\n Reading Slave flight vehicle: " << fgdef
441 << AC_cfg.GetValue("NAME") << endl;
442 if (!ReadSlave(&AC_cfg)) result = false;
443 } else if (token == "AERODYNAMICS") {
444 if (debug_lvl > 0) cout << fgcyan << "\n Reading Aerodynamics" << fgdef << endl;
445 if (!ReadAerodynamics(&AC_cfg)) result = false;
446 } else if (token == "UNDERCARRIAGE") {
447 if (debug_lvl > 0) cout << fgcyan << "\n Reading Landing Gear" << fgdef << endl;
448 if (!ReadUndercarriage(&AC_cfg)) result = false;
449 } else if (token == "PROPULSION") {
450 if (debug_lvl > 0) cout << fgcyan << "\n Reading Propulsion" << fgdef << endl;
451 if (!ReadPropulsion(&AC_cfg)) result = false;
452 } else if (token == "FLIGHT_CONTROL") {
453 if (debug_lvl > 0) cout << fgcyan << "\n Reading Flight Control" << fgdef << endl;
454 if (!ReadFlightControls(&AC_cfg)) result = false;
455 } else if (token == "AUTOPILOT") {
456 if (debug_lvl > 0) cout << fgcyan << "\n Reading Autopilot" << fgdef << endl;
457 if (!ReadFlightControls(&AC_cfg)) result = false;
458 } else if (token == "OUTPUT") {
459 if (debug_lvl > 0) cout << fgcyan << "\n Reading Output directives" << fgdef << endl;
460 if (!ReadOutput(&AC_cfg)) result = false;
469 << " FGFDMExec: Failed to load aircraft and/or engine model"
476 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
480 string token = AC_cfg->GetValue();
484 AircraftName = AC_cfg->GetValue("NAME");
485 Aircraft->SetAircraftName(AircraftName);
487 if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
488 << underoff << ": " << highint << AircraftName << normint << endl;
489 scratch = AC_cfg->GetValue("VERSION").c_str();
491 CFGVersion = AC_cfg->GetValue("VERSION");
492 Release = AC_cfg->GetValue("RELEASE");
495 cout << " Version: " << highint << CFGVersion
497 if (CFGVersion != needed_cfg_version) {
498 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
499 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
500 cerr << "Current version needed is: " << needed_cfg_version << endl;
501 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
505 if (Release == "ALPHA") {
507 system("banner ALPHA");
510 << highint << "This aircraft model is an " << fgred << Release
511 << reset << highint << " release!!!" << endl << endl << reset
512 << "This aircraft model may not even properly load, and probably"
513 << " will not fly as expected." << endl << endl
514 << fgred << highint << "Use this model for development purposes ONLY!!!"
515 << normint << reset << endl << endl;
516 } else if (Release == "BETA") {
518 system("banner BETA");
521 << highint << "This aircraft model is a " << fgred << Release
522 << reset << highint << " release!!!" << endl << endl << reset
523 << "This aircraft model probably will not fly as expected." << endl << endl
524 << fgblue << highint << "Use this model for development purposes ONLY!!!"
525 << normint << reset << endl << endl;
531 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 bool FGFDMExec::ReadSlave(FGConfigFile* AC_cfg)
535 // Add a new slaveData object to the slave FDM list
536 // Populate that slaveData element with a new FDMExec object
537 // Set the IsSlave flag for that FDMExec object
538 // Get the aircraft name
539 // set debug level to print out no additional data for slave objects
540 // Load the model given the aircraft name
541 // reset debug level to prior setting
543 int saved_debug_lvl = debug_lvl;
546 SlaveFDMList.push_back(new slaveData);
547 SlaveFDMList.back()->exec = new FGFDMExec();
548 SlaveFDMList.back()->exec->SetSlave();
550 string AircraftName = AC_cfg->GetValue("FILE");
552 debug_lvl = 0; // turn off debug output for slave vehicle
554 SlaveFDMList.back()->exec->SetAircraftPath( AircraftPath );
555 SlaveFDMList.back()->exec->SetEnginePath( EnginePath );
556 SlaveFDMList.back()->exec->LoadModel(AircraftName);
557 debug_lvl = saved_debug_lvl; // turn debug output back on for master vehicle
559 AC_cfg->GetNextConfigLine();
560 while ((token = AC_cfg->GetValue()) != string("/SLAVE")) {
562 if (token == "XLOC") { *AC_cfg >> SlaveFDMList.back()->x; }
563 else if (token == "YLOC") { *AC_cfg >> SlaveFDMList.back()->y; }
564 else if (token == "ZLOC") { *AC_cfg >> SlaveFDMList.back()->z; }
565 else if (token == "PITCH") { *AC_cfg >> SlaveFDMList.back()->pitch;}
566 else if (token == "YAW") { *AC_cfg >> SlaveFDMList.back()->yaw; }
567 else if (token == "ROLL") { *AC_cfg >> SlaveFDMList.back()->roll; }
568 else cerr << "Unknown identifier: " << token << " in slave vehicle definition" << endl;
572 cout << " X = " << SlaveFDMList.back()->x << endl;
573 cout << " Y = " << SlaveFDMList.back()->y << endl;
574 cout << " Z = " << SlaveFDMList.back()->z << endl;
575 cout << " Pitch = " << SlaveFDMList.back()->pitch << endl;
576 cout << " Yaw = " << SlaveFDMList.back()->yaw << endl;
577 cout << " Roll = " << SlaveFDMList.back()->roll << endl;
583 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
587 if (!Propulsion->Load(AC_cfg)) {
588 cerr << " Propulsion not successfully loaded" << endl;
594 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
598 if (!FCS->Load(AC_cfg)) {
599 cerr << " Flight Controls not successfully loaded" << endl;
605 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
609 if (!Aerodynamics->Load(AC_cfg)) {
610 cerr << " Aerodynamics not successfully loaded" << endl;
616 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
620 if (!GroundReactions->Load(AC_cfg)) {
621 cerr << " Ground Reactions not successfully loaded" << endl;
627 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
631 if (!Aircraft->Load(AC_cfg)) {
632 cerr << " Aircraft metrics not successfully loaded" << endl;
638 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
642 if (!Output->Load(AC_cfg)) {
643 cerr << " Output not successfully loaded" << endl;
649 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 FGPropertyManager* FGFDMExec::GetPropertyManager(void) {
655 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 FGTrim* FGFDMExec::GetTrim(void) {
659 Trim = new FGTrim(this,tNone);
663 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 // The bitmasked value choices are as follows:
665 // unset: In this case (the default) JSBSim would only print
666 // out the normally expected messages, essentially echoing
667 // the config files as they are read. If the environment
668 // variable is not set, debug_lvl is set to 1 internally
669 // 0: This requests JSBSim not to output any messages
671 // 1: This value explicity requests the normal JSBSim
673 // 2: This value asks for a message to be printed out when
674 // a class is instantiated
675 // 4: When this value is set, a message is displayed when a
676 // FGModel object executes its Run() method
677 // 8: When this value is set, various runtime state variables
678 // are printed out periodically
679 // 16: When set various parameters are sanity checked and
680 // a message is printed out when they go out of bounds
682 void FGFDMExec::Debug(int from)
684 if (debug_lvl <= 0) return;
686 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
687 if (from == 0) { // Constructor
688 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
689 << JSBSim_version << underoff << normint << endl;
690 cout << halfint << " [cfg file spec v" << needed_cfg_version << "]\n\n";
691 cout << normint << "JSBSim startup beginning ...\n\n";
692 } else if (from == 3) {
693 cout << "\n\nJSBSim startup complete\n\n";
696 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
697 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
698 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
700 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
702 cout << "================== Frame: " << Frame << " Time: "
703 << State->Getsim_time() << endl;
706 if (debug_lvl & 8 ) { // Runtime state variables
708 if (debug_lvl & 16) { // Sanity checking
710 if (debug_lvl & 64) {
711 if (from == 0) { // Constructor
712 cout << IdSrc << endl;
713 cout << IdHdr << endl;