1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 file The header file for the JSBSim executive.
7 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
27 --------------------------------------------------------------------------------
29 7/31/99 TP Added RunIC function that runs the sim so that every frame
30 begins with the IC values from the given FGInitialCondition
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 #ifndef FGFDMEXEC_HEADER_H
38 #define FGFDMEXEC_HEADER_H
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 //#include "models/FGModel.h"
48 #include "models/FGOutput.h"
49 #include "models/FGInput.h"
50 #include "initialization/FGTrim.h"
51 #include "initialization/FGInitialCondition.h"
52 #include "FGJSBBase.h"
53 #include "input_output/FGPropertyManager.h"
54 #include "input_output/FGGroundCallback.h"
55 #include "input_output/FGXMLFileRead.h"
56 #include "models/FGPropagate.h"
57 #include "math/FGColumnVector3.h"
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 #define ID_FDMEXEC "$Id: FGFDMExec.h,v 1.56 2010/11/18 20:37:10 jberndt Exp $"
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 class FGBuoyantForces;
78 class FGExternalReactions;
79 class FGGroundReactions;
88 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92 /** Encapsulates the JSBSim simulation executive.
93 This class is the executive class through which all other simulation classes
94 are instantiated, initialized, and run. When integrated with FlightGear (or
95 other flight simulator) this class is typically instantiated by an interface
96 class on the simulator side.
98 At the time of simulation initialization, the interface
99 class creates an instance of this executive class. The
100 executive is subsequently directed to load the chosen aircraft specification
104 fdmex = new FGFDMExec(
\85 );
105 result = fdmex->LoadModel(
\85 );
108 When an aircraft model is loaded, the config file is parsed and for each of the
109 sections of the config file (propulsion, flight control, etc.) the
110 corresponding Load() method is called (e.g. FGFCS::Load()).
112 Subsequent to the creation of the executive and loading of the model,
113 initialization is performed. Initialization involves copying control inputs
114 into the appropriate JSBSim data storage locations, configuring it for the set
115 of user supplied initial conditions, and then copying state variables from
116 JSBSim. The state variables are used to drive the instrument displays and to
117 place the vehicle model in world space for visual rendering:
120 copy_to_JSBsim(); // copy control inputs to JSBSim
121 fdmex->RunIC(); // loop JSBSim once w/o integrating
122 copy_from_JSBsim(); // update the bus
125 Once initialization is complete, cyclic execution proceeds:
128 copy_to_JSBsim(); // copy control inputs to JSBSim
129 fdmex->Run(); // execute JSBSim
130 copy_from_JSBsim(); // update the bus
133 JSBSim can be used in a standalone mode by creating a compact stub program
134 that effectively performs the same progression of steps as outlined above for
135 the integrated version, but with two exceptions. First, the copy_to_JSBSim()
136 and copy_from_JSBSim() functions are not used because the control inputs are
137 handled directly by the scripting facilities and outputs are handled by the
138 output (data logging) class. Second, the name of a script file can be supplied
139 to the stub program. Scripting (see FGScript) provides a way to supply command
140 inputs to the simulation:
143 FDMExec = new JSBSim::FGFDMExec();
144 FDMExec->LoadScript( ScriptName ); // the script loads the aircraft and ICs
145 result = FDMExec->Run();
146 while (result) { // cyclic execution
147 result = FDMExec->Run(); // execute JSBSim
151 The standalone mode has been useful for verifying changes before committing
152 updates to the source code repository. It is also useful for running sets of
153 tests that reveal some aspects of simulated aircraft performance, such as
154 range, time-to-climb, takeoff distance, etc.
156 <h3>JSBSim Debugging Directives</h3>
158 This describes to any interested entity the debug level
159 requested by setting the JSBSIM_DEBUG environment variable.
160 The bitmasked value choices are as follows:
161 - <b>unset</b>: In this case (the default) JSBSim would only print
162 out the normally expected messages, essentially echoing
163 the config files as they are read. If the environment
164 variable is not set, debug_lvl is set to 1 internally
165 - <b>0</b>: This requests JSBSim not to output any messages
167 - <b>1</b>: This value explicity requests the normal JSBSim
169 - <b>2</b>: This value asks for a message to be printed out when
170 a class is instantiated
171 - <b>4</b>: When this value is set, a message is displayed when a
172 FGModel object executes its Run() method
173 - <b>8</b>: When this value is set, various runtime state variables
174 are printed out periodically
175 - <b>16</b>: When set various parameters are sanity checked and
176 a message is printed out when they go out of bounds
179 @property simulator/do_trim (write only) Can be set to the integer equivalent to one of
180 tLongitudinal (0), tFull (1), tGround (2), tPullup (3),
181 tCustom (4), tTurn (5). Setting this to a legal value
182 (such as by a script) causes a trim to be performed. This
183 property actually maps toa function call of DoTrim().
185 @author Jon S. Berndt
186 @version $Revision: 1.56 $
189 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
193 class FGFDMExec : public FGJSBBase, public FGXMLFileRead
199 FGColumnVector3 Orient;
205 Loc = FGColumnVector3(0,0,0);
206 Orient = FGColumnVector3(0,0,0);
211 void Run(void) {exec->Run();}
212 void AssignState(FGPropagate* source_prop) {
213 exec->GetPropagate()->SetVState(source_prop->GetVState());
223 /// Default constructor
224 FGFDMExec(FGPropertyManager* root = 0, unsigned int* fdmctr = 0);
226 /// Default destructor
229 /** This routine places a model into the runlist at the specified rate. The
230 "rate" is not really a clock rate. It represents how many calls to the
231 FGFDMExec::Run() method must be made before the model is executed. A
232 value of 1 means that the model will be executed for each call to the
233 exec's Run() method. A value of 5 means that the model will only be
234 executed every 5th call to the exec's Run() method. Use of a rate other than
235 one is at this time not recommended.
236 @param model A pointer to the model being scheduled.
237 @param rate The rate at which to execute the model as described above.
238 @return Currently returns 0 always. */
239 void Schedule(FGModel* model, int rate);
241 /** This function executes each scheduled model in succession.
242 @return true if successful, false if sim should be ended */
245 /** Initializes the sim from the initial condition object and executes
246 each scheduled model without integrating i.e. dt=0.
247 @return true if successful */
250 /** Sets the ground callback pointer.
251 @param gc A pointer to a ground callback object. */
252 void SetGroundCallback(FGGroundCallback* gc);
254 /** Loads an aircraft model.
255 @param AircraftPath path to the aircraft/ directory. For instance:
256 "aircraft". Under aircraft, then, would be directories for various
257 modeled aircraft such as C172/, x15/, etc.
258 @param EnginePath path to the directory under which engine config
259 files are kept, for instance "engine"
260 @param SystemsPath path to the directory under which systems config
261 files are kept, for instance "systems"
262 @param model the name of the aircraft model itself. This file will
263 be looked for in the directory specified in the AircraftPath variable,
264 and in turn under the directory with the same name as the model. For
265 instance: "aircraft/x15/x15.xml"
266 @param addModelToPath set to true to add the model name to the
267 AircraftPath, defaults to true
268 @return true if successful */
269 bool LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
270 const string& model, bool addModelToPath = true);
272 /** Loads an aircraft model. The paths to the aircraft and engine
273 config file directories must be set prior to calling this. See
275 @param model the name of the aircraft model itself. This file will
276 be looked for in the directory specified in the AircraftPath variable,
277 and in turn under the directory with the same name as the model. For
278 instance: "aircraft/x15/x15.xml"
279 @param addModelToPath set to true to add the model name to the
280 AircraftPath, defaults to true
281 @return true if successful*/
282 bool LoadModel(const string& model, bool addModelToPath = true);
285 @param Script the full path name and file name for the script to be loaded.
286 @return true if successfully loadsd; false otherwise. */
287 bool LoadScript(const string& Script, double deltaT);
289 /** Sets the path to the engine config file directories.
290 @param path path to the directory under which engine config
291 files are kept, for instance "engine" */
292 bool SetEnginePath(const string& path) { EnginePath = RootDir + path; return true; }
294 /** Sets the path to the aircraft config file directories.
295 @param path path to the aircraft directory. For instance:
296 "aircraft". Under aircraft, then, would be directories for various
297 modeled aircraft such as C172/, x15/, etc. */
298 bool SetAircraftPath(const string& path) { AircraftPath = RootDir + path; return true; }
300 /** Sets the path to the systems config file directories.
301 @param path path to the directory under which systems config
302 files are kept, for instance "systems" */
303 bool SetSystemsPath(const string& path) { SystemsPath = RootDir + path; return true; }
305 /// @name Top-level executive State and Model retrieval mechanism
307 /// Returns the FGAtmosphere pointer.
308 FGAtmosphere* GetAtmosphere(void) {return Atmosphere;}
309 /// Returns the FGFCS pointer.
310 FGFCS* GetFCS(void) {return FCS;}
311 /// Returns the FGPropulsion pointer.
312 FGPropulsion* GetPropulsion(void) {return Propulsion;}
313 /// Returns the FGAircraft pointer.
314 FGMassBalance* GetMassBalance(void) {return MassBalance;}
315 /// Returns the FGAerodynamics pointer
316 FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
317 /// Returns the FGInertial pointer.
318 FGInertial* GetInertial(void) {return Inertial;}
319 /// Returns the FGGroundReactions pointer.
320 FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
321 /// Returns the FGExternalReactions pointer.
322 FGExternalReactions* GetExternalReactions(void) {return ExternalReactions;}
323 /// Returns the FGBuoyantForces pointer.
324 FGBuoyantForces* GetBuoyantForces(void) {return BuoyantForces;}
325 /// Returns the FGAircraft pointer.
326 FGAircraft* GetAircraft(void) {return Aircraft;}
327 /// Returns the FGPropagate pointer.
328 FGPropagate* GetPropagate(void) {return Propagate;}
329 /// Returns the FGAuxiliary pointer.
330 FGAuxiliary* GetAuxiliary(void) {return Auxiliary;}
331 /// Returns the FGInput pointer.
332 FGInput* GetInput(void) {return Input;}
333 /// Returns the FGGroundCallback pointer.
334 FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
335 /// Retrieves the script object
336 FGScript* GetScript(void) {return Script;}
337 // Returns a pointer to the FGInitialCondition object
338 FGInitialCondition* GetIC(void) {return IC;}
339 // Returns a pointer to the FGTrim object
340 FGTrim* GetTrim(void);
343 /// Retrieves the engine path.
344 const string& GetEnginePath(void) {return EnginePath;}
345 /// Retrieves the aircraft path.
346 const string& GetAircraftPath(void) {return AircraftPath;}
347 /// Retrieves the systems path.
348 const string& GetSystemsPath(void) {return SystemsPath;}
349 /// Retrieves the full aircraft path name.
350 const string& GetFullAircraftPath(void) {return FullAircraftPath;}
352 /** Retrieves the value of a property.
353 @param property the name of the property
354 @result the value of the specified property */
355 inline double GetPropertyValue(const string& property) {return instance->GetDouble(property);}
357 /** Sets a property value.
358 @param property the property to be set
359 @param value the value to set the property to */
360 inline void SetPropertyValue(const string& property, double value) {
361 instance->SetDouble(property, value);
364 /// Returns the model name.
365 const string& GetModelName(void) { return modelName; }
367 /// Returns the current time.
368 double GetSimTime(void);
370 /// Returns the current frame time (delta T).
371 double GetDeltaT(void);
373 /// Returns a pointer to the property manager object.
374 FGPropertyManager* GetPropertyManager(void);
375 /// Returns a vector of strings representing the names of all loaded models (future)
376 vector <string> EnumerateFDMs(void);
377 /// Gets the number of child FDMs.
378 int GetFDMCount(void) {return (int)ChildFDMList.size();}
379 /// Gets a particular child FDM.
380 childData* GetChildFDM(int i) {return ChildFDMList[i];}
381 /// Marks this instance of the Exec object as a "child" object.
382 void SetChild(bool ch) {IsChild = ch;}
384 /** Sets the output (logging) mechanism for this run.
385 Calling this function passes the name of an output directives file to
386 the FGOutput object associated with this run. The call to this function
387 should be made prior to loading an aircraft model. This call results in an
388 FGOutput object being built as the first Output object in the FDMExec-managed
389 list of Output objects that may be created for an aircraft model. If this call
390 is made after an aircraft model is loaded, there is no effect. Any Output
391 objects added by the aircraft model itself (in an <output> element) will be
392 added after this one. Care should be taken not to refer to the same file
394 An output directives file contains an <output> </output> element, within
395 which should be specified the parameters or parameter groups that should
397 @param fname the filename of an output directives file.
399 bool SetOutputDirectives(const string& fname);
401 /** Sets (or overrides) the output filename
402 @param fname the name of the file to output data to
403 @return true if successful, false if there is no output specified for the flight model */
404 bool SetOutputFileName(const string& fname) {
405 if (Outputs.size() > 0) Outputs[0]->SetOutputFileName(fname);
410 /** Retrieves the current output filename.
411 @return the name of the output file for the first output specified by the flight model.
412 If none is specified, the empty string is returned. */
413 string GetOutputFileName(void) {
414 if (Outputs.size() > 0) return Outputs[0]->GetOutputFileName();
415 else return string("");
418 /** Executes trimming in the selected mode.
419 * @param mode Specifies how to trim:
427 void DoTrim(int mode);
428 // void DoTrimAnalysis(int mode);
430 /// Disables data logging to all outputs.
431 void DisableOutput(void);
432 /// Enables data logging to all outputs.
433 void EnableOutput(void);
434 /// Pauses execution by preventing time from incrementing.
435 void Hold(void) {holding = true;}
436 /// Resumes execution from a "Hold".
437 void Resume(void) {holding = false;}
438 /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
439 bool Holding(void) {return holding;}
440 /// Resets the initial conditions object and prepares the simulation to run again.
441 void ResetToInitialConditions(void);
442 /// Sets the debug level.
443 void SetDebugLevel(int level) {debug_lvl = level;}
445 struct PropertyCatalogStructure {
446 /// Name of the property.
448 /// The node for the property.
449 FGPropertyManager *node;
452 /** Builds a catalog of properties.
453 * This function descends the property tree and creates a list (an STL vector)
454 * containing the name and node for all properties.
455 * @param pcs The "root" property catalog structure pointer. */
456 void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
458 /** Retrieves property or properties matching the supplied string.
459 * A string is returned that contains a carriage return delimited list of all
460 * strings in the property catalog that matches the supplied check string.
461 * @param check The string to search for in the property catalog.
462 * @return the carriage-return-delimited string containing all matching strings
464 string QueryPropertyCatalog(const string& check);
466 // Print the contents of the property catalog for the loaded aircraft.
467 void PrintPropertyCatalog(void);
469 vector<string>& GetPropertyCatalog(void) {return PropertyCatalog;}
471 /// Use the MSIS atmosphere model.
472 void UseAtmosphereMSIS(void);
474 /// Use the Mars atmosphere model. (Not operative yet.)
475 void UseAtmosphereMars(void);
477 void SetTrimStatus(bool status){ trim_status = status; }
478 bool GetTrimStatus(void) const { return trim_status; }
479 void SetTrimMode(int mode){ ta_mode = mode; }
480 int GetTrimMode(void) const { return ta_mode; }
482 /// Returns the cumulative simulation time in seconds.
483 double GetSimTime(void) const { return sim_time; }
485 /// Returns the simulation delta T.
486 double GetDeltaT(void) {return dT;}
488 /// Suspends the simulation and sets the delta T to zero.
489 void SuspendIntegration(void) {saved_dT = dT; dT = 0.0;}
491 /// Resumes the simulation by resetting delta T to the correct value.
492 void ResumeIntegration(void) {dT = saved_dT;}
494 /** Returns the simulation suspension state.
495 @return true if suspended, false if executing */
496 bool IntegrationSuspended(void) {return dT == 0.0;}
498 /** Sets the current sim time.
499 @param cur_time the current time
500 @return the current simulation time. */
501 double Setsim_time(double cur_time) {
506 /** Sets the integration time step for the simulation executive.
507 @param delta_t the time step in seconds. */
508 void Setdt(double delta_t) { dT = delta_t; }
510 /** Sets the root directory where JSBSim starts looking for its system directories.
511 @param rootDir the string containing the root directory. */
512 void SetRootDir(const string& rootDir) {RootDir = rootDir;}
514 /** Retrieves teh Root Directory.
515 @return the string representing the root (base) JSBSim directory. */
516 const string& GetRootDir(void) const {return RootDir;}
518 /** Increments the simulation time.
519 @return the new simulation time. */
520 double IncrTime(void) {
525 /** Retrieves the current debug level setting. */
526 int GetDebugLevel(void) const {return debug_lvl;};
532 unsigned short Terminate;
542 string FullAircraftPath;
552 FGGroundCallback* GroundCallback;
553 FGAtmosphere* Atmosphere;
555 FGPropulsion* Propulsion;
556 FGMassBalance* MassBalance;
557 FGAerodynamics* Aerodynamics;
558 FGInertial* Inertial;
559 FGGroundReactions* GroundReactions;
560 FGExternalReactions* ExternalReactions;
561 FGBuoyantForces* BuoyantForces;
562 FGAircraft* Aircraft;
563 FGPropagate* Propagate;
564 FGAuxiliary* Auxiliary;
567 FGInitialCondition* IC;
570 FGPropertyManager* Root;
572 FGPropertyManager* instance;
574 // The FDM counter is used to give each child FDM an unique ID. The root FDM has the ID 0
575 unsigned int* FDMctr;
577 vector <string> PropertyCatalog;
578 vector <FGOutput*> Outputs;
579 vector <childData*> ChildFDMList;
580 vector <FGModel*> Models;
582 bool ReadFileHeader(Element*);
583 bool ReadChild(Element*);
584 bool ReadPrologue(Element*);
585 void ResetToInitialConditions(int mode);
587 bool DeAllocate(void);
588 void Initialize(FGInitialCondition *FGIC);
590 void Debug(int from);
593 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%