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( ... );
105 result = fdmex->LoadModel( ... );
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 /** Unbind all tied JSBSim properties. */
230 void Unbind(void) {instance->Unbind();}
232 /** This routine places a model into the runlist at the specified rate. The
233 "rate" is not really a clock rate. It represents how many calls to the
234 FGFDMExec::Run() method must be made before the model is executed. A
235 value of 1 means that the model will be executed for each call to the
236 exec's Run() method. A value of 5 means that the model will only be
237 executed every 5th call to the exec's Run() method. Use of a rate other than
238 one is at this time not recommended.
239 @param model A pointer to the model being scheduled.
240 @param rate The rate at which to execute the model as described above.
241 @return Currently returns 0 always. */
242 void Schedule(FGModel* model, int rate);
244 /** This function executes each scheduled model in succession.
245 @return true if successful, false if sim should be ended */
248 /** Initializes the sim from the initial condition object and executes
249 each scheduled model without integrating i.e. dt=0.
250 @return true if successful */
253 /** Sets the ground callback pointer.
254 @param gc A pointer to a ground callback object. */
255 void SetGroundCallback(FGGroundCallback* gc);
257 /** Loads an aircraft model.
258 @param AircraftPath path to the aircraft/ directory. For instance:
259 "aircraft". Under aircraft, then, would be directories for various
260 modeled aircraft such as C172/, x15/, etc.
261 @param EnginePath path to the directory under which engine config
262 files are kept, for instance "engine"
263 @param SystemsPath path to the directory under which systems config
264 files are kept, for instance "systems"
265 @param model the name of the aircraft model itself. This file will
266 be looked for in the directory specified in the AircraftPath variable,
267 and in turn under the directory with the same name as the model. For
268 instance: "aircraft/x15/x15.xml"
269 @param addModelToPath set to true to add the model name to the
270 AircraftPath, defaults to true
271 @return true if successful */
272 bool LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
273 const string& model, bool addModelToPath = true);
275 /** Loads an aircraft model. The paths to the aircraft and engine
276 config file directories must be set prior to calling this. See
278 @param model the name of the aircraft model itself. This file will
279 be looked for in the directory specified in the AircraftPath variable,
280 and in turn under the directory with the same name as the model. For
281 instance: "aircraft/x15/x15.xml"
282 @param addModelToPath set to true to add the model name to the
283 AircraftPath, defaults to true
284 @return true if successful*/
285 bool LoadModel(const string& model, bool addModelToPath = true);
288 @param Script the full path name and file name for the script to be loaded.
289 @return true if successfully loadsd; false otherwise. */
290 bool LoadScript(const string& Script, double deltaT);
292 /** Sets the path to the engine config file directories.
293 @param path path to the directory under which engine config
294 files are kept, for instance "engine" */
295 bool SetEnginePath(const string& path) { EnginePath = RootDir + path; return true; }
297 /** Sets the path to the aircraft config file directories.
298 @param path path to the aircraft directory. For instance:
299 "aircraft". Under aircraft, then, would be directories for various
300 modeled aircraft such as C172/, x15/, etc. */
301 bool SetAircraftPath(const string& path) { AircraftPath = RootDir + path; return true; }
303 /** Sets the path to the systems config file directories.
304 @param path path to the directory under which systems config
305 files are kept, for instance "systems" */
306 bool SetSystemsPath(const string& path) { SystemsPath = RootDir + path; return true; }
308 /// @name Top-level executive State and Model retrieval mechanism
310 /// Returns the FGAtmosphere pointer.
311 FGAtmosphere* GetAtmosphere(void) {return Atmosphere;}
312 /// Returns the FGFCS pointer.
313 FGFCS* GetFCS(void) {return FCS;}
314 /// Returns the FGPropulsion pointer.
315 FGPropulsion* GetPropulsion(void) {return Propulsion;}
316 /// Returns the FGAircraft pointer.
317 FGMassBalance* GetMassBalance(void) {return MassBalance;}
318 /// Returns the FGAerodynamics pointer
319 FGAerodynamics* GetAerodynamics(void){return Aerodynamics;}
320 /// Returns the FGInertial pointer.
321 FGInertial* GetInertial(void) {return Inertial;}
322 /// Returns the FGGroundReactions pointer.
323 FGGroundReactions* GetGroundReactions(void) {return GroundReactions;}
324 /// Returns the FGExternalReactions pointer.
325 FGExternalReactions* GetExternalReactions(void) {return ExternalReactions;}
326 /// Returns the FGBuoyantForces pointer.
327 FGBuoyantForces* GetBuoyantForces(void) {return BuoyantForces;}
328 /// Returns the FGAircraft pointer.
329 FGAircraft* GetAircraft(void) {return Aircraft;}
330 /// Returns the FGPropagate pointer.
331 FGPropagate* GetPropagate(void) {return Propagate;}
332 /// Returns the FGAuxiliary pointer.
333 FGAuxiliary* GetAuxiliary(void) {return Auxiliary;}
334 /// Returns the FGInput pointer.
335 FGInput* GetInput(void) {return Input;}
336 /// Returns the FGGroundCallback pointer.
337 FGGroundCallback* GetGroundCallback(void) {return GroundCallback;}
338 /// Retrieves the script object
339 FGScript* GetScript(void) {return Script;}
340 // Returns a pointer to the FGInitialCondition object
341 FGInitialCondition* GetIC(void) {return IC;}
342 // Returns a pointer to the FGTrim object
343 FGTrim* GetTrim(void);
346 /// Retrieves the engine path.
347 const string& GetEnginePath(void) {return EnginePath;}
348 /// Retrieves the aircraft path.
349 const string& GetAircraftPath(void) {return AircraftPath;}
350 /// Retrieves the systems path.
351 const string& GetSystemsPath(void) {return SystemsPath;}
352 /// Retrieves the full aircraft path name.
353 const string& GetFullAircraftPath(void) {return FullAircraftPath;}
355 /** Retrieves the value of a property.
356 @param property the name of the property
357 @result the value of the specified property */
358 inline double GetPropertyValue(const string& property) {return instance->GetDouble(property);}
360 /** Sets a property value.
361 @param property the property to be set
362 @param value the value to set the property to */
363 inline void SetPropertyValue(const string& property, double value) {
364 instance->SetDouble(property, value);
367 /// Returns the model name.
368 const string& GetModelName(void) { return modelName; }
370 /// Returns the current time.
371 double GetSimTime(void);
373 /// Returns the current frame time (delta T).
374 double GetDeltaT(void);
376 /// Returns a pointer to the property manager object.
377 FGPropertyManager* GetPropertyManager(void);
378 /// Returns a vector of strings representing the names of all loaded models (future)
379 vector <string> EnumerateFDMs(void);
380 /// Gets the number of child FDMs.
381 int GetFDMCount(void) {return (int)ChildFDMList.size();}
382 /// Gets a particular child FDM.
383 childData* GetChildFDM(int i) {return ChildFDMList[i];}
384 /// Marks this instance of the Exec object as a "child" object.
385 void SetChild(bool ch) {IsChild = ch;}
387 /** Sets the output (logging) mechanism for this run.
388 Calling this function passes the name of an output directives file to
389 the FGOutput object associated with this run. The call to this function
390 should be made prior to loading an aircraft model. This call results in an
391 FGOutput object being built as the first Output object in the FDMExec-managed
392 list of Output objects that may be created for an aircraft model. If this call
393 is made after an aircraft model is loaded, there is no effect. Any Output
394 objects added by the aircraft model itself (in an <output> element) will be
395 added after this one. Care should be taken not to refer to the same file
397 An output directives file contains an <output> </output> element, within
398 which should be specified the parameters or parameter groups that should
400 @param fname the filename of an output directives file.
402 bool SetOutputDirectives(const string& fname);
404 /** Sets (or overrides) the output filename
405 @param fname the name of the file to output data to
406 @return true if successful, false if there is no output specified for the flight model */
407 bool SetOutputFileName(const string& fname) {
408 if (Outputs.size() > 0) Outputs[0]->SetOutputFileName(fname);
413 /** Retrieves the current output filename.
414 @return the name of the output file for the first output specified by the flight model.
415 If none is specified, the empty string is returned. */
416 string GetOutputFileName(void) {
417 if (Outputs.size() > 0) return Outputs[0]->GetOutputFileName();
418 else return string("");
421 /** Executes trimming in the selected mode.
422 * @param mode Specifies how to trim:
430 void DoTrim(int mode);
431 // void DoTrimAnalysis(int mode);
433 /// Disables data logging to all outputs.
434 void DisableOutput(void);
435 /// Enables data logging to all outputs.
436 void EnableOutput(void);
437 /// Pauses execution by preventing time from incrementing.
438 void Hold(void) {holding = true;}
439 /// Resumes execution from a "Hold".
440 void Resume(void) {holding = false;}
441 /// Returns true if the simulation is Holding (i.e. simulation time is not moving).
442 bool Holding(void) {return holding;}
443 /// Resets the initial conditions object and prepares the simulation to run again.
444 void ResetToInitialConditions(void);
445 /// Sets the debug level.
446 void SetDebugLevel(int level) {debug_lvl = level;}
448 struct PropertyCatalogStructure {
449 /// Name of the property.
451 /// The node for the property.
452 FGPropertyManager *node;
455 /** Builds a catalog of properties.
456 * This function descends the property tree and creates a list (an STL vector)
457 * containing the name and node for all properties.
458 * @param pcs The "root" property catalog structure pointer. */
459 void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
461 /** Retrieves property or properties matching the supplied string.
462 * A string is returned that contains a carriage return delimited list of all
463 * strings in the property catalog that matches the supplied check string.
464 * @param check The string to search for in the property catalog.
465 * @return the carriage-return-delimited string containing all matching strings
467 string QueryPropertyCatalog(const string& check);
469 // Print the contents of the property catalog for the loaded aircraft.
470 void PrintPropertyCatalog(void);
472 vector<string>& GetPropertyCatalog(void) {return PropertyCatalog;}
474 /// Use the MSIS atmosphere model.
475 void UseAtmosphereMSIS(void);
477 /// Use the Mars atmosphere model. (Not operative yet.)
478 void UseAtmosphereMars(void);
480 void SetTrimStatus(bool status){ trim_status = status; }
481 bool GetTrimStatus(void) const { return trim_status; }
482 void SetTrimMode(int mode){ ta_mode = mode; }
483 int GetTrimMode(void) const { return ta_mode; }
485 /// Returns the cumulative simulation time in seconds.
486 double GetSimTime(void) const { return sim_time; }
488 /// Returns the simulation delta T.
489 double GetDeltaT(void) {return dT;}
491 /// Suspends the simulation and sets the delta T to zero.
492 void SuspendIntegration(void) {saved_dT = dT; dT = 0.0;}
494 /// Resumes the simulation by resetting delta T to the correct value.
495 void ResumeIntegration(void) {dT = saved_dT;}
497 /** Returns the simulation suspension state.
498 @return true if suspended, false if executing */
499 bool IntegrationSuspended(void) {return dT == 0.0;}
501 /** Sets the current sim time.
502 @param cur_time the current time
503 @return the current simulation time. */
504 double Setsim_time(double cur_time) {
509 /** Sets the integration time step for the simulation executive.
510 @param delta_t the time step in seconds. */
511 void Setdt(double delta_t) { dT = delta_t; }
513 /** Sets the root directory where JSBSim starts looking for its system directories.
514 @param rootDir the string containing the root directory. */
515 void SetRootDir(const string& rootDir) {RootDir = rootDir;}
517 /** Retrieves teh Root Directory.
518 @return the string representing the root (base) JSBSim directory. */
519 const string& GetRootDir(void) const {return RootDir;}
521 /** Increments the simulation time.
522 @return the new simulation time. */
523 double IncrTime(void) {
528 /** Retrieves the current debug level setting. */
529 int GetDebugLevel(void) const {return debug_lvl;};
535 unsigned short Terminate;
545 string FullAircraftPath;
555 FGGroundCallback* GroundCallback;
556 FGAtmosphere* Atmosphere;
558 FGPropulsion* Propulsion;
559 FGMassBalance* MassBalance;
560 FGAerodynamics* Aerodynamics;
561 FGInertial* Inertial;
562 FGGroundReactions* GroundReactions;
563 FGExternalReactions* ExternalReactions;
564 FGBuoyantForces* BuoyantForces;
565 FGAircraft* Aircraft;
566 FGPropagate* Propagate;
567 FGAuxiliary* Auxiliary;
570 FGInitialCondition* IC;
573 FGPropertyManager* Root;
575 FGPropertyManager* instance;
577 // The FDM counter is used to give each child FDM an unique ID. The root FDM has the ID 0
578 unsigned int* FDMctr;
580 vector <string> PropertyCatalog;
581 vector <FGOutput*> Outputs;
582 vector <childData*> ChildFDMList;
583 vector <FGModel*> Models;
585 bool ReadFileHeader(Element*);
586 bool ReadChild(Element*);
587 bool ReadPrologue(Element*);
588 void ResetToInitialConditions(int mode);
590 bool DeAllocate(void);
591 void Initialize(FGInitialCondition *FGIC);
593 void Debug(int from);
596 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%