]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
f5b920900c6f9fc20161f3cd05a727ae77a12f39
[flightgear.git] / src / FDM / JSBSim / FGFDMExec.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGFDMExec.cpp
4  Author:       Jon S. Berndt
5  Date started: 11/17/98
6  Purpose:      Schedules and runs the model routines.
7
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
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 Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser 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.
23
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 This class wraps up the simulation scheduling routines.
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 11/17/98   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES,  and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 INCLUDES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
44 #ifdef FGFS
45 #  include <simgear/compiler.h>
46 #  include STL_IOSTREAM
47 #  include STL_ITERATOR
48 #else
49 #  if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
50 #    include <iostream.h>
51 #  else
52 #    include <iostream>
53 #  endif
54 #  include <iterator>
55 #endif
56
57 #include "FGFDMExec.h"
58 #include "FGState.h"
59 #include <models/FGAtmosphere.h>
60 #include <models/atmosphere/FGMSIS.h>
61 #include <models/atmosphere/FGMars.h>
62 #include <models/FGFCS.h>
63 #include <models/FGPropulsion.h>
64 #include <models/FGMassBalance.h>
65 #include <models/FGGroundReactions.h>
66 #include <models/FGAerodynamics.h>
67 #include <models/FGInertial.h>
68 #include <models/FGAircraft.h>
69 #include <models/FGPropagate.h>
70 #include <models/FGAuxiliary.h>
71 #include <models/FGInput.h>
72 #include <models/FGOutput.h>
73 #include <initialization/FGInitialCondition.h>
74 #include <input_output/FGPropertyManager.h>
75 #include <input_output/FGScript.h>
76
77 namespace JSBSim {
78
79 static const char *IdSrc = "$Id$";
80 static const char *IdHdr = ID_FDMEXEC;
81
82 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 GLOBAL DECLARATIONS
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85
86 unsigned int FGFDMExec::FDMctr = 0;
87 FGPropertyManager* FGFDMExec::master=0;
88
89 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 CLASS IMPLEMENTATION
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92
93 void checkTied ( FGPropertyManager *node )
94 {
95   int N = node->nChildren();
96   string name;
97
98   for (int i=0; i<N; i++) {
99     if (node->getChild(i)->nChildren() ) {
100       checkTied( (FGPropertyManager*)node->getChild(i) );
101     } else if ( node->getChild(i)->isTied() ) {
102       name = ((FGPropertyManager*)node->getChild(i))->GetFullyQualifiedName();
103       cerr << name << " is tied" << endl;
104     }
105   }
106 }
107
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 // Constructor
110
111 FGFDMExec::FGFDMExec(FGPropertyManager* root) : Root(root)
112 {
113
114   Frame           = 0;
115   FirstModel      = 0;
116   Error           = 0;
117   GroundCallback  = 0;
118   State           = 0;
119   Atmosphere      = 0;
120   FCS             = 0;
121   Propulsion      = 0;
122   MassBalance     = 0;
123   Aerodynamics    = 0;
124   Inertial        = 0;
125   GroundReactions = 0;
126   Aircraft        = 0;
127   Propagate       = 0;
128   Auxiliary       = 0;
129   Input           = 0;
130   IC              = 0;
131   Trim            = 0;
132   Script          = 0;
133
134   modelLoaded = false;
135   IsSlave = false;
136   holding = false;
137
138   // Multiple FDM's are stopped for now.  We need to ensure that
139   // the "user" instance always gets the zeroeth instance number,
140   // because there may be instruments or scripts tied to properties
141   // in the jsbsim[0] node.
142   // ToDo: it could be that when JSBSim is reset and a new FDM is wanted, that
143   // process might try setting FDMctr = 0. Then the line below would not need
144   // to be commented out.
145   IdFDM = FDMctr;
146   //FDMctr++;
147
148   try {
149     char* num = getenv("JSBSIM_DEBUG");
150     if (num) debug_lvl = atoi(num); // set debug level
151   } catch (...) {               // if error set to 1
152     debug_lvl = 1;
153   }
154
155   if (Root == 0)  master= new FGPropertyManager;
156   else            master = Root;
157
158   instance = master->GetNode("/fdm/jsbsim",IdFDM,true);
159   Debug(0);
160   // this is to catch errors in binding member functions to the property tree.
161   try {
162     Allocate();
163   } catch ( string msg ) {
164     cout << "Caught error: " << msg << endl;
165     exit(1);
166   }
167
168   Constructing = true;
169   typedef int (FGFDMExec::*iPMF)(void) const;
170   instance->Tie("simulation/do_trim", this, (iPMF)0, &FGFDMExec::DoTrim);
171   Constructing = false;
172 }
173
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175
176 FGFDMExec::~FGFDMExec()
177 {
178   instance->Untie("simulation/do_trim");
179
180   try {
181     DeAllocate();
182     checkTied( instance );
183     if (Root == 0)  delete master;
184   } catch ( string msg ) {
185     cout << "Caught error: " << msg << endl;
186   }
187
188   for (unsigned int i=1; i<SlaveFDMList.size(); i++) delete SlaveFDMList[i]->exec;
189   SlaveFDMList.clear();
190
191   //ToDo remove property catalog.
192
193   Debug(1);
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 bool FGFDMExec::Allocate(void)
199 {
200   bool result=true;
201
202   Atmosphere      = new FGAtmosphere(this);
203   FCS             = new FGFCS(this);
204   Propulsion      = new FGPropulsion(this);
205   MassBalance     = new FGMassBalance(this);
206   Aerodynamics    = new FGAerodynamics (this);
207   Inertial        = new FGInertial(this);
208   GroundReactions = new FGGroundReactions(this);
209   Aircraft        = new FGAircraft(this);
210   Propagate       = new FGPropagate(this);
211   Auxiliary       = new FGAuxiliary(this);
212   Input           = new FGInput(this);
213
214   GroundCallback  = new FGGroundCallback();
215   State           = new FGState(this); // This must be done here, as the FGState
216                                        // class needs valid pointers to the above
217                                        // model classes
218
219   // Initialize models so they can communicate with each other
220
221   if (!Atmosphere->InitModel()) {
222     cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
223     Error+=1;}
224   if (!FCS->InitModel())        {
225     cerr << fgred << "FCS model init failed" << fgdef << endl;
226     Error+=2;}
227   if (!Propulsion->InitModel()) {
228     cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
229     Error+=4;}
230   if (!MassBalance->InitModel()) {
231     cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
232     Error+=8;}
233   if (!Aerodynamics->InitModel()) {
234     cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
235     Error+=16;}
236   if (!Inertial->InitModel()) {
237     cerr << fgred << "FGInertial model init failed" << fgdef << endl;
238     Error+=32;}
239   if (!GroundReactions->InitModel())   {
240     cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
241     Error+=64;}
242   if (!Aircraft->InitModel())   {
243     cerr << fgred << "Aircraft model init failed" << fgdef << endl;
244     Error+=128;}
245   if (!Propagate->InitModel())   {
246     cerr << fgred << "Propagate model init failed" << fgdef << endl;
247     Error+=256;}
248   if (!Auxiliary->InitModel())  {
249     cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
250     Error+=512;}
251   if (!Input->InitModel())  {
252     cerr << fgred << "Input model init failed" << fgdef << endl;
253     Error+=1024;}
254
255   if (Error > 0) result = false;
256
257   IC = new FGInitialCondition(this);
258
259   // Schedule a model. The second arg (the integer) is the pass number. For
260   // instance, the atmosphere model could get executed every fifth pass it is called
261   // by the executive. IC and Trim objects are NOT scheduled.
262
263   Schedule(Input,           1);
264   Schedule(Atmosphere,      1);
265   Schedule(FCS,             1);
266   Schedule(Propulsion,      1);
267   Schedule(MassBalance,     1);
268   Schedule(Aerodynamics,    1);
269   Schedule(Inertial,        1);
270   Schedule(GroundReactions, 1);
271   Schedule(Aircraft,        1);
272   Schedule(Propagate,       1);
273   Schedule(Auxiliary,       1);
274
275   modelLoaded = false;
276
277   return result;
278 }
279
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281
282 bool FGFDMExec::DeAllocate(void)
283 {
284   delete Input;
285   delete Atmosphere;
286   delete FCS;
287   delete Propulsion;
288   delete MassBalance;
289   delete Aerodynamics;
290   delete Inertial;
291   delete GroundReactions;
292   delete Aircraft;
293   delete Propagate;
294   delete Auxiliary;
295   delete State;
296   delete Script;
297
298   for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
299   Outputs.clear();
300
301   delete IC;
302   delete Trim;
303
304   delete GroundCallback;
305
306   FirstModel  = 0L;
307   Error       = 0;
308
309   State           = 0;
310   Input           = 0;
311   Atmosphere      = 0;
312   FCS             = 0;
313   Propulsion      = 0;
314   MassBalance     = 0;
315   Aerodynamics    = 0;
316   Inertial        = 0;
317   GroundReactions = 0;
318   Aircraft        = 0;
319   Propagate       = 0;
320   Auxiliary       = 0;
321
322   modelLoaded = false;
323   return modelLoaded;
324 }
325
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327
328 int FGFDMExec::Schedule(FGModel* model, int rate)
329 {
330   FGModel* model_iterator;
331
332   model_iterator = FirstModel;
333
334   if (model_iterator == 0L) {                  // this is the first model
335
336     FirstModel = model;
337     FirstModel->NextModel = 0L;
338     FirstModel->SetRate(rate);
339
340   } else {                                     // subsequent model
341
342     while (model_iterator->NextModel != 0L) {
343       model_iterator = model_iterator->NextModel;
344     }
345     model_iterator->NextModel = model;
346     model_iterator->NextModel->SetRate(rate);
347
348   }
349
350   return 0;
351 }
352
353 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354
355 bool FGFDMExec::Run(void)
356 {
357   bool success;
358   FGModel* model_iterator;
359
360   model_iterator = FirstModel;
361   if (model_iterator == 0L) return false;
362
363   Debug(2);
364
365   for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
366 //    SlaveFDMList[i]->exec->State->Initialize(); // Transfer state to the slave FDM
367 //    SlaveFDMList[i]->exec->Run();
368   }
369
370   if (Script != 0) success = Script->RunScript(); // returns true if success
371                                                   // false if complete
372   while (model_iterator != 0L) {
373     model_iterator->Run();
374     model_iterator = model_iterator->NextModel;
375   }
376
377   frame = Frame++;
378   if (!Holding()) State->IncrTime();
379   return (success);
380 }
381
382 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 // This call will cause the sim time to reset to 0.0
384
385 bool FGFDMExec::RunIC(void)
386 {
387   State->SuspendIntegration();
388   State->Initialize(IC);
389   Run();
390   State->ResumeIntegration();
391
392   return true;
393 }
394
395 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396
397 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
398 {
399   delete GroundCallback;
400   GroundCallback = p;
401 }
402
403 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404
405 double FGFDMExec::GetSimTime(void)
406 {
407   return (State->Getsim_time());
408 }
409
410 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411
412 double FGFDMExec::GetDeltaT(void)
413 {
414   return (State->Getdt());
415 }
416
417 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418
419 vector <string> FGFDMExec::EnumerateFDMs(void)
420 {
421   vector <string> FDMList;
422
423   FDMList.push_back(Aircraft->GetAircraftName());
424
425   for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
426     FDMList.push_back(SlaveFDMList[i]->exec->GetAircraft()->GetAircraftName());
427   }
428
429   return FDMList;
430 }
431
432 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433
434 bool FGFDMExec::LoadScript(string script)
435 {
436   bool result;
437
438   Script = new FGScript(this);
439   result = Script->LoadScript(script);
440
441   return result;
442 }
443
444 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445
446 bool FGFDMExec::LoadModel(string AircraftPath, string EnginePath, string model,
447                 bool addModelToPath)
448 {
449   FGFDMExec::AircraftPath = AircraftPath;
450   FGFDMExec::EnginePath = EnginePath;
451
452   return LoadModel(model, addModelToPath);
453 }
454
455 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456
457 bool FGFDMExec::LoadModel(string model, bool addModelToPath)
458 {
459   string token;
460   string aircraftCfgFileName;
461   string separator = "/";
462
463 # ifdef macintosh
464     separator = ";";
465 # endif
466
467   if( AircraftPath.empty() || EnginePath.empty() ) {
468     cerr << "Error: attempted to load aircraft with undefined ";
469     cerr << "aircraft and engine paths" << endl;
470     return false;
471   }
472
473   FullAircraftPath = AircraftPath;
474   if (addModelToPath) FullAircraftPath += separator + model;
475   aircraftCfgFileName = FullAircraftPath + separator + model + ".xml";
476
477   FGXMLParse *XMLParse = new FGXMLParse();
478   Element* element = 0L;
479   Element* document;
480
481   ifstream input_file(aircraftCfgFileName.c_str());
482
483   if (!input_file.is_open()) { // file open failed
484     cerr << "Could not open file " << aircraftCfgFileName.c_str() << endl;
485     return false;
486   }
487
488   readXML(input_file, *XMLParse);
489   document = XMLParse->GetDocument();
490
491   modelName = model;
492
493   if (modelLoaded) {
494     DeAllocate();
495     Allocate();
496   }
497
498   ReadPrologue(document);
499   element = document->GetElement();
500
501   bool result = true;
502   while (element && result) {
503     string element_name = element->GetName();
504     if (element_name == "fileheader" )           result = ReadFileHeader(element);
505     else if (element_name == "slave")            result = ReadSlave(element);
506     else if (element_name == "metrics")          result = Aircraft->Load(element);
507     else if (element_name == "mass_balance")     result = MassBalance->Load(element);
508     else if (element_name == "ground_reactions") result = GroundReactions->Load(element);
509     else if (element_name == "propulsion")       result = Propulsion->Load(element);
510     else if (element_name == "autopilot")        result = FCS->Load(element);
511     else if (element_name == "flight_control")   result = FCS->Load(element);
512     else if (element_name == "aerodynamics")     result = Aerodynamics->Load(element);
513     else if (element_name == "input")            result = Input->Load(element);
514     else if (element_name == "output")           {
515         FGOutput* Output = new FGOutput(this);
516         Output->InitModel();
517         Schedule(Output,       1);
518         Outputs.push_back(Output);
519         result = Output->Load(element);
520     }
521     else {
522       cerr << "Found unexpected subsystem: " << element_name << ", exiting." << endl;
523       result = false;
524       break;
525     }
526     element = document->GetNextElement();
527   }
528
529   if (result) {
530     modelLoaded = true;
531     Debug(3);
532   } else {
533     cerr << fgred
534          << "  JSBSim failed to load aircraft and/or engine model"
535          << fgdef << endl;
536     return false;
537   }
538
539   struct PropertyCatalogStructure masterPCS;
540   masterPCS.base_string = "";
541   masterPCS.node = (FGPropertyManager*)master;
542
543   BuildPropertyCatalog(&masterPCS);
544
545   return result;
546 }
547
548 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549
550 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
551 {
552   struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
553   int node_idx = 0;
554   char int_buf[10];
555
556   for (int i=0; i<pcs->node->nChildren(); i++) {
557     pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
558     node_idx = pcs->node->getChild(i)->getIndex();
559     sprintf(int_buf, "[%d]", node_idx);
560     if (node_idx != 0) pcsNew->base_string += string(int_buf);
561     if (pcs->node->getChild(i)->nChildren() == 0) {
562       PropertyCatalog.push_back(pcsNew->base_string);
563     } else {
564       pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
565       BuildPropertyCatalog(pcsNew);
566     }
567   }
568   delete pcsNew;
569 }
570
571 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572
573 string FGFDMExec::QueryPropertyCatalog(string in)
574 {
575   string results="";
576   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
577     if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
578   }
579   if (results.empty()) return "No matches found\n";
580   return results;
581 }
582
583 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584
585 void FGFDMExec::PrintPropertyCatalog(void)
586 {
587   cout << endl;
588   cout << "  " << fgblue << highint << underon << "Property Catalog for "
589        << modelName << reset << endl << endl;
590   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
591     cout << "    " << PropertyCatalog[i] << endl;
592   }
593 }
594
595 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596
597 bool FGFDMExec::ReadFileHeader(Element* el)
598 {
599   bool result = true; // true for success
600
601   if (debug_lvl & ~1) return result;
602
603   if (el->FindElement("author"))
604     cout << "  Model Author:  " << el->FindElement("author")->GetDataLine() << endl;
605   if (el->FindElement("filecreationdate"))
606     cout << "  Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
607   if (el->FindElement("version"))
608     cout << "  Version:       " << el->FindElement("version")->GetDataLine() << endl;
609   if (el->FindElement("description"))
610     cout << "  Description:   " << el->FindElement("description")->GetDataLine() << endl;
611
612   return result;
613 }
614
615 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616
617 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
618 {
619   bool result = true; // true for success
620
621   if (!el) return false;
622
623   string AircraftName = el->GetAttributeValue("name");
624   Aircraft->SetAircraftName(AircraftName);
625
626   if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
627             << underoff << ": " << highint << AircraftName << normint << endl;
628
629   CFGVersion = el->GetAttributeValue("version");
630   Release    = el->GetAttributeValue("release");
631
632   if (debug_lvl & 1)
633     cout << "                            Version: " << highint << CFGVersion
634                                                     << normint << endl;
635   if (CFGVersion != needed_cfg_version) {
636     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
637             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
638     cerr << "Current version needed is: " << needed_cfg_version << endl;
639     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
640     return false;
641   }
642
643   if (Release == "ALPHA" && (debug_lvl & 1)) {
644     cout << endl << endl
645          << highint << "This aircraft model is an " << fgred << Release
646          << reset << highint << " release!!!" << endl << endl << reset
647          << "This aircraft model may not even properly load, and probably"
648          << " will not fly as expected." << endl << endl
649          << fgred << highint << "Use this model for development purposes ONLY!!!"
650          << normint << reset << endl << endl;
651   } else if (Release == "BETA" && (debug_lvl & 1)) {
652     cout << endl << endl
653          << highint << "This aircraft model is a " << fgred << Release
654          << reset << highint << " release!!!" << endl << endl << reset
655          << "This aircraft model probably will not fly as expected." << endl << endl
656          << fgblue << highint << "Use this model for development purposes ONLY!!!"
657          << normint << reset << endl << endl;
658   } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
659     cout << endl << endl
660          << highint << "This aircraft model is a " << fgblue << Release
661          << reset << highint << " release." << endl << endl << reset;
662   } else if (debug_lvl & 1) {
663     cout << endl << endl
664          << highint << "This aircraft model is an " << fgred << Release
665          << reset << highint << " release!!!" << endl << endl << reset
666          << "This aircraft model may not even properly load, and probably"
667          << " will not fly as expected." << endl << endl
668          << fgred << highint << "Use this model for development purposes ONLY!!!"
669          << normint << reset << endl << endl;
670   }
671
672   return result;
673 }
674
675 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676
677 bool FGFDMExec::ReadSlave(Element* el)
678 {
679   // Add a new slaveData object to the slave FDM list
680   // Populate that slaveData element with a new FDMExec object
681   // Set the IsSlave flag for that FDMExec object
682   // Get the aircraft name
683   // set debug level to print out no additional data for slave objects
684   // Load the model given the aircraft name
685   // reset debug level to prior setting
686
687   int saved_debug_lvl = debug_lvl;
688   string token;
689
690   SlaveFDMList.push_back(new slaveData);
691   SlaveFDMList.back()->exec = new FGFDMExec();
692   SlaveFDMList.back()->exec->SetSlave(true);
693 /*
694   string AircraftName = AC_cfg->GetValue("file");
695
696   debug_lvl = 0;                 // turn off debug output for slave vehicle
697
698   SlaveFDMList.back()->exec->SetAircraftPath( AircraftPath );
699   SlaveFDMList.back()->exec->SetEnginePath( EnginePath );
700   SlaveFDMList.back()->exec->LoadModel(AircraftName);
701   debug_lvl = saved_debug_lvl;   // turn debug output back on for master vehicle
702
703   AC_cfg->GetNextConfigLine();
704   while ((token = AC_cfg->GetValue()) != string("/SLAVE")) {
705     *AC_cfg >> token;
706     if      (token == "xloc")  { *AC_cfg >> SlaveFDMList.back()->x;    }
707     else if (token == "yloc")  { *AC_cfg >> SlaveFDMList.back()->y;    }
708     else if (token == "zloc")  { *AC_cfg >> SlaveFDMList.back()->z;    }
709     else if (token == "pitch") { *AC_cfg >> SlaveFDMList.back()->pitch;}
710     else if (token == "yaw")   { *AC_cfg >> SlaveFDMList.back()->yaw;  }
711     else if (token == "roll")  { *AC_cfg >> SlaveFDMList.back()->roll;  }
712     else cerr << "Unknown identifier: " << token << " in slave vehicle definition" << endl;
713   }
714 */
715   if (debug_lvl > 0)  {
716     cout << "      X = " << SlaveFDMList.back()->x << endl;
717     cout << "      Y = " << SlaveFDMList.back()->y << endl;
718     cout << "      Z = " << SlaveFDMList.back()->z << endl;
719     cout << "      Pitch = " << SlaveFDMList.back()->pitch << endl;
720     cout << "      Yaw = " << SlaveFDMList.back()->yaw << endl;
721     cout << "      Roll = " << SlaveFDMList.back()->roll << endl;
722   }
723
724   return true;
725 }
726
727 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728
729 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
730 {
731   return instance;
732 }
733
734 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735
736 FGTrim* FGFDMExec::GetTrim(void)
737 {
738   delete Trim;
739   Trim = new FGTrim(this,tNone);
740   return Trim;
741 }
742
743 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744
745 void FGFDMExec::DisableOutput(void)
746 {
747   for (unsigned i=0; i<Outputs.size(); i++) {
748     Outputs[i]->Disable();
749   }
750 }
751
752 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753
754 void FGFDMExec::EnableOutput(void)
755 {
756   for (unsigned i=0; i<Outputs.size(); i++) {
757     Outputs[i]->Enable();
758   }
759 }
760
761 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762
763 bool FGFDMExec::SetOutputDirectives(string fname)
764 {
765   bool result=true; // for now always return true
766
767   if (Outputs.size() == 0) {
768     FGOutput* Output = new FGOutput(this);
769     Output->InitModel();
770     Schedule(Output,       1);
771     Output->SetDirectivesFile(fname);
772     Output->Load(0);
773     Outputs.push_back(Output);
774   } else { // Outputs > 1
775     cerr << "First output file being overridden" << endl;
776     Outputs[0]->SetDirectivesFile(fname);
777   }
778   return result;
779 }
780
781 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782
783 void FGFDMExec::DoTrim(int mode)
784 {
785   double saved_time;
786
787   if (Constructing) return;
788
789   if (mode < 0 || mode > JSBSim::tNone) {
790     cerr << endl << "Illegal trimming mode!" << endl << endl;
791     return;
792   }
793   saved_time = State->Getsim_time();
794   FGTrim trim(this, (JSBSim::TrimMode)mode);
795   if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
796   trim.Report();
797   State->Setsim_time(saved_time);
798 }
799
800 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801
802 void FGFDMExec::UseAtmosphereMSIS(void)
803 {
804   FGAtmosphere *oldAtmosphere = Atmosphere;
805   Atmosphere = new MSIS(this);
806   if (!Atmosphere->InitModel()) {
807     cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
808     Error+=1;
809   }
810   delete oldAtmosphere;
811 }
812
813 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814
815 void FGFDMExec::UseAtmosphereMars(void)
816 {
817 /*
818   FGAtmosphere *oldAtmosphere = Atmosphere;
819   Atmosphere = new FGMars(this);
820   if (!Atmosphere->InitModel()) {
821     cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
822     Error+=1;
823   }
824   delete oldAtmosphere;
825 */
826 }
827
828 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 //    The bitmasked value choices are as follows:
830 //    unset: In this case (the default) JSBSim would only print
831 //       out the normally expected messages, essentially echoing
832 //       the config files as they are read. If the environment
833 //       variable is not set, debug_lvl is set to 1 internally
834 //    0: This requests JSBSim not to output any messages
835 //       whatsoever.
836 //    1: This value explicity requests the normal JSBSim
837 //       startup messages
838 //    2: This value asks for a message to be printed out when
839 //       a class is instantiated
840 //    4: When this value is set, a message is displayed when a
841 //       FGModel object executes its Run() method
842 //    8: When this value is set, various runtime state variables
843 //       are printed out periodically
844 //    16: When set various parameters are sanity checked and
845 //       a message is printed out when they go out of bounds
846
847 void FGFDMExec::Debug(int from)
848 {
849   if (debug_lvl <= 0) return;
850
851   if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
852     if (from == 0) { // Constructor
853       cout << "\n\n     " << highint << underon << "JSBSim Flight Dynamics Model v"
854                                      << JSBSim_version << underoff << normint << endl;
855       cout << halfint << "            [JSBSim-ML v" << needed_cfg_version << "]\n\n";
856       cout << normint << "JSBSim startup beginning ...\n\n";
857     } else if (from == 3) {
858       cout << "\n\nJSBSim startup complete\n\n";
859     }
860   }
861   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
862     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
863     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
864   }
865   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
866     if (from == 2) {
867       cout << "================== Frame: " << Frame << "  Time: "
868            << State->Getsim_time() << " dt: " << State->Getdt() << endl;
869     }
870   }
871   if (debug_lvl & 8 ) { // Runtime state variables
872   }
873   if (debug_lvl & 16) { // Sanity checking
874   }
875   if (debug_lvl & 64) {
876     if (from == 0) { // Constructor
877       cout << IdSrc << endl;
878       cout << IdHdr << endl;
879     }
880   }
881 }
882 }
883
884