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