]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBsim/FGFDMExec.cpp
e89ee88446e27d7b2d0ef2669df941e67502dadb
[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 <Include/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(Atmosphere,  5);
112   Schedule(FCS,         1);
113   Schedule(Aircraft,    1);
114   Schedule(Rotation,    1);
115   Schedule(Translation, 1);
116   Schedule(Position,    1);
117   Schedule(Auxiliary,   1);
118   Schedule(Output,      1);
119
120   terminate = false;
121   frozen = false;
122 }
123
124
125 FGFDMExec::~FGFDMExec(void)
126 {
127 }
128
129
130 int FGFDMExec::Schedule(FGModel* model, int rate)
131 {
132   FGModel* model_iterator;
133
134   model_iterator = FirstModel;
135
136   if (model_iterator == 0L) {                  // this is the first model
137
138     FirstModel = model;
139     FirstModel->NextModel = 0L;
140     FirstModel->SetRate(rate);
141
142   } else {                                     // subsequent model
143
144     while (model_iterator->NextModel != 0L) {
145       model_iterator = model_iterator->NextModel;
146     }
147     model_iterator->NextModel = model;
148     model_iterator->NextModel->SetRate(rate);
149
150   }
151   return 0;
152 }
153
154
155 bool FGFDMExec::Run(void)
156 {
157   FGModel* model_iterator;
158
159   if (frozen) return true;
160
161   model_iterator = FirstModel;
162   if (model_iterator == 0L) return false;
163
164   while (!model_iterator->Run())
165   {
166     model_iterator = model_iterator->NextModel;
167     if (model_iterator == 0L) break;
168   }
169
170   State->IncrTime();
171
172   return true;
173 }
174