]> git.mxchange.org Git - flightgear.git/blob - JSBsim/FGFDMExec.cpp
6f2cc1b9dab3cbf2ee0a7230bfea0f45edd95d8b
[flightgear.git] / 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 #include <stdio.h>
42 #include <stdlib.h>
43 #include <iostream.h>
44 #include <time.h>
45
46 #include "FGFDMExec.h"
47 #include "FGState.h"
48
49 #include "FGAtmosphere.h"
50 #include "FGFCS.h"
51 #include "FGAircraft.h"
52 #include "FGTranslation.h"
53 #include "FGRotation.h"
54 #include "FGPosition.h"
55 #include "FGAuxiliary.h"
56 #include "FGOutput.h"
57
58 /*******************************************************************************
59 ************************************ CODE **************************************
60 *******************************************************************************/
61
62
63 // Constructor
64
65 FGFDMExec::FGFDMExec(void)
66 {
67   FirstModel  = 0;
68   Error       = 0;
69   State       = 0;
70   Atmosphere  = 0;
71   FCS         = 0;
72   Aircraft    = 0;
73   Translation = 0;
74   Rotation    = 0;
75   Position    = 0;
76   Auxiliary   = 0;
77   Output      = 0;
78
79   // Instantiate this FDM Executive's Models
80
81   Atmosphere  = new FGAtmosphere(this);
82   FCS         = new FGFCS(this);
83   Aircraft    = new FGAircraft(this);
84   Translation = new FGTranslation(this);
85   Rotation    = new FGRotation(this);
86   Position    = new FGPosition(this);
87   Auxiliary   = new FGAuxiliary(this);
88   Output      = new FGOutput(this);
89
90   State       = new FGState(this);
91
92   // Initialize models so they can communicate with each other
93
94   if (!Atmosphere->InitModel()) {cerr << "Atmosphere model init failed"; Error+=1;}
95   if (!FCS->InitModel())        {cerr << "FCS model init failed"; Error+=2;}
96   if (!Aircraft->InitModel())   {cerr << "Aircraft model init failed"; Error+=4;}
97   if (!Translation->InitModel()){cerr << "Translation model init failed"; Error+=8;}
98   if (!Rotation->InitModel())   {cerr << "Rotation model init failed"; Error+=16;}
99   if (!Position->InitModel())   {cerr << "Position model init failed"; Error+=32;}
100   if (!Auxiliary->InitModel())  {cerr << "Auxiliary model init failed"; Error+=64;}
101   if (!Output->InitModel())     {cerr << "Output model init failed"; Error+=128;}
102
103   Schedule(Atmosphere,  5);
104   Schedule(FCS,         1);
105   Schedule(Aircraft,    1);
106   Schedule(Rotation,    1);
107   Schedule(Translation, 1);
108   Schedule(Position,    1);
109   Schedule(Auxiliary,   1);
110   Schedule(Output,      5);
111
112   terminate = false;
113   frozen = false;
114 }
115
116
117 FGFDMExec::~FGFDMExec(void)
118 {
119 }
120
121
122 int FGFDMExec::Schedule(FGModel* model, int rate)
123 {
124   FGModel* model_iterator;
125
126   model_iterator = FirstModel;
127
128   if (model_iterator == 0L) {                  // this is the first model
129
130     FirstModel = model;
131     FirstModel->NextModel = 0L;
132     FirstModel->SetRate(rate);
133
134   } else {                                     // subsequent model
135
136     while (model_iterator->NextModel != 0L) {
137       model_iterator = model_iterator->NextModel;
138     }
139     model_iterator->NextModel = model;
140     model_iterator->NextModel->SetRate(rate);
141
142   }
143   return 0;
144 }
145
146
147 bool FGFDMExec::Run(void)
148 {
149   FGModel* model_iterator;
150
151   if (frozen) return true;
152
153   model_iterator = FirstModel;
154   if (model_iterator == 0L) return false;
155
156   while (!model_iterator->Run())
157   {
158     model_iterator = model_iterator->NextModel;
159     if (model_iterator == 0L) break;
160   }
161
162   State->IncrTime();
163
164   return true;
165 }
166