1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
22 Further information about the GNU General Public License can also be found on
23 the world wide web at http://www.gnu.org.
26 --------------------------------------------------------------------------------
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
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36 #ifndef FGFDMEXEC_HEADER_H
37 #define FGFDMEXEC_HEADER_H
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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>
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 #define ID_FDMEXEC "$Id$"
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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.
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
84 fdmex = new FGFDMExec(
\85 );
85 result = fdmex->LoadModel(
\85 );
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()).
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:
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
105 Once initialization is complete, cyclic execution proceeds:
108 copy_to_JSBsim(); // copy control inputs to JSBSim
109 fdmex->Run(); // execute JSBSim
110 copy_from_JSBsim(); // update the bus
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:
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
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.
138 <h3>JSBSim Debugging Directives</h3>
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
149 - <b>1</b>: This value explicity requests the normal JSBSim
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
161 @property simulator/do_trim (write only) 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. This
165 property actually maps toa function call of DoTrim().
167 @author Jon S. Berndt
171 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
175 class FGFDMExec : public FGJSBBase
179 /// Default constructor
180 FGFDMExec(FGPropertyManager* root = 0);
182 /// Default destructor
185 /** This routine places a model into the runlist at the specified rate. The
186 "rate" is not really a clock rate. It represents how many calls to the
187 FGFDMExec::Run() method must be made before the model is executed. A
188 value of 1 means that the model will be executed for each call to the
189 exec's Run() method. A value of 5 means that the model will only be
190 executed every 5th call to the exec's Run() method. Use of a rate other than
191 one is at this time not recommended.
192 @param model A pointer to the model being scheduled.
193 @param rate The rate at which to execute the model as described above.
194 @return Currently returns 0 always. */
195 int Schedule(FGModel* model, int rate);
197 /** This function executes each scheduled model in succession.
198 @return true if successful, false if sim should be ended */
201 /** Initializes the sim from the initial condition object and executes
202 each scheduled model without integrating i.e. dt=0.
203 @return true if successful */
206 /** Sets the ground callback pointer.
207 @param gc A pointer to a ground callback object. */
208 void SetGroundCallback(FGGroundCallback* gc);
210 /** Loads an aircraft model.
211 @param AircraftPath path to the aircraft/ directory. For instance:
212 "aircraft". Under aircraft, then, would be directories for various
213 modeled aircraft such as C172/, x15/, etc.
214 @param EnginePath path to the directory under which engine config
215 files are kept, for instance "engine"
216 @param model the name of the aircraft model itself. This file will
217 be looked for in the directory specified in the AircraftPath variable,
218 and in turn under the directory with the same name as the model. For
219 instance: "aircraft/x15/x15.xml"
220 @param addModelToPath set to true to add the model name to the
221 AircraftPath, defaults to true
222 @return true if successful */
223 bool LoadModel(string AircraftPath, string EnginePath, string model,
224 bool addModelToPath = true);
226 /** Loads an aircraft model. The paths to the aircraft and engine
227 config file directories must be set prior to calling this. See
229 @param model the name of the aircraft model itself. This file will
230 be looked for in the directory specified in the AircraftPath variable,
231 and in turn under the directory with the same name as the model. For
232 instance: "aircraft/x15/x15.xml"
233 @param addModelToPath set to true to add the model name to the
234 AircraftPath, defaults to true
235 @return true if successful*/
236 bool LoadModel(string model, bool addModelToPath = true);
238 /** Sets the path to the engine config file directories.
239 @param path path to the directory under which engine config
240 files are kept, for instance "engine" */
241 bool SetEnginePath(string path) { EnginePath = path; return true; }
243 /** Sets the path to the aircraft config file directories.
244 @param path path to the aircraft directory. For instance:
245 "aircraft". Under aircraft, then, would be directories for various
246 modeled aircraft such as C172/, x15/, etc. */
247 bool SetAircraftPath(string path) { AircraftPath = path; return true; }
249 /// @name Top-level executive State and Model retrieval mechanism
251 /// Returns the FGAtmosphere pointer.
252 inline FGAtmosphere* GetAtmosphere(void) {return Atmosphere;}
253 /// Returns the FGFCS pointer.
254 inline FGFCS* GetFCS(void) {return FCS;}
255 /// Returns the FGPropulsion pointer.
256 inline FGPropulsion* GetPropulsion(void) {return Propulsion;}
257 /// Returns the FGAircraft pointer.
258 inline FGMassBalance* GetMassBalance(void) {return MassBalance;}
259 /// Returns the FGAerodynamics pointer
260 inline FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
261 /// Returns the FGInertial pointer.
262 inline FGInertial* GetInertial(void) {return Inertial;}
263 /// Returns the FGGroundReactions pointer.
264 inline FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
265 /// Returns the FGAircraft pointer.
266 inline FGAircraft* GetAircraft(void) {return Aircraft;}
267 /// Returns the FGPropagate pointer.
268 inline FGPropagate* GetPropagate(void) {return Propagate;}
269 /// Returns the FGAuxiliary pointer.
270 inline FGAuxiliary* GetAuxiliary(void) {return Auxiliary;}
271 /// Returns the FGInput pointer.
272 inline FGInput* GetInput(void) {return Input;}
273 /// Returns the FGGroundCallback pointer.
274 inline FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
275 /// Returns the FGState pointer.
276 inline FGState* GetState(void) {return State;}
277 // Returns a pointer to the FGInitialCondition object
278 inline FGInitialCondition* GetIC(void) {return IC;}
279 // Returns a pointer to the FGTrim object
280 inline FGTrim* GetTrim(void);
283 /// Retrieves the engine path.
284 inline string GetEnginePath(void) {return EnginePath;}
285 /// Retrieves the aircraft path.
286 inline string GetAircraftPath(void) {return AircraftPath;}
288 /// Returns the model name.
289 string GetModelName(void) { return modelName; }
291 /// Returns a pointer to the property manager object.
292 FGPropertyManager* GetPropertyManager(void);
293 /// Returns a vector of strings representing the names of all loaded models (future)
294 vector <string> EnumerateFDMs(void);
295 /// Marks this instance of the Exec object as a "slave" object.
296 void SetSlave(void) {IsSlave = true;}
298 /** Executes trimming in the selected mode.
299 * @param mode Specifies how to trim:
307 void DoTrim(int mode);
309 /// Disables data logging to all outputs.
310 void DisableOutput(void);
311 /// Enables data logging to all outputs.
312 void EnableOutput(void);
313 /// Pauses execution by preventing time from incrementing.
314 void Hold(void) {holding = true;}
315 /// Resumes execution from a "Hold".
316 void Resume(void) {holding = false;}
317 /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
318 bool Holding(void) {return holding;}
320 struct PropertyCatalogStructure {
321 /// Name of the property.
323 /// The node for the property.
324 FGPropertyManager *node;
327 /** Builds a catalog of properties.
328 * This function descends the property tree and creates a list (an STL vector)
329 * containing the name and node for all properties.
330 * @param pcs The "root" property catalog structure pointer. */
331 void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
333 /** Retrieves property or properties matching the supplied string.
334 * A string is returned that contains a carriage return delimited list of all
335 * strings in the property catalog that matches the supplied check string.
336 * @param check The string to search for in the property catalog.
337 * @return the carriage-return-delimited string containing all matching strings
339 string QueryPropertyCatalog(string check);
341 /// Use the MSIS atmosphere model.
342 void UseAtmosphereMSIS(void);
344 /// Use the Mars atmosphere model. (Not operative yet.)
345 void UseAtmosphereMars(void);
356 FGPropertyManager* Root;
357 static unsigned int FDMctr;
361 static FGPropertyManager *master;
362 FGPropertyManager *instance;
363 vector <string> PropertyCatalog;
369 double roll, pitch, yaw;
375 roll = pitch = yaw = 0.0;
390 FGGroundCallback* GroundCallback;
392 FGAtmosphere* Atmosphere;
394 FGPropulsion* Propulsion;
395 FGMassBalance* MassBalance;
396 FGAerodynamics* Aerodynamics;
397 FGInertial* Inertial;
398 FGGroundReactions* GroundReactions;
399 FGAircraft* Aircraft;
400 FGPropagate* Propagate;
401 FGAuxiliary* Auxiliary;
403 vector <FGOutput*> Outputs;
405 FGInitialCondition* IC;
408 vector <slaveData*> SlaveFDMList;
410 bool ReadFileHeader(Element*);
411 bool ReadSlave(Element*);
412 bool ReadPrologue(Element*);
415 bool DeAllocate(void);
416 void Debug(int from);
419 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%