]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.h
Sync. w. JSBSim
[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 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.
165
166     @author Jon S. Berndt
167     @version $Revision$
168 */
169
170 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 CLASS DECLARATION
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
173
174 class FGFDMExec : public FGJSBBase
175 {
176 public:
177
178   /// Default constructor
179   FGFDMExec(FGPropertyManager* root = 0);
180
181   /// Default destructor
182   ~FGFDMExec();
183
184   /** This routine places a model into the runlist at the specified rate. The
185       "rate" is not really a clock rate. It represents how many calls to the
186       FGFDMExec::Run() method must be made before the model is executed. A
187       value of 1 means that the model will be executed for each call to the
188       exec's Run() method. A value of 5 means that the model will only be
189       executed every 5th call to the exec's Run() method. Use of a rate other than
190       one is at this time not recommended.
191       @param model A pointer to the model being scheduled.
192       @param rate The rate at which to execute the model as described above.
193       @return Currently returns 0 always. */
194   int  Schedule(FGModel* model, int rate);
195
196   /** This function executes each scheduled model in succession.
197       @return true if successful, false if sim should be ended  */
198   bool Run(void);
199
200   /** Initializes the sim from the initial condition object and executes
201       each scheduled model without integrating i.e. dt=0.
202       @return true if successful */
203   bool RunIC(void);
204
205   /** Sets the ground callback pointer.
206       @param gc A pointer to a ground callback object.  */
207   void SetGroundCallback(FGGroundCallback* gc);
208
209   /** Loads an aircraft model.
210       @param AircraftPath path to the aircraft/ directory. For instance:
211       "aircraft". Under aircraft, then, would be directories for various
212       modeled aircraft such as C172/, x15/, etc.
213       @param EnginePath path to the directory under which engine config
214       files are kept, for instance "engine"
215       @param model the name of the aircraft model itself. This file will
216       be looked for in the directory specified in the AircraftPath variable,
217       and in turn under the directory with the same name as the model. For
218       instance: "aircraft/x15/x15.xml"
219       @param addModelToPath set to true to add the model name to the
220       AircraftPath, defaults to true
221       @return true if successful */
222   bool LoadModel(string AircraftPath, string EnginePath, string model,
223                  bool addModelToPath = true);
224
225   /** Loads an aircraft model.  The paths to the aircraft and engine
226       config file directories must be set prior to calling this.  See
227       below.
228       @param model the name of the aircraft model itself. This file will
229       be looked for in the directory specified in the AircraftPath variable,
230       and in turn under the directory with the same name as the model. For
231       instance: "aircraft/x15/x15.xml"
232       @param addModelToPath set to true to add the model name to the
233       AircraftPath, defaults to true
234       @return true if successful*/
235   bool LoadModel(string model, bool addModelToPath = true);
236
237   /** Sets the path to the engine config file directories.
238       @param path path to the directory under which engine config
239       files are kept, for instance "engine"  */
240   bool SetEnginePath(string path)   { EnginePath = path; return true; }
241
242   /** Sets the path to the aircraft config file directories.
243       @param path path to the aircraft directory. For instance:
244       "aircraft". Under aircraft, then, would be directories for various
245       modeled aircraft such as C172/, x15/, etc.  */
246   bool SetAircraftPath(string path) { AircraftPath = path; return true; }
247
248   /// @name Top-level executive State and Model retrieval mechanism
249   //@{
250   /// Returns the FGAtmosphere pointer.
251   inline FGAtmosphere* GetAtmosphere(void)    {return Atmosphere;}
252   /// Returns the FGFCS pointer.
253   inline FGFCS* GetFCS(void)                  {return FCS;}
254   /// Returns the FGPropulsion pointer.
255   inline FGPropulsion* GetPropulsion(void)    {return Propulsion;}
256   /// Returns the FGAircraft pointer.
257   inline FGMassBalance* GetMassBalance(void)  {return MassBalance;}
258   /// Returns the FGAerodynamics pointer
259   inline FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
260   /// Returns the FGInertial pointer.
261   inline FGInertial* GetInertial(void)        {return Inertial;}
262   /// Returns the FGGroundReactions pointer.
263   inline FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
264   /// Returns the FGAircraft pointer.
265   inline FGAircraft* GetAircraft(void)        {return Aircraft;}
266   /// Returns the FGPropagate pointer.
267   inline FGPropagate* GetPropagate(void)      {return Propagate;}
268   /// Returns the FGAuxiliary pointer.
269   inline FGAuxiliary* GetAuxiliary(void)      {return Auxiliary;}
270   /// Returns the FGInput pointer.
271   inline FGInput* GetInput(void)              {return Input;}
272   /// Returns the FGGroundCallback pointer.
273   inline FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
274   /// Returns the FGState pointer.
275   inline FGState* GetState(void)              {return State;}
276   // Returns a pointer to the FGInitialCondition object
277   inline FGInitialCondition* GetIC(void)      {return IC;}
278   // Returns a pointer to the FGTrim object
279   inline FGTrim* GetTrim(void);
280   //@}
281
282   /// Retrieves the engine path.
283   inline string GetEnginePath(void)          {return EnginePath;}
284   /// Retrieves the aircraft path.
285   inline string GetAircraftPath(void)        {return AircraftPath;}
286
287   /// Returns the model name.
288   string GetModelName(void) { return modelName; }
289
290   /// Returns a pointer to the property manager object.
291   FGPropertyManager* GetPropertyManager(void);
292   /// Returns a vector of strings representing the names of all loaded models (future)
293   vector <string> EnumerateFDMs(void);
294   /// Marks this instance of the Exec object as a "slave" object.
295   void SetSlave(void) {IsSlave = true;}
296
297   /** Executes trimming in the selected mode.
298   *   @param mode Specifies how to trim:
299   * - tLongitudinal=0
300   * - tFull
301   * - tGround
302   * - tPullup
303   * - tCustom
304   * - tTurn
305   * - tNone  */
306   void DoTrim(int mode);
307
308   /// Disables data logging to all outputs.
309   void DisableOutput(void);
310   /// Enables data logging to all outputs.
311   void EnableOutput(void);
312   /// Pauses execution by preventing time from incrementing.
313   void Hold(void) {holding = true;}
314   /// Resumes execution from a "Hold".
315   void Resume(void) {holding = false;}
316   /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
317   bool Holding(void) {return holding;}
318
319   struct PropertyCatalogStructure {
320     /// Name of the property.
321     string base_string;
322     /// The node for the property.
323     FGPropertyManager *node;
324   };
325
326   /** Builds a catalog of properties.
327   *   This function descends the property tree and creates a list (an STL vector)
328   *   containing the name and node for all properties.
329   *   @param pcs The "root" property catalog structure pointer.  */
330   void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
331
332   /** Retrieves property or properties matching the supplied string.
333   *   A string is returned that contains a carriage return delimited list of all
334   *   strings in the property catalog that matches the supplied check string.
335   *   @param check The string to search for in the property catalog.
336   *   @return the carriage-return-delimited string containing all matching strings
337   *               in the catalog.  */
338   string QueryPropertyCatalog(string check);
339
340   /// Use the MSIS atmosphere model.
341   void UseAtmosphereMSIS(void);
342
343   /// Use the Mars atmosphere model. (Not operative yet.)
344   void UseAtmosphereMars(void);
345
346 private:
347   FGModel* FirstModel;
348
349   bool terminate;
350   bool holding;
351   bool Constructing;
352   int  Error;
353   unsigned int Frame;
354   unsigned int IdFDM;
355   FGPropertyManager* Root;
356   static unsigned int FDMctr;
357   bool modelLoaded;
358   string modelName;
359   bool IsSlave;
360   static FGPropertyManager *master;
361   FGPropertyManager *instance;
362   vector <string> PropertyCatalog;
363
364   struct slaveData {
365     FGFDMExec* exec;
366     string info;
367     double x, y, z;
368     double roll, pitch, yaw;
369     bool mated;
370
371     slaveData(void) {
372       info = "";
373       x = y = z = 0.0;
374       roll = pitch = yaw = 0.0;
375       mated = true;
376     }
377
378     ~slaveData(void) {
379       delete exec;
380     }
381   };
382
383   string AircraftPath;
384   string EnginePath;
385
386   string CFGVersion;
387   string Release;
388
389   FGGroundCallback*  GroundCallback;
390   FGState*           State;
391   FGAtmosphere*      Atmosphere;
392   FGFCS*             FCS;
393   FGPropulsion*      Propulsion;
394   FGMassBalance*     MassBalance;
395   FGAerodynamics*    Aerodynamics;
396   FGInertial*        Inertial;
397   FGGroundReactions* GroundReactions;
398   FGAircraft*        Aircraft;
399   FGPropagate*       Propagate;
400   FGAuxiliary*       Auxiliary;
401   FGInput*           Input;
402   vector <FGOutput*> Outputs;
403
404   FGInitialCondition* IC;
405   FGTrim *Trim;
406
407   vector <slaveData*> SlaveFDMList;
408
409   bool ReadFileHeader(Element*);
410   bool ReadSlave(Element*);
411   bool ReadPrologue(Element*);
412
413   bool Allocate(void);
414   bool DeAllocate(void);
415   void Debug(int from);
416 };
417 }
418 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 #endif
420