]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
Friday the 13th JSBSim update ... :-0 !!!
[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  Called by:    The GUI.
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  details.
20
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 This class wraps up the simulation scheduling routines.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 11/17/98   JSB   Created
36
37 ********************************************************************************
38 INCLUDES
39 *******************************************************************************/
40
41 #ifdef FGFS
42 #  include <simgear/compiler.h>
43 #  ifdef FG_HAVE_STD_INCLUDES
44 #    include <iostream>
45 #    include <ctime>
46 #  else
47 #    include <iostream.h>
48 #    include <time.h>
49 #  endif
50 #else
51 #  include <iostream>
52 #  include <ctime>
53 #endif
54
55 #include "FGFDMExec.h"
56 #include "FGState.h"
57 #include "FGAtmosphere.h"
58 #include "FGFCS.h"
59 #include "FGAircraft.h"
60 #include "FGTranslation.h"
61 #include "FGRotation.h"
62 #include "FGPosition.h"
63 #include "FGAuxiliary.h"
64 #include "FGOutput.h"
65
66 static const char *IdSrc = "$Header$";
67 static const char *IdHdr = "ID_FDMEXEC";
68
69 /*******************************************************************************
70 ************************************ CODE **************************************
71 *******************************************************************************/
72
73
74 // Constructor
75
76 FGFDMExec::FGFDMExec(void)
77 {
78   FirstModel  = 0;
79   Error       = 0;
80   State       = 0;
81   Atmosphere  = 0;
82   FCS         = 0;
83   Aircraft    = 0;
84   Translation = 0;
85   Rotation    = 0;
86   Position    = 0;
87   Auxiliary   = 0;
88   Output      = 0;
89
90   terminate = false;
91   frozen = false;
92   modelLoaded = false;
93   
94   Allocate();
95   
96 }
97
98 FGFDMExec::~FGFDMExec(void){
99   
100   DeAllocate();
101
102 }
103
104 bool FGFDMExec::Allocate(void) {
105   
106   cout << "FGFDMExec::Allocate ... ";
107   bool result=true;
108   
109   Atmosphere  = new FGAtmosphere(this);
110   FCS         = new FGFCS(this);
111   Aircraft    = new FGAircraft(this);
112   Translation = new FGTranslation(this);
113   Rotation    = new FGRotation(this);
114   Position    = new FGPosition(this);
115   Auxiliary   = new FGAuxiliary(this);
116   Output      = new FGOutput(this);
117
118   State       = new FGState(this);
119
120   // Initialize models so they can communicate with each other
121
122   if (!Atmosphere->InitModel()) {cerr << "Atmosphere model init failed"; Error+=1;}
123   if (!FCS->InitModel())        {cerr << "FCS model init failed"; Error+=2;}
124   if (!Aircraft->InitModel())   {cerr << "Aircraft model init failed"; Error+=4;}
125   if (!Translation->InitModel()){cerr << "Translation model init failed"; Error+=8;}
126   if (!Rotation->InitModel())   {cerr << "Rotation model init failed"; Error+=16;}
127   if (!Position->InitModel())   {cerr << "Position model init failed"; Error+=32;}
128   if (!Auxiliary->InitModel())  {cerr << "Auxiliary model init failed"; Error+=64;}
129   if (!Output->InitModel())     {cerr << "Output model init failed"; Error+=128;}
130   
131   if(Error > 0) result=false;
132   
133   // Schedule a model. The second arg (the integer) is the pass number. For
134   // instance, the atmosphere model gets executed every fifth pass it is called
135   // by the executive. Everything else here gets executed each pass.
136
137   Schedule(Atmosphere,  1);
138   Schedule(FCS,         1);
139   Schedule(Aircraft,    1);
140   Schedule(Rotation,    1);
141   Schedule(Translation, 1);
142   Schedule(Position,    1);
143   Schedule(Auxiliary,   1);
144   Schedule(Output,     1);
145   
146   modelLoaded = false;
147   cout << "done." << endl;
148   return result;
149
150 }
151
152 bool FGFDMExec::DeAllocate(void) {
153   cout << "FGFDMExec::DeAllocate ... ";
154  
155   if ( Atmosphere != 0 )  delete Atmosphere;
156   if ( FCS != 0 )         delete FCS;
157   if ( Aircraft != 0 )    delete Aircraft;
158   if ( Translation != 0 ) delete Translation;
159   if ( Rotation != 0 )    delete Rotation;
160   if ( Position != 0 )    delete Position;
161   if ( Auxiliary != 0 )   delete Auxiliary;
162   if ( Output != 0 )      delete Output;
163   if ( State != 0 )       delete State;
164
165   FirstModel  = 0L;
166   Error       = 0;
167
168   State       = 0;
169   Atmosphere  = 0;
170   FCS         = 0;
171   Aircraft    = 0;
172   Translation = 0;
173   Rotation    = 0;
174   Position    = 0;
175   Auxiliary   = 0;
176   Output      = 0;
177
178   modelLoaded = false;
179   
180   cout << "done" << endl;
181 }
182
183
184 int FGFDMExec::Schedule(FGModel* model, int rate)
185 {
186   FGModel* model_iterator;
187
188   model_iterator = FirstModel;
189
190   if (model_iterator == 0L) {                  // this is the first model
191
192     FirstModel = model;
193     FirstModel->NextModel = 0L;
194     FirstModel->SetRate(rate);
195
196   } else {                                     // subsequent model
197
198     while (model_iterator->NextModel != 0L) {
199       model_iterator = model_iterator->NextModel;
200     }
201     model_iterator->NextModel = model;
202     model_iterator->NextModel->SetRate(rate);
203
204   }
205   return 0;
206 }
207
208
209 bool FGFDMExec::Run(void)
210 {
211   FGModel* model_iterator;
212
213   if (frozen) return true;
214
215   model_iterator = FirstModel;
216   if (model_iterator == 0L) return false;
217
218   while (!model_iterator->Run())
219   {
220     model_iterator = model_iterator->NextModel;
221     if (model_iterator == 0L) break;
222   }
223
224   State->IncrTime();
225
226   return true;
227 }
228
229
230 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
231 {
232   State->Suspend();
233   State->Initialize(fgic);
234   Run();
235   State->Resume();
236   return true;
237 }
238   
239
240 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
241 {
242         bool result=false;
243   cout << "FGFDMExec::LoadModel ..." << endl;
244   if(modelLoaded) {
245      DeAllocate();
246      Allocate();
247   }   
248   AircraftPath = APath;
249         EnginePath = EPath;
250   result = Aircraft->LoadAircraft(AircraftPath, EnginePath, model);
251   if(result) {
252     modelLoaded=true;
253   } else {
254     cerr << "FGFDMExec: Failed to load aircraft and/or engine model" << endl;
255   }  
256   cout << "FGFDMExec::LoadModel complete." << endl;;
257   return result;
258 }
259
260
261 bool FGFDMExec::RunScript(string script)
262 {
263 }