| 1 | |
| 2 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | |
| 4 | Module: FGFDMExec.cpp |
| 5 | Author: Jon S. Berndt |
| 6 | Date started: 11/17/98 |
| 7 | Purpose: Schedules and runs the model routines. |
| 8 | |
| 9 | ------------- Copyright (C) 1999 Jon S. Berndt ([email protected]) ------------- |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify it under |
| 12 | the terms of the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 19 | details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License along |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on the world wide web at http://www.gnu.org. |
| 27 | |
| 28 | FUNCTIONAL DESCRIPTION |
| 29 | -------------------------------------------------------------------------------- |
| 30 | |
| 31 | This class wraps up the simulation scheduling routines. |
| 32 | |
| 33 | HISTORY |
| 34 | -------------------------------------------------------------------------------- |
| 35 | 11/17/98 JSB Created |
| 36 | |
| 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | COMMENTS, REFERENCES, and NOTES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | |
| 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 42 | INCLUDES |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 44 | |
| 45 | #include <iomanip> |
| 46 | |
| 47 | #include "FGFDMExec.h" |
| 48 | #include "models/atmosphere/FGStandardAtmosphere.h" |
| 49 | #include "models/atmosphere/FGMSIS.h" |
| 50 | #include "models/atmosphere/FGWinds.h" |
| 51 | #include "models/FGFCS.h" |
| 52 | #include "models/FGPropulsion.h" |
| 53 | #include "models/FGMassBalance.h" |
| 54 | #include "models/FGExternalReactions.h" |
| 55 | #include "models/FGBuoyantForces.h" |
| 56 | #include "models/FGAerodynamics.h" |
| 57 | #include "models/FGInertial.h" |
| 58 | #include "models/FGAircraft.h" |
| 59 | #include "models/FGAccelerations.h" |
| 60 | #include "models/FGAuxiliary.h" |
| 61 | #include "models/FGInput.h" |
| 62 | #include "initialization/FGTrim.h" |
| 63 | #include "input_output/FGScript.h" |
| 64 | #include "input_output/FGXMLFileRead.h" |
| 65 | |
| 66 | using namespace std; |
| 67 | |
| 68 | namespace JSBSim { |
| 69 | |
| 70 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 | CLASS IMPLEMENTATION |
| 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 73 | |
| 74 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | // Constructor |
| 76 | |
| 77 | FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) |
| 78 | : Root(root), RandomSeed(0), |
| 79 | RandomGenerator(make_shared<RandomNumberGenerator>(RandomSeed)), FDMctr(fdmctr) |
| 80 | { |
| 81 | Frame = 0; |
| 82 | IC = nullptr; |
| 83 | Trim = nullptr; |
| 84 | Script = nullptr; |
| 85 | disperse = 0; |
| 86 | |
| 87 | RootDir = ""; |
| 88 | |
| 89 | modelLoaded = false; |
| 90 | IsChild = false; |
| 91 | holding = false; |
| 92 | Terminate = false; |
| 93 | StandAlone = false; |
| 94 | ResetMode = 0; |
| 95 | HoldDown = false; |
| 96 | |
| 97 | IncrementThenHolding = false; // increment then hold is off by default |
| 98 | TimeStepsUntilHold = -1; |
| 99 | |
| 100 | sim_time = 0.0; |
| 101 | dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is |
| 102 | // run in standalone mode with no initialization file. |
| 103 | |
| 104 | AircraftPath = "aircraft"; |
| 105 | EnginePath = "engine"; |
| 106 | SystemsPath = "systems"; |
| 107 | |
| 108 | try { |
| 109 | char* num = getenv("JSBSIM_DEBUG"); |
| 110 | if (num) debug_lvl = atoi(num); // set debug level |
| 111 | } catch (...) { // if error set to 1 |
| 112 | debug_lvl = 1; |
| 113 | } |
| 114 | |
| 115 | if (Root == 0) { // Then this is the root FDM |
| 116 | Root = new FGPropertyManager; // Create the property manager |
| 117 | StandAlone = true; |
| 118 | } |
| 119 | |
| 120 | if (FDMctr == 0) { |
| 121 | FDMctr = new unsigned int; // Create and initialize the child FDM counter |
| 122 | (*FDMctr) = 0; |
| 123 | } |
| 124 | |
| 125 | // Store this FDM's ID |
| 126 | IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth" |
| 127 | |
| 128 | // Prepare FDMctr for the next child FDM id |
| 129 | (*FDMctr)++; // instance. "child" instances are loaded last. |
| 130 | |
| 131 | FGPropertyNode* instanceRoot = Root->GetNode("/fdm/jsbsim",IdFDM,true); |
| 132 | instance = new FGPropertyManager(instanceRoot); |
| 133 | |
| 134 | try { |
| 135 | char* num = getenv("JSBSIM_DISPERSE"); |
| 136 | if (num) { |
| 137 | if (atoi(num) != 0) disperse = 1; // set dispersions on |
| 138 | } |
| 139 | } catch (...) { // if error set to false |
| 140 | disperse = 0; |
| 141 | std::cerr << "Could not process JSBSIM_DISPERSIONS environment variable: Assumed NO dispersions." << endl; |
| 142 | } |
| 143 | |
| 144 | Debug(0); |
| 145 | // this is to catch errors in binding member functions to the property tree. |
| 146 | try { |
| 147 | Allocate(); |
| 148 | } |
| 149 | catch (const string& msg) { |
| 150 | cerr << endl << "Caught error: " << msg << endl; |
| 151 | throw; |
| 152 | } |
| 153 | catch (const BaseException& e) { |
| 154 | cout << endl << "Caught error: " << e.what() << endl; |
| 155 | throw; |
| 156 | } |
| 157 | |
| 158 | trim_status = false; |
| 159 | ta_mode = 99; |
| 160 | trim_completed = 0; |
| 161 | |
| 162 | Constructing = true; |
| 163 | typedef int (FGFDMExec::*iPMF)(void) const; |
| 164 | instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim); |
| 165 | instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions); |
| 166 | instance->Tie("simulation/disperse", this, &FGFDMExec::GetDisperse); |
| 167 | instance->Tie("simulation/randomseed", this, (iPMF)&FGFDMExec::SRand, &FGFDMExec::SRand); |
| 168 | instance->Tie("simulation/terminate", (int *)&Terminate); |
| 169 | instance->Tie("simulation/pause", (int *)&holding); |
| 170 | instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime); |
| 171 | instance->Tie("simulation/dt", this, &FGFDMExec::GetDeltaT); |
| 172 | instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel); |
| 173 | instance->Tie("simulation/frame", (int *)&Frame); |
| 174 | instance->Tie("simulation/trim-completed", (int *)&trim_completed); |
| 175 | instance->Tie("forces/hold-down", this, &FGFDMExec::GetHoldDown, &FGFDMExec::SetHoldDown); |
| 176 | |
| 177 | Constructing = false; |
| 178 | } |
| 179 | |
| 180 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 | |
| 182 | FGFDMExec::~FGFDMExec() |
| 183 | { |
| 184 | try { |
| 185 | Unbind(); |
| 186 | DeAllocate(); |
| 187 | |
| 188 | delete instance; |
| 189 | |
| 190 | if (IdFDM == 0) { // Meaning this is no child FDM |
| 191 | if(Root != 0) { |
| 192 | if(StandAlone) |
| 193 | delete Root; |
| 194 | Root = 0; |
| 195 | } |
| 196 | if(FDMctr != 0) { |
| 197 | delete FDMctr; |
| 198 | FDMctr = 0; |
| 199 | } |
| 200 | } |
| 201 | } catch (const string& msg ) { |
| 202 | cout << "Caught error: " << msg << endl; |
| 203 | } |
| 204 | |
| 205 | for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec; |
| 206 | |
| 207 | if (FDMctr != 0) (*FDMctr)--; |
| 208 | |
| 209 | Debug(1); |
| 210 | } |
| 211 | |
| 212 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 213 | |
| 214 | double FGFDMExec::Setsim_time(double cur_time) { |
| 215 | sim_time = cur_time; |
| 216 | Inertial->SetTime(sim_time); |
| 217 | return sim_time; |
| 218 | } |
| 219 | |
| 220 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 221 | |
| 222 | double FGFDMExec::IncrTime(void) { |
| 223 | if (!holding && !IntegrationSuspended()) { |
| 224 | sim_time += dT; |
| 225 | Inertial->SetTime(sim_time); |
| 226 | Frame++; |
| 227 | } |
| 228 | return sim_time; |
| 229 | } |
| 230 | |
| 231 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 232 | |
| 233 | bool FGFDMExec::Allocate(void) |
| 234 | { |
| 235 | bool result=true; |
| 236 | |
| 237 | Models.resize(eNumStandardModels); |
| 238 | |
| 239 | // First build the inertial model since some other models are relying on |
| 240 | // the inertial model and the ground callback to build themselves. |
| 241 | // Note that this does not affect the order in which the models will be |
| 242 | // executed later. |
| 243 | Models[eInertial] = new FGInertial(this); |
| 244 | |
| 245 | // See the eModels enum specification in the header file. The order of the |
| 246 | // enums specifies the order of execution. The Models[] vector is the primary |
| 247 | // storage array for the list of models. |
| 248 | Models[ePropagate] = new FGPropagate(this); |
| 249 | Models[eInput] = new FGInput(this); |
| 250 | Models[eAtmosphere] = new FGStandardAtmosphere(this); |
| 251 | Models[eWinds] = new FGWinds(this); |
| 252 | Models[eSystems] = new FGFCS(this); |
| 253 | Models[eMassBalance] = new FGMassBalance(this); |
| 254 | Models[eAuxiliary] = new FGAuxiliary(this); |
| 255 | Models[ePropulsion] = new FGPropulsion(this); |
| 256 | Models[eAerodynamics] = new FGAerodynamics (this); |
| 257 | Models[eGroundReactions] = new FGGroundReactions(this); |
| 258 | Models[eExternalReactions] = new FGExternalReactions(this); |
| 259 | Models[eBuoyantForces] = new FGBuoyantForces(this); |
| 260 | Models[eAircraft] = new FGAircraft(this); |
| 261 | Models[eAccelerations] = new FGAccelerations(this); |
| 262 | Models[eOutput] = new FGOutput(this); |
| 263 | |
| 264 | // Assign the Model shortcuts for internal executive use only. |
| 265 | Propagate = (FGPropagate*)Models[ePropagate]; |
| 266 | Inertial = (FGInertial*)Models[eInertial]; |
| 267 | Atmosphere = (FGAtmosphere*)Models[eAtmosphere]; |
| 268 | Winds = (FGWinds*)Models[eWinds]; |
| 269 | FCS = (FGFCS*)Models[eSystems]; |
| 270 | MassBalance = (FGMassBalance*)Models[eMassBalance]; |
| 271 | Auxiliary = (FGAuxiliary*)Models[eAuxiliary]; |
| 272 | Propulsion = (FGPropulsion*)Models[ePropulsion]; |
| 273 | Aerodynamics = (FGAerodynamics*)Models[eAerodynamics]; |
| 274 | GroundReactions = (FGGroundReactions*)Models[eGroundReactions]; |
| 275 | ExternalReactions = (FGExternalReactions*)Models[eExternalReactions]; |
| 276 | BuoyantForces = (FGBuoyantForces*)Models[eBuoyantForces]; |
| 277 | Aircraft = (FGAircraft*)Models[eAircraft]; |
| 278 | Accelerations = (FGAccelerations*)Models[eAccelerations]; |
| 279 | Output = (FGOutput*)Models[eOutput]; |
| 280 | |
| 281 | // Initialize planet (environment) constants |
| 282 | LoadPlanetConstants(); |
| 283 | |
| 284 | // Initialize models |
| 285 | InitializeModels(); |
| 286 | |
| 287 | IC = new FGInitialCondition(this); |
| 288 | IC->bind(instance); |
| 289 | |
| 290 | modelLoaded = false; |
| 291 | |
| 292 | return result; |
| 293 | } |
| 294 | |
| 295 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 296 | |
| 297 | void FGFDMExec::InitializeModels(void) |
| 298 | { |
| 299 | for (unsigned int i = 0; i < Models.size(); i++) { |
| 300 | // The Input/Output models must not be initialized prior to IC loading |
| 301 | if (i == eInput || i == eOutput) continue; |
| 302 | |
| 303 | LoadInputs(i); |
| 304 | Models[i]->InitModel(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 309 | |
| 310 | bool FGFDMExec::DeAllocate(void) |
| 311 | { |
| 312 | |
| 313 | for (unsigned int i=0; i<eNumStandardModels; i++) delete Models[i]; |
| 314 | Models.clear(); |
| 315 | |
| 316 | delete Script; |
| 317 | delete IC; |
| 318 | delete Trim; |
| 319 | |
| 320 | modelLoaded = false; |
| 321 | return modelLoaded; |
| 322 | } |
| 323 | |
| 324 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 325 | |
| 326 | bool FGFDMExec::Run(void) |
| 327 | { |
| 328 | bool success=true; |
| 329 | |
| 330 | Debug(2); |
| 331 | |
| 332 | for (auto &ChildFDM: ChildFDMList) { |
| 333 | ChildFDM->AssignState( (FGPropagate*)Models[ePropagate] ); // Transfer state to the child FDM |
| 334 | ChildFDM->Run(); |
| 335 | } |
| 336 | |
| 337 | IncrTime(); |
| 338 | |
| 339 | // returns true if success, false if complete |
| 340 | if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript(); |
| 341 | |
| 342 | for (unsigned int i = 0; i < Models.size(); i++) { |
| 343 | LoadInputs(i); |
| 344 | Models[i]->Run(holding); |
| 345 | } |
| 346 | |
| 347 | if (ResetMode) { |
| 348 | unsigned int mode = ResetMode; |
| 349 | |
| 350 | ResetMode = 0; |
| 351 | ResetToInitialConditions(mode); |
| 352 | } |
| 353 | |
| 354 | if (Terminate) success = false; |
| 355 | |
| 356 | return success; |
| 357 | } |
| 358 | |
| 359 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 360 | |
| 361 | void FGFDMExec::LoadInputs(unsigned int idx) |
| 362 | { |
| 363 | switch(idx) { |
| 364 | case ePropagate: |
| 365 | Propagate->in.vPQRidot = Accelerations->GetPQRidot(); |
| 366 | Propagate->in.vUVWidot = Accelerations->GetUVWidot(); |
| 367 | Propagate->in.DeltaT = dT; |
| 368 | break; |
| 369 | case eInput: |
| 370 | break; |
| 371 | case eInertial: |
| 372 | Inertial->in.Position = Propagate->GetLocation(); |
| 373 | break; |
| 374 | case eAtmosphere: |
| 375 | Atmosphere->in.altitudeASL = Propagate->GetAltitudeASL(); |
| 376 | Atmosphere->in.GeodLatitudeDeg = Propagate->GetGeodLatitudeDeg(); |
| 377 | Atmosphere->in.LongitudeDeg = Propagate->GetLongitudeDeg(); |
| 378 | break; |
| 379 | case eWinds: |
| 380 | Winds->in.AltitudeASL = Propagate->GetAltitudeASL(); |
| 381 | Winds->in.DistanceAGL = Propagate->GetDistanceAGL(); |
| 382 | Winds->in.Tl2b = Propagate->GetTl2b(); |
| 383 | Winds->in.Tw2b = Auxiliary->GetTw2b(); |
| 384 | Winds->in.V = Auxiliary->GetVt(); |
| 385 | Winds->in.totalDeltaT = dT * Winds->GetRate(); |
| 386 | break; |
| 387 | case eAuxiliary: |
| 388 | Auxiliary->in.Pressure = Atmosphere->GetPressure(); |
| 389 | Auxiliary->in.Density = Atmosphere->GetDensity(); |
| 390 | Auxiliary->in.Temperature = Atmosphere->GetTemperature(); |
| 391 | Auxiliary->in.SoundSpeed = Atmosphere->GetSoundSpeed(); |
| 392 | Auxiliary->in.KinematicViscosity = Atmosphere->GetKinematicViscosity(); |
| 393 | Auxiliary->in.DistanceAGL = Propagate->GetDistanceAGL(); |
| 394 | Auxiliary->in.Mass = MassBalance->GetMass(); |
| 395 | Auxiliary->in.Tl2b = Propagate->GetTl2b(); |
| 396 | Auxiliary->in.Tb2l = Propagate->GetTb2l(); |
| 397 | Auxiliary->in.vPQR = Propagate->GetPQR(); |
| 398 | Auxiliary->in.vPQRi = Propagate->GetPQRi(); |
| 399 | Auxiliary->in.vPQRidot = Accelerations->GetPQRidot(); |
| 400 | Auxiliary->in.vUVW = Propagate->GetUVW(); |
| 401 | Auxiliary->in.vUVWdot = Accelerations->GetUVWdot(); |
| 402 | Auxiliary->in.vVel = Propagate->GetVel(); |
| 403 | Auxiliary->in.vBodyAccel = Accelerations->GetBodyAccel(); |
| 404 | Auxiliary->in.ToEyePt = MassBalance->StructuralToBody(Aircraft->GetXYZep()); |
| 405 | Auxiliary->in.VRPBody = MassBalance->StructuralToBody(Aircraft->GetXYZvrp()); |
| 406 | Auxiliary->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp()); |
| 407 | Auxiliary->in.vFw = Aerodynamics->GetvFw(); |
| 408 | Auxiliary->in.vLocation = Propagate->GetLocation(); |
| 409 | Auxiliary->in.CosTht = Propagate->GetCosEuler(eTht); |
| 410 | Auxiliary->in.SinTht = Propagate->GetSinEuler(eTht); |
| 411 | Auxiliary->in.CosPhi = Propagate->GetCosEuler(ePhi); |
| 412 | Auxiliary->in.SinPhi = Propagate->GetSinEuler(ePhi); |
| 413 | Auxiliary->in.TotalWindNED = Winds->GetTotalWindNED(); |
| 414 | Auxiliary->in.TurbPQR = Winds->GetTurbPQR(); |
| 415 | break; |
| 416 | case eSystems: |
| 417 | // Dynamic inputs come into the components that FCS manages through properties |
| 418 | break; |
| 419 | case ePropulsion: |
| 420 | Propulsion->in.Pressure = Atmosphere->GetPressure(); |
| 421 | Propulsion->in.PressureRatio = Atmosphere->GetPressureRatio(); |
| 422 | Propulsion->in.Temperature = Atmosphere->GetTemperature(); |
| 423 | Propulsion->in.DensityRatio = Atmosphere->GetDensityRatio(); |
| 424 | Propulsion->in.Density = Atmosphere->GetDensity(); |
| 425 | Propulsion->in.Soundspeed = Atmosphere->GetSoundSpeed(); |
| 426 | Propulsion->in.TotalPressure = Auxiliary->GetTotalPressure(); |
| 427 | Propulsion->in.Vc = Auxiliary->GetVcalibratedKTS(); |
| 428 | Propulsion->in.Vt = Auxiliary->GetVt(); |
| 429 | Propulsion->in.qbar = Auxiliary->Getqbar(); |
| 430 | Propulsion->in.TAT_c = Auxiliary->GetTAT_C(); |
| 431 | Propulsion->in.AeroUVW = Auxiliary->GetAeroUVW(); |
| 432 | Propulsion->in.AeroPQR = Auxiliary->GetAeroPQR(); |
| 433 | Propulsion->in.alpha = Auxiliary->Getalpha(); |
| 434 | Propulsion->in.beta = Auxiliary->Getbeta(); |
| 435 | Propulsion->in.TotalDeltaT = dT * Propulsion->GetRate(); |
| 436 | Propulsion->in.ThrottlePos = FCS->GetThrottlePos(); |
| 437 | Propulsion->in.MixturePos = FCS->GetMixturePos(); |
| 438 | Propulsion->in.ThrottleCmd = FCS->GetThrottleCmd(); |
| 439 | Propulsion->in.MixtureCmd = FCS->GetMixtureCmd(); |
| 440 | Propulsion->in.PropAdvance = FCS->GetPropAdvance(); |
| 441 | Propulsion->in.PropFeather = FCS->GetPropFeather(); |
| 442 | Propulsion->in.H_agl = Propagate->GetDistanceAGL(); |
| 443 | Propulsion->in.PQRi = Propagate->GetPQRi(); |
| 444 | |
| 445 | break; |
| 446 | case eAerodynamics: |
| 447 | Aerodynamics->in.Alpha = Auxiliary->Getalpha(); |
| 448 | Aerodynamics->in.Beta = Auxiliary->Getbeta(); |
| 449 | Aerodynamics->in.Qbar = Auxiliary->Getqbar(); |
| 450 | Aerodynamics->in.Vt = Auxiliary->GetVt(); |
| 451 | Aerodynamics->in.Tb2w = Auxiliary->GetTb2w(); |
| 452 | Aerodynamics->in.Tw2b = Auxiliary->GetTw2b(); |
| 453 | Aerodynamics->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp()); |
| 454 | break; |
| 455 | case eGroundReactions: |
| 456 | // There are no external inputs to this model. |
| 457 | GroundReactions->in.Vground = Auxiliary->GetVground(); |
| 458 | GroundReactions->in.VcalibratedKts = Auxiliary->GetVcalibratedKTS(); |
| 459 | GroundReactions->in.Temperature = Atmosphere->GetTemperature(); |
| 460 | GroundReactions->in.TakeoffThrottle = (FCS->GetThrottlePos().size() > 0) ? (FCS->GetThrottlePos(0) > 0.90) : false; |
| 461 | GroundReactions->in.BrakePos = FCS->GetBrakePos(); |
| 462 | GroundReactions->in.FCSGearPos = FCS->GetGearPos(); |
| 463 | GroundReactions->in.EmptyWeight = MassBalance->GetEmptyWeight(); |
| 464 | GroundReactions->in.Tb2l = Propagate->GetTb2l(); |
| 465 | GroundReactions->in.Tec2l = Propagate->GetTec2l(); |
| 466 | GroundReactions->in.Tec2b = Propagate->GetTec2b(); |
| 467 | GroundReactions->in.PQR = Propagate->GetPQR(); |
| 468 | GroundReactions->in.UVW = Propagate->GetUVW(); |
| 469 | GroundReactions->in.DistanceAGL = Propagate->GetDistanceAGL(); |
| 470 | GroundReactions->in.DistanceASL = Propagate->GetAltitudeASL(); |
| 471 | GroundReactions->in.TotalDeltaT = dT * GroundReactions->GetRate(); |
| 472 | GroundReactions->in.WOW = GroundReactions->GetWOW(); |
| 473 | GroundReactions->in.Location = Propagate->GetLocation(); |
| 474 | GroundReactions->in.vXYZcg = MassBalance->GetXYZcg(); |
| 475 | break; |
| 476 | case eExternalReactions: |
| 477 | // There are no external inputs to this model. |
| 478 | break; |
| 479 | case eBuoyantForces: |
| 480 | BuoyantForces->in.Density = Atmosphere->GetDensity(); |
| 481 | BuoyantForces->in.Pressure = Atmosphere->GetPressure(); |
| 482 | BuoyantForces->in.Temperature = Atmosphere->GetTemperature(); |
| 483 | BuoyantForces->in.gravity = Inertial->GetGravity().Magnitude(); |
| 484 | break; |
| 485 | case eMassBalance: |
| 486 | MassBalance->in.GasInertia = BuoyantForces->GetGasMassInertia(); |
| 487 | MassBalance->in.GasMass = BuoyantForces->GetGasMass(); |
| 488 | MassBalance->in.GasMoment = BuoyantForces->GetGasMassMoment(); |
| 489 | MassBalance->in.TanksWeight = Propulsion->GetTanksWeight(); |
| 490 | MassBalance->in.TanksMoment = Propulsion->GetTanksMoment(); |
| 491 | MassBalance->in.TankInertia = Propulsion->CalculateTankInertias(); |
| 492 | MassBalance->in.WOW = GroundReactions->GetWOW(); |
| 493 | break; |
| 494 | case eAircraft: |
| 495 | Aircraft->in.AeroForce = Aerodynamics->GetForces(); |
| 496 | Aircraft->in.PropForce = Propulsion->GetForces(); |
| 497 | Aircraft->in.GroundForce = GroundReactions->GetForces(); |
| 498 | Aircraft->in.ExternalForce = ExternalReactions->GetForces(); |
| 499 | Aircraft->in.BuoyantForce = BuoyantForces->GetForces(); |
| 500 | Aircraft->in.AeroMoment = Aerodynamics->GetMoments(); |
| 501 | Aircraft->in.PropMoment = Propulsion->GetMoments(); |
| 502 | Aircraft->in.GroundMoment = GroundReactions->GetMoments(); |
| 503 | Aircraft->in.ExternalMoment = ExternalReactions->GetMoments(); |
| 504 | Aircraft->in.BuoyantMoment = BuoyantForces->GetMoments(); |
| 505 | break; |
| 506 | case eAccelerations: |
| 507 | Accelerations->in.J = MassBalance->GetJ(); |
| 508 | Accelerations->in.Jinv = MassBalance->GetJinv(); |
| 509 | Accelerations->in.Ti2b = Propagate->GetTi2b(); |
| 510 | Accelerations->in.Tb2i = Propagate->GetTb2i(); |
| 511 | Accelerations->in.Tec2b = Propagate->GetTec2b(); |
| 512 | Accelerations->in.Tec2i = Propagate->GetTec2i(); |
| 513 | Accelerations->in.Moment = Aircraft->GetMoments(); |
| 514 | Accelerations->in.GroundMoment = GroundReactions->GetMoments(); |
| 515 | Accelerations->in.Force = Aircraft->GetForces(); |
| 516 | Accelerations->in.GroundForce = GroundReactions->GetForces(); |
| 517 | Accelerations->in.vGravAccel = Inertial->GetGravity(); |
| 518 | Accelerations->in.vPQRi = Propagate->GetPQRi(); |
| 519 | Accelerations->in.vPQR = Propagate->GetPQR(); |
| 520 | Accelerations->in.vUVW = Propagate->GetUVW(); |
| 521 | Accelerations->in.vInertialPosition = Propagate->GetInertialPosition(); |
| 522 | Accelerations->in.DeltaT = dT; |
| 523 | Accelerations->in.Mass = MassBalance->GetMass(); |
| 524 | Accelerations->in.MultipliersList = GroundReactions->GetMultipliersList(); |
| 525 | Accelerations->in.TerrainVelocity = Propagate->GetTerrainVelocity(); |
| 526 | Accelerations->in.TerrainAngularVel = Propagate->GetTerrainAngularVelocity(); |
| 527 | break; |
| 528 | default: |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 534 | |
| 535 | void FGFDMExec::LoadPlanetConstants(void) |
| 536 | { |
| 537 | Propagate->in.vOmegaPlanet = Inertial->GetOmegaPlanet(); |
| 538 | Accelerations->in.vOmegaPlanet = Inertial->GetOmegaPlanet(); |
| 539 | Propagate->in.SemiMajor = Inertial->GetSemimajor(); |
| 540 | Propagate->in.SemiMinor = Inertial->GetSemiminor(); |
| 541 | Auxiliary->in.StandardGravity = Inertial->GetStandardGravity(); |
| 542 | Auxiliary->in.StdDaySLsoundspeed = Atmosphere->StdDaySLsoundspeed; |
| 543 | } |
| 544 | |
| 545 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 546 | |
| 547 | void FGFDMExec::LoadModelConstants(void) |
| 548 | { |
| 549 | Winds->in.wingspan = Aircraft->GetWingSpan(); |
| 550 | Aerodynamics->in.Wingarea = Aircraft->GetWingArea(); |
| 551 | Aerodynamics->in.Wingchord = Aircraft->Getcbar(); |
| 552 | Aerodynamics->in.Wingincidence = Aircraft->GetWingIncidence(); |
| 553 | Aerodynamics->in.Wingspan = Aircraft->GetWingSpan(); |
| 554 | Auxiliary->in.Wingspan = Aircraft->GetWingSpan(); |
| 555 | Auxiliary->in.Wingchord = Aircraft->Getcbar(); |
| 556 | GroundReactions->in.vXYZcg = MassBalance->GetXYZcg(); |
| 557 | } |
| 558 | |
| 559 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 560 | // This call will cause the sim time to reset to 0.0 |
| 561 | |
| 562 | bool FGFDMExec::RunIC(void) |
| 563 | { |
| 564 | FGPropulsion* propulsion = (FGPropulsion*)Models[ePropulsion]; |
| 565 | |
| 566 | SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0. |
| 567 | Initialize(IC); |
| 568 | |
| 569 | Models[eInput]->InitModel(); |
| 570 | Models[eOutput]->InitModel(); |
| 571 | |
| 572 | Run(); |
| 573 | Propagate->InitializeDerivatives(); |
| 574 | ResumeIntegration(); // Restores the integration rate to what it was. |
| 575 | |
| 576 | if (debug_lvl > 0) { |
| 577 | MassBalance->GetMassPropertiesReport(0); |
| 578 | |
| 579 | cout << endl << fgblue << highint |
| 580 | << "End of vehicle configuration loading." << endl |
| 581 | << "-------------------------------------------------------------------------------" |
| 582 | << reset << std::setprecision(6) << endl; |
| 583 | } |
| 584 | |
| 585 | for (unsigned int n=0; n < propulsion->GetNumEngines(); ++n) { |
| 586 | if (IC->IsEngineRunning(n)) { |
| 587 | try { |
| 588 | propulsion->InitRunning(n); |
| 589 | } catch (const string& str) { |
| 590 | cerr << str << endl; |
| 591 | return false; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 600 | |
| 601 | void FGFDMExec::Initialize(FGInitialCondition* FGIC) |
| 602 | { |
| 603 | Propagate->SetInitialState(FGIC); |
| 604 | Winds->SetWindNED(FGIC->GetWindNEDFpsIC()); |
| 605 | Run(); |
| 606 | } |
| 607 | |
| 608 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 609 | |
| 610 | void FGFDMExec::ResetToInitialConditions(int mode) |
| 611 | { |
| 612 | if (Constructing) return; |
| 613 | |
| 614 | // mode flags |
| 615 | |
| 616 | if (mode & START_NEW_OUTPUT) Output->SetStartNewOutput(); |
| 617 | |
| 618 | InitializeModels(); |
| 619 | |
| 620 | if (Script) |
| 621 | Script->ResetEvents(); |
| 622 | else |
| 623 | Setsim_time(0.0); |
| 624 | |
| 625 | if (!(mode & DONT_EXECUTE_RUN_IC)) |
| 626 | RunIC(); |
| 627 | } |
| 628 | |
| 629 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 630 | |
| 631 | void FGFDMExec::SetHoldDown(bool hd) |
| 632 | { |
| 633 | HoldDown = hd; |
| 634 | Accelerations->SetHoldDown(hd); |
| 635 | if (hd) { |
| 636 | Propagate->in.vPQRidot = Accelerations->GetPQRidot(); |
| 637 | Propagate->in.vUVWidot = Accelerations->GetUVWidot(); |
| 638 | } |
| 639 | Propagate->SetHoldDown(hd); |
| 640 | } |
| 641 | |
| 642 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 643 | |
| 644 | vector <string> FGFDMExec::EnumerateFDMs(void) |
| 645 | { |
| 646 | vector <string> FDMList; |
| 647 | FGAircraft* Aircraft = (FGAircraft*)Models[eAircraft]; |
| 648 | |
| 649 | FDMList.push_back(Aircraft->GetAircraftName()); |
| 650 | |
| 651 | for (auto &ChildFDM: ChildFDMList) |
| 652 | FDMList.push_back(ChildFDM->exec->GetAircraft()->GetAircraftName()); |
| 653 | |
| 654 | return FDMList; |
| 655 | } |
| 656 | |
| 657 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 658 | |
| 659 | bool FGFDMExec::LoadScript(const SGPath& script, double deltaT, |
| 660 | const SGPath& initfile) |
| 661 | { |
| 662 | bool result; |
| 663 | |
| 664 | Script = new FGScript(this); |
| 665 | result = Script->LoadScript(GetFullPath(script), deltaT, initfile); |
| 666 | |
| 667 | return result; |
| 668 | } |
| 669 | |
| 670 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 671 | |
| 672 | bool FGFDMExec::LoadPlanet(const SGPath& PlanetPath, bool useAircraftPath) |
| 673 | { |
| 674 | SGPath PlanetFileName; |
| 675 | |
| 676 | if(useAircraftPath && PlanetPath.isRelative()) { |
| 677 | PlanetFileName = AircraftPath/PlanetPath.utf8Str(); |
| 678 | } else { |
| 679 | PlanetFileName = PlanetPath; |
| 680 | } |
| 681 | |
| 682 | FGXMLFileRead XMLFileRead; |
| 683 | Element* document = XMLFileRead.LoadXMLDocument(PlanetFileName); |
| 684 | |
| 685 | // Make sure that the document is valid |
| 686 | if (!document) { |
| 687 | stringstream s; |
| 688 | s << "File: " << PlanetFileName << " could not be read."; |
| 689 | cerr << s.str() << endl; |
| 690 | throw BaseException(s.str()); |
| 691 | } |
| 692 | |
| 693 | if (document->GetName() != "planet") { |
| 694 | stringstream s; |
| 695 | s << "File: " << PlanetFileName << " is not a planet file."; |
| 696 | cerr << s.str() << endl; |
| 697 | throw BaseException(s.str()); |
| 698 | } |
| 699 | |
| 700 | bool result = LoadPlanet(document); |
| 701 | |
| 702 | if (!result) |
| 703 | cerr << endl << "Planet element has problems in file " << PlanetFileName << endl; |
| 704 | |
| 705 | return result; |
| 706 | } |
| 707 | |
| 708 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 709 | |
| 710 | bool FGFDMExec::LoadPlanet(Element* element) |
| 711 | { |
| 712 | bool result = Models[eInertial]->Load(element); |
| 713 | |
| 714 | if (result) { |
| 715 | // Reload the planet constants and re-initialize the models. |
| 716 | LoadPlanetConstants(); |
| 717 | IC->InitializeIC(); |
| 718 | InitializeModels(); |
| 719 | |
| 720 | // Process the atmosphere element. This element is OPTIONAL. |
| 721 | Element* atm_element = element->FindElement("atmosphere"); |
| 722 | if (atm_element && atm_element->HasAttribute("model")) { |
| 723 | string model = atm_element->GetAttributeValue("model"); |
| 724 | if (model == "MSIS") { |
| 725 | // Replace the existing atmosphere model |
| 726 | instance->Unbind(Models[eAtmosphere]); |
| 727 | Models[eAtmosphere] = new FGMSIS(this); |
| 728 | Atmosphere = static_cast<FGAtmosphere*>(Models[eAtmosphere]); |
| 729 | |
| 730 | // Model initialization sequence |
| 731 | LoadInputs(eAtmosphere); |
| 732 | Atmosphere->InitModel(); |
| 733 | result = Atmosphere->Load(atm_element); |
| 734 | if (!result) { |
| 735 | cerr << endl << "Incorrect definition of <atmosphere>." << endl; |
| 736 | return result; |
| 737 | } |
| 738 | InitializeModels(); |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return result; |
| 744 | } |
| 745 | |
| 746 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 747 | |
| 748 | bool FGFDMExec::LoadModel(const SGPath& AircraftPath, const SGPath& EnginePath, |
| 749 | const SGPath& SystemsPath, const string& model, |
| 750 | bool addModelToPath) |
| 751 | { |
| 752 | FGFDMExec::AircraftPath = GetFullPath(AircraftPath); |
| 753 | FGFDMExec::EnginePath = GetFullPath(EnginePath); |
| 754 | FGFDMExec::SystemsPath = GetFullPath(SystemsPath); |
| 755 | |
| 756 | return LoadModel(model, addModelToPath); |
| 757 | } |
| 758 | |
| 759 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 760 | |
| 761 | bool FGFDMExec::LoadModel(const string& model, bool addModelToPath) |
| 762 | { |
| 763 | SGPath aircraftCfgFileName; |
| 764 | bool result = false; // initialize result to false, indicating input file not yet read |
| 765 | |
| 766 | modelName = model; // Set the class modelName attribute |
| 767 | |
| 768 | if( AircraftPath.isNull() || EnginePath.isNull() || SystemsPath.isNull()) { |
| 769 | cerr << "Error: attempted to load aircraft with undefined " |
| 770 | << "aircraft, engine, and system paths" << endl; |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | FullAircraftPath = AircraftPath; |
| 775 | if (addModelToPath) FullAircraftPath.append(model); |
| 776 | aircraftCfgFileName = FullAircraftPath/(model + ".xml"); |
| 777 | |
| 778 | if (modelLoaded) { |
| 779 | DeAllocate(); |
| 780 | Allocate(); |
| 781 | } |
| 782 | |
| 783 | int saved_debug_lvl = debug_lvl; |
| 784 | FGXMLFileRead XMLFileRead; |
| 785 | Element *document = XMLFileRead.LoadXMLDocument(aircraftCfgFileName); // "document" is a class member |
| 786 | |
| 787 | if (document) { |
| 788 | if (IsChild) debug_lvl = 0; |
| 789 | |
| 790 | ReadPrologue(document); |
| 791 | |
| 792 | if (IsChild) debug_lvl = saved_debug_lvl; |
| 793 | |
| 794 | // Process the fileheader element in the aircraft config file. This element is OPTIONAL. |
| 795 | Element* element = document->FindElement("fileheader"); |
| 796 | if (element) { |
| 797 | result = ReadFileHeader(element); |
| 798 | if (!result) { |
| 799 | cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl; |
| 800 | return result; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | if (IsChild) debug_lvl = 0; |
| 805 | |
| 806 | // Process the planet element. This element is OPTIONAL. |
| 807 | element = document->FindElement("planet"); |
| 808 | if (element) { |
| 809 | result = LoadPlanet(element); |
| 810 | if (!result) { |
| 811 | cerr << endl << "Planet element has problems in file " << aircraftCfgFileName << endl; |
| 812 | return result; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // Process the metrics element. This element is REQUIRED. |
| 817 | element = document->FindElement("metrics"); |
| 818 | if (element) { |
| 819 | result = Models[eAircraft]->Load(element); |
| 820 | if (!result) { |
| 821 | cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl; |
| 822 | return result; |
| 823 | } |
| 824 | } else { |
| 825 | cerr << endl << "No metrics element was found in the aircraft config file." << endl; |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | // Process the mass_balance element. This element is REQUIRED. |
| 830 | element = document->FindElement("mass_balance"); |
| 831 | if (element) { |
| 832 | result = Models[eMassBalance]->Load(element); |
| 833 | if (!result) { |
| 834 | cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl; |
| 835 | return result; |
| 836 | } |
| 837 | } else { |
| 838 | cerr << endl << "No mass_balance element was found in the aircraft config file." << endl; |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | // Process the ground_reactions element. This element is REQUIRED. |
| 843 | element = document->FindElement("ground_reactions"); |
| 844 | if (element) { |
| 845 | result = Models[eGroundReactions]->Load(element); |
| 846 | if (!result) { |
| 847 | cerr << endl << element->ReadFrom() |
| 848 | << "Aircraft ground_reactions element has problems in file " |
| 849 | << aircraftCfgFileName << endl; |
| 850 | return result; |
| 851 | } |
| 852 | } else { |
| 853 | cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl; |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | // Process the external_reactions element. This element is OPTIONAL. |
| 858 | element = document->FindElement("external_reactions"); |
| 859 | if (element) { |
| 860 | result = Models[eExternalReactions]->Load(element); |
| 861 | if (!result) { |
| 862 | cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl; |
| 863 | return result; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // Process the buoyant_forces element. This element is OPTIONAL. |
| 868 | element = document->FindElement("buoyant_forces"); |
| 869 | if (element) { |
| 870 | result = Models[eBuoyantForces]->Load(element); |
| 871 | if (!result) { |
| 872 | cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl; |
| 873 | return result; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | // Process the propulsion element. This element is OPTIONAL. |
| 878 | element = document->FindElement("propulsion"); |
| 879 | if (element) { |
| 880 | auto propulsion = static_cast<FGPropulsion*>(Models[ePropulsion]); |
| 881 | result = propulsion->Load(element); |
| 882 | if (!result) { |
| 883 | cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl; |
| 884 | return result; |
| 885 | } |
| 886 | for (unsigned int i=0; i < propulsion->GetNumEngines(); i++) |
| 887 | ((FGFCS*)Models[eSystems])->AddThrottle(); |
| 888 | } |
| 889 | |
| 890 | // Process the system element[s]. This element is OPTIONAL, and there may be more than one. |
| 891 | element = document->FindElement("system"); |
| 892 | while (element) { |
| 893 | result = Models[eSystems]->Load(element); |
| 894 | if (!result) { |
| 895 | cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl; |
| 896 | return result; |
| 897 | } |
| 898 | element = document->FindNextElement("system"); |
| 899 | } |
| 900 | |
| 901 | // Process the autopilot element. This element is OPTIONAL. |
| 902 | element = document->FindElement("autopilot"); |
| 903 | if (element) { |
| 904 | result = Models[eSystems]->Load(element); |
| 905 | if (!result) { |
| 906 | cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl; |
| 907 | return result; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // Process the flight_control element. This element is OPTIONAL. |
| 912 | element = document->FindElement("flight_control"); |
| 913 | if (element) { |
| 914 | result = Models[eSystems]->Load(element); |
| 915 | if (!result) { |
| 916 | cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl; |
| 917 | return result; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // Process the aerodynamics element. This element is OPTIONAL, but almost always expected. |
| 922 | element = document->FindElement("aerodynamics"); |
| 923 | if (element) { |
| 924 | result = Models[eAerodynamics]->Load(element); |
| 925 | if (!result) { |
| 926 | cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl; |
| 927 | return result; |
| 928 | } |
| 929 | } else { |
| 930 | cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl; |
| 931 | } |
| 932 | |
| 933 | // Process the input element. This element is OPTIONAL, and there may be more than one. |
| 934 | element = document->FindElement("input"); |
| 935 | while (element) { |
| 936 | if (!static_cast<FGInput*>(Models[eInput])->Load(element)) |
| 937 | return false; |
| 938 | |
| 939 | element = document->FindNextElement("input"); |
| 940 | } |
| 941 | |
| 942 | // Process the output element[s]. This element is OPTIONAL, and there may be |
| 943 | // more than one. |
| 944 | element = document->FindElement("output"); |
| 945 | while (element) { |
| 946 | if (!static_cast<FGOutput*>(Models[eOutput])->Load(element)) |
| 947 | return false; |
| 948 | |
| 949 | element = document->FindNextElement("output"); |
| 950 | } |
| 951 | |
| 952 | // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED. |
| 953 | element = document->FindElement("child"); |
| 954 | if (element) { |
| 955 | result = ReadChild(element); |
| 956 | if (!result) { |
| 957 | cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl; |
| 958 | return result; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | // Since all vehicle characteristics have been loaded, place the values in the Inputs |
| 963 | // structure for the FGModel-derived classes. |
| 964 | LoadModelConstants(); |
| 965 | |
| 966 | modelLoaded = true; |
| 967 | |
| 968 | if (IsChild) debug_lvl = saved_debug_lvl; |
| 969 | |
| 970 | } else { |
| 971 | cerr << fgred |
| 972 | << " JSBSim failed to open the configuration file: " << aircraftCfgFileName |
| 973 | << fgdef << endl; |
| 974 | } |
| 975 | |
| 976 | for (unsigned int i=0; i< Models.size(); i++) LoadInputs(i); |
| 977 | |
| 978 | if (result) { |
| 979 | struct PropertyCatalogStructure masterPCS; |
| 980 | masterPCS.base_string = ""; |
| 981 | masterPCS.node = Root->GetNode(); |
| 982 | BuildPropertyCatalog(&masterPCS); |
| 983 | } |
| 984 | |
| 985 | return result; |
| 986 | } |
| 987 | |
| 988 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 989 | |
| 990 | string FGFDMExec::GetPropulsionTankReport() |
| 991 | { |
| 992 | return ((FGPropulsion*)Models[ePropulsion])->GetPropulsionTankReport(); |
| 993 | } |
| 994 | |
| 995 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 996 | |
| 997 | void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs) |
| 998 | { |
| 999 | struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure; |
| 1000 | |
| 1001 | for (int i=0; i<pcs->node->nChildren(); i++) { |
| 1002 | string access=""; |
| 1003 | pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getNameString(); |
| 1004 | int node_idx = pcs->node->getChild(i)->getIndex(); |
| 1005 | if (node_idx != 0) { |
| 1006 | pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx); |
| 1007 | } |
| 1008 | if (pcs->node->getChild(i)->nChildren() == 0) { |
| 1009 | if (pcsNew->base_string.substr(0,12) == string("/fdm/jsbsim/")) { |
| 1010 | pcsNew->base_string = pcsNew->base_string.erase(0,12); |
| 1011 | } |
| 1012 | if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::READ)) access="R"; |
| 1013 | if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::WRITE)) access+="W"; |
| 1014 | PropertyCatalog.push_back(pcsNew->base_string+" ("+access+")"); |
| 1015 | } else { |
| 1016 | pcsNew->node = (FGPropertyNode*)pcs->node->getChild(i); |
| 1017 | BuildPropertyCatalog(pcsNew); |
| 1018 | } |
| 1019 | } |
| 1020 | delete pcsNew; |
| 1021 | } |
| 1022 | |
| 1023 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1024 | |
| 1025 | string FGFDMExec::QueryPropertyCatalog(const string& in, const string& end_of_line) |
| 1026 | { |
| 1027 | string results; |
| 1028 | for (auto &catalogElm: PropertyCatalog) { |
| 1029 | if (catalogElm.find(in) != string::npos) results += catalogElm + end_of_line; |
| 1030 | } |
| 1031 | if (results.empty()) return "No matches found"+end_of_line; |
| 1032 | return results; |
| 1033 | } |
| 1034 | |
| 1035 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1036 | |
| 1037 | void FGFDMExec::PrintPropertyCatalog(void) |
| 1038 | { |
| 1039 | cout << endl; |
| 1040 | cout << " " << fgblue << highint << underon << "Property Catalog for " |
| 1041 | << modelName << reset << endl << endl; |
| 1042 | for (auto &catalogElm: PropertyCatalog) |
| 1043 | cout << " " << catalogElm << endl; |
| 1044 | } |
| 1045 | |
| 1046 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1047 | |
| 1048 | void FGFDMExec::PrintSimulationConfiguration(void) const |
| 1049 | { |
| 1050 | cout << endl << "Simulation Configuration" << endl << "------------------------" << endl; |
| 1051 | cout << MassBalance->GetName() << endl; |
| 1052 | cout << GroundReactions->GetName() << endl; |
| 1053 | cout << Aerodynamics->GetName() << endl; |
| 1054 | cout << Propulsion->GetName() << endl; |
| 1055 | } |
| 1056 | |
| 1057 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1058 | |
| 1059 | bool FGFDMExec::ReadFileHeader(Element* el) |
| 1060 | { |
| 1061 | bool result = true; // true for success |
| 1062 | |
| 1063 | if (debug_lvl == 0) return result; |
| 1064 | |
| 1065 | if (IsChild) { |
| 1066 | cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl; |
| 1067 | } |
| 1068 | |
| 1069 | if (el->FindElement("description")) |
| 1070 | cout << " Description: " << el->FindElement("description")->GetDataLine() << endl; |
| 1071 | if (el->FindElement("author")) |
| 1072 | cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl; |
| 1073 | if (el->FindElement("filecreationdate")) |
| 1074 | cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl; |
| 1075 | if (el->FindElement("version")) |
| 1076 | cout << " Version: " << el->FindElement("version")->GetDataLine() << endl; |
| 1077 | |
| 1078 | return result; |
| 1079 | } |
| 1080 | |
| 1081 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1082 | |
| 1083 | bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element |
| 1084 | { |
| 1085 | bool result = true; // true for success |
| 1086 | |
| 1087 | if (!el) return false; |
| 1088 | |
| 1089 | string AircraftName = el->GetAttributeValue("name"); |
| 1090 | ((FGAircraft*)Models[eAircraft])->SetAircraftName(AircraftName); |
| 1091 | |
| 1092 | if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File" |
| 1093 | << underoff << ": " << highint << AircraftName << normint << endl; |
| 1094 | |
| 1095 | CFGVersion = el->GetAttributeValue("version"); |
| 1096 | Release = el->GetAttributeValue("release"); |
| 1097 | |
| 1098 | if (debug_lvl & 1) |
| 1099 | cout << " Version: " << highint << CFGVersion |
| 1100 | << normint << endl; |
| 1101 | if (CFGVersion != needed_cfg_version) { |
| 1102 | cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT." |
| 1103 | " RESULTS WILL BE UNPREDICTABLE !!" << endl; |
| 1104 | cerr << "Current version needed is: " << needed_cfg_version << endl; |
| 1105 | cerr << " You have version: " << CFGVersion << endl << fgdef << endl; |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | if (Release == "ALPHA" && (debug_lvl & 1)) { |
| 1110 | cout << endl << endl |
| 1111 | << highint << "This aircraft model is an " << fgred << Release |
| 1112 | << reset << highint << " release!!!" << endl << endl << reset |
| 1113 | << "This aircraft model may not even properly load, and probably" |
| 1114 | << " will not fly as expected." << endl << endl |
| 1115 | << fgred << highint << "Use this model for development purposes ONLY!!!" |
| 1116 | << normint << reset << endl << endl; |
| 1117 | } else if (Release == "BETA" && (debug_lvl & 1)) { |
| 1118 | cout << endl << endl |
| 1119 | << highint << "This aircraft model is a " << fgred << Release |
| 1120 | << reset << highint << " release!!!" << endl << endl << reset |
| 1121 | << "This aircraft model probably will not fly as expected." << endl << endl |
| 1122 | << fgblue << highint << "Use this model for development purposes ONLY!!!" |
| 1123 | << normint << reset << endl << endl; |
| 1124 | } else if (Release == "PRODUCTION" && (debug_lvl & 1)) { |
| 1125 | cout << endl << endl |
| 1126 | << highint << "This aircraft model is a " << fgblue << Release |
| 1127 | << reset << highint << " release." << endl << endl << reset; |
| 1128 | } else if (debug_lvl & 1) { |
| 1129 | cout << endl << endl |
| 1130 | << highint << "This aircraft model is an " << fgred << Release |
| 1131 | << reset << highint << " release!!!" << endl << endl << reset |
| 1132 | << "This aircraft model may not even properly load, and probably" |
| 1133 | << " will not fly as expected." << endl << endl |
| 1134 | << fgred << highint << "Use this model for development purposes ONLY!!!" |
| 1135 | << normint << reset << endl << endl; |
| 1136 | } |
| 1137 | |
| 1138 | return result; |
| 1139 | } |
| 1140 | |
| 1141 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1142 | |
| 1143 | bool FGFDMExec::ReadChild(Element* el) |
| 1144 | { |
| 1145 | // Add a new childData object to the child FDM list |
| 1146 | // Populate that childData element with a new FDMExec object |
| 1147 | // Set the IsChild flag for that FDMExec object |
| 1148 | // Get the aircraft name |
| 1149 | // set debug level to print out no additional data for child objects |
| 1150 | // Load the model given the aircraft name |
| 1151 | // reset debug level to prior setting |
| 1152 | |
| 1153 | struct childData* child = new childData; |
| 1154 | |
| 1155 | child->exec = new FGFDMExec(Root, FDMctr); |
| 1156 | child->exec->SetChild(true); |
| 1157 | |
| 1158 | string childAircraft = el->GetAttributeValue("name"); |
| 1159 | string sMated = el->GetAttributeValue("mated"); |
| 1160 | if (sMated == "false") child->mated = false; // child objects are mated by default. |
| 1161 | string sInternal = el->GetAttributeValue("internal"); |
| 1162 | if (sInternal == "true") child->internal = true; // child objects are external by default. |
| 1163 | |
| 1164 | child->exec->SetAircraftPath( AircraftPath ); |
| 1165 | child->exec->SetEnginePath( EnginePath ); |
| 1166 | child->exec->SetSystemsPath( SystemsPath ); |
| 1167 | child->exec->LoadModel(childAircraft); |
| 1168 | |
| 1169 | Element* location = el->FindElement("location"); |
| 1170 | if (location) { |
| 1171 | child->Loc = location->FindElementTripletConvertTo("IN"); |
| 1172 | } else { |
| 1173 | const string s(" No location was found for this child object!"); |
| 1174 | cerr << el->ReadFrom() << endl << highint << fgred |
| 1175 | << s << reset << endl; |
| 1176 | throw BaseException(s); |
| 1177 | } |
| 1178 | |
| 1179 | Element* orientation = el->FindElement("orient"); |
| 1180 | if (orientation) { |
| 1181 | child->Orient = orientation->FindElementTripletConvertTo("RAD"); |
| 1182 | } else if (debug_lvl > 0) { |
| 1183 | cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl; |
| 1184 | } |
| 1185 | |
| 1186 | ChildFDMList.push_back(child); |
| 1187 | |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
| 1191 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1192 | |
| 1193 | FGPropertyManager* FGFDMExec::GetPropertyManager(void) |
| 1194 | { |
| 1195 | return instance; |
| 1196 | } |
| 1197 | |
| 1198 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1199 | |
| 1200 | FGTrim* FGFDMExec::GetTrim(void) |
| 1201 | { |
| 1202 | delete Trim; |
| 1203 | Trim = new FGTrim(this,tNone); |
| 1204 | return Trim; |
| 1205 | } |
| 1206 | |
| 1207 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1208 | |
| 1209 | void FGFDMExec::CheckIncrementalHold(void) |
| 1210 | { |
| 1211 | // Only check if increment then hold is on |
| 1212 | if( IncrementThenHolding ) { |
| 1213 | |
| 1214 | if (TimeStepsUntilHold == 0) { |
| 1215 | |
| 1216 | // Should hold simulation if TimeStepsUntilHold has reached zero |
| 1217 | holding = true; |
| 1218 | |
| 1219 | // Still need to decrement TimeStepsUntilHold as value of -1 |
| 1220 | // indicates that incremental then hold is turned off |
| 1221 | IncrementThenHolding = false; |
| 1222 | TimeStepsUntilHold--; |
| 1223 | |
| 1224 | } else if ( TimeStepsUntilHold > 0 ) { |
| 1225 | // Keep decrementing until 0 is reached |
| 1226 | TimeStepsUntilHold--; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1232 | |
| 1233 | void FGFDMExec::DoTrim(int mode) |
| 1234 | { |
| 1235 | if (Constructing) return; |
| 1236 | |
| 1237 | if (mode < 0 || mode > JSBSim::tNone) |
| 1238 | throw("Illegal trimming mode!"); |
| 1239 | |
| 1240 | FGTrim trim(this, (JSBSim::TrimMode)mode); |
| 1241 | bool success = trim.DoTrim(); |
| 1242 | |
| 1243 | if (debug_lvl > 0) |
| 1244 | trim.Report(); |
| 1245 | |
| 1246 | if (!success) |
| 1247 | throw TrimFailureException("Trim Failed"); |
| 1248 | |
| 1249 | trim_completed = 1; |
| 1250 | } |
| 1251 | |
| 1252 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1253 | |
| 1254 | void FGFDMExec::SRand(int sr) |
| 1255 | { |
| 1256 | RandomSeed = sr; |
| 1257 | RandomGenerator->seed(RandomSeed); |
| 1258 | } |
| 1259 | |
| 1260 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1261 | // The bitmasked value choices are as follows: |
| 1262 | // unset: In this case (the default) JSBSim would only print |
| 1263 | // out the normally expected messages, essentially echoing |
| 1264 | // the config files as they are read. If the environment |
| 1265 | // variable is not set, debug_lvl is set to 1 internally |
| 1266 | // 0: This requests JSBSim not to output any messages |
| 1267 | // whatsoever. |
| 1268 | // 1: This value explicity requests the normal JSBSim |
| 1269 | // startup messages |
| 1270 | // 2: This value asks for a message to be printed out when |
| 1271 | // a class is instantiated |
| 1272 | // 4: When this value is set, a message is displayed when a |
| 1273 | // FGModel object executes its Run() method |
| 1274 | // 8: When this value is set, various runtime state variables |
| 1275 | // are printed out periodically |
| 1276 | // 16: When set various parameters are sanity checked and |
| 1277 | // a message is printed out when they go out of bounds |
| 1278 | |
| 1279 | void FGFDMExec::Debug(int from) |
| 1280 | { |
| 1281 | if (debug_lvl <= 0) return; |
| 1282 | |
| 1283 | if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output |
| 1284 | if (from == 0) { // Constructor |
| 1285 | cout << "\n\n " |
| 1286 | << "JSBSim Flight Dynamics Model v" << JSBSim_version << endl; |
| 1287 | cout << " [JSBSim-ML v" << needed_cfg_version << "]\n\n"; |
| 1288 | cout << "JSBSim startup beginning ...\n\n"; |
| 1289 | if (disperse == 1) cout << "Dispersions are ON." << endl << endl; |
| 1290 | } else if (from == 3) { |
| 1291 | cout << "\n\nJSBSim startup complete\n\n"; |
| 1292 | } |
| 1293 | } |
| 1294 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 1295 | if (from == 0) cout << "Instantiated: FGFDMExec" << endl; |
| 1296 | if (from == 1) cout << "Destroyed: FGFDMExec" << endl; |
| 1297 | } |
| 1298 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 1299 | if (from == 2) { |
| 1300 | cout << "================== Frame: " << Frame << " Time: " |
| 1301 | << sim_time << " dt: " << dT << endl; |
| 1302 | } |
| 1303 | } |
| 1304 | if (debug_lvl & 8 ) { // Runtime state variables |
| 1305 | } |
| 1306 | if (debug_lvl & 16) { // Sanity checking |
| 1307 | } |
| 1308 | if (debug_lvl & 64) { |
| 1309 | if (from == 0) { // Constructor |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |