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 "initialization/FGTrim.h"
48 #include "FGJSBBase.h"
49 #include "input_output/FGPropertyManager.h"
50 #include "input_output/FGGroundCallback.h"
51 #include "input_output/FGXMLFileRead.h"
52 #include "models/FGPropagate.h"
53 #include "math/FGColumnVector3.h"
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 #define ID_FDMEXEC "$Id: FGFDMExec.h,v 1.71 2011/09/07 02:37:04 jberndt Exp $"
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 class FGAccelerations;
75 class FGBuoyantForces;
76 class FGExternalReactions;
77 class FGGroundReactions;
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90 /** Encapsulates the JSBSim simulation executive.
91 This class is the executive class through which all other simulation classes
92 are instantiated, initialized, and run. When integrated with FlightGear (or
93 other flight simulator) this class is typically instantiated by an interface
94 class on the simulator side.
96 At the time of simulation initialization, the interface
97 class creates an instance of this executive class. The
98 executive is subsequently directed to load the chosen aircraft specification
102 fdmex = new FGFDMExec( ... );
103 result = fdmex->LoadModel( ... );
106 When an aircraft model is loaded, the config file is parsed and for each of the
107 sections of the config file (propulsion, flight control, etc.) the
108 corresponding Load() method is called (e.g. FGFCS::Load()).
110 Subsequent to the creation of the executive and loading of the model,
111 initialization is performed. Initialization involves copying control inputs
112 into the appropriate JSBSim data storage locations, configuring it for the set
113 of user supplied initial conditions, and then copying state variables from
114 JSBSim. The state variables are used to drive the instrument displays and to
115 place the vehicle model in world space for visual rendering:
118 copy_to_JSBsim(); // copy control inputs to JSBSim
119 fdmex->RunIC(); // loop JSBSim once w/o integrating
120 copy_from_JSBsim(); // update the bus
123 Once initialization is complete, cyclic execution proceeds:
126 copy_to_JSBsim(); // copy control inputs to JSBSim
127 fdmex->Run(); // execute JSBSim
128 copy_from_JSBsim(); // update the bus
131 JSBSim can be used in a standalone mode by creating a compact stub program
132 that effectively performs the same progression of steps as outlined above for
133 the integrated version, but with two exceptions. First, the copy_to_JSBSim()
134 and copy_from_JSBSim() functions are not used because the control inputs are
135 handled directly by the scripting facilities and outputs are handled by the
136 output (data logging) class. Second, the name of a script file can be supplied
137 to the stub program. Scripting (see FGScript) provides a way to supply command
138 inputs to the simulation:
141 FDMExec = new JSBSim::FGFDMExec();
142 FDMExec->LoadScript( ScriptName ); // the script loads the aircraft and ICs
143 result = FDMExec->Run();
144 while (result) { // cyclic execution
145 result = FDMExec->Run(); // execute JSBSim
149 The standalone mode has been useful for verifying changes before committing
150 updates to the source code repository. It is also useful for running sets of
151 tests that reveal some aspects of simulated aircraft performance, such as
152 range, time-to-climb, takeoff distance, etc.
154 <h3>JSBSim Debugging Directives</h3>
156 This describes to any interested entity the debug level
157 requested by setting the JSBSIM_DEBUG environment variable.
158 The bitmasked value choices are as follows:
159 - <b>unset</b>: In this case (the default) JSBSim would only print
160 out the normally expected messages, essentially echoing
161 the config files as they are read. If the environment
162 variable is not set, debug_lvl is set to 1 internally
163 - <b>0</b>: This requests JSBSim not to output any messages
165 - <b>1</b>: This value explicity requests the normal JSBSim
167 - <b>2</b>: This value asks for a message to be printed out when
168 a class is instantiated
169 - <b>4</b>: When this value is set, a message is displayed when a
170 FGModel object executes its Run() method
171 - <b>8</b>: When this value is set, various runtime state variables
172 are printed out periodically
173 - <b>16</b>: When set various parameters are sanity checked and
174 a message is printed out when they go out of bounds
177 @property simulator/do_trim (write only) Can be set to the integer equivalent to one of
178 tLongitudinal (0), tFull (1), tGround (2), tPullup (3),
179 tCustom (4), tTurn (5). Setting this to a legal value
180 (such as by a script) causes a trim to be performed. This
181 property actually maps toa function call of DoTrim().
183 @author Jon S. Berndt
184 @version $Revision: 1.71 $
187 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
191 class FGFDMExec : public FGJSBBase, public FGXMLFileRead
197 FGColumnVector3 Orient;
203 Loc = FGColumnVector3(0,0,0);
204 Orient = FGColumnVector3(0,0,0);
209 void Run(void) {exec->Run();}
210 void AssignState(FGPropagate* source_prop) {
211 exec->GetPropagate()->SetVState(source_prop->GetVState());
221 /// Default constructor
222 FGFDMExec(FGPropertyManager* root = 0, unsigned int* fdmctr = 0);
224 /// Default destructor
227 // This list of enums is very important! The order in which models are listed here
228 // determines the order of execution of the models.
229 enum eModels { ePropagate=0,
244 eNumStandardModels };
246 /** Unbind all tied JSBSim properties. */
247 void Unbind(void) {instance->Unbind();}
249 /** This routine places a model into the runlist at the specified rate. The
250 "rate" is not really a clock rate. It represents how many calls to the
251 FGFDMExec::Run() method must be made before the model is executed. A
252 value of 1 means that the model will be executed for each call to the
253 exec's Run() method. A value of 5 means that the model will only be
254 executed every 5th call to the exec's Run() method. Use of a rate other than
255 one is at this time not recommended.
256 @param model A pointer to the model being scheduled.
257 @param rate The rate at which to execute the model as described above.
258 Default is every frame (rate=1).
259 @return Currently returns 0 always. */
260 void Schedule(FGModel* model, int rate=1);
262 /** This function executes each scheduled model in succession.
263 @return true if successful, false if sim should be ended */
266 /** Initializes the sim from the initial condition object and executes
267 each scheduled model without integrating i.e. dt=0.
268 @return true if successful */
271 /** Sets the ground callback pointer.
272 @param gc A pointer to a ground callback object. */
273 void SetGroundCallback(FGGroundCallback* gc);
275 /** Loads an aircraft model.
276 @param AircraftPath path to the aircraft/ directory. For instance:
277 "aircraft". Under aircraft, then, would be directories for various
278 modeled aircraft such as C172/, x15/, etc.
279 @param EnginePath path to the directory under which engine config
280 files are kept, for instance "engine"
281 @param SystemsPath path to the directory under which systems config
282 files are kept, for instance "systems"
283 @param model the name of the aircraft model itself. This file will
284 be looked for in the directory specified in the AircraftPath variable,
285 and in turn under the directory with the same name as the model. For
286 instance: "aircraft/x15/x15.xml"
287 @param addModelToPath set to true to add the model name to the
288 AircraftPath, defaults to true
289 @return true if successful */
290 bool LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
291 const string& model, bool addModelToPath = true);
293 /** Loads an aircraft model. The paths to the aircraft and engine
294 config file directories must be set prior to calling this. See
296 @param model the name of the aircraft model itself. This file will
297 be looked for in the directory specified in the AircraftPath variable,
298 and in turn under the directory with the same name as the model. For
299 instance: "aircraft/x15/x15.xml"
300 @param addModelToPath set to true to add the model name to the
301 AircraftPath, defaults to true
302 @return true if successful*/
303 bool LoadModel(const string& model, bool addModelToPath = true);
306 @param Script the full path name and file name for the script to be loaded.
307 @param deltaT The simulation integration step size, if given. If no value is supplied
308 then 0.0 is used and the value is expected to be supplied in
309 the script file itself.
310 @return true if successfully loadsd; false otherwise. */
311 bool LoadScript(const string& Script, double deltaT=0.0);
313 /** Sets the path to the engine config file directories.
314 @param path path to the directory under which engine config
315 files are kept, for instance "engine" */
316 bool SetEnginePath(const string& path) { EnginePath = RootDir + path; return true; }
318 /** Sets the path to the aircraft config file directories.
319 @param path path to the aircraft directory. For instance:
320 "aircraft". Under aircraft, then, would be directories for various
321 modeled aircraft such as C172/, x15/, etc. */
322 bool SetAircraftPath(const string& path) { AircraftPath = RootDir + path; return true; }
324 /** Sets the path to the systems config file directories.
325 @param path path to the directory under which systems config
326 files are kept, for instance "systems" */
327 bool SetSystemsPath(const string& path) { SystemsPath = RootDir + path; return true; }
329 /// @name Top-level executive State and Model retrieval mechanism
331 /// Returns the FGAtmosphere pointer.
332 FGAtmosphere* GetAtmosphere(void) {return (FGAtmosphere*)Models[eAtmosphere];}
333 /// Returns the FGAccelerations pointer.
334 FGAccelerations* GetAccelerations(void) {return (FGAccelerations*)Models[eAccelerations];}
335 /// Returns the FGWinds pointer.
336 FGWinds* GetWinds(void) {return (FGWinds*)Models[eWinds];}
337 /// Returns the FGFCS pointer.
338 FGFCS* GetFCS(void) {return (FGFCS*)Models[eSystems];}
339 /// Returns the FGPropulsion pointer.
340 FGPropulsion* GetPropulsion(void) {return (FGPropulsion*)Models[ePropulsion];}
341 /// Returns the FGAircraft pointer.
342 FGMassBalance* GetMassBalance(void) {return (FGMassBalance*)Models[eMassBalance];}
343 /// Returns the FGAerodynamics pointer
344 FGAerodynamics* GetAerodynamics(void){return (FGAerodynamics*)Models[eAerodynamics];}
345 /// Returns the FGInertial pointer.
346 FGInertial* GetInertial(void) {return (FGInertial*)Models[eInertial];}
347 /// Returns the FGGroundReactions pointer.
348 FGGroundReactions* GetGroundReactions(void) {return (FGGroundReactions*)Models[eGroundReactions];}
349 /// Returns the FGExternalReactions pointer.
350 FGExternalReactions* GetExternalReactions(void) {return (FGExternalReactions*)Models[eExternalReactions];}
351 /// Returns the FGBuoyantForces pointer.
352 FGBuoyantForces* GetBuoyantForces(void) {return (FGBuoyantForces*)Models[eBuoyantForces];}
353 /// Returns the FGAircraft pointer.
354 FGAircraft* GetAircraft(void) {return (FGAircraft*)Models[eAircraft];}
355 /// Returns the FGPropagate pointer.
356 FGPropagate* GetPropagate(void) {return (FGPropagate*)Models[ePropagate];}
357 /// Returns the FGAuxiliary pointer.
358 FGAuxiliary* GetAuxiliary(void) {return (FGAuxiliary*)Models[eAuxiliary];}
359 /// Returns the FGInput pointer.
360 FGInput* GetInput(void) {return (FGInput*)Models[eInput];}
361 /// Returns the FGGroundCallback pointer.
362 FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
363 /// Retrieves the script object
364 FGScript* GetScript(void) {return Script;}
365 // Returns a pointer to the FGInitialCondition object
366 FGInitialCondition* GetIC(void) {return IC;}
367 // Returns a pointer to the FGTrim object
368 FGTrim* GetTrim(void);
371 /// Retrieves the engine path.
372 const string& GetEnginePath(void) {return EnginePath;}
373 /// Retrieves the aircraft path.
374 const string& GetAircraftPath(void) {return AircraftPath;}
375 /// Retrieves the systems path.
376 const string& GetSystemsPath(void) {return SystemsPath;}
377 /// Retrieves the full aircraft path name.
378 const string& GetFullAircraftPath(void) {return FullAircraftPath;}
380 /** Retrieves the value of a property.
381 @param property the name of the property
382 @result the value of the specified property */
383 inline double GetPropertyValue(const string& property) {return instance->GetDouble(property);}
385 /** Sets a property value.
386 @param property the property to be set
387 @param value the value to set the property to */
388 inline void SetPropertyValue(const string& property, double value) {
389 instance->SetDouble(property, value);
392 /// Returns the model name.
393 const string& GetModelName(void) { return modelName; }
395 /// Returns the current time.
396 double GetSimTime(void);
398 /// Returns the current frame time (delta T).
399 double GetDeltaT(void);
401 /// Returns a pointer to the property manager object.
402 FGPropertyManager* GetPropertyManager(void);
403 /// Returns a vector of strings representing the names of all loaded models (future)
404 vector <string> EnumerateFDMs(void);
405 /// Gets the number of child FDMs.
406 int GetFDMCount(void) {return (int)ChildFDMList.size();}
407 /// Gets a particular child FDM.
408 childData* GetChildFDM(int i) {return ChildFDMList[i];}
409 /// Marks this instance of the Exec object as a "child" object.
410 void SetChild(bool ch) {IsChild = ch;}
412 /** Sets the output (logging) mechanism for this run.
413 Calling this function passes the name of an output directives file to
414 the FGOutput object associated with this run. The call to this function
415 should be made prior to loading an aircraft model. This call results in an
416 FGOutput object being built as the first Output object in the FDMExec-managed
417 list of Output objects that may be created for an aircraft model. If this call
418 is made after an aircraft model is loaded, there is no effect. Any Output
419 objects added by the aircraft model itself (in an <output> element) will be
420 added after this one. Care should be taken not to refer to the same file
422 An output directives file contains an <output> </output> element, within
423 which should be specified the parameters or parameter groups that should
425 @param fname the filename of an output directives file.
427 bool SetOutputDirectives(const string& fname);
429 /** Forces the specified output object to print its items once */
430 void ForceOutput(int idx=0);
432 /** Sets (or overrides) the output filename
433 @param fname the name of the file to output data to
434 @return true if successful, false if there is no output specified for the flight model */
435 bool SetOutputFileName(const string& fname);
437 /** Retrieves the current output filename.
438 @return the name of the output file for the first output specified by the flight model.
439 If none is specified, the empty string is returned. */
440 string GetOutputFileName(void);
442 /** Executes trimming in the selected mode.
443 * @param mode Specifies how to trim:
451 void DoTrim(int mode);
453 /// Disables data logging to all outputs.
454 void DisableOutput(void);
455 /// Enables data logging to all outputs.
456 void EnableOutput(void);
457 /// Pauses execution by preventing time from incrementing.
458 void Hold(void) {holding = true;}
459 /// Resumes execution from a "Hold".
460 void Resume(void) {holding = false;}
461 /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
462 bool Holding(void) {return holding;}
463 /// Resets the initial conditions object and prepares the simulation to run again.
464 void ResetToInitialConditions(void);
465 /// Sets the debug level.
466 void SetDebugLevel(int level) {debug_lvl = level;}
468 struct PropertyCatalogStructure {
469 /// Name of the property.
471 /// The node for the property.
472 FGPropertyManager *node;
475 /** Builds a catalog of properties.
476 * This function descends the property tree and creates a list (an STL vector)
477 * containing the name and node for all properties.
478 * @param pcs The "root" property catalog structure pointer. */
479 void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
481 /** Retrieves property or properties matching the supplied string.
482 * A string is returned that contains a carriage return delimited list of all
483 * strings in the property catalog that matches the supplied check string.
484 * @param check The string to search for in the property catalog.
485 * @return the carriage-return-delimited string containing all matching strings
487 string QueryPropertyCatalog(const string& check);
489 // Print the contents of the property catalog for the loaded aircraft.
490 void PrintPropertyCatalog(void);
492 vector<string>& GetPropertyCatalog(void) {return PropertyCatalog;}
494 void SetTrimStatus(bool status){ trim_status = status; }
495 bool GetTrimStatus(void) const { return trim_status; }
496 void SetTrimMode(int mode){ ta_mode = mode; }
497 int GetTrimMode(void) const { return ta_mode; }
499 string GetPropulsionTankReport();
501 /// Returns the cumulative simulation time in seconds.
502 double GetSimTime(void) const { return sim_time; }
504 /// Returns the simulation delta T.
505 double GetDeltaT(void) {return dT;}
507 /// Suspends the simulation and sets the delta T to zero.
508 void SuspendIntegration(void) {saved_dT = dT; dT = 0.0;}
510 /// Resumes the simulation by resetting delta T to the correct value.
511 void ResumeIntegration(void) {dT = saved_dT;}
513 /** Returns the simulation suspension state.
514 @return true if suspended, false if executing */
515 bool IntegrationSuspended(void) {return dT == 0.0;}
517 /** Sets the current sim time.
518 @param cur_time the current time
519 @return the current simulation time. */
520 double Setsim_time(double cur_time) {
525 /** Sets the integration time step for the simulation executive.
526 @param delta_t the time step in seconds. */
527 void Setdt(double delta_t) { dT = delta_t; }
529 /** Sets the root directory where JSBSim starts looking for its system directories.
530 @param rootDir the string containing the root directory. */
531 void SetRootDir(const string& rootDir) {RootDir = rootDir;}
533 /** Retrieves the Root Directory.
534 @return the string representing the root (base) JSBSim directory. */
535 const string& GetRootDir(void) const {return RootDir;}
537 /** Increments the simulation time if not in Holding mode. The Frame counter
539 @return the new simulation time. */
540 double IncrTime(void) {
541 if (!holding) sim_time += dT;
546 /** Retrieves the current debug level setting. */
547 int GetDebugLevel(void) const {return debug_lvl;};
553 unsigned short Terminate;
564 string FullAircraftPath;
571 // Standard Model pointers - shortcuts for internal executive use only.
572 FGPropagate* Propagate;
573 FGInertial* Inertial;
574 FGAtmosphere* Atmosphere;
576 FGAuxiliary* Auxiliary;
578 FGPropulsion* Propulsion;
579 FGAerodynamics* Aerodynamics;
580 FGGroundReactions* GroundReactions;
581 FGExternalReactions* ExternalReactions;
582 FGBuoyantForces* BuoyantForces;
583 FGMassBalance* MassBalance;
584 FGAircraft* Aircraft;
585 FGAccelerations* Accelerations;
590 FGGroundCallback* GroundCallback;
592 FGInitialCondition* IC;
595 FGPropertyManager* Root;
597 FGPropertyManager* instance;
599 // The FDM counter is used to give each child FDM an unique ID. The root FDM has the ID 0
600 unsigned int* FDMctr;
602 vector <string> PropertyCatalog;
603 vector <FGOutput*> Outputs;
604 vector <childData*> ChildFDMList;
605 vector <FGModel*> Models;
607 bool ReadFileHeader(Element*);
608 bool ReadChild(Element*);
609 bool ReadPrologue(Element*);
610 void ResetToInitialConditions(int mode);
611 void LoadInputs(unsigned int idx);
612 void LoadPlanetConstants(void);
613 void LoadModelConstants(void);
615 bool DeAllocate(void);
616 void Initialize(FGInitialCondition *FGIC);
618 void Debug(int from);
621 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%