]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
Fixes to jsbsim.
[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   // Instantiate this FDM Executive's Models
88
89   Atmosphere  = new FGAtmosphere(this);
90   FCS         = new FGFCS(this);
91   Aircraft    = new FGAircraft(this);
92   Translation = new FGTranslation(this);
93   Rotation    = new FGRotation(this);
94   Position    = new FGPosition(this);
95   Auxiliary   = new FGAuxiliary(this);
96   Output      = new FGOutput(this);
97
98   State       = new FGState(this);
99
100   // Initialize models so they can communicate with each other
101
102   if (!Atmosphere->InitModel()) {cerr << "Atmosphere model init failed"; Error+=1;}
103   if (!FCS->InitModel())        {cerr << "FCS model init failed"; Error+=2;}
104   if (!Aircraft->InitModel())   {cerr << "Aircraft model init failed"; Error+=4;}
105   if (!Translation->InitModel()){cerr << "Translation model init failed"; Error+=8;}
106   if (!Rotation->InitModel())   {cerr << "Rotation model init failed"; Error+=16;}
107   if (!Position->InitModel())   {cerr << "Position model init failed"; Error+=32;}
108   if (!Auxiliary->InitModel())  {cerr << "Auxiliary model init failed"; Error+=64;}
109   if (!Output->InitModel())     {cerr << "Output model init failed"; Error+=128;}
110
111   // Schedule a model. The second arg (the integer) is the pass number. For
112   // instance, the atmosphere model gets executed every fifth pass it is called
113   // by the executive. Everything else here gets executed each pass.
114
115   Schedule(Atmosphere,  1);
116   Schedule(FCS,         1);
117   Schedule(Aircraft,    1);
118   Schedule(Rotation,    1);
119   Schedule(Translation, 1);
120   Schedule(Position,    1);
121   Schedule(Auxiliary,   1);
122   Schedule(Output,     1);
123
124   terminate = false;
125   frozen = false;
126 }
127
128
129 FGFDMExec::~FGFDMExec(void)
130 {
131 }
132
133
134 int FGFDMExec::Schedule(FGModel* model, int rate)
135 {
136   FGModel* model_iterator;
137
138   model_iterator = FirstModel;
139
140   if (model_iterator == 0L) {                  // this is the first model
141
142     FirstModel = model;
143     FirstModel->NextModel = 0L;
144     FirstModel->SetRate(rate);
145
146   } else {                                     // subsequent model
147
148     while (model_iterator->NextModel != 0L) {
149       model_iterator = model_iterator->NextModel;
150     }
151     model_iterator->NextModel = model;
152     model_iterator->NextModel->SetRate(rate);
153
154   }
155   return 0;
156 }
157
158
159 bool FGFDMExec::Run(void)
160 {
161   FGModel* model_iterator;
162
163   if (frozen) return true;
164
165   model_iterator = FirstModel;
166   if (model_iterator == 0L) return false;
167
168   while (!model_iterator->Run())
169   {
170     model_iterator = model_iterator->NextModel;
171     if (model_iterator == 0L) break;
172   }
173
174   State->IncrTime();
175
176   return true;
177 }
178
179
180 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
181 {
182   float save_dt = State->Getdt();
183
184   State->Setdt(0.0);
185   State->Initialize(fgic);
186   Run();
187   State->Setdt(save_dt);
188
189   return true;
190 }
191   
192