]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.h
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[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 <models/FGModel.h>
44 #include <models/FGOutput.h>
45 #include <models/FGInput.h>
46 #include <initialization/FGTrim.h>
47 #include <initialization/FGInitialCondition.h>
48 #include <FGJSBBase.h>
49 #include <input_output/FGPropertyManager.h>
50 #include <input_output/FGXMLParse.h>
51 #include <input_output/FGGroundCallback.h>
52 #include <models/FGPropagate.h>
53
54 #include <vector>
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 DEFINITIONS
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 #define ID_FDMEXEC "$Id$"
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 FORWARD DECLARATIONS
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 namespace JSBSim {
67
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 CLASS DOCUMENTATION
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71
72 /** Encapsulates the JSBSim simulation executive.
73     This class is the executive class through which all other simulation classes
74     are instantiated, initialized, and run. When integrated with FlightGear (or
75     other flight simulator) this class is typically instantiated by an interface
76     class on the simulator side.
77
78     At the time of simulation initialization, the interface
79     class creates an instance of this executive class. The
80     executive is subsequently directed to load the chosen aircraft specification
81     file:
82
83     @code
84     fdmex = new FGFDMExec( \85 );
85     result = fdmex->LoadModel( \85 );
86     @endcode
87
88     When an aircraft model is loaded, the config file is parsed and for each of the
89     sections of the config file (propulsion, flight control, etc.) the
90     corresponding Load() method is called (e.g. FGFCS::Load()).
91
92     Subsequent to the creation of the executive and loading of the model,
93     initialization is performed. Initialization involves copying control inputs
94     into the appropriate JSBSim data storage locations, configuring it for the set
95     of user supplied initial conditions, and then copying state variables from
96     JSBSim. The state variables are used to drive the instrument displays and to
97     place the vehicle model in world space for visual rendering:
98
99     @code
100     copy_to_JSBsim(); // copy control inputs to JSBSim
101     fdmex->RunIC(); // loop JSBSim once w/o integrating
102     copy_from_JSBsim(); // update the bus
103     @endcode
104
105     Once initialization is complete, cyclic execution proceeds:
106
107     @code
108     copy_to_JSBsim(); // copy control inputs to JSBSim
109     fdmex->Run(); // execute JSBSim
110     copy_from_JSBsim(); // update the bus
111     @endcode
112
113     JSBSim can be used in a standalone mode by creating a compact stub program
114     that effectively performs the same progression of steps as outlined above for
115     the integrated version, but with two exceptions. First, the copy_to_JSBSim()
116     and copy_from_JSBSim() functions are not used because the control inputs are
117     handled directly by the scripting facilities and outputs are handled by the
118     output (data logging) class. Second, the name of a script file can be supplied
119     to the stub program. Scripting (see FGScript) provides a way to supply command
120     inputs to the simulation:
121
122     @code
123     FDMExec = new JSBSim::FGFDMExec();
124     Script = new JSBSim::FGScript( \85 );
125     Script->LoadScript( ScriptName ); // the script loads the aircraft and ICs
126     result = FDMExec->Run();
127     while (result) { // cyclic execution
128       if (Scripted) if (!Script->RunScript()) break; // execute script
129       result = FDMExec->Run(); // execute JSBSim
130     }
131     @endcode
132
133     The standalone mode has been useful for verifying changes before committing
134     updates to the source code repository. It is also useful for running sets of
135     tests that reveal some aspects of simulated aircraft performance, such as
136     range, time-to-climb, takeoff distance, etc.
137
138     <h3>JSBSim Debugging Directives</h3>
139
140     This describes to any interested entity the debug level
141     requested by setting the JSBSIM_DEBUG environment variable.
142     The bitmasked value choices are as follows:
143     - <b>unset</b>: In this case (the default) JSBSim would only print
144        out the normally expected messages, essentially echoing
145        the config files as they are read. If the environment
146        variable is not set, debug_lvl is set to 1 internally
147     - <b>0</b>: This requests JSBSim not to output any messages
148        whatsoever
149     - <b>1</b>: This value explicity requests the normal JSBSim
150        startup messages
151     - <b>2</b>: This value asks for a message to be printed out when
152        a class is instantiated
153     - <b>4</b>: When this value is set, a message is displayed when a
154        FGModel object executes its Run() method
155     - <b>8</b>: When this value is set, various runtime state variables
156        are printed out periodically
157     - <b>16</b>: When set various parameters are sanity checked and
158        a message is printed out when they go out of bounds
159
160     <h3>Properties</h3>
161     @property simulator/do_trim (write only) Can be set to the integer equivalent to one of
162                                 tLongitudinal (0), tFull (1), tGround (2), tPullup (3),
163                                 tCustom (4), tTurn (5). Setting this to a legal value
164                                 (such as by a script) causes a trim to be performed. This
165                                 property actually maps toa function call of DoTrim().
166
167     @author Jon S. Berndt
168     @version $Revision$
169 */
170
171 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 CLASS DECLARATION
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
174
175 class FGFDMExec : public FGJSBBase
176 {
177 public:
178
179   /// Default constructor
180   FGFDMExec(FGPropertyManager* root = 0);
181
182   /// Default destructor
183   ~FGFDMExec();
184
185   /** This routine places a model into the runlist at the specified rate. The
186       "rate" is not really a clock rate. It represents how many calls to the
187       FGFDMExec::Run() method must be made before the model is executed. A
188       value of 1 means that the model will be executed for each call to the
189       exec's Run() method. A value of 5 means that the model will only be
190       executed every 5th call to the exec's Run() method. Use of a rate other than
191       one is at this time not recommended.
192       @param model A pointer to the model being scheduled.
193       @param rate The rate at which to execute the model as described above.
194       @return Currently returns 0 always. */
195   int  Schedule(FGModel* model, int rate);
196
197   /** This function executes each scheduled model in succession.
198       @return true if successful, false if sim should be ended  */
199   bool Run(void);
200
201   /** Initializes the sim from the initial condition object and executes
202       each scheduled model without integrating i.e. dt=0.
203       @return true if successful */
204   bool RunIC(void);
205
206   /** Sets the ground callback pointer.
207       @param gc A pointer to a ground callback object.  */
208   void SetGroundCallback(FGGroundCallback* gc);
209
210   /** Loads an aircraft model.
211       @param AircraftPath path to the aircraft/ directory. For instance:
212       "aircraft". Under aircraft, then, would be directories for various
213       modeled aircraft such as C172/, x15/, etc.
214       @param EnginePath path to the directory under which engine config
215       files are kept, for instance "engine"
216       @param model the name of the aircraft model itself. This file will
217       be looked for in the directory specified in the AircraftPath variable,
218       and in turn under the directory with the same name as the model. For
219       instance: "aircraft/x15/x15.xml"
220       @param addModelToPath set to true to add the model name to the
221       AircraftPath, defaults to true
222       @return true if successful */
223   bool LoadModel(string AircraftPath, string EnginePath, string model,
224                  bool addModelToPath = true);
225
226   /** Loads an aircraft model.  The paths to the aircraft and engine
227       config file directories must be set prior to calling this.  See
228       below.
229       @param model the name of the aircraft model itself. This file will
230       be looked for in the directory specified in the AircraftPath variable,
231       and in turn under the directory with the same name as the model. For
232       instance: "aircraft/x15/x15.xml"
233       @param addModelToPath set to true to add the model name to the
234       AircraftPath, defaults to true
235       @return true if successful*/
236   bool LoadModel(string model, bool addModelToPath = true);
237
238   /** Sets the path to the engine config file directories.
239       @param path path to the directory under which engine config
240       files are kept, for instance "engine"  */
241   bool SetEnginePath(string path)   { EnginePath = path; return true; }
242
243   /** Sets the path to the aircraft config file directories.
244       @param path path to the aircraft directory. For instance:
245       "aircraft". Under aircraft, then, would be directories for various
246       modeled aircraft such as C172/, x15/, etc.  */
247   bool SetAircraftPath(string path) { AircraftPath = path; return true; }
248
249   /// @name Top-level executive State and Model retrieval mechanism
250   //@{
251   /// Returns the FGAtmosphere pointer.
252   inline FGAtmosphere* GetAtmosphere(void)    {return Atmosphere;}
253   /// Returns the FGFCS pointer.
254   inline FGFCS* GetFCS(void)                  {return FCS;}
255   /// Returns the FGPropulsion pointer.
256   inline FGPropulsion* GetPropulsion(void)    {return Propulsion;}
257   /// Returns the FGAircraft pointer.
258   inline FGMassBalance* GetMassBalance(void)  {return MassBalance;}
259   /// Returns the FGAerodynamics pointer
260   inline FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
261   /// Returns the FGInertial pointer.
262   inline FGInertial* GetInertial(void)        {return Inertial;}
263   /// Returns the FGGroundReactions pointer.
264   inline FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
265   /// Returns the FGAircraft pointer.
266   inline FGAircraft* GetAircraft(void)        {return Aircraft;}
267   /// Returns the FGPropagate pointer.
268   inline FGPropagate* GetPropagate(void)      {return Propagate;}
269   /// Returns the FGAuxiliary pointer.
270   inline FGAuxiliary* GetAuxiliary(void)      {return Auxiliary;}
271   /// Returns the FGInput pointer.
272   inline FGInput* GetInput(void)              {return Input;}
273   /// Returns the FGGroundCallback pointer.
274   inline FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
275   /// Returns the FGState pointer.
276   inline FGState* GetState(void)              {return State;}
277   // Returns a pointer to the FGInitialCondition object
278   inline FGInitialCondition* GetIC(void)      {return IC;}
279   // Returns a pointer to the FGTrim object
280   inline FGTrim* GetTrim(void);
281   //@}
282
283   /// Retrieves the engine path.
284   inline string GetEnginePath(void)          {return EnginePath;}
285   /// Retrieves the aircraft path.
286   inline string GetAircraftPath(void)        {return AircraftPath;}
287
288   /// Returns the model name.
289   string GetModelName(void) { return modelName; }
290
291   /// Returns a pointer to the property manager object.
292   FGPropertyManager* GetPropertyManager(void);
293   /// Returns a vector of strings representing the names of all loaded models (future)
294   vector <string> EnumerateFDMs(void);
295   /// Marks this instance of the Exec object as a "slave" object.
296   void SetSlave(void) {IsSlave = true;}
297
298   /** Executes trimming in the selected mode.
299   *   @param mode Specifies how to trim:
300   * - tLongitudinal=0
301   * - tFull
302   * - tGround
303   * - tPullup
304   * - tCustom
305   * - tTurn
306   * - tNone  */
307   void DoTrim(int mode);
308
309   /// Disables data logging to all outputs.
310   void DisableOutput(void);
311   /// Enables data logging to all outputs.
312   void EnableOutput(void);
313   /// Pauses execution by preventing time from incrementing.
314   void Hold(void) {holding = true;}
315   /// Resumes execution from a "Hold".
316   void Resume(void) {holding = false;}
317   /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
318   bool Holding(void) {return holding;}
319
320   struct PropertyCatalogStructure {
321     /// Name of the property.
322     string base_string;
323     /// The node for the property.
324     FGPropertyManager *node;
325   };
326
327   /** Builds a catalog of properties.
328   *   This function descends the property tree and creates a list (an STL vector)
329   *   containing the name and node for all properties.
330   *   @param pcs The "root" property catalog structure pointer.  */
331   void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
332
333   /** Retrieves property or properties matching the supplied string.
334   *   A string is returned that contains a carriage return delimited list of all
335   *   strings in the property catalog that matches the supplied check string.
336   *   @param check The string to search for in the property catalog.
337   *   @return the carriage-return-delimited string containing all matching strings
338   *               in the catalog.  */
339   string QueryPropertyCatalog(string check);
340
341   /// Use the MSIS atmosphere model.
342   void UseAtmosphereMSIS(void);
343
344   /// Use the Mars atmosphere model. (Not operative yet.)
345   void UseAtmosphereMars(void);
346
347 private:
348   FGModel* FirstModel;
349
350   bool terminate;
351   bool holding;
352   bool Constructing;
353   int  Error;
354   unsigned int Frame;
355   unsigned int IdFDM;
356   FGPropertyManager* Root;
357   static unsigned int FDMctr;
358   bool modelLoaded;
359   string modelName;
360   bool IsSlave;
361   static FGPropertyManager *master;
362   FGPropertyManager *instance;
363   vector <string> PropertyCatalog;
364
365   struct slaveData {
366     FGFDMExec* exec;
367     string info;
368     double x, y, z;
369     double roll, pitch, yaw;
370     bool mated;
371
372     slaveData(void) {
373       info = "";
374       x = y = z = 0.0;
375       roll = pitch = yaw = 0.0;
376       mated = true;
377     }
378
379     ~slaveData(void) {
380       delete exec;
381     }
382   };
383
384   string AircraftPath;
385   string EnginePath;
386
387   string CFGVersion;
388   string Release;
389
390   FGGroundCallback*  GroundCallback;
391   FGState*           State;
392   FGAtmosphere*      Atmosphere;
393   FGFCS*             FCS;
394   FGPropulsion*      Propulsion;
395   FGMassBalance*     MassBalance;
396   FGAerodynamics*    Aerodynamics;
397   FGInertial*        Inertial;
398   FGGroundReactions* GroundReactions;
399   FGAircraft*        Aircraft;
400   FGPropagate*       Propagate;
401   FGAuxiliary*       Auxiliary;
402   FGInput*           Input;
403   vector <FGOutput*> Outputs;
404
405   FGInitialCondition* IC;
406   FGTrim *Trim;
407
408   vector <slaveData*> SlaveFDMList;
409
410   bool ReadFileHeader(Element*);
411   bool ReadSlave(Element*);
412   bool ReadPrologue(Element*);
413
414   bool Allocate(void);
415   bool DeAllocate(void);
416   void Debug(int from);
417 };
418 }
419 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 #endif
421