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