]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
- fixed fuel-need calculations
[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
75 static const char *IdSrc = "$Id$";
76 static const char *IdHdr = ID_FDMEXEC;
77
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 GLOBAL DECLARATIONS
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81
82 unsigned int FGFDMExec::FDMctr = 0;
83
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 CLASS IMPLEMENTATION
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87
88 // Constructor
89
90 FGFDMExec::FGFDMExec(void)
91 {
92   Frame           = 0;
93   FirstModel      = 0;
94   Error           = 0;
95   State           = 0;
96   Atmosphere      = 0;
97   FCS             = 0;
98   Propulsion      = 0;
99   MassBalance     = 0;
100   Aerodynamics    = 0;
101   Inertial        = 0;
102   GroundReactions = 0;
103   Aircraft        = 0;
104   Translation     = 0;
105   Rotation        = 0;
106   Position        = 0;
107   Auxiliary       = 0;
108   Output          = 0;
109
110   terminate = false;
111   frozen = false;
112   modelLoaded = false;
113
114   IdFDM = FDMctr;
115   FDMctr++;
116
117   try {
118     char* num = getenv("JSBSIM_DEBUG");
119     if (!num) debug_lvl = 1;
120     else debug_lvl = atoi(num); // set debug level
121   } catch (...) {               // if error set to 1
122     debug_lvl = 1;
123   }
124
125   Debug(0);
126
127   Allocate();
128 }
129
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 FGFDMExec::~FGFDMExec()
133 {
134   DeAllocate();
135
136   Debug(1);
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 bool FGFDMExec::Allocate(void)
142 {
143   bool result=true;
144
145   Atmosphere      = new FGAtmosphere(this);
146   FCS             = new FGFCS(this);
147   Propulsion      = new FGPropulsion(this);
148   MassBalance     = new FGMassBalance(this);
149   Aerodynamics    = new FGAerodynamics (this);
150   Inertial        = new FGInertial(this);
151   GroundReactions = new FGGroundReactions(this);
152   Aircraft        = new FGAircraft(this);
153   Translation     = new FGTranslation(this);
154   Rotation        = new FGRotation(this);
155   Position        = new FGPosition(this);
156   Auxiliary       = new FGAuxiliary(this);
157   Output          = new FGOutput(this);
158
159   State        = new FGState(this); // This must be done here, as the FGState
160                                     // class needs valid pointers to the above
161                                     // model classes
162   
163   // Initialize models so they can communicate with each other
164
165   if (!Atmosphere->InitModel()) {
166     cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
167     Error+=1;}
168   if (!FCS->InitModel())        {
169     cerr << fgred << "FCS model init failed" << fgdef << endl;
170     Error+=2;}
171   if (!Propulsion->InitModel()) {
172     cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
173     Error+=4;}
174   if (!MassBalance->InitModel()) {
175     cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
176     Error+=8;}
177   if (!Aerodynamics->InitModel()) {
178     cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
179     Error+=16;}
180   if (!Inertial->InitModel()) {
181     cerr << fgred << "FGInertial model init failed" << fgdef << endl;
182     Error+=32;}
183   if (!GroundReactions->InitModel())   {
184     cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
185     Error+=64;}
186   if (!Aircraft->InitModel())   {
187     cerr << fgred << "Aircraft model init failed" << fgdef << endl;
188     Error+=128;}
189   if (!Translation->InitModel()){
190     cerr << fgred << "Translation model init failed" << fgdef << endl;
191     Error+=256;}
192   if (!Rotation->InitModel())   {
193     cerr << fgred << "Rotation model init failed" << fgdef << endl;
194     Error+=512;}
195   if (!Position->InitModel())   {
196     cerr << fgred << "Position model init failed" << fgdef << endl;
197     Error+=1024;}
198   if (!Auxiliary->InitModel())  {
199     cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
200     Error+=2058;}
201   if (!Output->InitModel())     {
202     cerr << fgred << "Output model init failed" << fgdef << endl;
203     Error+=4096;}
204
205   if (Error > 0) result = false;
206
207   // Schedule a model. The second arg (the integer) is the pass number. For
208   // instance, the atmosphere model gets executed every fifth pass it is called
209   // by the executive. Everything else here gets executed each pass.
210
211   Schedule(Atmosphere,      1);
212   Schedule(FCS,             1);
213   Schedule(Propulsion,      1);
214   Schedule(MassBalance,     1);
215   Schedule(Aerodynamics,    1);
216   Schedule(Inertial,        1);
217   Schedule(GroundReactions, 1);
218   Schedule(Aircraft,        1);
219   Schedule(Rotation,        1);
220   Schedule(Translation,     1);
221   Schedule(Position,        1);
222   Schedule(Auxiliary,       1);
223   Schedule(Output,          1);
224
225   modelLoaded = false;
226
227   return result;
228 }
229
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231
232 bool FGFDMExec::DeAllocate(void) {
233
234   if ( Atmosphere != 0 )     delete Atmosphere;
235   if ( FCS != 0 )            delete FCS;
236   if ( Propulsion != 0)      delete Propulsion;
237   if ( MassBalance != 0)     delete MassBalance;
238   if ( Aerodynamics != 0)    delete Aerodynamics;
239   if ( Inertial != 0)        delete Inertial;
240   if ( GroundReactions != 0) delete GroundReactions;
241   if ( Aircraft != 0 )       delete Aircraft;
242   if ( Translation != 0 )    delete Translation;
243   if ( Rotation != 0 )       delete Rotation;
244   if ( Position != 0 )       delete Position;
245   if ( Auxiliary != 0 )      delete Auxiliary;
246   if ( Output != 0 )         delete Output;
247   if ( State != 0 )          delete State;
248
249   FirstModel  = 0L;
250   Error       = 0;
251
252   State           = 0;
253   Atmosphere      = 0;
254   FCS             = 0;
255   Propulsion      = 0;
256   MassBalance     = 0;
257   Aerodynamics    = 0;
258   Inertial        = 0;
259   GroundReactions = 0;
260   Aircraft        = 0;
261   Translation     = 0;
262   Rotation        = 0;
263   Position        = 0;
264   Auxiliary       = 0;
265   Output          = 0;
266
267   modelLoaded = false;
268   return modelLoaded;
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 int FGFDMExec::Schedule(FGModel* model, int rate)
274 {
275   FGModel* model_iterator;
276
277   model_iterator = FirstModel;
278
279   if (model_iterator == 0L) {                  // this is the first model
280
281     FirstModel = model;
282     FirstModel->NextModel = 0L;
283     FirstModel->SetRate(rate);
284
285   } else {                                     // subsequent model
286
287     while (model_iterator->NextModel != 0L) {
288       model_iterator = model_iterator->NextModel;
289     }
290     model_iterator->NextModel = model;
291     model_iterator->NextModel->SetRate(rate);
292
293   }
294   
295   return 0;
296 }
297
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299
300 bool FGFDMExec::Run(void)
301 {
302   FGModel* model_iterator;
303
304   if (frozen) return true;
305
306   model_iterator = FirstModel;
307   if (model_iterator == 0L) return false;
308
309   Debug(2);
310
311   while (!model_iterator->Run()) {
312     model_iterator = model_iterator->NextModel;
313     if (model_iterator == 0L) break;
314   }
315
316   frame = Frame++;
317   State->IncrTime();
318
319   return true;
320 }
321
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323
324 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
325 {
326   State->Suspend();
327   State->Initialize(fgic);
328   Run();
329   State->Resume();
330   return true;
331 }
332
333 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334
335 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
336 {
337   bool result = true;
338   string token;
339   string aircraftCfgFileName;
340
341   AircraftPath = APath;
342   EnginePath   = EPath;
343
344 # ifndef macintosh
345   aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
346 # else
347   aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
348 # endif
349
350   FGConfigFile AC_cfg(aircraftCfgFileName);
351   if (!AC_cfg.IsOpen()) return false;
352
353   if (modelLoaded) {
354     DeAllocate();
355     Allocate();
356   }
357
358   if (!ReadPrologue(&AC_cfg)) return false;
359
360   while ((AC_cfg.GetNextConfigLine() != string("EOF")) &&
361          (token = AC_cfg.GetValue()) != string("/FDM_CONFIG")) {
362     if (token == "METRICS") {
363       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Metrics" << fgdef << endl;
364       if (!ReadMetrics(&AC_cfg)) result = false;
365     } else if (token == "AERODYNAMICS") {
366       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Aerodynamics" << fgdef << endl;
367       if (!ReadAerodynamics(&AC_cfg)) result = false;
368     } else if (token == "UNDERCARRIAGE") {
369       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Landing Gear" << fgdef << endl;
370       if (!ReadUndercarriage(&AC_cfg)) result = false;
371     } else if (token == "PROPULSION") {
372       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Propulsion" << fgdef << endl;
373       if (!ReadPropulsion(&AC_cfg)) result = false;
374     } else if (token == "FLIGHT_CONTROL") {
375       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Flight Control" << fgdef << endl;
376       if (!ReadFlightControls(&AC_cfg)) result = false;
377     } else if (token == "OUTPUT") {
378       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Output directives" << fgdef << endl;
379       if (!ReadOutput(&AC_cfg)) result = false;
380     }
381   }
382
383   if (result) {
384     modelLoaded = true;
385     Debug(3);
386   } else {
387     cerr << fgred
388          << "  FGFDMExec: Failed to load aircraft and/or engine model"
389          << fgdef << endl;
390   }
391
392   return result;
393 }
394
395 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396
397 bool FGFDMExec::ReadPrologue(FGConfigFile* AC_cfg)
398 {
399   string token = AC_cfg->GetValue();
400   string scratch;
401   string AircraftName;
402   
403   AircraftName = AC_cfg->GetValue("NAME");
404   Aircraft->SetAircraftName(AircraftName);
405
406   if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
407             << underoff << ": " << highint << AircraftName << normint << endl;
408   scratch = AC_cfg->GetValue("VERSION").c_str();
409
410   CFGVersion = AC_cfg->GetValue("VERSION");
411
412   if (debug_lvl > 0)
413     cout << "                            Version: " << highint << CFGVersion
414                                                              << normint << endl;
415   if (CFGVersion != needed_cfg_version) {
416     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
417             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
418     cerr << "Current version needed is: " << needed_cfg_version << endl;
419     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
420     return false;
421   }
422   return true;
423 }
424
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426
427 bool FGFDMExec::ReadPropulsion(FGConfigFile* AC_cfg)
428 {
429   if (!Propulsion->Load(AC_cfg)) {
430     cerr << "  Propulsion not successfully loaded" << endl;
431     return false;
432   }
433   return true;
434 }
435
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437
438 bool FGFDMExec::ReadFlightControls(FGConfigFile* AC_cfg)
439 {
440   if (!FCS->Load(AC_cfg)) {
441     cerr << "  Flight Controls not successfully loaded" << endl;
442     return false;
443   }
444   return true;
445 }
446
447 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448
449 bool FGFDMExec::ReadAerodynamics(FGConfigFile* AC_cfg)
450 {
451   if (!Aerodynamics->Load(AC_cfg)) {
452     cerr << "  Aerodynamics not successfully loaded" << endl;
453     return false;
454   }
455   return true;
456 }
457
458 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459
460 bool FGFDMExec::ReadUndercarriage(FGConfigFile* AC_cfg)
461 {
462   if (!GroundReactions->Load(AC_cfg)) {
463     cerr << "  Ground Reactions not successfully loaded" << endl;
464     return false;
465   }
466   return true;
467 }
468
469 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470
471 bool FGFDMExec::ReadMetrics(FGConfigFile* AC_cfg)
472 {
473   if (!Aircraft->Load(AC_cfg)) {
474     cerr << "  Aircraft metrics not successfully loaded" << endl;
475     return false;
476   }
477   return true;
478 }
479
480 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481
482 bool FGFDMExec::ReadOutput(FGConfigFile* AC_cfg)
483 {
484   if (!Output->Load(AC_cfg)) {
485     cerr << "  Output not successfully loaded" << endl;
486     return false;
487   }
488   return true;
489 }
490
491 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 //    The bitmasked value choices are as follows:
493 //    unset: In this case (the default) JSBSim would only print
494 //       out the normally expected messages, essentially echoing
495 //       the config files as they are read. If the environment
496 //       variable is not set, debug_lvl is set to 1 internally
497 //    0: This requests JSBSim not to output any messages
498 //       whatsoever.
499 //    1: This value explicity requests the normal JSBSim
500 //       startup messages
501 //    2: This value asks for a message to be printed out when
502 //       a class is instantiated
503 //    4: When this value is set, a message is displayed when a
504 //       FGModel object executes its Run() method
505 //    8: When this value is set, various runtime state variables
506 //       are printed out periodically
507 //    16: When set various parameters are sanity checked and
508 //       a message is printed out when they go out of bounds
509
510 void FGFDMExec::Debug(int from)
511 {
512   if (debug_lvl <= 0) return;
513
514   if (debug_lvl & 1) { // Standard console startup message output
515     if (from == 0) { // Constructor
516       cout << "\n\n     " << highint << underon << "JSBSim Flight Dynamics Model v"
517                                      << JSBSim_version << underoff << normint << endl;
518       cout << halfint << "            [cfg file spec v" << needed_cfg_version << "]\n\n";
519       cout << normint << "JSBSim startup beginning ...\n\n";
520     } else if (from == 3) {
521       cout << "\n\nJSBSim startup complete\n\n";
522     }
523   }
524   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
525     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
526     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
527   }
528   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
529     if (from == 2) {
530       cout << "================== Frame: " << Frame << "  Time: "
531            << State->Getsim_time() << endl;
532     }
533   }
534   if (debug_lvl & 8 ) { // Runtime state variables
535   }
536   if (debug_lvl & 16) { // Sanity checking
537   }
538   if (debug_lvl & 64) {
539     if (from == 0) { // Constructor
540       cout << IdSrc << endl;
541       cout << IdHdr << endl;
542     }
543   }
544 }
545