]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.h
b5a3ee7cf7023d38d28c6ae7c18c44cf3be97cff
[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 "FGInitialCondition.h"
45 #include <vector>
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 DEFINITIONS
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 #define ID_FDMEXEC "$Id$"
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 FORWARD DECLARATIONS
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 class FGState;
58 class FGAtmosphere;
59 class FGFCS;
60 class FGPropulsion;
61 class FGMassBalance;
62 class FGAerodynamics;
63 class FGInertial;
64 class FGAircraft;
65 class FGTranslation;
66 class FGRotation;
67 class FGPosition;
68 class FGAuxiliary;
69 class FGOutput;
70 class FGInitialCondition;
71
72 struct condition {
73   vector <eParam>  TestParam;
74   vector <eParam>  SetParam;
75   vector <float>   TestValue;
76   vector <float>   SetValue;
77   vector <string>  Comparison;
78   vector <float>   TC;
79   vector <bool>    Persistent;
80   vector <eAction> Action;
81   vector <eType>   Type;
82   vector <bool>    Triggered;
83   vector <float>   newValue;
84   vector <float>   OriginalValue;
85   vector <float>   StartTime;
86   vector <float>   EndTime;
87
88   condition() {
89   }
90 };
91
92 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
95
96 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 CLASS DOCUMENTATION
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
99
100 /** Encapsulates the JSBSim simulation executive.
101     @author Jon S. Berndt
102     @version $Id$
103
104     @doc This class is the interface class through which all other simulation classes
105     are instantiated, initialized, and run. When integrated with FlightGear (or
106     other flight simulator) this class is typically instantiated by an interface
107     class on the simulator side.
108
109     <h4>Scripting support provided in the Executive</h4>
110
111     <p>There is simple scripting support provided in the FGFDMExec
112     class. Commands are specified using the <u>Simple Scripting
113     Directives for JSBSim</u> (SSDJ). The script file is in XML
114     format. A test condition (or conditions) can be set up in the
115     script and when the condition evaluates to true, the specified
116     action[s] is/are taken. A test condition can be <em>persistent</em>,
117     meaning that if a test condition evaluates to true, then passes
118     and evaluates to false, the condition is reset and may again be
119     triggered. When the set of tests evaluates to true for a given
120     condition, an item may be set to another value. This value might
121     be a boolean, a value, or a delta value, and the change from the
122     current value to the new value can be either via a step function,
123     a ramp, or an exponential approach. The speed of a ramp or
124     approach is specified via the time constant. Here is the format
125     of the script file:</p>
126
127     <pre><strong>&lt;?xml version=&quot;1.0&quot;?&gt;
128     &lt;runscript name=&quot;C172-01A&quot;&gt;
129
130     &lt;!--
131     This run is for testing C172 runs
132     --&gt;
133
134     &lt;use aircraft=&quot;c172&quot;&gt;
135     &lt;use initialize=&quot;reset00&quot;&gt;
136
137     &lt;run start=&quot;0.0&quot; end=&quot;4.5&quot; dt=&quot;0.05&quot;&gt;
138       &lt;when&gt;
139         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;ge&quot; value=&quot;0.25&quot;&gt;
140         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;le&quot; value=&quot;0.50&quot;&gt;
141         &lt;set name=&quot;FG_AILERON_CMD&quot; type=&quot;FG_VALUE&quot; value=&quot;0.25&quot;
142         action=&quot;FG_STEP&quot; persistent=&quot;false&quot; tc =&quot;0.25&quot;&gt;
143       &lt;/when&gt;
144       &lt;when&gt;
145         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;ge&quot; value=&quot;0.5&quot;&gt;
146         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;le&quot; value=&quot;1.5&quot;&gt;
147         &lt;set name=&quot;FG_AILERON_CMD&quot; type=&quot;FG_DELTA&quot; value=&quot;0.5&quot;
148         action=&quot;FG_EXP&quot; persistent=&quot;false&quot; tc =&quot;0.5&quot;&gt;
149       &lt;/when&gt;
150       &lt;when&gt;
151         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;ge&quot; value=&quot;1.5&quot;&gt;
152         &lt;parameter name=&quot;FG_TIME&quot; comparison=&quot;le&quot; value=&quot;2.5&quot;&gt;
153         &lt;set name=&quot;FG_RUDDER_CMD&quot; type=&quot;FG_DELTA&quot; value=&quot;0.5&quot;
154         action=&quot;FG_RAMP&quot; persistent=&quot;false&quot; tc =&quot;0.5&quot;&gt;
155       &lt;/when&gt;
156     &lt;/run&gt;
157
158     &lt;/runscript&gt;</strong></pre>
159
160     <p>The first line must always be present. The second line
161     identifies this file as a script file, and gives a descriptive
162     name to the script file. Comments are next, delineated by the
163     &lt;!-- and --&gt; symbols. The aircraft and initialization files
164     to be used are specified in the &quot;use&quot; lines. Next,
165     comes the &quot;run&quot; section, where the conditions are
166     described in &quot;when&quot; clauses.</p>
167
168     <h4>JSBSim Debugging Directives</h4>
169
170     This describes to any interested entity the debug level
171     requested by setting the JSBSIM_DEBUG environment variable.
172     The bitmasked value choices are as follows:<ol>
173     <li><b>unset</b>: In this case (the default) JSBSim would only print
174        out the normally expected messages, essentially echoing
175        the config files as they are read. If the environment
176        variable is not set, debug_lvl is set to 1 internally</li>
177     <li><b>0</b>: This requests JSBSim not to output any messages
178        whatsoever.</li>
179     <li><b>1</b>: This value explicity requests the normal JSBSim
180        startup messages</li>
181     <li><b>2</b>: This value asks for a message to be printed out when
182        a class is instantiated</li>
183     <li><b>4</b>: When this value is set, a message is displayed when a
184        FGModel object executes its Run() method</li>
185     <li><b>8</b>: When this value is set, various runtime state variables
186        are printed out periodically</li>
187     <li><b>16</b>: When set various parameters are sanity checked and
188        a message is printed out when they go out of bounds</li>
189     </ol>
190
191 */
192
193 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 CLASS DECLARATION
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
196
197 class FGFDMExec
198 {
199 public:
200   /// Default constructor
201   FGFDMExec(void);
202
203   /// Default destructor
204   ~FGFDMExec();
205
206   /** This routine places a model into the runlist at the specified rate. The
207       "rate" is not really a clock rate. It represents how many calls to the
208       FGFDMExec::Run() method must be made before the model is executed. A
209       value of 1 means that the model will be executed for each call to the
210       exec's Run() method. A value of 5 means that the model will only be
211       executed every 5th call to the exec's Run() method.
212       @param model A pointer to the model being scheduled.
213       @param rate The rate at which to execute the model as described above.
214       @return Currently returns 0 always. */
215   int  Schedule(FGModel* model, int rate);
216
217   /** This executes each scheduled model in succession, as well as running any
218       scripts which are loaded.
219       @return true if successful, false if sim should be ended  */
220   bool Run(void);
221
222   /** Initializes the sim with a set of initial conditions.
223       @param fgic A pointer to a filled out initial conditions class which
224       describes the desired initial conditions.
225       @return true if successful
226        */
227   bool RunIC(FGInitialCondition *fgic);
228
229   /// Freezes the sim
230   void Freeze(void) {frozen = true;}
231
232   /// Resumes the sim
233   void Resume(void) {frozen = false;}
234
235   /** Loads an aircraft model.
236       @param AircraftPath path to the aircraft directory. For instance:
237       "aircraft". Under aircraft, then, would be directories for various
238       modeled aircraft such as C172/, x15/, etc.
239       @param EnginePath path to the directory under which engine config
240       files are kept, for instance "engine"
241       @param model the name of the aircraft model itself. This file will
242       be looked for in the directory specified in the AircraftPath variable,
243       and in turn under the directory with the same name as the model. For
244       instance: "aircraft/x15/x15.xml"
245       @return true if successful*/
246   bool LoadModel(string AircraftPath, string EnginePath, string model);
247
248   /** Loads a script to drive JSBSim (usually in standalone mode).
249       The language is the Simple Script Directives for JSBSim (SSDJ).
250       @param script the filename (including path name, if any) for the script.
251       @return true if successful */
252   bool LoadScript(string script);
253
254   /** This function is called each pass through the executive Run() method IF
255       scripting is enabled. */
256   void RunScript(void);
257
258   bool SetEnginePath(string path)   {EnginePath = path; return true;}
259   bool SetAircraftPath(string path) {AircraftPath = path; return true;}
260   bool SetScriptPath(string path)   {ScriptPath = path; return true;}
261
262   /// @name Top-level executive State and Model retrieval mechanism
263   //@{
264   /// Returns the FGState pointer.
265   inline FGState* GetState(void)              {return State;}
266   /// Returns the FGAtmosphere pointer.
267   inline FGAtmosphere* GetAtmosphere(void)    {return Atmosphere;}
268   /// Returns the FGFCS pointer.
269   inline FGFCS* GetFCS(void)                  {return FCS;}
270   /// Returns the FGPropulsion pointer.
271   inline FGPropulsion* GetPropulsion(void)    {return Propulsion;}
272   /// Returns the FGAircraft pointer.
273   inline FGMassBalance* GetMassBalance(void)  {return MassBalance;}
274   /// Returns the FGAerodynamics pointer
275   inline FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
276   /// Returns the FGInertial pointer.
277   inline FGInertial* GetInertial(void)        {return Inertial;}
278   /// Returns the FGAircraft pointer.
279   inline FGAircraft* GetAircraft(void)        {return Aircraft;}
280   /// Returns the FGTranslation pointer.
281   inline FGTranslation* GetTranslation(void)  {return Translation;}
282   /// Returns the FGRotation pointer.
283   inline FGRotation* GetRotation(void)        {return Rotation;}
284   /// Returns the FGPosition pointer.
285   inline FGPosition* GetPosition(void)        {return Position;}
286   /// Returns the FGAuxiliary pointer.
287   inline FGAuxiliary* GetAuxiliary(void)      {return Auxiliary;}
288   /// Returns the FGOutput pointer.
289   inline FGOutput* GetOutput(void)            {return Output;}
290   //@}
291
292   /// Retrieves the engine path.
293   inline string GetEnginePath(void)          {return EnginePath;}
294   /// Retrieves the aircraft path.
295   inline string GetAircraftPath(void)        {return AircraftPath;}
296
297 private:
298   FGModel* FirstModel;
299
300   bool frozen;
301   bool terminate;
302   int  Error;
303   int  Frame;
304   bool modelLoaded;
305   bool Scripted;
306
307   string AircraftPath;
308   string EnginePath;
309   string ScriptPath;
310   string ScriptName;
311   float  StartTime;
312   float  EndTime;
313   vector <struct condition> Conditions;
314
315   FGState*        State;
316   FGAtmosphere*   Atmosphere;
317   FGFCS*          FCS;
318   FGPropulsion*   Propulsion;
319   FGMassBalance*  MassBalance;
320   FGAerodynamics* Aerodynamics;
321   FGInertial*     Inertial;
322   FGAircraft*     Aircraft;
323   FGTranslation*  Translation;
324   FGRotation*     Rotation;
325   FGPosition*     Position;
326   FGAuxiliary*    Auxiliary;
327   FGOutput*       Output;
328
329   bool Allocate(void);
330   bool DeAllocate(void);
331   void Debug(void);
332 };
333
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 #endif
336