| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | Header: FGFDMExec.h |
| 3 | Author: Jon Berndt |
| 4 | Date started: 11/17/98 |
| 5 | file The header file for the JSBSim executive. |
| 6 | |
| 7 | ------------- Copyright (C) 1999 Jon S. Berndt ([email protected]) ------------- |
| 8 | |
| 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 |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later version. |
| 13 | |
| 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 |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | 11/17/98 JSB Created |
| 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 |
| 31 | object and dt=0. |
| 32 | |
| 33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 | SENTRY |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 36 | |
| 37 | #ifndef FGFDMEXEC_HEADER_H |
| 38 | #define FGFDMEXEC_HEADER_H |
| 39 | |
| 40 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 41 | INCLUDES |
| 42 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 43 | |
| 44 | #include <memory> |
| 45 | |
| 46 | #include "models/FGPropagate.h" |
| 47 | #include "models/FGOutput.h" |
| 48 | #include "math/FGTemplateFunc.h" |
| 49 | |
| 50 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 | FORWARD DECLARATIONS |
| 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 53 | |
| 54 | namespace JSBSim { |
| 55 | |
| 56 | class FGScript; |
| 57 | class FGTrim; |
| 58 | class FGAerodynamics; |
| 59 | class FGAircraft; |
| 60 | class FGAtmosphere; |
| 61 | class FGAccelerations; |
| 62 | class FGWinds; |
| 63 | class FGAuxiliary; |
| 64 | class FGBuoyantForces; |
| 65 | class FGExternalReactions; |
| 66 | class FGGroundReactions; |
| 67 | class FGFCS; |
| 68 | class FGInertial; |
| 69 | class FGInput; |
| 70 | class FGPropulsion; |
| 71 | class FGMassBalance; |
| 72 | class FGTrim; |
| 73 | |
| 74 | class TrimFailureException : public BaseException { |
| 75 | public: |
| 76 | TrimFailureException(const std::string& msg) : BaseException(msg) {} |
| 77 | }; |
| 78 | |
| 79 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 80 | CLASS DOCUMENTATION |
| 81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 82 | |
| 83 | /** Encapsulates the JSBSim simulation executive. |
| 84 | This class is the executive class through which all other simulation classes |
| 85 | are instantiated, initialized, and run. When integrated with FlightGear (or |
| 86 | other flight simulator) this class is typically instantiated by an interface |
| 87 | class on the simulator side. |
| 88 | |
| 89 | At the time of simulation initialization, the interface |
| 90 | class creates an instance of this executive class. The |
| 91 | executive is subsequently directed to load the chosen aircraft specification |
| 92 | file: |
| 93 | |
| 94 | @code{.cpp} |
| 95 | fdmex = new FGFDMExec( ... ); |
| 96 | result = fdmex->LoadModel( ... ); |
| 97 | @endcode |
| 98 | |
| 99 | When an aircraft model is loaded, the config file is parsed and for each of |
| 100 | the sections of the config file (propulsion, flight control, etc.) the |
| 101 | corresponding Load() method is called (e.g. FGFCS::Load()). |
| 102 | |
| 103 | Subsequent to the creation of the executive and loading of the model, |
| 104 | initialization is performed. Initialization involves copying control inputs |
| 105 | into the appropriate JSBSim data storage locations, configuring it for the |
| 106 | set of user supplied initial conditions, and then copying state variables |
| 107 | from JSBSim. The state variables are used to drive the instrument displays |
| 108 | and to place the vehicle model in world space for visual rendering: |
| 109 | |
| 110 | @code{.cpp} |
| 111 | copy_to_JSBsim(); // copy control inputs to JSBSim |
| 112 | fdmex->RunIC(); // loop JSBSim once w/o integrating |
| 113 | copy_from_JSBsim(); // update the bus |
| 114 | @endcode |
| 115 | |
| 116 | Once initialization is complete, cyclic execution proceeds: |
| 117 | |
| 118 | @code{.cpp} |
| 119 | copy_to_JSBsim(); // copy control inputs to JSBSim |
| 120 | fdmex->Run(); // execute JSBSim |
| 121 | copy_from_JSBsim(); // update the bus |
| 122 | @endcode |
| 123 | |
| 124 | JSBSim can be used in a standalone mode by creating a compact stub program |
| 125 | that effectively performs the same progression of steps as outlined above |
| 126 | for the integrated version, but with two exceptions. First, the |
| 127 | copy_to_JSBSim() and copy_from_JSBSim() functions are not used because the |
| 128 | control inputs are handled directly by the scripting facilities and outputs |
| 129 | are handled by the output (data logging) class. Second, the name of a script |
| 130 | file can be supplied to the stub program. Scripting (see FGScript) provides |
| 131 | a way to supply command inputs to the simulation: |
| 132 | |
| 133 | @code{.cpp} |
| 134 | FDMExec = new JSBSim::FGFDMExec(); |
| 135 | FDMExec->LoadScript( ScriptName ); // the script loads the aircraft and ICs |
| 136 | result = FDMExec->Run(); |
| 137 | while (result) { // cyclic execution |
| 138 | result = FDMExec->Run(); // execute JSBSim |
| 139 | } |
| 140 | @endcode |
| 141 | |
| 142 | The standalone mode has been useful for verifying changes before committing |
| 143 | updates to the source code repository. It is also useful for running sets of |
| 144 | tests that reveal some aspects of simulated aircraft performance, such as |
| 145 | range, time-to-climb, takeoff distance, etc. |
| 146 | |
| 147 | <h3>JSBSim Debugging Directives</h3> |
| 148 | |
| 149 | This describes to any interested entity the debug level |
| 150 | requested by setting the JSBSIM_DEBUG environment variable. |
| 151 | The bitmasked value choices are as follows: |
| 152 | - <b>unset</b>: In this case (the default) JSBSim would only print |
| 153 | out the normally expected messages, essentially echoing |
| 154 | the config files as they are read. If the environment |
| 155 | variable is not set, debug_lvl is set to 1 internally |
| 156 | - <b>0</b>: This requests JSBSim not to output any messages |
| 157 | whatsoever |
| 158 | - <b>1</b>: This value explicity requests the normal JSBSim |
| 159 | startup messages |
| 160 | - <b>2</b>: This value asks for a message to be printed out when |
| 161 | a class is instantiated |
| 162 | - <b>4</b>: When this value is set, a message is displayed when a |
| 163 | FGModel object executes its Run() method |
| 164 | - <b>8</b>: When this value is set, various runtime state variables |
| 165 | are printed out periodically |
| 166 | - <b>16</b>: When set various parameters are sanity checked and |
| 167 | a message is printed out when they go out of bounds |
| 168 | |
| 169 | <h3>Properties</h3> |
| 170 | @property simulator/do_trim (write only) Can be set to the integer equivalent to one of |
| 171 | tLongitudinal (0), tFull (1), tGround (2), tPullup (3), |
| 172 | tCustom (4), tTurn (5). Setting this to a legal value |
| 173 | (such as by a script) causes a trim to be performed. This |
| 174 | property actually maps toa function call of DoTrim(). |
| 175 | |
| 176 | @author Jon S. Berndt |
| 177 | @version $Revision: 1.106 $ |
| 178 | */ |
| 179 | |
| 180 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 | CLASS DECLARATION |
| 182 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 183 | |
| 184 | class JSBSIM_API FGFDMExec : public FGJSBBase |
| 185 | { |
| 186 | struct childData { |
| 187 | FGFDMExec* exec; |
| 188 | std::string info; |
| 189 | FGColumnVector3 Loc; |
| 190 | FGColumnVector3 Orient; |
| 191 | bool mated; |
| 192 | bool internal; |
| 193 | |
| 194 | childData(void) { |
| 195 | info = ""; |
| 196 | Loc = FGColumnVector3(0,0,0); |
| 197 | Orient = FGColumnVector3(0,0,0); |
| 198 | mated = true; |
| 199 | internal = false; |
| 200 | } |
| 201 | |
| 202 | void Run(void) {exec->Run();} |
| 203 | void AssignState(FGPropagate* source_prop) { |
| 204 | exec->GetPropagate()->SetVState(source_prop->GetVState()); |
| 205 | } |
| 206 | |
| 207 | ~childData(void) { |
| 208 | delete exec; |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | public: |
| 213 | |
| 214 | /// Default constructor |
| 215 | FGFDMExec(FGPropertyManager* root = 0, unsigned int* fdmctr = 0); |
| 216 | |
| 217 | /// Default destructor |
| 218 | ~FGFDMExec(); |
| 219 | |
| 220 | // This list of enums is very important! The order in which models are listed |
| 221 | // here determines the order of execution of the models. |
| 222 | // |
| 223 | // There are some conditions that need to be met : |
| 224 | // 1. FCS can request mass geometry changes via the inertia/pointmass-* |
| 225 | // properties so it must be executed before MassBalance |
| 226 | // 2. MassBalance must be executed before Propulsion, Aerodynamics, |
| 227 | // GroundReactions, ExternalReactions and BuoyantForces to ensure that |
| 228 | // their moments are computed with the updated CG position. |
| 229 | enum eModels { ePropagate=0, |
| 230 | eInput, |
| 231 | eInertial, |
| 232 | eAtmosphere, |
| 233 | eWinds, |
| 234 | eSystems, |
| 235 | eMassBalance, |
| 236 | eAuxiliary, |
| 237 | ePropulsion, |
| 238 | eAerodynamics, |
| 239 | eGroundReactions, |
| 240 | eExternalReactions, |
| 241 | eBuoyantForces, |
| 242 | eAircraft, |
| 243 | eAccelerations, |
| 244 | eOutput, |
| 245 | eNumStandardModels }; |
| 246 | |
| 247 | /** Unbind all tied JSBSim properties. */ |
| 248 | void Unbind(void) {instance->Unbind();} |
| 249 | |
| 250 | /** This function executes each scheduled model in succession. |
| 251 | @return true if successful, false if sim should be ended */ |
| 252 | bool Run(void); |
| 253 | |
| 254 | /** Initializes the sim from the initial condition object and executes |
| 255 | each scheduled model without integrating i.e. dt=0. |
| 256 | @return true if successful */ |
| 257 | bool RunIC(void); |
| 258 | |
| 259 | /** Loads the planet. |
| 260 | Loads the definition of the planet on which the vehicle will evolve such as |
| 261 | its radius, gravity or its atmosphere characteristics. |
| 262 | @param PlanetPath The name of a planet definition file |
| 263 | @param useAircraftPath true if path is given relative to the aircraft path. |
| 264 | @return true if successful */ |
| 265 | bool LoadPlanet(const SGPath& PlanetPath, bool useAircraftPath = true); |
| 266 | |
| 267 | /** Loads an aircraft model. |
| 268 | @param AircraftPath path to the aircraft/ directory. For instance: |
| 269 | "aircraft". Under aircraft, then, would be directories for various |
| 270 | modeled aircraft such as C172/, x15/, etc. |
| 271 | @param EnginePath path to the directory under which engine config |
| 272 | files are kept, for instance "engine" |
| 273 | @param SystemsPath path to the directory under which systems config |
| 274 | files are kept, for instance "systems" |
| 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 SGPath& AircraftPath, const SGPath& EnginePath, |
| 283 | const SGPath& SystemsPath, const std::string& model, |
| 284 | bool addModelToPath = true); |
| 285 | |
| 286 | /** Loads an aircraft model. The paths to the aircraft and engine |
| 287 | config file directories must be set prior to calling this. See |
| 288 | below. |
| 289 | @param model the name of the aircraft model itself. This file will |
| 290 | be looked for in the directory specified in the AircraftPath variable, |
| 291 | and in turn under the directory with the same name as the model. For |
| 292 | instance: "aircraft/x15/x15.xml" |
| 293 | @param addModelToPath set to true to add the model name to the |
| 294 | AircraftPath, defaults to true |
| 295 | @return true if successful*/ |
| 296 | bool LoadModel(const std::string& model, bool addModelToPath = true); |
| 297 | |
| 298 | /** Load a script |
| 299 | @param Script The full path name and file name for the script to be loaded. |
| 300 | @param deltaT The simulation integration step size, if given. If no value |
| 301 | is supplied then 0.0 is used and the value is expected to be |
| 302 | supplied in the script file itself. |
| 303 | @param initfile The initialization file that will override the |
| 304 | initialization file specified in the script file. If no |
| 305 | file name is given on the command line, the file specified |
| 306 | in the script will be used. If an initialization file is |
| 307 | not given in either place, an error will result. |
| 308 | @return true if successfully loads; false otherwise. */ |
| 309 | bool LoadScript(const SGPath& Script, double deltaT=0.0, |
| 310 | const SGPath& initfile=SGPath()); |
| 311 | |
| 312 | /** Set the path to the engine config file directories. |
| 313 | Relative paths are taken from the root directory. |
| 314 | @param path path to the directory under which engine config files are |
| 315 | kept, for instance "engine". |
| 316 | @see SetRootDir |
| 317 | @see GetEnginePath */ |
| 318 | bool SetEnginePath(const SGPath& path) { |
| 319 | EnginePath = GetFullPath(path); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | /** Set the path to the aircraft config file directories. |
| 324 | Under this path, then, would be directories for various modeled aircraft |
| 325 | such as C172/, x15/, etc. |
| 326 | Relative paths are taken from the root directory. |
| 327 | @param path path to the aircraft directory, for instance "aircraft". |
| 328 | @see SetRootDir |
| 329 | @see GetAircraftPath */ |
| 330 | bool SetAircraftPath(const SGPath& path) { |
| 331 | AircraftPath = GetFullPath(path); |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | /** Set the path to the systems config file directories. |
| 336 | Relative paths are taken from the root directory. |
| 337 | @param path path to the directory under which systems config files are |
| 338 | kept, for instance "systems" |
| 339 | @see SetRootDir |
| 340 | @see GetSystemsPath */ |
| 341 | bool SetSystemsPath(const SGPath& path) { |
| 342 | SystemsPath = GetFullPath(path); |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | /** Set the directory where the output files will be written. |
| 347 | Relative paths are taken from the root directory. |
| 348 | @param path path to the directory under which the output files will be |
| 349 | written. |
| 350 | @see SetRootDir |
| 351 | @see GetOutputPath */ |
| 352 | bool SetOutputPath(const SGPath& path) { |
| 353 | OutputPath = GetFullPath(path); |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | /// @name Top-level executive State and Model retrieval mechanism |
| 358 | ///@{ |
| 359 | /// Returns the FGAtmosphere pointer. |
| 360 | FGAtmosphere* GetAtmosphere(void) {return (FGAtmosphere*)Models[eAtmosphere];} |
| 361 | /// Returns the FGAccelerations pointer. |
| 362 | FGAccelerations* GetAccelerations(void) {return (FGAccelerations*)Models[eAccelerations];} |
| 363 | /// Returns the FGWinds pointer. |
| 364 | FGWinds* GetWinds(void) {return (FGWinds*)Models[eWinds];} |
| 365 | /// Returns the FGFCS pointer. |
| 366 | FGFCS* GetFCS(void) {return (FGFCS*)Models[eSystems];} |
| 367 | /// Returns the FGPropulsion pointer. |
| 368 | FGPropulsion* GetPropulsion(void) {return (FGPropulsion*)Models[ePropulsion];} |
| 369 | /// Returns the FGAircraft pointer. |
| 370 | FGMassBalance* GetMassBalance(void) {return (FGMassBalance*)Models[eMassBalance];} |
| 371 | /// Returns the FGAerodynamics pointer |
| 372 | FGAerodynamics* GetAerodynamics(void){return (FGAerodynamics*)Models[eAerodynamics];} |
| 373 | /// Returns the FGInertial pointer. |
| 374 | FGInertial* GetInertial(void) {return (FGInertial*)Models[eInertial];} |
| 375 | /// Returns the FGGroundReactions pointer. |
| 376 | FGGroundReactions* GetGroundReactions(void) {return (FGGroundReactions*)Models[eGroundReactions];} |
| 377 | /// Returns the FGExternalReactions pointer. |
| 378 | FGExternalReactions* GetExternalReactions(void) {return (FGExternalReactions*)Models[eExternalReactions];} |
| 379 | /// Returns the FGBuoyantForces pointer. |
| 380 | FGBuoyantForces* GetBuoyantForces(void) {return (FGBuoyantForces*)Models[eBuoyantForces];} |
| 381 | /// Returns the FGAircraft pointer. |
| 382 | FGAircraft* GetAircraft(void) {return (FGAircraft*)Models[eAircraft];} |
| 383 | /// Returns the FGPropagate pointer. |
| 384 | FGPropagate* GetPropagate(void) {return (FGPropagate*)Models[ePropagate];} |
| 385 | /// Returns the FGAuxiliary pointer. |
| 386 | FGAuxiliary* GetAuxiliary(void) {return (FGAuxiliary*)Models[eAuxiliary];} |
| 387 | /// Returns the FGInput pointer. |
| 388 | FGInput* GetInput(void) {return (FGInput*)Models[eInput];} |
| 389 | /// Returns the FGOutput pointer. |
| 390 | FGOutput* GetOutput(void) {return (FGOutput*)Models[eOutput];} |
| 391 | /// Retrieves the script object |
| 392 | FGScript* GetScript(void) {return Script;} |
| 393 | /// Returns a pointer to the FGInitialCondition object |
| 394 | FGInitialCondition* GetIC(void) {return IC;} |
| 395 | /// Returns a pointer to the FGTrim object |
| 396 | FGTrim* GetTrim(void); |
| 397 | ///@} |
| 398 | |
| 399 | /// Retrieves the engine path. |
| 400 | const SGPath& GetEnginePath(void) { return EnginePath; } |
| 401 | /// Retrieves the aircraft path. |
| 402 | const SGPath& GetAircraftPath(void) { return AircraftPath; } |
| 403 | /// Retrieves the systems path. |
| 404 | const SGPath& GetSystemsPath(void) { return SystemsPath; } |
| 405 | /// Retrieves the full aircraft path name. |
| 406 | const SGPath& GetFullAircraftPath(void) { return FullAircraftPath; } |
| 407 | /// Retrieves the path to the output files. |
| 408 | const SGPath& GetOutputPath(void) { return OutputPath; } |
| 409 | |
| 410 | /** Retrieves the value of a property. |
| 411 | @param property the name of the property |
| 412 | @result the value of the specified property */ |
| 413 | inline double GetPropertyValue(const std::string& property) |
| 414 | { return instance->GetNode()->GetDouble(property); } |
| 415 | |
| 416 | /** Sets a property value. |
| 417 | @param property the property to be set |
| 418 | @param value the value to set the property to */ |
| 419 | inline void SetPropertyValue(const std::string& property, double value) { |
| 420 | instance->GetNode()->SetDouble(property, value); |
| 421 | } |
| 422 | |
| 423 | /// Returns the model name. |
| 424 | const std::string& GetModelName(void) const { return modelName; } |
| 425 | |
| 426 | /// Returns a pointer to the property manager object. |
| 427 | FGPropertyManager* GetPropertyManager(void); |
| 428 | /// Returns a vector of strings representing the names of all loaded models (future) |
| 429 | std::vector <std::string> EnumerateFDMs(void); |
| 430 | /// Gets the number of child FDMs. |
| 431 | int GetFDMCount(void) const {return (int)ChildFDMList.size();} |
| 432 | /// Gets a particular child FDM. |
| 433 | childData* GetChildFDM(int i) const {return ChildFDMList[i];} |
| 434 | /// Marks this instance of the Exec object as a "child" object. |
| 435 | void SetChild(bool ch) {IsChild = ch;} |
| 436 | |
| 437 | /** Sets the output (logging) mechanism for this run. |
| 438 | Calling this function passes the name of an output directives file to |
| 439 | the FGOutput object associated with this run. The call to this function |
| 440 | should be made prior to loading an aircraft model. This call results in an |
| 441 | FGOutput object being built as the first Output object in the FDMExec-managed |
| 442 | list of Output objects that may be created for an aircraft model. If this call |
| 443 | is made after an aircraft model is loaded, there is no effect. Any Output |
| 444 | objects added by the aircraft model itself (in an <output> element) will be |
| 445 | added after this one. Care should be taken not to refer to the same file |
| 446 | name. |
| 447 | An output directives file contains an <output> </output> element, within |
| 448 | which should be specified the parameters or parameter groups that should |
| 449 | be logged. |
| 450 | @param fname the filename of an output directives file. |
| 451 | */ |
| 452 | bool SetOutputDirectives(const SGPath& fname) |
| 453 | { return Output->SetDirectivesFile(GetFullPath(fname)); } |
| 454 | |
| 455 | /** Forces the specified output object to print its items once */ |
| 456 | void ForceOutput(int idx=0) { Output->ForceOutput(idx); } |
| 457 | |
| 458 | /** Sets the logging rate in Hz for all output objects (if any). */ |
| 459 | void SetLoggingRate(double rate) { Output->SetRateHz(rate); } |
| 460 | |
| 461 | /** Sets (or overrides) the output filename |
| 462 | @param n index of file |
| 463 | @param fname the name of the file to output data to |
| 464 | @return true if successful, false if there is no output specified for the flight model */ |
| 465 | bool SetOutputFileName(const int n, const std::string& fname) { return Output->SetOutputName(n, fname); } |
| 466 | |
| 467 | /** Retrieves the current output filename. |
| 468 | @param n index of file |
| 469 | @return the name of the output file for the output specified by the flight model. |
| 470 | If none is specified, the empty string is returned. */ |
| 471 | std::string GetOutputFileName(int n) const { return Output->GetOutputName(n); } |
| 472 | |
| 473 | /** Executes trimming in the selected mode. |
| 474 | * @param mode Specifies how to trim: |
| 475 | * - tLongitudinal=0 |
| 476 | * - tFull |
| 477 | * - tGround |
| 478 | * - tPullup |
| 479 | * - tCustom |
| 480 | * - tTurn |
| 481 | * - tNone */ |
| 482 | void DoTrim(int mode); |
| 483 | |
| 484 | /// Disables data logging to all outputs. |
| 485 | void DisableOutput(void) { Output->Disable(); } |
| 486 | /// Enables data logging to all outputs. |
| 487 | void EnableOutput(void) { Output->Enable(); } |
| 488 | /// Pauses execution by preventing time from incrementing. |
| 489 | void Hold(void) {holding = true;} |
| 490 | /// Turn on hold after increment |
| 491 | void EnableIncrementThenHold(int Timesteps) {TimeStepsUntilHold = Timesteps; IncrementThenHolding = true;} |
| 492 | /// Checks if required to hold afer increment |
| 493 | void CheckIncrementalHold(void); |
| 494 | /// Resumes execution from a "Hold". |
| 495 | void Resume(void) {holding = false;} |
| 496 | /// Returns true if the simulation is Holding (i.e. simulation time is not moving). |
| 497 | bool Holding(void) {return holding;} |
| 498 | /// Mode flags for ResetToInitialConditions |
| 499 | static const int START_NEW_OUTPUT = 0x1; |
| 500 | static const int DONT_EXECUTE_RUN_IC = 0x2; |
| 501 | /** Resets the initial conditions object and prepares the simulation to run |
| 502 | again. If the mode's first bit is set the output instances will take special actions |
| 503 | such as closing the current output file and open a new one with a different name. |
| 504 | If the second bit is set then RunIC() won't be executed, leaving it to the caller |
| 505 | to call RunIC(), e.g. in case the caller wants to set some other state like control |
| 506 | surface deflections which would've been reset. |
| 507 | @param mode Sets the reset mode.*/ |
| 508 | void ResetToInitialConditions(int mode); |
| 509 | /// Sets the debug level. |
| 510 | void SetDebugLevel(int level) {debug_lvl = level;} |
| 511 | |
| 512 | struct PropertyCatalogStructure { |
| 513 | /// Name of the property. |
| 514 | std::string base_string; |
| 515 | /// The node for the property. |
| 516 | FGPropertyNode_ptr node; |
| 517 | }; |
| 518 | |
| 519 | /** Builds a catalog of properties. |
| 520 | * This function descends the property tree and creates a list (an STL vector) |
| 521 | * containing the name and node for all properties. |
| 522 | * @param pcs The "root" property catalog structure pointer. */ |
| 523 | void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs); |
| 524 | |
| 525 | /** Retrieves property or properties matching the supplied string. |
| 526 | * A string is returned that contains a carriage return delimited list of all |
| 527 | * strings in the property catalog that matches the supplied check string. |
| 528 | * @param check The string to search for in the property catalog. |
| 529 | * @param end_of_line End of line (CR+LF if needed for Windows). |
| 530 | * @return the carriage-return-delimited string containing all matching strings |
| 531 | * in the catalog. */ |
| 532 | std::string QueryPropertyCatalog(const std::string& check, const std::string& end_of_line="\n"); |
| 533 | |
| 534 | // Print the contents of the property catalog for the loaded aircraft. |
| 535 | void PrintPropertyCatalog(void); |
| 536 | |
| 537 | // Print the simulation configuration |
| 538 | void PrintSimulationConfiguration(void) const; |
| 539 | |
| 540 | std::vector<std::string>& GetPropertyCatalog(void) {return PropertyCatalog;} |
| 541 | |
| 542 | void SetTrimStatus(bool status){ trim_status = status; } |
| 543 | bool GetTrimStatus(void) const { return trim_status; } |
| 544 | void SetTrimMode(int mode){ ta_mode = mode; } |
| 545 | int GetTrimMode(void) const { return ta_mode; } |
| 546 | |
| 547 | std::string GetPropulsionTankReport(); |
| 548 | |
| 549 | /// Returns the cumulative simulation time in seconds. |
| 550 | double GetSimTime(void) const { return sim_time; } |
| 551 | |
| 552 | /// Returns the simulation delta T. |
| 553 | double GetDeltaT(void) const {return dT;} |
| 554 | |
| 555 | /// Suspends the simulation and sets the delta T to zero. |
| 556 | void SuspendIntegration(void) {saved_dT = dT; dT = 0.0;} |
| 557 | |
| 558 | /// Resumes the simulation by resetting delta T to the correct value. |
| 559 | void ResumeIntegration(void) {dT = saved_dT;} |
| 560 | |
| 561 | /** Returns the simulation suspension state. |
| 562 | @return true if suspended, false if executing */ |
| 563 | bool IntegrationSuspended(void) const {return dT == 0.0;} |
| 564 | |
| 565 | /** Sets the current sim time. |
| 566 | @param cur_time the current time |
| 567 | @return the current simulation time. */ |
| 568 | double Setsim_time(double cur_time); |
| 569 | |
| 570 | /** Sets the integration time step for the simulation executive. |
| 571 | @param delta_t the time step in seconds. */ |
| 572 | void Setdt(double delta_t) { dT = delta_t; } |
| 573 | |
| 574 | /** Set the root directory that is used to obtain absolute paths from |
| 575 | relative paths. |
| 576 | Aircraft, engine, systems and output paths are not updated by this |
| 577 | method. You must call each methods (SetAircraftPath(), SetEnginePath(), |
| 578 | etc.) individually if you need to update these paths as well. |
| 579 | @param rootDir the path to the root directory. |
| 580 | @see GetRootDir |
| 581 | @see SetAircraftPath |
| 582 | @see SetEnginePath |
| 583 | @see SetSystemsPath |
| 584 | @see SetOutputPath |
| 585 | */ |
| 586 | void SetRootDir(const SGPath& rootDir) {RootDir = rootDir;} |
| 587 | |
| 588 | /** Retrieve the Root Directory. |
| 589 | @return the path to the root (base) JSBSim directory. |
| 590 | @see SetRootDir */ |
| 591 | const SGPath& GetRootDir(void) const {return RootDir;} |
| 592 | |
| 593 | /** Increments the simulation time if not in Holding mode. The Frame counter |
| 594 | is also incremented. |
| 595 | @return the new simulation time. */ |
| 596 | double IncrTime(void); |
| 597 | |
| 598 | /** Retrieves the current frame count. */ |
| 599 | unsigned int GetFrame(void) const {return Frame;} |
| 600 | |
| 601 | /** Retrieves the current debug level setting. */ |
| 602 | int GetDebugLevel(void) const {return debug_lvl;}; |
| 603 | |
| 604 | /** Initializes the simulation with initial conditions |
| 605 | @param FGIC The initial conditions that will be passed to the simulation. */ |
| 606 | void Initialize(FGInitialCondition *FGIC); |
| 607 | |
| 608 | /** Sets the property forces/hold-down. This allows to do hard 'hold-down' |
| 609 | such as for rockets on a launch pad with engines ignited. |
| 610 | @param hd enables the 'hold-down' function if non-zero |
| 611 | */ |
| 612 | void SetHoldDown(bool hd); |
| 613 | |
| 614 | /** Gets the value of the property forces/hold-down. |
| 615 | @result zero if the 'hold-down' function is disabled, non-zero otherwise. |
| 616 | */ |
| 617 | bool GetHoldDown(void) const {return HoldDown;} |
| 618 | |
| 619 | FGTemplateFunc* GetTemplateFunc(const std::string& name) { |
| 620 | return TemplateFunctions.count(name) ? TemplateFunctions[name] : nullptr; |
| 621 | } |
| 622 | |
| 623 | void AddTemplateFunc(const std::string& name, Element* el) { |
| 624 | TemplateFunctions[name] = new FGTemplateFunc(this, el); |
| 625 | } |
| 626 | |
| 627 | const std::shared_ptr<RandomNumberGenerator>& GetRandomGenerator(void) const |
| 628 | { return RandomGenerator; } |
| 629 | |
| 630 | private: |
| 631 | unsigned int Frame; |
| 632 | unsigned int IdFDM; |
| 633 | int disperse; |
| 634 | unsigned short Terminate; |
| 635 | double dT; |
| 636 | double saved_dT; |
| 637 | double sim_time; |
| 638 | bool holding; |
| 639 | bool IncrementThenHolding; |
| 640 | int TimeStepsUntilHold; |
| 641 | bool Constructing; |
| 642 | bool modelLoaded; |
| 643 | bool IsChild; |
| 644 | std::string modelName; |
| 645 | SGPath AircraftPath; |
| 646 | SGPath FullAircraftPath; |
| 647 | SGPath EnginePath; |
| 648 | SGPath SystemsPath; |
| 649 | SGPath OutputPath; |
| 650 | std::string CFGVersion; |
| 651 | std::string Release; |
| 652 | SGPath RootDir; |
| 653 | |
| 654 | // Standard Model pointers - shortcuts for internal executive use only. |
| 655 | FGPropagate* Propagate; |
| 656 | FGInertial* Inertial; |
| 657 | FGAtmosphere* Atmosphere; |
| 658 | FGWinds* Winds; |
| 659 | FGAuxiliary* Auxiliary; |
| 660 | FGFCS* FCS; |
| 661 | FGPropulsion* Propulsion; |
| 662 | FGAerodynamics* Aerodynamics; |
| 663 | FGGroundReactions* GroundReactions; |
| 664 | FGExternalReactions* ExternalReactions; |
| 665 | FGBuoyantForces* BuoyantForces; |
| 666 | FGMassBalance* MassBalance; |
| 667 | FGAircraft* Aircraft; |
| 668 | FGAccelerations* Accelerations; |
| 669 | FGOutput* Output; |
| 670 | |
| 671 | bool trim_status; |
| 672 | int ta_mode; |
| 673 | unsigned int ResetMode; |
| 674 | int trim_completed; |
| 675 | |
| 676 | FGScript* Script; |
| 677 | FGInitialCondition* IC; |
| 678 | FGTrim* Trim; |
| 679 | |
| 680 | FGPropertyManager* Root; |
| 681 | bool StandAlone; |
| 682 | FGPropertyManager* instance; |
| 683 | |
| 684 | bool HoldDown; |
| 685 | |
| 686 | unsigned int RandomSeed; |
| 687 | std::shared_ptr<RandomNumberGenerator> RandomGenerator; |
| 688 | |
| 689 | // The FDM counter is used to give each child FDM an unique ID. The root FDM |
| 690 | // has the ID 0 |
| 691 | unsigned int* FDMctr; |
| 692 | |
| 693 | std::vector <std::string> PropertyCatalog; |
| 694 | std::vector <childData*> ChildFDMList; |
| 695 | std::vector <FGModel*> Models; |
| 696 | std::map<std::string, FGTemplateFunc_ptr> TemplateFunctions; |
| 697 | |
| 698 | bool ReadFileHeader(Element*); |
| 699 | bool ReadChild(Element*); |
| 700 | bool ReadPrologue(Element*); |
| 701 | void SRand(int sr); |
| 702 | int SRand(void) const {return RandomSeed;} |
| 703 | void LoadInputs(unsigned int idx); |
| 704 | void LoadPlanetConstants(void); |
| 705 | bool LoadPlanet(Element* el); |
| 706 | void LoadModelConstants(void); |
| 707 | bool Allocate(void); |
| 708 | bool DeAllocate(void); |
| 709 | void InitializeModels(void); |
| 710 | int GetDisperse(void) const {return disperse;} |
| 711 | SGPath GetFullPath(const SGPath& name) { |
| 712 | if (name.isRelative()) |
| 713 | return RootDir/name.utf8Str(); |
| 714 | else |
| 715 | return name; |
| 716 | } |
| 717 | |
| 718 | void Debug(int from); |
| 719 | }; |
| 720 | } |
| 721 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 722 | #endif |