| 1 | |
| 2 | #ifdef HAVE_CONFIG_H |
| 3 | # include "config.h" |
| 4 | #endif |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <cstdio> |
| 8 | |
| 9 | #include <simgear/debug/logstream.hxx> |
| 10 | #include <simgear/math/sg_geodesy.hxx> |
| 11 | #include <simgear/misc/sg_path.hxx> |
| 12 | #include <simgear/scene/model/placement.hxx> |
| 13 | #include <simgear/xml/easyxml.hxx> |
| 14 | |
| 15 | #include <Main/globals.hxx> |
| 16 | #include <Main/fg_props.hxx> |
| 17 | |
| 18 | #include "yasim-common.hpp" |
| 19 | #include "FGFDM.hpp" |
| 20 | #include "Atmosphere.hpp" |
| 21 | #include "Math.hpp" |
| 22 | #include "Airplane.hpp" |
| 23 | #include "Model.hpp" |
| 24 | #include "Integrator.hpp" |
| 25 | #include "Glue.hpp" |
| 26 | #include "Gear.hpp" |
| 27 | #include "Hook.hpp" |
| 28 | #include "Launchbar.hpp" |
| 29 | #include "FGGround.hpp" |
| 30 | #include "PropEngine.hpp" |
| 31 | #include "PistonEngine.hpp" |
| 32 | |
| 33 | #include "YASim.hxx" |
| 34 | |
| 35 | using namespace yasim; |
| 36 | using std::string; |
| 37 | |
| 38 | YASim::YASim(double dt) : |
| 39 | _simTime(0) |
| 40 | { |
| 41 | // set_delta_t(dt); |
| 42 | _fdm = new FGFDM(); |
| 43 | |
| 44 | _dt = dt; |
| 45 | |
| 46 | _fdm->getAirplane()->getModel()->setGroundCallback( new FGGround(this) ); |
| 47 | _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt); |
| 48 | } |
| 49 | |
| 50 | YASim::~YASim() |
| 51 | { |
| 52 | delete _fdm; |
| 53 | |
| 54 | _gearProps.clear(); |
| 55 | } |
| 56 | |
| 57 | void YASim::property_associations( |
| 58 | std::function<void(const std::string& from, const std::string& to)> fn |
| 59 | ) |
| 60 | { |
| 61 | return _fdm->property_associations(fn); |
| 62 | } |
| 63 | |
| 64 | void YASim::report() |
| 65 | { |
| 66 | Airplane* a = _fdm->getAirplane(); |
| 67 | |
| 68 | float aoa = a->getCruiseAoA() * RAD2DEG; |
| 69 | float tail = -1 * a->getTailIncidence() * RAD2DEG; |
| 70 | float drag = 1000 * a->getDragCoefficient(); |
| 71 | |
| 72 | SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:"); |
| 73 | SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations()); |
| 74 | SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag); |
| 75 | SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio()); |
| 76 | SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa); |
| 77 | SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail); |
| 78 | SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<<a->getApproachElevator()); |
| 79 | |
| 80 | float cg[3]; |
| 81 | char buf[256]; |
| 82 | a->getModel()->getBody()->getCG(cg); |
| 83 | snprintf(buf, 256, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]); |
| 84 | SG_LOG(SG_FLIGHT, SG_INFO, buf); |
| 85 | |
| 86 | if(a->getFailureMsg()) { |
| 87 | SG_LOG(SG_FLIGHT, SG_POPUP, std::string("YASim flight dynamics problem:") + a->getFailureMsg()+"\nThe aircraft may not fly correctly"); |
| 88 | //throw sg_error(std::string("YASim SOLUTION FAILURE:") + a->getFailureMsg();); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void YASim::bind() |
| 93 | { |
| 94 | // Run the superclass bind to set up a bunch of property ties |
| 95 | FGInterface::bind(); |
| 96 | |
| 97 | //Torsten Dreyer: we shouldn't do this anymore because we don't set these values nomore |
| 98 | // Now UNtie the ones that we are going to set ourselves. |
| 99 | // fgUntie("/consumables/fuel/tank[0]/level-gal_us"); |
| 100 | // fgUntie("/consumables/fuel/tank[1]/level-gal_us"); |
| 101 | |
| 102 | char buf[256]; |
| 103 | for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) { |
| 104 | snprintf(buf, 256, "/engines/engine[%d]/fuel-flow-gph", i); |
| 105 | fgUntieIfDefined(buf); |
| 106 | snprintf(buf, 256, "/engines/engine[%d]/rpm", i); |
| 107 | fgUntieIfDefined(buf); |
| 108 | snprintf(buf, 256, "/engines/engine[%d]/mp-osi", i); |
| 109 | fgUntieIfDefined(buf); |
| 110 | snprintf(buf, 256, "/engines/engine[%d]/egt-degf", i); |
| 111 | fgUntieIfDefined(buf); |
| 112 | snprintf(buf, 256, "/engines/engine[%d]/oil-temperature-degf", i); |
| 113 | fgUntieIfDefined(buf); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | YASim::GearProps::GearProps(SGPropertyNode_ptr gear_root) : |
| 118 | has_brake(gear_root->getNode("has-brake", true)), |
| 119 | wow(gear_root->getNode("wow", true)), |
| 120 | compression_norm(gear_root->getNode("compression-norm", true)), |
| 121 | compression_m(gear_root->getNode("compression-m", true)), |
| 122 | caster_angle_deg(gear_root->getNode("caster-angle-deg", true)), |
| 123 | rollspeed_ms(gear_root->getNode("rollspeed-ms", true)), |
| 124 | ground_is_solid(gear_root->getNode("ground-is-solid", true)), |
| 125 | ground_friction_factor(gear_root->getNode("ground-friction-factor", true)) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | void YASim::init() |
| 130 | { |
| 131 | Airplane* airplane = _fdm->getAirplane(); |
| 132 | Model* model = airplane->getModel(); |
| 133 | |
| 134 | _crashed = fgGetNode("/sim/crashed", true); |
| 135 | _pressure_inhg = fgGetNode("/environment/pressure-inhg", true); |
| 136 | _temp_degc = fgGetNode("/environment/temperature-degc", true); |
| 137 | _density_slugft3 = fgGetNode("/environment/density-slugft3", true); |
| 138 | _gear_agl_m = fgGetNode("/position/gear-agl-m", true); |
| 139 | _gear_agl_ft = fgGetNode("/position/gear-agl-ft", true); |
| 140 | _pilot_g = fgGetNode("/accelerations/pilot-g", true); |
| 141 | _speed_setprop = fgGetNode("/sim/presets/speed-set", true); |
| 142 | |
| 143 | // Superclass hook |
| 144 | common_init(); |
| 145 | |
| 146 | model->setCrashed(false); |
| 147 | _crashed->setBoolValue(false); |
| 148 | |
| 149 | // Figure out the initial speed type |
| 150 | string speed_set = _speed_setprop->getStringValue(); |
| 151 | if ((speed_set == "") || (speed_set == "UVW")) |
| 152 | _speed_set = UVW; |
| 153 | else if (speed_set == "NED") |
| 154 | _speed_set = NED; |
| 155 | else if (speed_set == "knots") |
| 156 | _speed_set = KNOTS; |
| 157 | else if (speed_set == "mach") |
| 158 | _speed_set = MACH; |
| 159 | else { |
| 160 | _speed_set = UVW; |
| 161 | SG_LOG(SG_FLIGHT, SG_ALERT, "Unknown speed type " << speed_set); |
| 162 | } |
| 163 | |
| 164 | // Build a filename and parse it |
| 165 | SGPath f(fgGetString("/sim/aircraft-dir")); |
| 166 | f.append(fgGetString("/sim/aero")); |
| 167 | f.concat(".xml"); |
| 168 | try { |
| 169 | readXML(f, *_fdm); |
| 170 | } catch (const sg_exception &e) { |
| 171 | SG_LOG(SG_FLIGHT, SG_ALERT, |
| 172 | "Error reading YASim FDM: '" << f << "'" << std::endl |
| 173 | << e.getFormattedMessage()); |
| 174 | throw e; |
| 175 | } |
| 176 | |
| 177 | // Compile it into a real airplane, and tell the user what they got |
| 178 | airplane->compile(); |
| 179 | report(); |
| 180 | |
| 181 | _fdm->init(); |
| 182 | |
| 183 | if (model->getLaunchbar()) |
| 184 | { |
| 185 | _catapult_launch_cmd = fgGetNode("/controls/gear/catapult-launch-cmd", true); |
| 186 | _launchbar_position_norm = fgGetNode("/gear/launchbar/position-norm", true); |
| 187 | _launchbar_holdback_pos_norm = fgGetNode("/gear/launchbar/holdback-position-norm", true); |
| 188 | _launchbar_state = fgGetNode("/gear/launchbar/state", true); |
| 189 | _launchbar_strop = fgGetNode("/gear/launchbar/strop", true); |
| 190 | } |
| 191 | if (airplane->getHook()) |
| 192 | { |
| 193 | _tailhook_position_norm = fgGetNode("/gear/tailhook/position-norm", 0, true); |
| 194 | } |
| 195 | |
| 196 | // Create some FG{Eng|Gear}Interface objects |
| 197 | for(int i=0; i<airplane->numGear(); i++) { |
| 198 | Gear* g = airplane->getGear(i); |
| 199 | SGPropertyNode * node = fgGetNode("gear/gear", i, true); |
| 200 | float pos[3]; |
| 201 | g->getPosition(pos); |
| 202 | node->setDoubleValue("xoffset-in", pos[0] * M2FT * 12); |
| 203 | node->setDoubleValue("yoffset-in", pos[1] * M2FT * 12); |
| 204 | node->setDoubleValue("zoffset-in", pos[2] * M2FT * 12); |
| 205 | |
| 206 | node->setDoubleValue("xoffset-m", pos[0]); |
| 207 | node->setDoubleValue("yoffset-m", pos[1]); |
| 208 | node->setDoubleValue("zoffset-m", pos[2]); |
| 209 | |
| 210 | node->setDoubleValue("wheel-radius-m", g->getWheelRadius()); |
| 211 | node->setDoubleValue("tyre-radius-m", g->getTyreRadius()); |
| 212 | |
| 213 | node->setDoubleValue("spring", g->getSpring()); |
| 214 | node->setDoubleValue("damping", g->getDamping()); |
| 215 | |
| 216 | _gearProps.push_back(GearProps(node)); |
| 217 | } |
| 218 | |
| 219 | // Are we at ground level? If so, lift the plane up so the gear |
| 220 | // clear the ground. |
| 221 | bool respect_external_gear_state = fgGetBool("/fdm/yasim/respect-external-gear-state"); |
| 222 | double runway_altitude = get_Runway_altitude(); |
| 223 | if(get_Altitude() - runway_altitude < 50) { |
| 224 | bool gear_state = fgGetBool("/controls/gear/gear-down"); |
| 225 | fgSetBool("/controls/gear/gear-down", false); |
| 226 | float minGearZ = 1e18; |
| 227 | for(int i=0; i<airplane->numGear(); i++) { |
| 228 | Gear* g = airplane->getGear(i); |
| 229 | float pos[3]; |
| 230 | g->getPosition(pos); |
| 231 | pos[2] -= (g->getWheelRadius() + g->getTyreRadius()); |
| 232 | if(pos[2] < minGearZ) |
| 233 | minGearZ = pos[2]; |
| 234 | } |
| 235 | _set_Altitude(runway_altitude - minGearZ*M2FT); |
| 236 | // ground start-up: gear down |
| 237 | fgSetBool("/controls/gear/gear-down", respect_external_gear_state ? gear_state : true); |
| 238 | } |
| 239 | else if (! respect_external_gear_state) |
| 240 | { |
| 241 | // airborne start-up: gear up |
| 242 | fgSetBool("/controls/gear/gear-down", false); |
| 243 | } |
| 244 | |
| 245 | // Blank the state, and copy in ours |
| 246 | yasim::State s; |
| 247 | model->setState(&s); |
| 248 | copyToYASim(true); |
| 249 | |
| 250 | _fdm->getExternalInput(); |
| 251 | _fdm->getAirplane()->initEngines(); |
| 252 | |
| 253 | set_inited(true); |
| 254 | } |
| 255 | |
| 256 | void YASim::update(double dt) |
| 257 | { |
| 258 | if (is_suspended()) |
| 259 | return; |
| 260 | |
| 261 | int iterations = _calc_multiloop(dt); |
| 262 | |
| 263 | // If we're crashed, then we don't care |
| 264 | if(_crashed->getBoolValue() || _fdm->getAirplane()->getModel()->isCrashed()) { |
| 265 | if(!_crashed->getBoolValue()) |
| 266 | _crashed->setBoolValue(true); |
| 267 | _fdm->getAirplane()->getModel()->setCrashed(false); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // ground. Calculate a cartesian coordinate for the ground under |
| 272 | // us, find the (geodetic) up vector normal to the ground, then |
| 273 | // use that to find the final (radius) term of the plane equation. |
| 274 | float v[3] = { |
| 275 | static_cast<float>(get_uBody()), |
| 276 | static_cast<float>(get_vBody()), |
| 277 | static_cast<float>(get_wBody()) |
| 278 | }; |
| 279 | float lat = get_Latitude(); float lon = get_Longitude(); |
| 280 | float alt = get_Altitude() * FT2M; double xyz[3]; |
| 281 | sgGeodToCart(lat, lon, alt, xyz); |
| 282 | // build the environment cache. |
| 283 | float vr = _fdm->getVehicleRadius(); |
| 284 | vr += 2.0*FT2M*dt*Math::mag3(v); |
| 285 | prepare_ground_cache_m( _simTime, _simTime + dt, xyz, vr ); |
| 286 | |
| 287 | // Track time increments. |
| 288 | FGGround* gr |
| 289 | = (FGGround*)_fdm->getAirplane()->getModel()->getGroundCallback(); |
| 290 | |
| 291 | int i; |
| 292 | for(i=0; i<iterations; i++) { |
| 293 | gr->setTimeOffset(_simTime + i*_dt); |
| 294 | copyToYASim(false); |
| 295 | _fdm->iterate(_dt); |
| 296 | copyFromYASim(); |
| 297 | } |
| 298 | |
| 299 | // Increment the local sim time |
| 300 | _simTime += dt; |
| 301 | gr->setTimeOffset(_simTime); |
| 302 | } |
| 303 | |
| 304 | void YASim::copyToYASim(bool copyState) |
| 305 | { |
| 306 | // Physical state |
| 307 | double lat = get_Latitude(); |
| 308 | double lon = get_Longitude(); |
| 309 | float alt = get_Altitude() * FT2M; |
| 310 | float roll = get_Phi(); |
| 311 | float pitch = get_Theta(); |
| 312 | float hdg = get_Psi(); |
| 313 | |
| 314 | // Environment |
| 315 | float wind[3]; |
| 316 | wind[0] = get_V_north_airmass() * FT2M * -1.0; |
| 317 | wind[1] = get_V_east_airmass() * FT2M * -1.0; |
| 318 | wind[2] = get_V_down_airmass() * FT2M * -1.0; |
| 319 | |
| 320 | float pressure = _pressure_inhg->getFloatValue() * INHG2PA; |
| 321 | float temp = _temp_degc->getFloatValue() + 273.15; |
| 322 | float dens = _density_slugft3->getFloatValue() * |
| 323 | SLUG2KG * M2FT*M2FT*M2FT; |
| 324 | Atmosphere atmo; |
| 325 | atmo.setDensity(dens); |
| 326 | atmo.setTemperature(temp); |
| 327 | atmo.setPressure(pressure); |
| 328 | |
| 329 | // Convert and set: |
| 330 | Model* model = _fdm->getAirplane()->getModel(); |
| 331 | yasim::State s; |
| 332 | float xyz2ned[9]; |
| 333 | Glue::xyz2nedMat(lat, lon, xyz2ned); |
| 334 | |
| 335 | // position |
| 336 | sgGeodToCart(lat, lon, alt, s.pos); |
| 337 | { |
| 338 | // allow setting of /position/[lat|long|alti]tude |
| 339 | double * dp = &model->getState()->pos[0]; |
| 340 | dp[0] = s.pos[0]; dp[1] = s.pos[1]; dp[2] = s.pos[2]; |
| 341 | } |
| 342 | |
| 343 | // orientation |
| 344 | Glue::euler2orient(roll, pitch, hdg, s.orient); |
| 345 | Math::mmul33(s.orient, xyz2ned, s.orient); |
| 346 | |
| 347 | // Velocity |
| 348 | float v[3] {0, 0, 0}; |
| 349 | bool needCopy = false; |
| 350 | switch (_speed_set) { |
| 351 | case NED: |
| 352 | v[0] = get_V_north() * FT2M * -1.0; |
| 353 | v[1] = get_V_east() * FT2M * -1.0; |
| 354 | v[2] = get_V_down() * FT2M * -1.0; |
| 355 | break; |
| 356 | case UVW: |
| 357 | v[0] = get_uBody() * FT2M; |
| 358 | v[1] = get_vBody() * FT2M; |
| 359 | v[2] = get_wBody() * FT2M; |
| 360 | Math::tmul33(s.orient, v, v); |
| 361 | break; |
| 362 | case KNOTS: |
| 363 | v[0] = atmo.speedFromVCAS(get_V_calibrated_kts()/MPS2KTS); |
| 364 | v[1] = 0; |
| 365 | v[2] = 0; |
| 366 | Math::tmul33(s.orient, v, v); |
| 367 | needCopy = true; |
| 368 | break; |
| 369 | case MACH: |
| 370 | v[0] = atmo.speedFromMach(get_Mach_number()); |
| 371 | v[1] = 0; |
| 372 | v[2] = 0; |
| 373 | Math::tmul33(s.orient, v, v); |
| 374 | needCopy = true; |
| 375 | break; |
| 376 | default: |
| 377 | v[0] = 0; |
| 378 | v[1] = 0; |
| 379 | v[2] = 0; |
| 380 | break; |
| 381 | } |
| 382 | if (!copyState) |
| 383 | _speed_set = UVW; // change to this after initial setting |
| 384 | Math::set3(v, s.v); |
| 385 | |
| 386 | if(copyState || needCopy) |
| 387 | model->setState(&s); |
| 388 | |
| 389 | Math::tmul33(xyz2ned, wind, wind); |
| 390 | model->setWind(wind); |
| 391 | model->setAtmosphere(atmo); |
| 392 | |
| 393 | // Query a ground plane for each gear/hook/launchbar and |
| 394 | // write that value into the corresponding class. |
| 395 | _fdm->getAirplane()->getModel()->updateGround(&s); |
| 396 | |
| 397 | Launchbar* l = model->getLaunchbar(); |
| 398 | if (l) { |
| 399 | l->setLaunchCmd(0.0 < _catapult_launch_cmd->getFloatValue()); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // All the settables: |
| 404 | // |
| 405 | // These are set below: |
| 406 | // _set_Accels_Local |
| 407 | // _set_Accels_Body |
| 408 | // _set_Accels_CG_Body |
| 409 | // _set_Accels_Pilot_Body |
| 410 | // _set_Accels_CG_Body_N |
| 411 | // _set_Velocities_Local |
| 412 | // _set_Velocities_Ground |
| 413 | // _set_Velocities_Body |
| 414 | // _set_Omega_Body |
| 415 | // _set_Euler_Rates |
| 416 | // _set_Euler_Angles |
| 417 | // _set_V_rel_wind |
| 418 | // _set_V_ground_speed |
| 419 | // _set_V_equiv_kts |
| 420 | // _set_V_calibrated_kts |
| 421 | // _set_Alpha |
| 422 | // _set_Beta |
| 423 | // _set_Mach_number |
| 424 | // _set_Climb_Rate |
| 425 | // _set_Tank1Fuel |
| 426 | // _set_Tank2Fuel |
| 427 | // _set_Altitude_AGL |
| 428 | // _set_Geodetic_Position |
| 429 | // _set_Runway_altitude |
| 430 | |
| 431 | // Ignoring these, because they're unused: |
| 432 | // _set_Geocentric_Position |
| 433 | // _set_Geocentric_Rates |
| 434 | // _set_Cos_phi |
| 435 | // _set_Cos_theta |
| 436 | // _set_Earth_position_angle (WTF?) |
| 437 | // _set_Gamma_vert_rad |
| 438 | // _set_Inertias |
| 439 | // _set_T_Local_to_Body |
| 440 | // _set_CG_Position |
| 441 | // _set_Sea_Level_Radius |
| 442 | |
| 443 | // Externally set via the weather code: |
| 444 | // _set_Velocities_Local_Airmass |
| 445 | // _set_Density |
| 446 | // _set_Static_pressure |
| 447 | // _set_Static_temperature |
| 448 | void YASim::copyFromYASim() |
| 449 | { |
| 450 | Airplane* airplane = _fdm->getAirplane(); |
| 451 | Model* model = airplane->getModel(); |
| 452 | yasim::State* s = model->getState(); |
| 453 | |
| 454 | // position |
| 455 | double lat, lon, alt; |
| 456 | sgCartToGeod(s->pos, &lat, &lon, &alt); |
| 457 | _set_Geodetic_Position(lat, lon, alt*M2FT); |
| 458 | double groundlevel_m = get_groundlevel_m(lat, lon, alt); |
| 459 | _set_Runway_altitude(groundlevel_m*SG_METER_TO_FEET); |
| 460 | _set_Altitude_AGL((alt-groundlevel_m)*SG_METER_TO_FEET); |
| 461 | |
| 462 | // the smallest agl of all gears |
| 463 | _gear_agl_m->setFloatValue(model->getAGL()); |
| 464 | _gear_agl_ft->setFloatValue(model->getAGL()*M2FT); |
| 465 | |
| 466 | // UNUSED |
| 467 | //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT); |
| 468 | |
| 469 | // useful conversion matrix |
| 470 | float xyz2ned[9]; |
| 471 | Glue::xyz2nedMat(lat, lon, xyz2ned); |
| 472 | |
| 473 | // velocity |
| 474 | float v[3]; |
| 475 | Math::vmul33(xyz2ned, s->v, v); |
| 476 | _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]); |
| 477 | _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] + |
| 478 | M2FT*v[1]*M2FT*v[1])); |
| 479 | _set_Climb_Rate(-M2FT*v[2]); |
| 480 | |
| 481 | // The HUD uses this, but inverts down (?!) |
| 482 | _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]); |
| 483 | |
| 484 | // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED |
| 485 | |
| 486 | // ecef speed in body axis |
| 487 | Math::vmul33(s->orient, s->v, v); |
| 488 | _set_Velocities_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT); |
| 489 | |
| 490 | // Airflow velocity. |
| 491 | float wind[3]; |
| 492 | wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED |
| 493 | wind[1] = get_V_east_airmass() * FT2M * -1.0; |
| 494 | wind[2] = get_V_down_airmass() * FT2M * -1.0; |
| 495 | Math::tmul33(xyz2ned, wind, wind); // Wind in global |
| 496 | Math::sub3(s->v, wind, v); // V - wind in global |
| 497 | Math::vmul33(s->orient, v, v); // to body coordinates |
| 498 | _set_V_rel_wind(Math::mag3(v)*M2FT); // units? |
| 499 | |
| 500 | float P = _pressure_inhg->getFloatValue() * INHG2PA; |
| 501 | float T = _temp_degc->getFloatValue() + 273.15; |
| 502 | float D = _density_slugft3->getFloatValue() |
| 503 | *SLUG2KG * M2FT*M2FT*M2FT; |
| 504 | _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS); |
| 505 | _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS); |
| 506 | _set_Mach_number(Atmosphere::calcMach(Math::mag3(v), T)); |
| 507 | |
| 508 | // acceleration |
| 509 | Math::vmul33(xyz2ned, s->acc, v); |
| 510 | _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]); |
| 511 | |
| 512 | Math::vmul33(s->orient, s->acc, v); |
| 513 | _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]); |
| 514 | _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]); |
| 515 | |
| 516 | _fdm->getAirplane()->getPilotAccel(v); |
| 517 | _set_Accels_Pilot_Body(M2FT*v[0], M2FT*v[1], M2FT*v[2]); |
| 518 | |
| 519 | // There is no property for pilot G's, but I need it for a panel |
| 520 | // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A |
| 521 | // REAL HOME! |
| 522 | _pilot_g->setFloatValue(-v[2]/9.8); |
| 523 | |
| 524 | // The one appears (!) to want inverted pilot acceleration |
| 525 | // numbers, in G's... |
| 526 | Math::mul3(1.0/9.8, v, v); |
| 527 | _set_Accels_CG_Body_N(v[0], -v[1], -v[2]); |
| 528 | |
| 529 | // orientation |
| 530 | float alpha, beta; |
| 531 | Glue::calcAlphaBeta(s, wind, &alpha, &beta); |
| 532 | _set_Alpha(alpha); |
| 533 | _set_Beta(-beta); |
| 534 | |
| 535 | float tmp[9]; |
| 536 | Math::trans33(xyz2ned, tmp); |
| 537 | Math::mmul33(s->orient, tmp, tmp); |
| 538 | float roll, pitch, hdg; |
| 539 | Glue::orient2euler(tmp, &roll, &pitch, &hdg); |
| 540 | // make heading positive value |
| 541 | if(hdg < 0.0) hdg += PI2; |
| 542 | _set_Euler_Angles(roll, pitch, hdg); |
| 543 | |
| 544 | // rotation |
| 545 | Math::vmul33(s->orient, s->rot, v); |
| 546 | _set_Omega_Body(v[0], -v[1], -v[2]); |
| 547 | |
| 548 | Glue::calcEulerRates(s, &roll, &pitch, &hdg); |
| 549 | _set_Euler_Rates(roll, pitch, hdg); |
| 550 | |
| 551 | // Fill out our engine and gear objects |
| 552 | for(int i=0; i<airplane->numGear(); i++) { |
| 553 | Gear* g = airplane->getGear(i); |
| 554 | GearProps& gearProps = _gearProps[i]; |
| 555 | gearProps.has_brake->setBoolValue( |
| 556 | g->getBrake() != 0); |
| 557 | gearProps.wow->setBoolValue( |
| 558 | g->getCompressFraction() != 0); |
| 559 | gearProps.compression_norm->setFloatValue( |
| 560 | g->getCompressFraction()); |
| 561 | gearProps.compression_m->setFloatValue( |
| 562 | g->getCompressDist()); |
| 563 | gearProps.caster_angle_deg->setFloatValue( |
| 564 | g->getCasterAngle() * RAD2DEG); |
| 565 | gearProps.rollspeed_ms->setFloatValue( |
| 566 | g->getRollSpeed()); |
| 567 | gearProps.ground_is_solid->setBoolValue( |
| 568 | g->getGroundIsSolid()!=0); |
| 569 | gearProps.ground_friction_factor->setFloatValue( |
| 570 | g->getGroundFrictionFactor()); |
| 571 | } |
| 572 | |
| 573 | Hook* h = airplane->getHook(); |
| 574 | if(h) { |
| 575 | _tailhook_position_norm->setFloatValue(h->getCompressFraction()); |
| 576 | } |
| 577 | |
| 578 | Launchbar* l = airplane->getLaunchbar(); |
| 579 | if(l) { |
| 580 | _launchbar_position_norm->setFloatValue(l->getCompressFraction()); |
| 581 | _launchbar_holdback_pos_norm->setFloatValue(l->getHoldbackCompressFraction()); |
| 582 | _launchbar_state->setStringValue(l->getState()); |
| 583 | _launchbar_strop->setBoolValue(l->getStrop()); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** Reinit the FDM. |
| 588 | * This is only used after a replay session and when the user requested to resume at |
| 589 | * a past point of time. In this case the FDM must reload all values from the property |
| 590 | * tree (as given by the replay system). */ |
| 591 | void YASim::reinit() |
| 592 | { |
| 593 | // Process inputs. Use excessive value for dt to make sure all transition effects |
| 594 | // have reached their final state (i.e. gear is extended/retracted) - which is vital |
| 595 | // for many properties to be complete before the first FDM run (otherwise the gear may |
| 596 | // still be up, thrust-reversers/speed-brakes/... may still be partially deployed...). |
| 597 | _fdm->getExternalInput(1000); |
| 598 | |
| 599 | // get current FDM values from the property tree |
| 600 | copyToYASim(true); |
| 601 | } |
| 602 | |
| 603 | |
| 604 | // Register the subsystem. |
| 605 | #if 0 |
| 606 | SGSubsystemMgr::Registrant<YASim> registrantYASim; |
| 607 | #endif |