]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
0eab1d6d00201ca0622a1de6d86d0912105847e5
[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 /*******************************************************************************
67 ************************************ CODE **************************************
68 *******************************************************************************/
69
70
71 // Constructor
72
73 FGFDMExec::FGFDMExec(void)
74 {
75   FirstModel  = 0;
76   Error       = 0;
77   State       = 0;
78   Atmosphere  = 0;
79   FCS         = 0;
80   Aircraft    = 0;
81   Translation = 0;
82   Rotation    = 0;
83   Position    = 0;
84   Auxiliary   = 0;
85   Output      = 0;
86
87   allocated = false;
88   terminate = false;
89   frozen = false;
90 }
91
92 FGFDMExec::~FGFDMExec(void){
93   
94   DeAllocate();
95
96 }
97
98 bool FGFDMExec::Allocate(void) {
99   
100   bool result=true;
101   
102   Atmosphere  = new FGAtmosphere(this);
103   FCS         = new FGFCS(this);
104   Aircraft    = new FGAircraft(this);
105   Translation = new FGTranslation(this);
106   Rotation    = new FGRotation(this);
107   Position    = new FGPosition(this);
108   Auxiliary   = new FGAuxiliary(this);
109   Output      = new FGOutput(this);
110
111   State       = new FGState(this);
112
113   // Initialize models so they can communicate with each other
114
115   if (!Atmosphere->InitModel()) {cerr << "Atmosphere model init failed"; Error+=1;}
116   if (!FCS->InitModel())        {cerr << "FCS model init failed"; Error+=2;}
117   if (!Aircraft->InitModel())   {cerr << "Aircraft model init failed"; Error+=4;}
118   if (!Translation->InitModel()){cerr << "Translation model init failed"; Error+=8;}
119   if (!Rotation->InitModel())   {cerr << "Rotation model init failed"; Error+=16;}
120   if (!Position->InitModel())   {cerr << "Position model init failed"; Error+=32;}
121   if (!Auxiliary->InitModel())  {cerr << "Auxiliary model init failed"; Error+=64;}
122   if (!Output->InitModel())     {cerr << "Output model init failed"; Error+=128;}
123   
124   if(Error > 0) result=false;
125   
126   // Schedule a model. The second arg (the integer) is the pass number. For
127   // instance, the atmosphere model gets executed every fifth pass it is called
128   // by the executive. Everything else here gets executed each pass.
129
130   Schedule(Atmosphere,  1);
131   Schedule(FCS,         1);
132   Schedule(Aircraft,    1);
133   Schedule(Rotation,    1);
134   Schedule(Translation, 1);
135   Schedule(Position,    1);
136   Schedule(Auxiliary,   1);
137   Schedule(Output,     1);
138   
139   allocated = true;
140   
141   return result;
142
143 }
144
145 bool FGFDMExec::DeAllocate(void) {
146   if(allocated) {
147     if ( Atmosphere != 0 )  delete Atmosphere;
148     if ( FCS != 0 )         delete FCS;
149     if ( Aircraft != 0 )    delete Aircraft;
150     if ( Translation != 0 ) delete Translation;
151     if ( Rotation != 0 )    delete Rotation;
152     if ( Position != 0 )    delete Position;
153     if ( Auxiliary != 0 )   delete Auxiliary;
154     if ( Output != 0 )      delete Output;
155     if ( State != 0 )       delete State;
156
157     FirstModel  = 0L;
158     Error       = 0;
159
160     State       = 0;
161     Atmosphere  = 0;
162     FCS         = 0;
163     Aircraft    = 0;
164     Translation = 0;
165     Rotation    = 0;
166     Position    = 0;
167     Auxiliary   = 0;
168     Output      = 0;
169     
170     allocated = false;
171   
172   }
173 }
174
175
176 int FGFDMExec::Schedule(FGModel* model, int rate)
177 {
178   FGModel* model_iterator;
179
180   model_iterator = FirstModel;
181
182   if (model_iterator == 0L) {                  // this is the first model
183
184     FirstModel = model;
185     FirstModel->NextModel = 0L;
186     FirstModel->SetRate(rate);
187
188   } else {                                     // subsequent model
189
190     while (model_iterator->NextModel != 0L) {
191       model_iterator = model_iterator->NextModel;
192     }
193     model_iterator->NextModel = model;
194     model_iterator->NextModel->SetRate(rate);
195
196   }
197   return 0;
198 }
199
200
201 bool FGFDMExec::Run(void)
202 {
203   FGModel* model_iterator;
204
205   if (frozen) return true;
206
207   model_iterator = FirstModel;
208   if (model_iterator == 0L) return false;
209
210   while (!model_iterator->Run())
211   {
212     model_iterator = model_iterator->NextModel;
213     if (model_iterator == 0L) break;
214   }
215
216   State->IncrTime();
217
218   return true;
219 }
220
221
222 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
223 {
224   State->Suspend();
225   State->Initialize(fgic);
226   Run();
227   State->Resume();
228   return true;
229 }
230   
231
232 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
233 {
234         DeAllocate();
235   Allocate();
236   AircraftPath = APath;
237         EnginePath = EPath;
238   return Aircraft->LoadAircraft(AircraftPath, EnginePath, model);
239 }
240
241
242 bool FGFDMExec::RunScript(string script)
243 {
244 }