]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
First commit of properties code. JSBSim now has a basic property tree all
[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 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 General Public License for more
18  details.
19
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.
23
24  Further information about the GNU 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__)
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 "FGAtmosphere.h"
60 #include "FGFCS.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"
71 #include "FGOutput.h"
72 #include "FGConfigFile.h"
73 #include "FGInitialCondition.h"
74 #include "FGPropertyManager.h"
75
76 static const char *IdSrc = "$Id$";
77 static const char *IdHdr = ID_FDMEXEC;
78
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 GLOBAL DECLARATIONS
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82
83 unsigned int FGFDMExec::FDMctr = 0;
84
85 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 CLASS IMPLEMENTATION
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
88
89 // Constructor
90
91 FGFDMExec::FGFDMExec(FGPropertyManager* root)
92 {
93   
94   Frame           = 0;
95   FirstModel      = 0;
96   Error           = 0;
97   State           = 0;
98   Atmosphere      = 0;
99   FCS             = 0;
100   Propulsion      = 0;
101   MassBalance     = 0;
102   Aerodynamics    = 0;
103   Inertial        = 0;
104   GroundReactions = 0;
105   Aircraft        = 0;
106   Translation     = 0;
107   Rotation        = 0;
108   Position        = 0;
109   Auxiliary       = 0;
110   Output          = 0;
111
112   terminate = false;
113   frozen = false;
114   modelLoaded = false;
115
116   IdFDM = FDMctr;
117   FDMctr++;
118
119   try {
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
124     debug_lvl = 1;
125   }
126
127
128   if(root == 0) 
129     master= new FGPropertyManager;
130   else
131     master = root;  
132   instance = master->GetNode("/fdm/jsbsim",IdFDM,true);
133   instance->SetDouble("zero",0);  
134   
135   Debug(0);
136   
137   // this is here to catch errors in binding member functions
138   // to the property tree.
139   try {
140     Allocate();
141   } catch ( string msg ) {
142     cout << "Caught error: " << msg << endl;
143     exit(1);
144   }    
145 }
146
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 FGFDMExec::~FGFDMExec()
150 {
151   try {
152     DeAllocate();
153   } catch ( string msg ) {
154     cout << "Caught error: " << msg << endl;
155   }    
156
157   Debug(1);
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 bool FGFDMExec::Allocate(void)
163 {
164   bool result=true;
165
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);
179
180   State        = new FGState(this); // This must be done here, as the FGState
181                                     // class needs valid pointers to the above
182                                     // model classes
183   
184   // Initialize models so they can communicate with each other
185
186   if (!Atmosphere->InitModel()) {
187     cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
188     Error+=1;}
189   if (!FCS->InitModel())        {
190     cerr << fgred << "FCS model init failed" << fgdef << endl;
191     Error+=2;}
192   if (!Propulsion->InitModel()) {
193     cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
194     Error+=4;}
195   if (!MassBalance->InitModel()) {
196     cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
197     Error+=8;}
198   if (!Aerodynamics->InitModel()) {
199     cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
200     Error+=16;}
201   if (!Inertial->InitModel()) {
202     cerr << fgred << "FGInertial model init failed" << fgdef << endl;
203     Error+=32;}
204   if (!GroundReactions->InitModel())   {
205     cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
206     Error+=64;}
207   if (!Aircraft->InitModel())   {
208     cerr << fgred << "Aircraft model init failed" << fgdef << endl;
209     Error+=128;}
210   if (!Translation->InitModel()){
211     cerr << fgred << "Translation model init failed" << fgdef << endl;
212     Error+=256;}
213   if (!Rotation->InitModel())   {
214     cerr << fgred << "Rotation model init failed" << fgdef << endl;
215     Error+=512;}
216   if (!Position->InitModel())   {
217     cerr << fgred << "Position model init failed" << fgdef << endl;
218     Error+=1024;}
219   if (!Auxiliary->InitModel())  {
220     cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
221     Error+=2058;}
222   if (!Output->InitModel())     {
223     cerr << fgred << "Output model init failed" << fgdef << endl;
224     Error+=4096;}
225
226   if (Error > 0) result = false;
227
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.
231
232   Schedule(Atmosphere,      1);
233   Schedule(FCS,             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);
244   Schedule(Output,          1);
245
246   modelLoaded = false;
247
248   return result;
249 }
250
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252
253 bool FGFDMExec::DeAllocate(void) {
254
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;
269
270   FirstModel  = 0L;
271   Error       = 0;
272
273   State           = 0;
274   Atmosphere      = 0;
275   FCS             = 0;
276   Propulsion      = 0;
277   MassBalance     = 0;
278   Aerodynamics    = 0;
279   Inertial        = 0;
280   GroundReactions = 0;
281   Aircraft        = 0;
282   Translation     = 0;
283   Rotation        = 0;
284   Position        = 0;
285   Auxiliary       = 0;
286   Output          = 0;
287
288   modelLoaded = false;
289   return modelLoaded;
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 int FGFDMExec::Schedule(FGModel* model, int rate)
295 {
296   FGModel* model_iterator;
297
298   model_iterator = FirstModel;
299
300   if (model_iterator == 0L) {                  // this is the first model
301
302     FirstModel = model;
303     FirstModel->NextModel = 0L;
304     FirstModel->SetRate(rate);
305
306   } else {                                     // subsequent model
307
308     while (model_iterator->NextModel != 0L) {
309       model_iterator = model_iterator->NextModel;
310     }
311     model_iterator->NextModel = model;
312     model_iterator->NextModel->SetRate(rate);
313
314   }
315   
316   return 0;
317 }
318
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320
321 bool FGFDMExec::Run(void)
322 {
323   FGModel* model_iterator;
324
325   if (frozen) return true;
326
327   model_iterator = FirstModel;
328   if (model_iterator == 0L) return false;
329
330   Debug(2);
331
332   while (!model_iterator->Run()) {
333     model_iterator = model_iterator->NextModel;
334     if (model_iterator == 0L) break;
335   }
336
337   frame = Frame++;
338   State->IncrTime();
339
340   return true;
341 }
342
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
346 {
347   State->Suspend();
348   State->Initialize(fgic);
349   Run();
350   State->Resume();
351   return true;
352 }
353
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355
356 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
357 {
358   bool result = true;
359   string token;
360   string aircraftCfgFileName;
361
362   AircraftPath = APath;
363   EnginePath   = EPath;
364
365 # ifndef macintosh
366   aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
367 # else
368   aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
369 # endif
370
371   FGConfigFile AC_cfg(aircraftCfgFileName);
372   if (!AC_cfg.IsOpen()) return false;
373
374   if (modelLoaded) {
375     DeAllocate();
376     Allocate();
377   }
378
379   if (!ReadPrologue(&AC_cfg)) return false;
380
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;
401     }
402   }
403
404   if (result) {
405     modelLoaded = true;
406     Debug(3);
407   } else {
408     cerr << fgred
409          << "  FGFDMExec: Failed to load aircraft and/or engine model"
410          << fgdef << endl;
411   }
412
413   return result;
414 }
415
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417
418 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
419 {
420   string token = AC_cfg->GetValue();
421   string scratch;
422   string AircraftName;
423   
424   AircraftName = AC_cfg->GetValue("NAME");
425   Aircraft->SetAircraftName(AircraftName);
426
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();
430
431   CFGVersion = AC_cfg->GetValue("VERSION");
432
433   if (debug_lvl > 0)
434     cout << "                            Version: " << highint << CFGVersion
435                                                              << normint << endl;
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;
441     return false;
442   }
443   return true;
444 }
445
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447
448 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
449 {
450   if (!Propulsion->Load(AC_cfg)) {
451     cerr << "  Propulsion not successfully loaded" << endl;
452     return false;
453   }
454   return true;
455 }
456
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458
459 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
460 {
461   if (!FCS->Load(AC_cfg)) {
462     cerr << "  Flight Controls not successfully loaded" << endl;
463     return false;
464   }
465   return true;
466 }
467
468 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469
470 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
471 {
472   if (!Aerodynamics->Load(AC_cfg)) {
473     cerr << "  Aerodynamics not successfully loaded" << endl;
474     return false;
475   }
476   return true;
477 }
478
479 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480
481 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
482 {
483   if (!GroundReactions->Load(AC_cfg)) {
484     cerr << "  Ground Reactions not successfully loaded" << endl;
485     return false;
486   }
487   return true;
488 }
489
490 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491
492 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
493 {
494   if (!Aircraft->Load(AC_cfg)) {
495     cerr << "  Aircraft metrics not successfully loaded" << endl;
496     return false;
497   }
498   return true;
499 }
500
501 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502
503 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
504 {
505   if (!Output->Load(AC_cfg)) {
506     cerr << "  Output not successfully loaded" << endl;
507     return false;
508   }
509   return true;
510 }
511
512 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513
514 FGPropertyManager* FGFDMExec::GetPropertyManager(void) { 
515   return instance;
516 }
517
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
525 //       whatsoever.
526 //    1: This value explicity requests the normal JSBSim
527 //       startup messages
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
536
537 void FGFDMExec::Debug(int from)
538 {
539   if (debug_lvl <= 0) return;
540
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";
549     }
550   }
551   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
552     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
553     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
554   }
555   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
556     if (from == 2) {
557       cout << "================== Frame: " << Frame << "  Time: "
558            << State->Getsim_time() << endl;
559     }
560   }
561   if (debug_lvl & 8 ) { // Runtime state variables
562   }
563   if (debug_lvl & 16) { // Sanity checking
564   }
565   if (debug_lvl & 64) {
566     if (from == 0) { // Constructor
567       cout << IdSrc << endl;
568       cout << IdHdr << endl;
569     }
570   }
571 }
572