| 1 | /* |
| 2 | * SPDX-FileName: JSBsim.cxx |
| 3 | * SPDX-FileComment: interface to the JSBsim flight model |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 1999 Curtis L. Olson - [email protected] |
| 5 | * SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | # include <config.h> |
| 10 | #endif |
| 11 | |
| 12 | #include <simgear/compiler.h> |
| 13 | #include <simgear/sg_inlines.h> |
| 14 | |
| 15 | #include <cstdlib> // size_t |
| 16 | #include <string> |
| 17 | |
| 18 | #include <simgear/constants.h> |
| 19 | #include <simgear/debug/logstream.hxx> |
| 20 | #include <simgear/math/sg_geodesy.hxx> |
| 21 | #include <simgear/misc/sg_path.hxx> |
| 22 | #include <simgear/structure/commands.hxx> |
| 23 | #include <simgear/bvh/BVHMaterial.hxx> |
| 24 | |
| 25 | #include <FDM/flight.hxx> |
| 26 | #include <FDM/groundreactions.hxx> |
| 27 | |
| 28 | #include <Aircraft/controls.hxx> |
| 29 | #include <Main/globals.hxx> |
| 30 | #include <Main/fg_props.hxx> |
| 31 | |
| 32 | #include "JSBSim.hxx" |
| 33 | #include <FDM/JSBSim/FGFDMExec.h> |
| 34 | #include <FDM/JSBSim/FGJSBBase.h> |
| 35 | #include <FDM/JSBSim/initialization/FGInitialCondition.h> |
| 36 | #include <FDM/JSBSim/initialization/FGTrim.h> |
| 37 | #include <FDM/JSBSim/models/FGModel.h> |
| 38 | #include <FDM/JSBSim/models/FGAircraft.h> |
| 39 | #include <FDM/JSBSim/models/FGFCS.h> |
| 40 | #include <FDM/JSBSim/models/FGPropagate.h> |
| 41 | #include <FDM/JSBSim/models/FGAuxiliary.h> |
| 42 | #include <FDM/JSBSim/models/FGInertial.h> |
| 43 | #include <FDM/JSBSim/models/FGMassBalance.h> |
| 44 | #include <FDM/JSBSim/models/FGAerodynamics.h> |
| 45 | #include <FDM/JSBSim/models/FGLGear.h> |
| 46 | #include <FDM/JSBSim/models/FGGroundReactions.h> |
| 47 | #include <FDM/JSBSim/models/FGPropulsion.h> |
| 48 | #include <FDM/JSBSim/models/FGAccelerations.h> |
| 49 | #include <FDM/JSBSim/models/atmosphere/FGStandardAtmosphere.h> |
| 50 | #include <FDM/JSBSim/models/atmosphere/FGWinds.h> |
| 51 | #include <FDM/JSBSim/models/propulsion/FGEngine.h> |
| 52 | #include <FDM/JSBSim/models/propulsion/FGPiston.h> |
| 53 | #include <FDM/JSBSim/models/propulsion/FGTurbine.h> |
| 54 | #include <FDM/JSBSim/models/propulsion/FGTurboProp.h> |
| 55 | #include <FDM/JSBSim/models/propulsion/FGRocket.h> |
| 56 | #include <FDM/JSBSim/models/propulsion/FGElectric.h> |
| 57 | #include <FDM/JSBSim/models/propulsion/FGNozzle.h> |
| 58 | #include <FDM/JSBSim/models/propulsion/FGPropeller.h> |
| 59 | #include <FDM/JSBSim/models/propulsion/FGRotor.h> |
| 60 | #include <FDM/JSBSim/models/propulsion/FGTank.h> |
| 61 | #include <FDM/JSBSim/input_output/FGPropertyManager.h> |
| 62 | #include <FDM/JSBSim/input_output/FGGroundCallback.h> |
| 63 | |
| 64 | using namespace JSBSim; |
| 65 | |
| 66 | static inline double |
| 67 | FMAX (double a, double b) |
| 68 | { |
| 69 | return a > b ? a : b; |
| 70 | } |
| 71 | |
| 72 | class FGFSGroundCallback : public FGGroundCallback { |
| 73 | public: |
| 74 | FGFSGroundCallback(FGJSBsim* ifc) : mInterface(ifc) {} |
| 75 | virtual ~FGFSGroundCallback() {} |
| 76 | |
| 77 | /** Get the altitude above sea level dependent on the location. */ |
| 78 | virtual double GetAltitude(const FGLocation& l) const { |
| 79 | double pt[3] = { SG_FEET_TO_METER*l(FGJSBBase::eX), |
| 80 | SG_FEET_TO_METER*l(FGJSBBase::eY), |
| 81 | SG_FEET_TO_METER*l(FGJSBBase::eZ) }; |
| 82 | double lat, lon, alt; |
| 83 | sgCartToGeod( pt, &lat, &lon, &alt); |
| 84 | return alt * SG_METER_TO_FEET; |
| 85 | } |
| 86 | |
| 87 | /** Compute the altitude above ground. */ |
| 88 | double GetAGLevel(double t, const FGLocation& l, FGLocation& cont, |
| 89 | FGColumnVector3& n, FGColumnVector3& v, FGColumnVector3& w) |
| 90 | const override { |
| 91 | double contact[3], normal[3], vel[3], angularVel[3]; |
| 92 | double agl = mInterface->get_agl_ft(t, l, SG_METER_TO_FEET*2, contact, |
| 93 | normal, vel, angularVel); |
| 94 | n = FGColumnVector3( normal[0], normal[1], normal[2] ); |
| 95 | v = FGColumnVector3( vel[0], vel[1], vel[2] ); |
| 96 | w = FGColumnVector3( angularVel[0], angularVel[1], angularVel[2] ); |
| 97 | cont = FGColumnVector3( contact[0], contact[1], contact[2] ); |
| 98 | return agl; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | FGJSBsim* mInterface; |
| 103 | }; |
| 104 | |
| 105 | // FG uses a squared normalized magnitude for turbulence |
| 106 | // this lookup table maps fg's severity levels |
| 107 | // none(0), light(1/3), moderate(2/3) and severe(3/3) |
| 108 | // to the POE table indexes 0, 3, 4 and 7 |
| 109 | class FGTurbulenceSeverityTable : public FGTable { |
| 110 | public: |
| 111 | FGTurbulenceSeverityTable() : FGTable(4) { |
| 112 | *this << (0.0/9.0) << 0.0; |
| 113 | *this << (1.0/9.0) << 3.0; |
| 114 | *this << (4.0/9.0) << 4.0; |
| 115 | *this << (9.0/9.0) << 7.0; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | /******************************************************************************/ |
| 120 | std::map<std::string,int> FGJSBsim::TURBULENCE_TYPE_NAMES; |
| 121 | |
| 122 | static FGTurbulenceSeverityTable TurbulenceSeverityTable; |
| 123 | |
| 124 | FGJSBsim::FGJSBsim( double dt ) |
| 125 | : FGInterface(dt) |
| 126 | { |
| 127 | bool result; |
| 128 | if( TURBULENCE_TYPE_NAMES.empty() ) { |
| 129 | TURBULENCE_TYPE_NAMES["ttNone"] = FGWinds::ttNone; |
| 130 | TURBULENCE_TYPE_NAMES["ttStandard"] = FGWinds::ttStandard; |
| 131 | TURBULENCE_TYPE_NAMES["ttCulp"] = FGWinds::ttCulp; |
| 132 | TURBULENCE_TYPE_NAMES["ttMilspec"] = FGWinds::ttMilspec; |
| 133 | TURBULENCE_TYPE_NAMES["ttTustin"] = FGWinds::ttTustin; |
| 134 | } |
| 135 | |
| 136 | // Set up the debugging level |
| 137 | // FIXME: this will not respond to |
| 138 | // runtime changes |
| 139 | |
| 140 | if ((sglog().get_log_classes() & SG_FLIGHT) != 0) { |
| 141 | |
| 142 | // do a rough-and-ready mapping to |
| 143 | // the levels documented in FGFDMExec.h |
| 144 | switch (sglog().get_log_priority()) { |
| 145 | case SG_BULK: |
| 146 | FGJSBBase::debug_lvl = 0x1f; |
| 147 | break; |
| 148 | case SG_DEBUG: |
| 149 | FGJSBBase::debug_lvl = 0x1f; |
| 150 | case SG_INFO: |
| 151 | case SG_WARN: |
| 152 | case SG_ALERT: |
| 153 | case SG_POPUP: |
| 154 | FGJSBBase::debug_lvl = 0x00; |
| 155 | break; |
| 156 | |
| 157 | default: |
| 158 | // silence warning about unhandled cases |
| 159 | FGJSBBase::debug_lvl = 0x00; |
| 160 | break; |
| 161 | } |
| 162 | } else { |
| 163 | // if flight is excluded, don't bother |
| 164 | FGJSBBase::debug_lvl = 0x00; |
| 165 | } |
| 166 | |
| 167 | PropertyManager = new FGPropertyManager( (FGPropertyNode*)globals->get_props() ); |
| 168 | fdmex = new FGFDMExec( PropertyManager ); |
| 169 | fdmex->Hold(); |
| 170 | |
| 171 | Atmosphere = fdmex->GetAtmosphere(); |
| 172 | Winds = fdmex->GetWinds(); |
| 173 | FCS = fdmex->GetFCS(); |
| 174 | MassBalance = fdmex->GetMassBalance(); |
| 175 | Propulsion = fdmex->GetPropulsion(); |
| 176 | Aircraft = fdmex->GetAircraft(); |
| 177 | Propagate = fdmex->GetPropagate(); |
| 178 | Auxiliary = fdmex->GetAuxiliary(); |
| 179 | Inertial = fdmex->GetInertial(); |
| 180 | Aerodynamics = fdmex->GetAerodynamics(); |
| 181 | GroundReactions = fdmex->GetGroundReactions(); |
| 182 | Accelerations = fdmex->GetAccelerations(); |
| 183 | |
| 184 | // Register ground callback. |
| 185 | Inertial->SetGroundCallback( new FGFSGroundCallback(this) ); |
| 186 | |
| 187 | fgic=fdmex->GetIC(); |
| 188 | needTrim=true; |
| 189 | |
| 190 | SGPath aircraft_path( fgGetString("/sim/aircraft-dir") ); |
| 191 | |
| 192 | SGPath engine_path( fgGetString("/sim/fg-root") ); |
| 193 | engine_path.append( "Aircraft/Generic/JSBSim/Engines" ); |
| 194 | |
| 195 | SGPath systems_path( fgGetString("/sim/fg-root") ); |
| 196 | systems_path.append( "Aircraft/Generic/JSBSim/Systems" ); |
| 197 | |
| 198 | // deprecate sim-time-sec for simulation/sim-time-sec |
| 199 | // remove alias with increased configuration file version number (2.1 or later) |
| 200 | SGPropertyNode * node = fgGetNode("/fdm/jsbsim/simulation/sim-time-sec"); |
| 201 | fgGetNode("/fdm/jsbsim/sim-time-sec", true)->alias(node, false); |
| 202 | // end of sim-time-sec deprecation patch |
| 203 | |
| 204 | _ai_wake_enabled = fgGetNode("fdm/ai-wake/enabled", true); |
| 205 | |
| 206 | terrain = fgGetNode("/sim/fdm/surface", true); |
| 207 | |
| 208 | fdmex->Setdt( dt ); |
| 209 | |
| 210 | result = fdmex->LoadModel( aircraft_path, engine_path, systems_path, |
| 211 | fgGetString("/sim/aero"), false ); |
| 212 | |
| 213 | if (result) { |
| 214 | SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero."); |
| 215 | } else { |
| 216 | SG_LOG( SG_FLIGHT, SG_INFO, |
| 217 | " aero does not exist (you may have mis-typed the name)."); |
| 218 | throw(-1); |
| 219 | } |
| 220 | |
| 221 | SG_LOG( SG_FLIGHT, SG_INFO, "" ); |
| 222 | SG_LOG( SG_FLIGHT, SG_INFO, "" ); |
| 223 | SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." ); |
| 224 | |
| 225 | int Neng = Propulsion->GetNumEngines(); |
| 226 | SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng ); |
| 227 | |
| 228 | if ( GroundReactions->GetNumGearUnits() <= 0 ) { |
| 229 | SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = " |
| 230 | << GroundReactions->GetNumGearUnits() ); |
| 231 | SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming"); |
| 232 | SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump."); |
| 233 | SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!"); |
| 234 | exit(-1); |
| 235 | } |
| 236 | |
| 237 | init_gear(); |
| 238 | |
| 239 | // Set initial fuel levels if provided. |
| 240 | for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) { |
| 241 | double d; |
| 242 | SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true); |
| 243 | FGTank* tank = Propulsion->GetTank(i); |
| 244 | SGPropertyNode * prop = node->getNode( "density-ppg", true ); |
| 245 | |
| 246 | d = prop->getDoubleValue(); |
| 247 | if( d > 0.0 ) { |
| 248 | tank->SetDensity( d ); |
| 249 | } else { |
| 250 | prop->setDoubleValue( SG_MAX2<double>(tank->GetDensity(), 0.1) ); |
| 251 | } |
| 252 | |
| 253 | prop = node->getNode( "level-lbs", true ); |
| 254 | d = prop->getDoubleValue(); |
| 255 | if( d > 0.0 ) { |
| 256 | tank->SetContents( d ); |
| 257 | } else { |
| 258 | prop->setDoubleValue( tank->GetContents() ); |
| 259 | } |
| 260 | |
| 261 | prop = node->getNode( "unusable-gal_us", true ); |
| 262 | d = prop->getDoubleValue(); |
| 263 | if ( d > 0.0 ) { |
| 264 | tank->SetUnusableVolume( d ); |
| 265 | } else { |
| 266 | prop->setDoubleValue(tank->GetUnusableVolume()); |
| 267 | } |
| 268 | /* Capacity is read-only in FGTank and can't be overwritten from FlightGear */ |
| 269 | node->getNode("capacity-gal_us", true )->setDoubleValue( tank->GetCapacityGallons() ); |
| 270 | } |
| 271 | Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue()); |
| 272 | |
| 273 | fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd()); |
| 274 | fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0)); |
| 275 | fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd()); |
| 276 | fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd()); |
| 277 | |
| 278 | startup_trim = fgGetNode("/sim/presets/trim", true); |
| 279 | |
| 280 | trimmed = fgGetNode("/fdm/trim/trimmed", true); |
| 281 | trimmed->setBoolValue(false); |
| 282 | |
| 283 | pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true ); |
| 284 | throttle_trim = fgGetNode("/fdm/trim/throttle", true ); |
| 285 | aileron_trim = fgGetNode("/fdm/trim/aileron", true ); |
| 286 | rudder_trim = fgGetNode("/fdm/trim/rudder", true ); |
| 287 | |
| 288 | stall_warning = fgGetNode("/sim/alarms/stall-warning",true); |
| 289 | stall_warning->setDoubleValue(0); |
| 290 | |
| 291 | |
| 292 | flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true); |
| 293 | elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true); |
| 294 | left_aileron_pos_pct |
| 295 | =fgGetNode("/surface-positions/left-aileron-pos-norm",true); |
| 296 | right_aileron_pos_pct |
| 297 | =fgGetNode("/surface-positions/right-aileron-pos-norm",true); |
| 298 | rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true); |
| 299 | speedbrake_pos_pct |
| 300 | =fgGetNode("/surface-positions/speedbrake-pos-norm",true); |
| 301 | spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true); |
| 302 | tailhook_pos_pct=fgGetNode("/gear/tailhook/position-norm",true); |
| 303 | wing_fold_pos_pct=fgGetNode("surface-positions/wing-fold-pos-norm",true); |
| 304 | |
| 305 | elevator_pos_pct->setDoubleValue(0); |
| 306 | left_aileron_pos_pct->setDoubleValue(0); |
| 307 | right_aileron_pos_pct->setDoubleValue(0); |
| 308 | rudder_pos_pct->setDoubleValue(0); |
| 309 | flap_pos_pct->setDoubleValue(0); |
| 310 | speedbrake_pos_pct->setDoubleValue(0); |
| 311 | spoilers_pos_pct->setDoubleValue(0); |
| 312 | |
| 313 | override_fg_brake_prop = fgGetNode("/fdm/jsbsim/systems/override-fg-brake-properties", true); |
| 314 | override_fg_brake_prop->setBoolValue(false); |
| 315 | ab_brake_engaged = fgGetNode("/autopilot/autobrake/engaged", true); |
| 316 | ab_brake_left_pct = fgGetNode("/autopilot/autobrake/brake-left-output", true); |
| 317 | ab_brake_right_pct = fgGetNode("/autopilot/autobrake/brake-right-output", true); |
| 318 | |
| 319 | arrestor_wire_engaged_hook = fgGetNode("/fdm/jsbsim/systems/hook/arrestor-wire-engaged-hook", true); |
| 320 | release_hook = fgGetNode("/fdm/jsbsim/systems/hook/tailhook-release-cmd", true); |
| 321 | |
| 322 | altitude = fgGetNode("/position/altitude-ft"); |
| 323 | precipitation = fgGetNode("/environment/rain-norm", true); |
| 324 | temperature = fgGetNode("/environment/temperature-degc",true); |
| 325 | pressure = fgGetNode("/environment/pressure-inhg",true); |
| 326 | pressureSL = fgGetNode("/environment/pressure-sea-level-inhg",true); |
| 327 | dew_point = fgGetNode("/environment/dewpoint-degc", true); |
| 328 | ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true); |
| 329 | turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true); |
| 330 | turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true); |
| 331 | turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true); |
| 332 | |
| 333 | wind_from_north= fgGetNode("/environment/wind-from-north-fps",true); |
| 334 | wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true); |
| 335 | wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true); |
| 336 | |
| 337 | slaved = fgGetNode("/sim/slaved/enabled", true); |
| 338 | |
| 339 | for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) { |
| 340 | SGPropertyNode * node = fgGetNode("engines/engine", i, true); |
| 341 | Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") / |
| 342 | Propulsion->GetEngine(i)->GetThruster()->GetGearRatio()); |
| 343 | } |
| 344 | |
| 345 | hook_root_struct = FGColumnVector3( |
| 346 | fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-x-in", 196), |
| 347 | fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-y-in", 0), |
| 348 | fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-z-in", -16)); |
| 349 | last_hook_tip[0] = 0; last_hook_tip[1] = 0; last_hook_tip[2] = 0; |
| 350 | last_hook_root[0] = 0; last_hook_root[1] = 0; last_hook_root[2] = 0; |
| 351 | |
| 352 | crashed = false; |
| 353 | |
| 354 | mesh = new AircraftMesh(fgGetDouble("/fdm/jsbsim/metrics/bw-ft"), |
| 355 | fgGetDouble("/fdm/jsbsim/metrics/cbarw-ft"), |
| 356 | fdmex->GetModelName()); |
| 357 | |
| 358 | // Trim once to initialize all output parameters |
| 359 | FGTrim *fgtrim = new FGTrim(fdmex,tFull); |
| 360 | fgtrim->DoTrim(); |
| 361 | delete fgtrim; |
| 362 | |
| 363 | std::string directive_file = fgGetString("/sim/jsbsim/output-directive-file"); |
| 364 | if (!directive_file.empty()) |
| 365 | fdmex->SetOutputDirectives(directive_file); |
| 366 | } |
| 367 | |
| 368 | /******************************************************************************/ |
| 369 | FGJSBsim::~FGJSBsim(void) |
| 370 | { |
| 371 | delete fdmex; |
| 372 | delete PropertyManager; |
| 373 | } |
| 374 | |
| 375 | /******************************************************************************/ |
| 376 | |
| 377 | // Initialize the JSBsim flight model, dt is the time increment for |
| 378 | // each subsequent iteration through the EOM |
| 379 | |
| 380 | void FGJSBsim::init() |
| 381 | { |
| 382 | SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" ); |
| 383 | |
| 384 | // Explicitly call the superclass's |
| 385 | // init method first. |
| 386 | |
| 387 | if (fgGetBool("/environment/params/control-fdm-atmosphere")) { |
| 388 | Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius); |
| 389 | Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue()); |
| 390 | // initialize to no turbulence, these values get set in the update loop |
| 391 | Winds->SetTurbType(FGWinds::ttNone); |
| 392 | Winds->SetTurbGain(0.0); |
| 393 | Winds->SetTurbRate(0.0); |
| 394 | Winds->SetWindspeed20ft(0.0); |
| 395 | Winds->SetProbabilityOfExceedence(0.0); |
| 396 | } |
| 397 | |
| 398 | fgic->SetWindNEDFpsIC(0.0, 0.0, 0.0); |
| 399 | |
| 400 | SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << Atmosphere->GetTemperature() |
| 401 | << ", " << Atmosphere->GetPressure() |
| 402 | << ", " << Atmosphere->GetDensity() ); |
| 403 | |
| 404 | FCS->SetDfPos( ofNorm, globals->get_controls()->get_flaps() ); |
| 405 | |
| 406 | needTrim = startup_trim->getBoolValue(); |
| 407 | common_init(); |
| 408 | |
| 409 | copy_to_JSBsim(); |
| 410 | fdmex->Resume(); |
| 411 | fdmex->RunIC(); //loop JSBSim once w/o integrating |
| 412 | if (fgGetBool("/sim/presets/running")) { |
| 413 | Propulsion->InitRunning(-1); |
| 414 | for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) { |
| 415 | FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i); |
| 416 | globals->get_controls()->set_magnetos(i, eng->GetMagnetos()); |
| 417 | globals->get_controls()->set_mixture(i, FCS->GetMixtureCmd(i)); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if ( needTrim ) { |
| 422 | const FGLocation& cart = fgic->GetPosition(); |
| 423 | double contact[3], d[3], vel[3]; |
| 424 | update_ground_cache(cart, 0.01); |
| 425 | |
| 426 | get_agl_ft(fdmex->GetSimTime(), cart, SG_METER_TO_FEET*2, contact, d, vel, |
| 427 | d); |
| 428 | double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1] |
| 429 | + contact[2]*contact[2]) - cart.GetSeaLevelRadius(); |
| 430 | |
| 431 | SG_LOG(SG_FLIGHT, SG_INFO, "Ready to trim, terrain elevation is: " |
| 432 | << terrain_alt ); |
| 433 | |
| 434 | if (fgGetBool("/sim/presets/onground")) { |
| 435 | FGColumnVector3 gndVelNED = cart.GetTec2l() |
| 436 | * FGColumnVector3(vel[0], vel[1], vel[2]); |
| 437 | fgic->SetVNorthFpsIC(gndVelNED(1)); |
| 438 | fgic->SetVEastFpsIC(gndVelNED(2)); |
| 439 | fgic->SetVDownFpsIC(gndVelNED(3)); |
| 440 | } |
| 441 | do_trim(); |
| 442 | needTrim = false; |
| 443 | } |
| 444 | |
| 445 | copy_from_JSBsim(); //update the bus |
| 446 | |
| 447 | _fmag = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/magnitude", false); |
| 448 | _fbx = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/x", false); |
| 449 | _fby = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/y", false); |
| 450 | _fbz = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/z", false); |
| 451 | _mmag = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/magnitude-lbsft", false); |
| 452 | _mbx = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/l", false); |
| 453 | _mby = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/m", false); |
| 454 | _mbz = fgGetNode("/fdm/jsbsim/external_reactions/ai-wake/n", false); |
| 455 | |
| 456 | SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" ); |
| 457 | |
| 458 | switch(fgic->GetSpeedSet()) { |
| 459 | case setned: |
| 460 | SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= " |
| 461 | << Propagate->GetVel(FGJSBBase::eNorth) << ", " |
| 462 | << Propagate->GetVel(FGJSBBase::eEast) << ", " |
| 463 | << Propagate->GetVel(FGJSBBase::eDown) << " ft/s"); |
| 464 | break; |
| 465 | case setuvw: |
| 466 | SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= " |
| 467 | << Propagate->GetUVW(1) << ", " |
| 468 | << Propagate->GetUVW(2) << ", " |
| 469 | << Propagate->GetUVW(3) << " ft/s"); |
| 470 | break; |
| 471 | case setmach: |
| 472 | SG_LOG(SG_FLIGHT,SG_INFO, " Mach: " |
| 473 | << Auxiliary->GetMach() ); |
| 474 | break; |
| 475 | case setvc: |
| 476 | default: |
| 477 | SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: " |
| 478 | << Auxiliary->GetVcalibratedKTS() << " knots" ); |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | stall_warning->setDoubleValue(0); |
| 483 | |
| 484 | SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: " |
| 485 | << Propagate->GetEuler(FGJSBBase::ePhi)*RADTODEG << " deg" ); |
| 486 | SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: " |
| 487 | << Propagate->GetEuler(FGJSBBase::eTht)*RADTODEG << " deg" ); |
| 488 | SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: " |
| 489 | << Propagate->GetEuler(FGJSBBase::ePsi)*RADTODEG << " deg" ); |
| 490 | SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: " |
| 491 | << Propagate->GetLocation().GetLatitudeDeg() << " deg" ); |
| 492 | SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: " |
| 493 | << Propagate->GetLocation().GetLongitudeDeg() << " deg" ); |
| 494 | SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: " |
| 495 | << Propagate->GetAltitudeASL() << " feet" ); |
| 496 | SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" ); |
| 497 | |
| 498 | SG_LOG( SG_FLIGHT, SG_INFO, " set dt" ); |
| 499 | |
| 500 | SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" ); |
| 501 | |
| 502 | SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " << |
| 503 | globals->get_controls()->get_gear_down() ); |
| 504 | } |
| 505 | |
| 506 | /******************************************************************************/ |
| 507 | |
| 508 | void FGJSBsim::unbind() |
| 509 | { |
| 510 | fdmex->Unbind(); |
| 511 | FGInterface::unbind(); |
| 512 | } |
| 513 | |
| 514 | /******************************************************************************/ |
| 515 | |
| 516 | // Run an iteration of the EOM (equations of motion) |
| 517 | void FGJSBsim::update( double dt ) |
| 518 | { |
| 519 | if(crashed) { |
| 520 | if(!fgGetBool("/sim/crashed")) |
| 521 | fgSetBool("/sim/crashed", true); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | if (is_suspended()) |
| 526 | return; |
| 527 | |
| 528 | int multiloop = _calc_multiloop(dt); |
| 529 | FGLocation cart = Auxiliary->GetLocationVRP(); |
| 530 | |
| 531 | update_ground_cache(cart, dt); |
| 532 | |
| 533 | copy_to_JSBsim(); |
| 534 | |
| 535 | trimmed->setBoolValue(false); |
| 536 | |
| 537 | for ( int i=0; i < multiloop; i++ ) { |
| 538 | if (!fdmex->Run()) { |
| 539 | // The property fdm/jsbsim/simulation/terminate has been set to true |
| 540 | // by the user. The sim is considered crashed. |
| 541 | crashed = true; |
| 542 | break; |
| 543 | } |
| 544 | update_external_forces(fdmex->GetSimTime() + i * fdmex->GetDeltaT()); |
| 545 | } |
| 546 | |
| 547 | reset_wake_group(); |
| 548 | |
| 549 | // translate JSBsim back to FG structure so that the |
| 550 | // autopilot (and the rest of the sim can use the updated values |
| 551 | copy_from_JSBsim(); |
| 552 | } |
| 553 | |
| 554 | /******************************************************************************/ |
| 555 | |
| 556 | void FGJSBsim::suspend() |
| 557 | { |
| 558 | fdmex->Hold(); |
| 559 | SGSubsystem::suspend(); |
| 560 | } |
| 561 | |
| 562 | /******************************************************************************/ |
| 563 | |
| 564 | void FGJSBsim::resume() |
| 565 | { |
| 566 | fdmex->Resume(); |
| 567 | SGSubsystem::resume(); |
| 568 | } |
| 569 | |
| 570 | /******************************************************************************/ |
| 571 | |
| 572 | // Convert from the FGInterface struct to the JSBsim generic_ struct |
| 573 | |
| 574 | bool FGJSBsim::copy_to_JSBsim() |
| 575 | { |
| 576 | unsigned int i; |
| 577 | |
| 578 | // copy control positions into the JSBsim structure |
| 579 | |
| 580 | FCS->SetDaCmd( globals->get_controls()->get_aileron()); |
| 581 | FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() ); |
| 582 | FCS->SetDeCmd( globals->get_controls()->get_elevator()); |
| 583 | FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() ); |
| 584 | FCS->SetDrCmd( -globals->get_controls()->get_rudder() ); |
| 585 | FCS->SetDsCmd( globals->get_controls()->get_rudder() ); |
| 586 | FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() ); |
| 587 | FCS->SetDfCmd( globals->get_controls()->get_flaps() ); |
| 588 | FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() ); |
| 589 | FCS->SetDspCmd( globals->get_controls()->get_spoilers() ); |
| 590 | |
| 591 | if (!override_fg_brake_prop->getBoolValue()) { |
| 592 | // Parking brake sets minimum braking |
| 593 | // level for mains. |
| 594 | double parking_brake = globals->get_controls()->get_brake_parking(); |
| 595 | double left_brake = globals->get_controls()->get_brake_left(); |
| 596 | double right_brake = globals->get_controls()->get_brake_right(); |
| 597 | |
| 598 | if (ab_brake_engaged->getBoolValue()) { |
| 599 | left_brake = ab_brake_left_pct->getDoubleValue(); |
| 600 | right_brake = ab_brake_right_pct->getDoubleValue(); |
| 601 | } |
| 602 | |
| 603 | FCS->SetLBrake(FMAX(left_brake, parking_brake)); |
| 604 | FCS->SetRBrake(FMAX(right_brake, parking_brake)); |
| 605 | |
| 606 | FCS->SetCBrake( 0.0 ); |
| 607 | // FCS->SetCBrake( globals->get_controls()->get_brake(2) ); |
| 608 | } |
| 609 | |
| 610 | FCS->SetGearCmd( globals->get_controls()->get_gear_down()); |
| 611 | for (i = 0; i < Propulsion->GetNumEngines(); i++) { |
| 612 | SGPropertyNode * node = fgGetNode("engines/engine", i, true); |
| 613 | |
| 614 | FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i)); |
| 615 | FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i)); |
| 616 | FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i)); |
| 617 | FCS->SetFeatherCmd(i, globals->get_controls()->get_feather(i)); |
| 618 | |
| 619 | switch (Propulsion->GetEngine(i)->GetType()) { |
| 620 | case FGEngine::etPiston: |
| 621 | { // FGPiston code block |
| 622 | FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i); |
| 623 | eng->SetMagnetos( globals->get_controls()->get_magnetos(i) ); |
| 624 | break; |
| 625 | } // end FGPiston code block |
| 626 | case FGEngine::etTurbine: |
| 627 | { // FGTurbine code block |
| 628 | FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i); |
| 629 | eng->SetAugmentation( globals->get_controls()->get_augmentation(i) ); |
| 630 | eng->SetReverse( globals->get_controls()->get_reverser(i) ); |
| 631 | //eng->SetInjection( globals->get_controls()->get_water_injection(i) ); |
| 632 | eng->SetCutoff( globals->get_controls()->get_cutoff(i) ); |
| 633 | eng->SetIgnition( globals->get_controls()->get_ignition(i) ); |
| 634 | break; |
| 635 | } // end FGTurbine code block |
| 636 | case FGEngine::etRocket: |
| 637 | { // FGRocket code block |
| 638 | // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i); |
| 639 | break; |
| 640 | } // end FGRocket code block |
| 641 | case FGEngine::etTurboprop: |
| 642 | { // FGTurboProp code block |
| 643 | FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i); |
| 644 | eng->SetReverse( globals->get_controls()->get_reverser(i) ); |
| 645 | eng->SetCutoff( globals->get_controls()->get_cutoff(i) ); |
| 646 | // eng->SetIgnition( globals->get_controls()->get_ignition(i) ); |
| 647 | |
| 648 | eng->SetGeneratorPower( globals->get_controls()->get_generator_breaker(i) ); |
| 649 | eng->SetCondition( globals->get_controls()->get_condition(i) ); |
| 650 | break; |
| 651 | } // end FGTurboProp code block |
| 652 | default: |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | { // FGEngine code block |
| 657 | FGEngine* eng = Propulsion->GetEngine(i); |
| 658 | |
| 659 | eng->SetStarter( globals->get_controls()->get_starter(i) ); |
| 660 | eng->SetRunning( node->getBoolValue("running") ); |
| 661 | } // end FGEngine code block |
| 662 | } |
| 663 | |
| 664 | Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius); |
| 665 | Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue()); |
| 666 | |
| 667 | // work-around for broken FlightGear dew-point calculation at altitude |
| 668 | // see https://gitlab.com/flightgear/flightgear/-/issues/3267 |
| 669 | const double humidity_cutoff_altitude = 140000; // feet |
| 670 | auto fgAtmosphere = static_cast<FGStandardAtmosphere*>(Atmosphere); |
| 671 | if (get_Altitude() < humidity_cutoff_altitude) { |
| 672 | fgAtmosphere->SetDewPoint(FGAtmosphere::eCelsius, dew_point->getDoubleValue()); |
| 673 | } else { |
| 674 | // Force humidity to zero. |
| 675 | fgAtmosphere->SetVaporMassFractionPPM(0.0); |
| 676 | } |
| 677 | |
| 678 | |
| 679 | Winds->SetTurbType((FGWinds::tType)TURBULENCE_TYPE_NAMES[turbulence_model->getStringValue()]); |
| 680 | switch( Winds->GetTurbType() ) { |
| 681 | case FGWinds::ttStandard: |
| 682 | case FGWinds::ttCulp: { |
| 683 | double tmp = turbulence_gain->getDoubleValue(); |
| 684 | Winds->SetTurbGain(tmp * tmp * 100.0); |
| 685 | Winds->SetTurbRate(turbulence_rate->getDoubleValue()); |
| 686 | break; |
| 687 | } |
| 688 | case FGWinds::ttMilspec: |
| 689 | case FGWinds::ttTustin: { |
| 690 | // milspec turbulence: 3=light, 4=moderate, 6=severe turbulence |
| 691 | // turbulence_gain normalized: 0: none, 1/3: light, 2/3: moderate, 3/3: severe |
| 692 | double tmp = turbulence_gain->getDoubleValue(); |
| 693 | Winds->SetProbabilityOfExceedence( |
| 694 | SGMiscd::roundToInt(TurbulenceSeverityTable.GetValue( tmp ) ) |
| 695 | ); |
| 696 | Winds->SetWindspeed20ft(ground_wind->getDoubleValue()); |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | default: |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | Winds->SetWindNED( -wind_from_north->getDoubleValue(), |
| 705 | -wind_from_east->getDoubleValue(), |
| 706 | -wind_from_down->getDoubleValue() ); |
| 707 | // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: " |
| 708 | // << get_V_north_airmass() << ", " |
| 709 | // << get_V_east_airmass() << ", " |
| 710 | // << get_V_down_airmass() ); |
| 711 | |
| 712 | for (i = 0; i < Propulsion->GetNumTanks(); i++) { |
| 713 | SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true); |
| 714 | FGTank * tank = Propulsion->GetTank(i); |
| 715 | double fuelDensity = node->getDoubleValue("density-ppg"); |
| 716 | |
| 717 | if (fuelDensity < 0.1) |
| 718 | fuelDensity = 6.0; // Use average fuel value |
| 719 | |
| 720 | tank->SetDensity(fuelDensity); |
| 721 | tank->SetContents(node->getDoubleValue("level-lbs")); |
| 722 | } |
| 723 | |
| 724 | Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue()); |
| 725 | fdmex->SetChild(slaved->getBoolValue()); |
| 726 | |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | /******************************************************************************/ |
| 731 | |
| 732 | // Convert from the JSBsim generic_ struct to the FGInterface struct |
| 733 | |
| 734 | bool FGJSBsim::copy_from_JSBsim() |
| 735 | { |
| 736 | unsigned int i, j; |
| 737 | /* |
| 738 | _set_Inertias( MassBalance->GetMass(), |
| 739 | MassBalance->GetIxx(), |
| 740 | MassBalance->GetIyy(), |
| 741 | MassBalance->GetIzz(), |
| 742 | MassBalance->GetIxz() ); |
| 743 | */ |
| 744 | _set_CG_Position( MassBalance->GetXYZcg(1), |
| 745 | MassBalance->GetXYZcg(2), |
| 746 | MassBalance->GetXYZcg(3) ); |
| 747 | |
| 748 | _set_Accels_Body( Accelerations->GetBodyAccel(1), |
| 749 | Accelerations->GetBodyAccel(2), |
| 750 | Accelerations->GetBodyAccel(3) ); |
| 751 | |
| 752 | _set_Accels_CG_Body_N ( Auxiliary->GetNcg(1), |
| 753 | Auxiliary->GetNcg(2), |
| 754 | Auxiliary->GetNcg(3) ); |
| 755 | |
| 756 | _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1), |
| 757 | Auxiliary->GetPilotAccel(2), |
| 758 | Auxiliary->GetPilotAccel(3) ); |
| 759 | |
| 760 | _set_Nlf( Auxiliary->GetNlf() ); |
| 761 | |
| 762 | // Velocities |
| 763 | |
| 764 | _set_Velocities_Local( Propagate->GetVel(FGJSBBase::eNorth), |
| 765 | Propagate->GetVel(FGJSBBase::eEast), |
| 766 | Propagate->GetVel(FGJSBBase::eDown) ); |
| 767 | |
| 768 | _set_Velocities_Body( Propagate->GetUVW(1), |
| 769 | Propagate->GetUVW(2), |
| 770 | Propagate->GetUVW(3) ); |
| 771 | |
| 772 | // Make the HUD work ... |
| 773 | _set_Velocities_Ground( Propagate->GetVel(FGJSBBase::eNorth), |
| 774 | Propagate->GetVel(FGJSBBase::eEast), |
| 775 | -Propagate->GetVel(FGJSBBase::eDown) ); |
| 776 | |
| 777 | _set_V_rel_wind( Auxiliary->GetVt() ); |
| 778 | |
| 779 | _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() ); |
| 780 | |
| 781 | _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() ); |
| 782 | |
| 783 | _set_V_ground_speed( Auxiliary->GetVground() ); |
| 784 | |
| 785 | _set_Omega_Body( Propagate->GetPQR(FGJSBBase::eP), |
| 786 | Propagate->GetPQR(FGJSBBase::eQ), |
| 787 | Propagate->GetPQR(FGJSBBase::eR) ); |
| 788 | |
| 789 | _set_Euler_Rates( Auxiliary->GetEulerRates(FGJSBBase::ePhi), |
| 790 | Auxiliary->GetEulerRates(FGJSBBase::eTht), |
| 791 | Auxiliary->GetEulerRates(FGJSBBase::ePsi) ); |
| 792 | |
| 793 | _set_Mach_number( Auxiliary->GetMach() ); |
| 794 | |
| 795 | // Positions of Visual Reference Point |
| 796 | FGLocation l = Auxiliary->GetLocationVRP(); |
| 797 | _updatePosition(SGGeoc::fromRadFt( l.GetLongitude(), l.GetLatitude(), |
| 798 | l.GetRadius() )); |
| 799 | |
| 800 | _set_Altitude_AGL( Propagate->GetDistanceAGL() ); |
| 801 | { |
| 802 | double contact[3], d[3], sd, t; |
| 803 | is_valid_m(&t, d, &sd); |
| 804 | get_agl_ft(t, l, SG_METER_TO_FEET*2, contact, d, d, d); |
| 805 | double rwrad |
| 806 | = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude(); |
| 807 | _set_Runway_altitude( rwrad - get_Sea_level_radius() ); |
| 808 | } |
| 809 | |
| 810 | _set_Euler_Angles( Propagate->GetEuler(FGJSBBase::ePhi), |
| 811 | Propagate->GetEuler(FGJSBBase::eTht), |
| 812 | Propagate->GetEuler(FGJSBBase::ePsi) ); |
| 813 | |
| 814 | _set_Alpha( Auxiliary->Getalpha() ); |
| 815 | _set_Beta( Auxiliary->Getbeta() ); |
| 816 | |
| 817 | |
| 818 | _set_Gamma_vert_rad( Auxiliary->GetGamma() ); |
| 819 | |
| 820 | _set_Earth_position_angle( Propagate->GetEarthPositionAngle() ); |
| 821 | |
| 822 | _set_Climb_Rate( Propagate->Gethdot() ); |
| 823 | |
| 824 | const FGMatrix33& Tl2b = Propagate->GetTl2b(); |
| 825 | for ( i = 1; i <= 3; i++ ) { |
| 826 | for ( j = 1; j <= 3; j++ ) { |
| 827 | _set_T_Local_to_Body( i, j, Tl2b(i,j) ); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // Copy the engine values from JSBSim. |
| 832 | for ( i=0; i < Propulsion->GetNumEngines(); i++ ) { |
| 833 | SGPropertyNode * node = fgGetNode("engines/engine", i, true); |
| 834 | SGPropertyNode * tnode = node->getChild("thruster", 0, true); |
| 835 | FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster(); |
| 836 | |
| 837 | switch (Propulsion->GetEngine(i)->GetType()) { |
| 838 | case FGEngine::etPiston: |
| 839 | { // FGPiston code block |
| 840 | FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i); |
| 841 | node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF()); |
| 842 | node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF()); |
| 843 | node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi()); |
| 844 | node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg()); |
| 845 | // NOTE: mp-osi is not in ounces per square inch. |
| 846 | // This error is left for reasons of backwards compatibility with |
| 847 | // existing FlightGear sound and instrument configurations. |
| 848 | node->setDoubleValue("mp-inhg", eng->getManifoldPressure_inHg()); |
| 849 | node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF()); |
| 850 | node->setDoubleValue("rpm", eng->getRPM()); |
| 851 | } // end FGPiston code block |
| 852 | break; |
| 853 | case FGEngine::etRocket: |
| 854 | { // FGRocket code block |
| 855 | // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i); |
| 856 | } // end FGRocket code block |
| 857 | break; |
| 858 | case FGEngine::etTurbine: |
| 859 | { // FGTurbine code block |
| 860 | FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i); |
| 861 | node->setDoubleValue("n1", eng->GetN1()); |
| 862 | node->setDoubleValue("n2", eng->GetN2()); |
| 863 | node->setDoubleValue("egt-degf", 32 + eng->GetEGT()*9/5); |
| 864 | node->setBoolValue("augmentation", eng->GetAugmentation()); |
| 865 | node->setBoolValue("water-injection", eng->GetInjection()); |
| 866 | node->setBoolValue("ignition", eng->GetIgnition() != 0); |
| 867 | node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle()); |
| 868 | node->setDoubleValue("inlet-pos-norm", eng->GetInlet()); |
| 869 | node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi()); |
| 870 | node->setBoolValue("reversed", eng->GetReversed()); |
| 871 | node->setBoolValue("cutoff", eng->GetCutoff()); |
| 872 | node->setDoubleValue("epr", eng->GetEPR()); |
| 873 | globals->get_controls()->set_reverser(i, eng->GetReversed() ); |
| 874 | globals->get_controls()->set_cutoff(i, eng->GetCutoff() ); |
| 875 | globals->get_controls()->set_water_injection(i, eng->GetInjection() ); |
| 876 | globals->get_controls()->set_augmentation(i, eng->GetAugmentation() ); |
| 877 | } // end FGTurbine code block |
| 878 | break; |
| 879 | case FGEngine::etTurboprop: |
| 880 | { // FGTurboProp code block |
| 881 | FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i); |
| 882 | node->setDoubleValue("n1", eng->GetN1()); |
| 883 | //node->setDoubleValue("n2", eng->GetN2()); |
| 884 | node->setDoubleValue("itt_degf", 32 + eng->GetITT()*9/5); |
| 885 | // node->setBoolValue("ignition", eng->GetIgnition() != 0); |
| 886 | // node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle()); |
| 887 | // node->setDoubleValue("inlet-pos-norm", eng->GetInlet()); |
| 888 | node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi()); |
| 889 | node->setBoolValue("reversed", eng->GetReversed()); |
| 890 | node->setBoolValue("cutoff", eng->GetCutoff()); |
| 891 | node->setBoolValue("starting", eng->GetEngStarting()); |
| 892 | node->setBoolValue("generator-power", eng->GetGeneratorPower()); |
| 893 | node->setBoolValue("damaged", eng->GetCondition() != 0); |
| 894 | node->setBoolValue("ielu-intervent", eng->GetIeluIntervent()); |
| 895 | node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF()); |
| 896 | // node->setBoolValue("onfire", eng->GetFire()); |
| 897 | globals->get_controls()->set_reverser(i, eng->GetReversed() ); |
| 898 | globals->get_controls()->set_cutoff(i, eng->GetCutoff() ); |
| 899 | } // end FGTurboProp code block |
| 900 | break; |
| 901 | case FGEngine::etElectric: |
| 902 | { // FGElectric code block |
| 903 | FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i); |
| 904 | node->setDoubleValue("rpm", eng->getRPM()); |
| 905 | } // end FGElectric code block |
| 906 | break; |
| 907 | case FGEngine::etUnknown: |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | { // FGEngine code block |
| 912 | FGEngine* eng = Propulsion->GetEngine(i); |
| 913 | node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph()); |
| 914 | node->setDoubleValue("thrust_lb", thruster->GetThrust()); |
| 915 | node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph()); |
| 916 | node->setBoolValue("running", eng->GetRunning()); |
| 917 | node->setBoolValue("starter", eng->GetStarter()); |
| 918 | node->setBoolValue("cranking", eng->GetCranking()); |
| 919 | globals->get_controls()->set_starter(i, eng->GetStarter() ); |
| 920 | } // end FGEngine code block |
| 921 | |
| 922 | switch (thruster->GetType()) { |
| 923 | case FGThruster::ttNozzle: |
| 924 | { // FGNozzle code block |
| 925 | // FGNozzle* noz = (FGNozzle*)thruster; |
| 926 | } // end FGNozzle code block |
| 927 | break; |
| 928 | case FGThruster::ttPropeller: |
| 929 | { // FGPropeller code block |
| 930 | FGPropeller* prop = (FGPropeller*)thruster; |
| 931 | tnode->setDoubleValue("rpm", thruster->GetRPM()); |
| 932 | tnode->setDoubleValue("pitch", prop->GetPitch()); |
| 933 | tnode->setDoubleValue("torque", prop->GetTorque()); |
| 934 | tnode->setBoolValue("feathered", prop->GetFeather()); |
| 935 | } // end FGPropeller code block |
| 936 | break; |
| 937 | case FGThruster::ttRotor: |
| 938 | { // FGRotor code block |
| 939 | // FGRotor* rotor = (FGRotor*)thruster; |
| 940 | } // end FGRotor code block |
| 941 | break; |
| 942 | case FGThruster::ttDirect: |
| 943 | { // Direct code block |
| 944 | } // end Direct code block |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | } |
| 949 | |
| 950 | // Copy the fuel levels from JSBSim if fuel |
| 951 | // freeze not enabled. |
| 952 | if ( ! Propulsion->GetFuelFreeze() ) { |
| 953 | for (i = 0; i < Propulsion->GetNumTanks(); i++) { |
| 954 | SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true); |
| 955 | FGTank* tank = Propulsion->GetTank(i); |
| 956 | double contents = tank->GetContents(); |
| 957 | double temp = tank->GetTemperature_degC(); |
| 958 | double fuelDensity = tank->GetDensity(); |
| 959 | |
| 960 | if (fuelDensity < 0.1) |
| 961 | fuelDensity = 6.0; // Use average fuel value |
| 962 | |
| 963 | node->setDoubleValue("density-ppg" , fuelDensity); |
| 964 | node->setDoubleValue("level-lbs", contents); |
| 965 | if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp); |
| 966 | |
| 967 | node->setDoubleValue("arm-in", tank->GetXYZ(FGJSBBase::eX ) ); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | update_gear(); |
| 972 | |
| 973 | stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() ); |
| 974 | |
| 975 | elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) ); |
| 976 | left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) ); |
| 977 | right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) ); |
| 978 | rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) ); |
| 979 | flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) ); |
| 980 | speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) ); |
| 981 | spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) ); |
| 982 | tailhook_pos_pct->setDoubleValue( FCS->GetTailhookPos() ); |
| 983 | wing_fold_pos_pct->setDoubleValue( FCS->GetWingFoldPos() ); |
| 984 | |
| 985 | // force a sim crashed if crashed (altitude AGL < 0) |
| 986 | if (get_Altitude_AGL() < -100.0) { |
| 987 | fdmex->SuspendIntegration(); |
| 988 | crashed = true; |
| 989 | } |
| 990 | |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | bool FGJSBsim::ToggleDataLogging(void) |
| 996 | { |
| 997 | // ToDo: handle this properly |
| 998 | fdmex->DisableOutput(); |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | bool FGJSBsim::ToggleDataLogging(bool state) |
| 1004 | { |
| 1005 | if (state) { |
| 1006 | fdmex->EnableOutput(); |
| 1007 | return true; |
| 1008 | } else { |
| 1009 | fdmex->DisableOutput(); |
| 1010 | return false; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | //Positions |
| 1016 | void FGJSBsim::set_Latitude(double lat) |
| 1017 | { |
| 1018 | double alt = altitude->getDoubleValue(); |
| 1019 | double sea_level_radius_meters, lat_geoc; |
| 1020 | |
| 1021 | if ( alt < -9990 ) alt = 0.0; |
| 1022 | |
| 1023 | SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat ); |
| 1024 | SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt ); |
| 1025 | |
| 1026 | if (needTrim) |
| 1027 | fgic->SetGeodLatitudeRadIC( lat ); |
| 1028 | else { |
| 1029 | sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, |
| 1030 | &sea_level_radius_meters, &lat_geoc ); |
| 1031 | |
| 1032 | double sea_level_radius_ft = sea_level_radius_meters * SG_METER_TO_FEET; |
| 1033 | _set_Sea_level_radius( sea_level_radius_ft ); |
| 1034 | |
| 1035 | Propagate->SetLatitude(lat_geoc); |
| 1036 | } |
| 1037 | |
| 1038 | FGInterface::set_Latitude(lat); |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | void FGJSBsim::set_Longitude(double lon) |
| 1043 | { |
| 1044 | SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon ); |
| 1045 | |
| 1046 | if (needTrim) |
| 1047 | fgic->SetLongitudeRadIC(lon); |
| 1048 | else |
| 1049 | Propagate->SetLongitude(lon); |
| 1050 | |
| 1051 | FGInterface::set_Longitude(lon); |
| 1052 | } |
| 1053 | |
| 1054 | // Sets the altitude above sea level. |
| 1055 | void FGJSBsim::set_Altitude(double alt) |
| 1056 | { |
| 1057 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt ); |
| 1058 | |
| 1059 | if (needTrim) |
| 1060 | fgic->SetAltitudeASLFtIC(alt); |
| 1061 | else |
| 1062 | Propagate->SetAltitudeASL(alt); |
| 1063 | |
| 1064 | FGInterface::set_Altitude(alt); |
| 1065 | } |
| 1066 | |
| 1067 | void FGJSBsim::set_V_calibrated_kts(double vc) |
| 1068 | { |
| 1069 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc ); |
| 1070 | |
| 1071 | if (needTrim) |
| 1072 | fgic->SetVcalibratedKtsIC(vc); |
| 1073 | else { |
| 1074 | double p=pressure->getDoubleValue(); |
| 1075 | double mach = Auxiliary->MachFromVcalibrated(vc, p); |
| 1076 | double temp = 1.8*(temperature->getDoubleValue()+273.15); |
| 1077 | double soundSpeed = sqrt(1.4*1716.0*temp); |
| 1078 | FGColumnVector3 vUVW = Propagate->GetUVW(); |
| 1079 | vUVW.Normalize(); |
| 1080 | vUVW *= mach * soundSpeed; |
| 1081 | Propagate->SetUVW(1, vUVW(1)); |
| 1082 | Propagate->SetUVW(2, vUVW(2)); |
| 1083 | Propagate->SetUVW(3, vUVW(3)); |
| 1084 | } |
| 1085 | |
| 1086 | FGInterface::set_V_calibrated_kts(vc); |
| 1087 | } |
| 1088 | |
| 1089 | void FGJSBsim::set_Mach_number(double mach) |
| 1090 | { |
| 1091 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach ); |
| 1092 | |
| 1093 | if (needTrim) |
| 1094 | fgic->SetMachIC(mach); |
| 1095 | else { |
| 1096 | double temp = 1.8*(temperature->getDoubleValue()+273.15); |
| 1097 | double soundSpeed = sqrt(1.4*1716.0*temp); |
| 1098 | FGColumnVector3 vUVW = Propagate->GetUVW(); |
| 1099 | vUVW.Normalize(); |
| 1100 | vUVW *= mach * soundSpeed; |
| 1101 | Propagate->SetUVW(1, vUVW(1)); |
| 1102 | Propagate->SetUVW(2, vUVW(2)); |
| 1103 | Propagate->SetUVW(3, vUVW(3)); |
| 1104 | } |
| 1105 | |
| 1106 | FGInterface::set_Mach_number(mach); |
| 1107 | } |
| 1108 | |
| 1109 | void FGJSBsim::set_Velocities_Local( double north, double east, double down ) |
| 1110 | { |
| 1111 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: " |
| 1112 | << north << ", " << east << ", " << down ); |
| 1113 | |
| 1114 | if (needTrim) { |
| 1115 | fgic->SetVNorthFpsIC(north); |
| 1116 | fgic->SetVEastFpsIC(east); |
| 1117 | fgic->SetVDownFpsIC(down); |
| 1118 | } |
| 1119 | else { |
| 1120 | FGColumnVector3 vNED(north, east, down); |
| 1121 | FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED; |
| 1122 | Propagate->SetUVW(1, vUVW(1)); |
| 1123 | Propagate->SetUVW(2, vUVW(2)); |
| 1124 | Propagate->SetUVW(3, vUVW(3)); |
| 1125 | } |
| 1126 | |
| 1127 | FGInterface::set_Velocities_Local(north, east, down); |
| 1128 | } |
| 1129 | |
| 1130 | void FGJSBsim::set_Velocities_Body( double u, double v, double w) |
| 1131 | { |
| 1132 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Body: " |
| 1133 | << u << ", " << v << ", " << w ); |
| 1134 | |
| 1135 | if (needTrim) { |
| 1136 | fgic->SetUBodyFpsIC(u); |
| 1137 | fgic->SetVBodyFpsIC(v); |
| 1138 | fgic->SetWBodyFpsIC(w); |
| 1139 | } |
| 1140 | else { |
| 1141 | Propagate->SetUVW(1, u); |
| 1142 | Propagate->SetUVW(2, v); |
| 1143 | Propagate->SetUVW(3, w); |
| 1144 | } |
| 1145 | |
| 1146 | FGInterface::set_Velocities_Body(u, v, w); |
| 1147 | } |
| 1148 | |
| 1149 | //Euler angles |
| 1150 | void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) |
| 1151 | { |
| 1152 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: " |
| 1153 | << phi << ", " << theta << ", " << psi ); |
| 1154 | |
| 1155 | if (needTrim) { |
| 1156 | fgic->SetThetaRadIC(theta); |
| 1157 | fgic->SetPhiRadIC(phi); |
| 1158 | fgic->SetPsiRadIC(psi); |
| 1159 | } |
| 1160 | else { |
| 1161 | FGQuaternion quat(phi, theta, psi); |
| 1162 | FGMatrix33 Tl2b = quat.GetT(); |
| 1163 | FGMatrix33 Ti2b = Tl2b*Propagate->GetTi2l(); |
| 1164 | FGQuaternion Qi = Ti2b.GetQuaternion(); |
| 1165 | Propagate->SetInertialOrientation(Qi); |
| 1166 | } |
| 1167 | |
| 1168 | FGInterface::set_Euler_Angles(phi, theta, psi); |
| 1169 | } |
| 1170 | |
| 1171 | //Flight Path |
| 1172 | void FGJSBsim::set_Climb_Rate( double roc) |
| 1173 | { |
| 1174 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc ); |
| 1175 | |
| 1176 | //since both climb rate and flight path angle are set in the FG |
| 1177 | //startup sequence, something is needed to keep one from cancelling |
| 1178 | //out the other. |
| 1179 | if( fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01 ) { |
| 1180 | if (needTrim) |
| 1181 | fgic->SetClimbRateFpsIC(roc); |
| 1182 | else { |
| 1183 | FGColumnVector3 vNED = Propagate->GetVel(); |
| 1184 | vNED(FGJSBBase::eDown) = -roc; |
| 1185 | FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED; |
| 1186 | Propagate->SetUVW(1, vUVW(1)); |
| 1187 | Propagate->SetUVW(2, vUVW(2)); |
| 1188 | Propagate->SetUVW(3, vUVW(3)); |
| 1189 | } |
| 1190 | |
| 1191 | FGInterface::set_Climb_Rate(roc); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | void FGJSBsim::set_Gamma_vert_rad( double gamma) |
| 1196 | { |
| 1197 | SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma ); |
| 1198 | |
| 1199 | if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) { |
| 1200 | if (needTrim) |
| 1201 | fgic->SetFlightPathAngleRadIC(gamma); |
| 1202 | else { |
| 1203 | FGColumnVector3 vNED = Propagate->GetVel(); |
| 1204 | double vt = vNED.Magnitude(); |
| 1205 | vNED(FGJSBBase::eDown) = -vt * sin(gamma); |
| 1206 | FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED; |
| 1207 | Propagate->SetUVW(1, vUVW(1)); |
| 1208 | Propagate->SetUVW(2, vUVW(2)); |
| 1209 | Propagate->SetUVW(3, vUVW(3)); |
| 1210 | } |
| 1211 | |
| 1212 | FGInterface::set_Gamma_vert_rad(gamma); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | void FGJSBsim::init_gear(void ) |
| 1217 | { |
| 1218 | FGGroundReactions* gr=fdmex->GetGroundReactions(); |
| 1219 | int Ngear=GroundReactions->GetNumGearUnits(); |
| 1220 | double max = 1.0; |
| 1221 | for (int i=0;i<Ngear;i++) { |
| 1222 | FGLGear *gear = gr->GetGearUnit(i); |
| 1223 | SGPropertyNode * node = fgGetNode("gear/gear", i, true); |
| 1224 | // coordinates are in feet so convert |
| 1225 | node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1) * 12); |
| 1226 | node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2) * 12); |
| 1227 | node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3) * 12); |
| 1228 | |
| 1229 | node->setDoubleValue("xoffset-m", gear->GetBodyLocation()(1) * SG_FEET_TO_METER); |
| 1230 | node->setDoubleValue("yoffset-m", gear->GetBodyLocation()(2) * SG_FEET_TO_METER); |
| 1231 | node->setDoubleValue("zoffset-m", gear->GetBodyLocation()(3) * SG_FEET_TO_METER); |
| 1232 | |
| 1233 | node->setBoolValue("wow", gear->GetWOW()); |
| 1234 | node->setDoubleValue("rollspeed-ms", gear->GetWheelRollVel() * SG_FEET_TO_METER); |
| 1235 | node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0); |
| 1236 | node->setDoubleValue("position-norm", gear->GetGearUnitPos()); |
| 1237 | // node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure()); |
| 1238 | max = node->getDoubleValue("max-compression-ft"); |
| 1239 | if (max < 0.00001) { |
| 1240 | max = node->getDoubleValue("max-compression-m"); |
| 1241 | if (max < 0.00001) max = 1.0; |
| 1242 | else max /= SG_FEET_TO_METER; |
| 1243 | } |
| 1244 | node->setDoubleValue("compression-norm", gear->GetCompLen() / max); |
| 1245 | node->setDoubleValue("compression-m", gear->GetCompLen() * SG_FEET_TO_METER); |
| 1246 | node->setDoubleValue("compression-ft", gear->GetCompLen()); |
| 1247 | if ( gear->GetSteerable() ) |
| 1248 | node->setDoubleValue("steering-norm", gear->GetSteerNorm()); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void FGJSBsim::update_gear(void) |
| 1253 | { |
| 1254 | FGGroundReactions* gr=fdmex->GetGroundReactions(); |
| 1255 | int Ngear=GroundReactions->GetNumGearUnits(); |
| 1256 | double max = 1.0; |
| 1257 | for (int i=0;i<Ngear;i++) { |
| 1258 | FGLGear *gear = gr->GetGearUnit(i); |
| 1259 | SGPropertyNode * node = fgGetNode("gear/gear", i, true); |
| 1260 | node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW()); |
| 1261 | node->getChild("rollspeed-ms", 0, true)->setDoubleValue(gear->GetWheelRollVel() * SG_FEET_TO_METER); |
| 1262 | node->getChild("position-norm", 0, true)->setDoubleValue(gear->GetGearUnitPos()); |
| 1263 | // gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm")); |
| 1264 | max = node->getDoubleValue("max-compression-ft"); |
| 1265 | if (max < 0.00001) { |
| 1266 | max = node->getDoubleValue("max-compression-m"); |
| 1267 | if (max < 0.00001) max = 1.0; |
| 1268 | else max /= SG_FEET_TO_METER; |
| 1269 | } |
| 1270 | node->setDoubleValue("compression-norm", gear->GetCompLen() / max); |
| 1271 | node->setDoubleValue("compression-m", gear->GetCompLen() * SG_FEET_TO_METER); |
| 1272 | node->setDoubleValue("compression-ft", gear->GetCompLen()); |
| 1273 | if ( gear->GetSteerable() ) |
| 1274 | node->setDoubleValue("steering-norm", gear->GetSteerNorm()); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | void FGJSBsim::do_trim(void) |
| 1279 | { |
| 1280 | FGTrim *fgtrim; |
| 1281 | |
| 1282 | if ( fgGetBool("/sim/presets/onground") ) |
| 1283 | { |
| 1284 | fgtrim = new FGTrim(fdmex,tGround); |
| 1285 | } else { |
| 1286 | fgtrim = new FGTrim(fdmex,tFull); |
| 1287 | } |
| 1288 | |
| 1289 | if ( !fgtrim->DoTrim() ) { |
| 1290 | fgtrim->Report(); |
| 1291 | fgtrim->TrimStats(); |
| 1292 | } else { |
| 1293 | trimmed->setBoolValue(true); |
| 1294 | } |
| 1295 | delete fgtrim; |
| 1296 | |
| 1297 | pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() ); |
| 1298 | throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) ); |
| 1299 | aileron_trim->setDoubleValue( FCS->GetDaCmd() ); |
| 1300 | rudder_trim->setDoubleValue( -FCS->GetDrCmd() ); |
| 1301 | |
| 1302 | globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd()); |
| 1303 | globals->get_controls()->set_elevator(FCS->GetDeCmd()); |
| 1304 | for( unsigned i = 0; i < Propulsion->GetNumEngines(); i++ ) |
| 1305 | globals->get_controls()->set_throttle(i, FCS->GetThrottleCmd(i)); |
| 1306 | |
| 1307 | globals->get_controls()->set_aileron(FCS->GetDaCmd()); |
| 1308 | globals->get_controls()->set_rudder( -FCS->GetDrCmd()); |
| 1309 | |
| 1310 | SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" ); |
| 1311 | } |
| 1312 | |
| 1313 | bool FGJSBsim::update_ground_cache(const FGLocation& cart, double dt) |
| 1314 | { |
| 1315 | // Compute the radius of the aircraft. That is the radius of a ball |
| 1316 | // where all gear units are in. At the moment it is at least 10ft ... |
| 1317 | double acrad = 10.0; |
| 1318 | int n_gears = GroundReactions->GetNumGearUnits(); |
| 1319 | for (int i=0; i<n_gears; ++i) { |
| 1320 | FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation(); |
| 1321 | double r = bl.Magnitude(); |
| 1322 | if (acrad < r) |
| 1323 | acrad = r; |
| 1324 | } |
| 1325 | |
| 1326 | // Compute the potential movement of this aircraft and query for the |
| 1327 | // ground in this area. |
| 1328 | double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude(); |
| 1329 | |
| 1330 | double cart_pos[3] {cart(1), cart(2), cart(3)}; |
| 1331 | double t0 = fdmex->GetSimTime(); |
| 1332 | bool cache_ok = prepare_ground_cache_ft( t0, t0 + dt, cart_pos, |
| 1333 | groundCacheRadius ); |
| 1334 | if (!cache_ok) { |
| 1335 | //SG_LOG(SG_FLIGHT, SG_WARN, |
| 1336 | // "FGInterface is being called without scenery below the aircraft!"); |
| 1337 | |
| 1338 | //SG_LOG(SG_FLIGHT, SG_WARN, "altitude = " |
| 1339 | // << fgic->GetAltitudeASLFtIC()); |
| 1340 | |
| 1341 | //SG_LOG(SG_FLIGHT, SG_WARN, "sea level radius = " |
| 1342 | // << get_Sea_level_radius()); |
| 1343 | |
| 1344 | //SG_LOG(SG_FLIGHT, SG_WARN, "latitude = " |
| 1345 | // << fgic->GetLatitudeRadIC()); |
| 1346 | |
| 1347 | //SG_LOG(SG_FLIGHT, SG_WARN, "longitude = " |
| 1348 | // << fgic->GetLongitudeRadIC()); |
| 1349 | } |
| 1350 | |
| 1351 | return cache_ok; |
| 1352 | } |
| 1353 | |
| 1354 | double |
| 1355 | FGJSBsim::get_agl_ft(double t, const FGColumnVector3& loc, double alt_off, |
| 1356 | double contact[3], double normal[3], double vel[3], |
| 1357 | double angularVel[3]) |
| 1358 | { |
| 1359 | const simgear::BVHMaterial* material = nullptr; |
| 1360 | simgear::BVHNode::Id id; |
| 1361 | double pt[3] {loc(1), loc(2), loc(3)}; |
| 1362 | |
| 1363 | if (!FGInterface::get_agl_ft(t, pt, alt_off, contact, normal, vel, |
| 1364 | angularVel, material, id)) |
| 1365 | material = nullptr; // Discard the material data when FGInterface reports an problem. |
| 1366 | |
| 1367 | SGGeod geodPt = SGGeod::fromCart(SG_FEET_TO_METER*SGVec3d(pt)); |
| 1368 | SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt); |
| 1369 | |
| 1370 | #ifdef JSBSIM_USE_GROUNDREACTIONS |
| 1371 | bool terrain_active = (terrain->getIntValue("override-level", -1) > 0) ? false : true; |
| 1372 | terrain->setBoolValue("active", terrain_active); |
| 1373 | terrain->setBoolValue("valid", (material && terrain_active) ? true : false); |
| 1374 | if (terrain_active) |
| 1375 | { |
| 1376 | static bool material_valid = false; |
| 1377 | |
| 1378 | if (updateGroundReactions(material)) { |
| 1379 | setPosition(pt); |
| 1380 | setHeading(Propagate->GetEuler(FGJSBBase::ePsi)); |
| 1381 | GroundReactions->SetRollingFFactor(getRolingFrictionFactor()); |
| 1382 | GroundReactions->SetStaticFFactor(getStaticFrictionFactor()); |
| 1383 | GroundReactions->SetMaximumForce(getPressure()*0.00014503773800721815); |
| 1384 | GroundReactions->SetBumpiness(getBumpiness()); |
| 1385 | GroundReactions->SetSolid(getSolid()); |
| 1386 | GroundReactions->SetPosition(pt); |
| 1387 | material_valid = true; |
| 1388 | } else { |
| 1389 | if (material_valid) { |
| 1390 | GroundReactions->resetValues(); |
| 1391 | material_valid = false; |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | #else |
| 1396 | terrain->setBoolValue("valid", false); |
| 1397 | #endif |
| 1398 | return dot(hlToEc.rotate(SGVec3d(0, 0, 1)), SGVec3d(contact) - SGVec3d(pt)) + |
| 1399 | getGroundDisplacement(); |
| 1400 | } |
| 1401 | |
| 1402 | inline static double sqr(double x) |
| 1403 | { |
| 1404 | return x * x; |
| 1405 | } |
| 1406 | |
| 1407 | static double angle_diff(double a, double b) |
| 1408 | { |
| 1409 | double diff = fabs(a - b); |
| 1410 | if (diff > 180) diff = 360 - diff; |
| 1411 | |
| 1412 | return diff; |
| 1413 | } |
| 1414 | |
| 1415 | static void check_hook_solution(const FGColumnVector3& ground_normal_body, double E, double hook_length, double sin_fi_guess, double cos_fi_guess, double* sin_fis, double* cos_fis, double* fis, int* points) |
| 1416 | { |
| 1417 | FGColumnVector3 tip(-hook_length * cos_fi_guess, 0, hook_length * sin_fi_guess); |
| 1418 | double dist = DotProduct(tip, ground_normal_body); |
| 1419 | if (fabs(dist + E) < 0.0001) { |
| 1420 | sin_fis[*points] = sin_fi_guess; |
| 1421 | cos_fis[*points] = cos_fi_guess; |
| 1422 | fis[*points] = atan2(sin_fi_guess, cos_fi_guess) * SG_RADIANS_TO_DEGREES; |
| 1423 | (*points)++; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | static void check_hook_solution(const FGColumnVector3& ground_normal_body, double E, double hook_length, double sin_fi_guess, double* sin_fis, double* cos_fis, double* fis, int* points) |
| 1429 | { |
| 1430 | if (sin_fi_guess >= -1 && sin_fi_guess <= 1) { |
| 1431 | double cos_fi_guess = sqrt(1 - sqr(sin_fi_guess)); |
| 1432 | check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, cos_fi_guess, sin_fis, cos_fis, fis, points); |
| 1433 | if (fabs(cos_fi_guess) > SG_EPSILON) { |
| 1434 | check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, -cos_fi_guess, sin_fis, cos_fis, fis, points); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | void FGJSBsim::update_external_forces(double t_off) |
| 1440 | { |
| 1441 | const FGMatrix33& Tb2l = Propagate->GetTb2l(); |
| 1442 | const FGMatrix33& Tl2b = Propagate->GetTl2b(); |
| 1443 | const FGLocation& Location = Propagate->GetLocation(); |
| 1444 | const FGMatrix33& Tec2l = Location.GetTec2l(); |
| 1445 | |
| 1446 | double hook_area[4][3]; |
| 1447 | |
| 1448 | FGColumnVector3 hook_root_body = MassBalance->StructuralToBody(hook_root_struct); |
| 1449 | FGColumnVector3 hook_root = Location.LocalToLocation(Tb2l * hook_root_body); |
| 1450 | hook_area[1][0] = hook_root(1); |
| 1451 | hook_area[1][1] = hook_root(2); |
| 1452 | hook_area[1][2] = hook_root(3); |
| 1453 | |
| 1454 | hook_length = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-length-ft", 6.75); |
| 1455 | double fi_min = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-min-deg", -18); |
| 1456 | double fi_max = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-max-deg", 30); |
| 1457 | double fi = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-norm") * (fi_max - fi_min) + fi_min; |
| 1458 | double cos_fi = cos(fi * SG_DEGREES_TO_RADIANS); |
| 1459 | double sin_fi = sin(fi * SG_DEGREES_TO_RADIANS); |
| 1460 | |
| 1461 | FGColumnVector3 hook_tip_body = hook_root_body; |
| 1462 | hook_tip_body(1) -= hook_length * cos_fi; |
| 1463 | hook_tip_body(3) += hook_length * sin_fi; |
| 1464 | if (!arrestor_wire_engaged_hook->getBoolValue()) { |
| 1465 | double contact[3]; |
| 1466 | double ground_normal[3]; |
| 1467 | double ground_vel[3]; |
| 1468 | double ground_angular_vel[3]; |
| 1469 | double root_agl_ft = get_agl_ft(t_off, hook_root, 0, contact, |
| 1470 | ground_normal, ground_vel, |
| 1471 | ground_angular_vel); |
| 1472 | if (root_agl_ft > 0 && root_agl_ft < hook_length) { |
| 1473 | FGColumnVector3 ground_normal_body = Tl2b * (Tec2l * FGColumnVector3(ground_normal[0], ground_normal[1], ground_normal[2])); |
| 1474 | FGColumnVector3 contact_body = Tl2b * Location.LocationToLocal(FGColumnVector3(contact[0], contact[1], contact[2])); |
| 1475 | double D = -DotProduct(contact_body, ground_normal_body); |
| 1476 | |
| 1477 | // check hook tip agl against same ground plane |
| 1478 | double hook_tip_agl_ft = DotProduct(hook_tip_body, ground_normal_body) + D; |
| 1479 | if (hook_tip_agl_ft < 0) { |
| 1480 | |
| 1481 | // hook tip: hx - l cos, hy, hz + l sin |
| 1482 | // on ground: - n0 l cos + n2 l sin + E = 0 |
| 1483 | |
| 1484 | double E = D + DotProduct(hook_root_body, ground_normal_body); |
| 1485 | |
| 1486 | // substitue x = sin fi, cos fi = sqrt(1 - x * x) |
| 1487 | // and rearrange to get a quadratic with coeffs: |
| 1488 | double a = sqr(hook_length) * (sqr(ground_normal_body(1)) + sqr(ground_normal_body(3))); |
| 1489 | double b = 2 * E * ground_normal_body(3) * hook_length; |
| 1490 | double c = sqr(E) - sqr(ground_normal_body(1) * hook_length); |
| 1491 | |
| 1492 | double disc = sqr(b) - 4 * a * c; |
| 1493 | if (disc >= 0) { |
| 1494 | double delta = sqrt(disc) / (2 * a); |
| 1495 | |
| 1496 | // allow 4 solutions for safety, should never happen |
| 1497 | double sin_fis[4]; |
| 1498 | double cos_fis[4]; |
| 1499 | double fis[4]; |
| 1500 | int points = 0; |
| 1501 | |
| 1502 | double sin_fi_guess = -b / (2 * a) - delta; |
| 1503 | check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, sin_fis, cos_fis, fis, &points); |
| 1504 | check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess + 2 * delta, sin_fis, cos_fis, fis, &points); |
| 1505 | |
| 1506 | if (points == 2) { |
| 1507 | double diff1 = angle_diff(fi, fis[0]); |
| 1508 | double diff2 = angle_diff(fi, fis[1]); |
| 1509 | int point = diff1 < diff2 ? 0 : 1; |
| 1510 | fi = fis[point]; |
| 1511 | sin_fi = sin_fis[point]; |
| 1512 | cos_fi = cos_fis[point]; |
| 1513 | hook_tip_body(1) = hook_root_body(1) - hook_length * cos_fi; |
| 1514 | hook_tip_body(3) = hook_root_body(3) + hook_length * sin_fi; |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | } else { |
| 1520 | if (release_hook->getBoolValue()) { |
| 1521 | release_wire(); |
| 1522 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", 0.0); |
| 1523 | arrestor_wire_engaged_hook->setBoolValue(false); |
| 1524 | release_hook->setBoolValue(false); |
| 1525 | } else { |
| 1526 | FGColumnVector3 hook_root_vel = Propagate->GetVel() + (Tb2l * (Propagate->GetPQR() * hook_root_body)); |
| 1527 | double wire_ends_ec[2][3]; |
| 1528 | double wire_vel_ec[2][3]; |
| 1529 | get_wire_ends_ft(t_off, wire_ends_ec, wire_vel_ec); |
| 1530 | FGColumnVector3 wire_vel_1 = Tec2l * FGColumnVector3(wire_vel_ec[0][0], wire_vel_ec[0][1], wire_vel_ec[0][2]); |
| 1531 | FGColumnVector3 wire_vel_2 = Tec2l * FGColumnVector3(wire_vel_ec[1][0], wire_vel_ec[1][1], wire_vel_ec[1][2]); |
| 1532 | FGColumnVector3 rel_vel = hook_root_vel - (wire_vel_1 + wire_vel_2) / 2; |
| 1533 | if (rel_vel.Magnitude() < 3) { |
| 1534 | release_wire(); |
| 1535 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", 0.0); |
| 1536 | arrestor_wire_engaged_hook->setBoolValue(false); |
| 1537 | release_hook->setBoolValue(false); |
| 1538 | } |
| 1539 | else { |
| 1540 | FGColumnVector3 wire_end1_body = Tl2b * Location.LocationToLocal(FGColumnVector3(wire_ends_ec[0][0], wire_ends_ec[0][1], wire_ends_ec[0][2])) - hook_root_body; |
| 1541 | FGColumnVector3 wire_end2_body = Tl2b * Location.LocationToLocal(FGColumnVector3(wire_ends_ec[1][0], wire_ends_ec[1][1], wire_ends_ec[1][2])) - hook_root_body; |
| 1542 | FGColumnVector3 force_plane_normal = wire_end1_body * wire_end2_body; |
| 1543 | force_plane_normal.Normalize(); |
| 1544 | cos_fi = DotProduct(force_plane_normal, FGColumnVector3(0, 0, 1)); |
| 1545 | if (cos_fi < 0) cos_fi = -cos_fi; |
| 1546 | sin_fi = sqrt(1 - sqr(cos_fi)); |
| 1547 | fi = atan2(sin_fi, cos_fi) * SG_RADIANS_TO_DEGREES; |
| 1548 | |
| 1549 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/x", -cos_fi); |
| 1550 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/y", 0); |
| 1551 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/z", sin_fi); |
| 1552 | fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", fgGetDouble("/fdm/jsbsim/systems/hook/force")); |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | FGColumnVector3 hook_tip = Location.LocalToLocation(Tb2l * hook_tip_body); |
| 1558 | |
| 1559 | hook_area[0][0] = hook_tip(1); |
| 1560 | hook_area[0][1] = hook_tip(2); |
| 1561 | hook_area[0][2] = hook_tip(3); |
| 1562 | |
| 1563 | if (!arrestor_wire_engaged_hook->getBoolValue()) { |
| 1564 | // The previous positions. |
| 1565 | hook_area[2][0] = last_hook_root[0]; |
| 1566 | hook_area[2][1] = last_hook_root[1]; |
| 1567 | hook_area[2][2] = last_hook_root[2]; |
| 1568 | hook_area[3][0] = last_hook_tip[0]; |
| 1569 | hook_area[3][1] = last_hook_tip[1]; |
| 1570 | hook_area[3][2] = last_hook_tip[2]; |
| 1571 | |
| 1572 | // Check if we caught a wire. |
| 1573 | // Returns true if we caught one. |
| 1574 | if (caught_wire_ft(t_off, hook_area)) { |
| 1575 | arrestor_wire_engaged_hook->setBoolValue(true); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | // save actual position as old position ... |
| 1580 | last_hook_tip[0] = hook_area[0][0]; |
| 1581 | last_hook_tip[1] = hook_area[0][1]; |
| 1582 | last_hook_tip[2] = hook_area[0][2]; |
| 1583 | last_hook_root[0] = hook_area[1][0]; |
| 1584 | last_hook_root[1] = hook_area[1][1]; |
| 1585 | last_hook_root[2] = hook_area[1][2]; |
| 1586 | |
| 1587 | fgSetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-deg", fi); |
| 1588 | |
| 1589 | if (_ai_wake_enabled->getBoolValue()) { |
| 1590 | FGColumnVector3 uvw = Propagate->GetUVW(); |
| 1591 | FGQuaternion ql2b = Propagate->GetQuaternion(); |
| 1592 | FGLocation l = Auxiliary->GetLocationVRP(); |
| 1593 | SGVec3d cartPos(l(1), l(2), l(3)); |
| 1594 | // The scalar component is the first component in FGQuaternion but |
| 1595 | // the last component in SGQuat. |
| 1596 | SGQuatd orient(ql2b(2), ql2b(3), ql2b(4), ql2b(1)); |
| 1597 | |
| 1598 | mesh->setPosition(cartPos * SG_FEET_TO_METER, orient); |
| 1599 | |
| 1600 | SGVec3d f = mesh->GetForce(get_wake_group(), |
| 1601 | SGVec3d(uvw(1), uvw(2), uvw(3)), |
| 1602 | Atmosphere->GetDensity()); |
| 1603 | const SGVec3d& m = mesh->GetMoment(); |
| 1604 | |
| 1605 | if (_fmag) _fmag->setDoubleValue(1.0); |
| 1606 | if (_fbx) _fbx->setDoubleValue(f[0]); |
| 1607 | if (_fby) _fby->setDoubleValue(f[1]); |
| 1608 | if (_fbz) _fbz->setDoubleValue(f[2]); |
| 1609 | |
| 1610 | if (_mmag) _mmag->setDoubleValue(1.0); |
| 1611 | if (_mbx) _mbx->setDoubleValue(m[0]); |
| 1612 | if (_mby) _mby->setDoubleValue(m[1]); |
| 1613 | if (_mbz) _mbz->setDoubleValue(m[2]); |
| 1614 | } |
| 1615 | else { |
| 1616 | if (_fmag) _fmag->setDoubleValue(0.0); |
| 1617 | if (_mmag) _mmag->setDoubleValue(0.0); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | // Register the subsystem. |
| 1623 | #if 0 |
| 1624 | SGSubsystemMgr::Registrant<FGJSBsim> registrantFGJSBsim; |
| 1625 | #endif |