]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.h
9e2d0a093b1d0d446ea1618dd3dd8d296cae1eed
[flightgear.git] / src / FDM / JSBSim / FGFDMExec.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  Header:       FGFDMExec.h
3  Author:       Jon Berndt
4  Date started: 11/17/98
5
6  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
7
8  This program is free software; you can redistribute it and/or modify it under
9  the terms of the GNU General Public License as published by the Free Software
10  Foundation; either version 2 of the License, or (at your option) any later
11  version.
12
13  This program is distributed in the hope that it will be useful, but WITHOUT
14  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  details.
17
18  You should have received a copy of the GNU General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA  02111-1307, USA.
21
22  Further information about the GNU General Public License can also be found on
23  the world wide web at http://www.gnu.org.
24
25 HISTORY
26 --------------------------------------------------------------------------------
27 11/17/98   JSB   Created
28 7/31/99     TP   Added RunIC function that runs the sim so that every frame
29                  begins with the IC values from the given FGInitialCondition
30                  object and dt=0.
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 SENTRY
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35
36 #ifndef FGFDMEXEC_HEADER_H
37 #define FGFDMEXEC_HEADER_H
38
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 INCLUDES
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
43 #include "FGModel.h"
44 #include "FGTrim.h"
45 #include "FGInitialCondition.h"
46 #include "FGJSBBase.h"
47 #include "FGPropertyManager.h"
48
49 #include <vector>
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 DEFINITIONS
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 #define ID_FDMEXEC "$Id$"
56
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 FORWARD DECLARATIONS
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60
61 namespace JSBSim {
62
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 CLASS DOCUMENTATION
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66
67 /** Encapsulates the JSBSim simulation executive.
68     This class is the interface class through which all other simulation classes
69     are instantiated, initialized, and run. When integrated with FlightGear (or
70     other flight simulator) this class is typically instantiated by an interface
71     class on the simulator side.
72
73     When an aircraft model is loaded the config file is parsed and for each of the
74     sections of the config file (propulsion, flight control, etc.) the
75     corresponding "ReadXXX()" method is called. From within this method the
76     "Load()" method of that system is called (e.g. LoadFCS).
77
78     <h4>JSBSim Debugging Directives</h4>
79
80     This describes to any interested entity the debug level
81     requested by setting the JSBSIM_DEBUG environment variable.
82     The bitmasked value choices are as follows:
83     - <b>unset</b>: In this case (the default) JSBSim would only print
84        out the normally expected messages, essentially echoing
85        the config files as they are read. If the environment
86        variable is not set, debug_lvl is set to 1 internally
87     - <b>0</b>: This requests JSBSim not to output any messages
88        whatsoever
89     - <b>1</b>: This value explicity requests the normal JSBSim
90        startup messages
91     - <b>2</b>: This value asks for a message to be printed out when
92        a class is instantiated
93     - <b>4</b>: When this value is set, a message is displayed when a
94        FGModel object executes its Run() method
95     - <b>8</b>: When this value is set, various runtime state variables
96        are printed out periodically
97     - <b>16</b>: When set various parameters are sanity checked and
98        a message is printed out when they go out of bounds
99
100     @author Jon S. Berndt
101     @version $Id$
102 */
103
104 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 CLASS DECLARATION
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
107
108 class FGFDMExec : public FGJSBBase
109 {
110 public:
111
112   /// Default constructor
113   FGFDMExec(FGPropertyManager* root = 0);
114
115   /// Default destructor
116   ~FGFDMExec();
117
118   /** This routine places a model into the runlist at the specified rate. The
119       "rate" is not really a clock rate. It represents how many calls to the
120       FGFDMExec::Run() method must be made before the model is executed. A
121       value of 1 means that the model will be executed for each call to the
122       exec's Run() method. A value of 5 means that the model will only be
123       executed every 5th call to the exec's Run() method.
124       @param model A pointer to the model being scheduled.
125       @param rate The rate at which to execute the model as described above.
126       @return Currently returns 0 always. */
127   int  Schedule(FGModel* model, int rate);
128
129   /** This executes each scheduled model in succession.
130       @return true if successful, false if sim should be ended  */
131   bool Run(void);
132
133   /** Initializes the sim from the initial condition object and executes
134       each scheduled model without integrating i.e. dt=0.
135       @return true if successful
136        */
137   bool RunIC(void);
138
139   /// Freezes the sim
140   void Freeze(void) {frozen = true;}
141
142   /// Resumes the sim
143   void Resume(void) {frozen = false;}
144
145   /** Loads an aircraft model.
146       @param AircraftPath path to the aircraft directory. For instance:
147       "aircraft". Under aircraft, then, would be directories for various
148       modeled aircraft such as C172/, x15/, etc.
149       @param EnginePath path to the directory under which engine config
150       files are kept, for instance "engine"
151       @param model the name of the aircraft model itself. This file will
152       be looked for in the directory specified in the AircraftPath variable,
153       and in turn under the directory with the same name as the model. For
154       instance: "aircraft/x15/x15.xml"
155       @param addModelToPath set to true to add the model name to the
156       AircraftPath, defaults to true
157       @return true if successful*/
158   bool LoadModel(string AircraftPath, string EnginePath, string model,
159                  bool addModelToPath = true);
160
161
162   /** Loads an aircraft model.  The paths to the aircraft and engine
163       config file directories must be set prior to calling this.  See
164       below.
165       @param model the name of the aircraft model itself. This file will
166       be looked for in the directory specified in the AircraftPath variable,
167       and in turn under the directory with the same name as the model. For
168       instance: "aircraft/x15/x15.xml"
169       @param addModelToPath set to true to add the model name to the
170       AircraftPath, defaults to true
171       @return true if successful*/
172   bool LoadModel(string model, bool addModelToPath = true);
173
174
175   /** Sets the path to the engine config file directories.
176       @param path path to the directory under which engine config
177       files are kept, for instance "engine"
178   */
179   bool SetEnginePath(string path)   { EnginePath = path; return true; }
180
181   /** Sets the path to the aircraft config file directories.
182       @param path path to the aircraft directory. For instance:
183       "aircraft". Under aircraft, then, would be directories for various
184       modeled aircraft such as C172/, x15/, etc.
185   */
186   bool SetAircraftPath(string path) { AircraftPath = path; return true; }
187
188   /** Sets the path to the autopilot config file directories.
189       @param path path to the control directory. For instance:
190       "control".
191   */
192 //  bool SetControlPath(string path) { ControlPath = path; return true; }
193
194
195   /// @name Top-level executive State and Model retrieval mechanism
196   //@{
197   /// Returns the FGState pointer.
198   inline FGState* GetState(void)              {return State;}
199   /// Returns the FGAtmosphere pointer.
200   inline FGAtmosphere* GetAtmosphere(void)    {return Atmosphere;}
201   /// Returns the FGFCS pointer.
202   inline FGFCS* GetFCS(void)                  {return FCS;}
203   /// Returns the FGPropulsion pointer.
204   inline FGPropulsion* GetPropulsion(void)    {return Propulsion;}
205   /// Returns the FGAircraft pointer.
206   inline FGMassBalance* GetMassBalance(void)  {return MassBalance;}
207   /// Returns the FGAerodynamics pointer
208   inline FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
209   /// Returns the FGInertial pointer.
210   inline FGInertial* GetInertial(void)        {return Inertial;}
211   /// Returns the FGGroundReactions pointer.
212   inline FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
213   /// Returns the FGAircraft pointer.
214   inline FGAircraft* GetAircraft(void)        {return Aircraft;}
215   /// Returns the FGPropagate pointer.
216   inline FGPropagate* GetPropagate(void)        {return Propagate;}
217   /// Returns the FGAuxiliary pointer.
218   inline FGAuxiliary* GetAuxiliary(void)      {return Auxiliary;}
219   /// Returns the FGOutput pointer.
220   inline FGOutput* GetOutput(void)            {return Output;}
221   // Returns a pointer to the FGInitialCondition object
222   inline FGInitialCondition* GetIC(void)      {return IC;}
223   // Returns a pointer to the FGTrim object
224   FGTrim* GetTrim(void);
225   //@}
226
227   /// Retrieves the engine path.
228   inline string GetEnginePath(void)          {return EnginePath;}
229   /// Retrieves the aircraft path.
230   inline string GetAircraftPath(void)        {return AircraftPath;}
231 //  /// Retrieves the control path.
232 //  inline string GetControlPath(void)        {return ControlPath;}
233
234   string GetModelName(void) { return modelName; }
235
236   FGPropertyManager* GetPropertyManager(void);
237   vector <string> EnumerateFDMs(void);
238   void SetSlave(void) {IsSlave = true;}
239
240 private:
241   FGModel* FirstModel;
242
243   bool frozen;
244   bool terminate;
245   int  Error;
246   unsigned int Frame;
247   unsigned int IdFDM;
248   static unsigned int FDMctr;
249   bool modelLoaded;
250   string modelName;
251   bool IsSlave;
252   static FGPropertyManager *master;
253   FGPropertyManager *instance;
254
255   struct slaveData {
256     FGFDMExec* exec;
257     string info;
258     double x, y, z;
259     double roll, pitch, yaw;
260     bool mated;
261
262     slaveData(void) {
263       info = "";
264       x = y = z = 0.0;
265       roll = pitch = yaw = 0.0;
266       mated = true;
267     }
268
269     ~slaveData(void) {
270       delete exec;
271     }
272   };
273
274   string AircraftPath;
275   string EnginePath;
276 //  string ControlPath;
277
278   string CFGVersion;
279   string Release;
280
281   FGState*           State;
282   FGAtmosphere*      Atmosphere;
283   FGFCS*             FCS;
284   FGPropulsion*      Propulsion;
285   FGMassBalance*     MassBalance;
286   FGAerodynamics*    Aerodynamics;
287   FGInertial*        Inertial;
288   FGGroundReactions* GroundReactions;
289   FGAircraft*        Aircraft;
290   FGPropagate*       Propagate;
291   FGAuxiliary*       Auxiliary;
292   FGOutput*          Output;
293
294   FGInitialCondition* IC;
295   FGTrim *Trim;
296
297   vector <slaveData*> SlaveFDMList;
298
299   bool ReadMetrics(FGConfigFile*);
300   bool ReadSlave(FGConfigFile*);
301   bool ReadPropulsion(FGConfigFile*);
302   bool ReadFlightControls(FGConfigFile*);
303   bool ReadAerodynamics(FGConfigFile*);
304   bool ReadUndercarriage(FGConfigFile*);
305   bool ReadPrologue(FGConfigFile*);
306   bool ReadOutput(FGConfigFile*);
307
308   bool Allocate(void);
309   bool DeAllocate(void);
310   void Debug(int from);
311 };
312 }
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 #endif
315