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