X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FFDM%2FYASim%2FYASim.cxx;h=730d57e12d5a587df8c8468a881a650860331c3a;hb=ce91286e19d6d66b316811d04b5b66b8b768827b;hp=faaaf5fe794b8823e7c5aa861af47aedcf24b6d7;hpb=5a849b66e855a643c600ad4cfcce1f82e0fcdeb3;p=flightgear.git diff --git a/src/FDM/YASim/YASim.cxx b/src/FDM/YASim/YASim.cxx index faaaf5fe7..730d57e12 100644 --- a/src/FDM/YASim/YASim.cxx +++ b/src/FDM/YASim/YASim.cxx @@ -1,9 +1,14 @@ -#include + #include +#include +#include +#include +#include #include + #include
#include
-#include +#include #include "FGFDM.hpp" #include "Atmosphere.hpp" @@ -20,7 +25,9 @@ using namespace yasim; -static const float RAD2DEG = 180/3.14159265358979323846; +static const float YASIM_PI = 3.14159265358979323846; +static const float RAD2DEG = 180/YASIM_PI; +static const float PI2 = YASIM_PI*2; static const float RAD2RPM = 9.54929658551; static const float M2FT = 3.2808399; static const float FT2M = 0.3048; @@ -28,32 +35,8 @@ static const float MPS2KTS = 3600.0/1852.0; static const float CM2GALS = 264.172037284; // gallons/cubic meter static const float KG2LBS = 2.20462262185; static const float W2HP = 1.3416e-3; - -void YASim::printDEBUG() -{ - static int debugCount = 0; - - debugCount++; - if(debugCount >= 3) { - debugCount = 0; - -// printf("N1 %5.1f N2 %5.1f FF %7.1f EPR %4.2f EGT %6.1f\n", -// fgGetFloat("/engines/engine[0]/n1"), -// fgGetFloat("/engines/engine[0]/n2"), -// fgGetFloat("/engines/engine[0]/fuel-flow-gph"), -// fgGetFloat("/engines/engine[0]/epr"), -// fgGetFloat("/engines/engine[0]/egt")); - -// printf("gear: %f\n", fgGetFloat("/controls/gear-down")); - -// printf("alpha %5.1f beta %5.1f\n", get_Alpha()*57.3, get_Beta()*57.3); - -// printf("pilot: %f %f %f\n", -// fgGetDouble("/sim/view/pilot/x-offset-m"), -// fgGetDouble("/sim/view/pilot/y-offset-m"), -// fgGetDouble("/sim/view/pilot/z-offset-m")); - } -} +static const float INHG2PA = 3386.389; +static const float SLUG2KG = 14.59390; YASim::YASim(double dt) { @@ -74,16 +57,18 @@ void YASim::report() float drag = 1000 * a->getDragCoefficient(); SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:"); - SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<getSolutionIterations()); - SG_LOG(SG_FLIGHT,SG_INFO,"Drag Coefficient: "<< drag); - SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<getLiftRatio()); - SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa); - SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail); + SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<getSolutionIterations()); + SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag); + SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<getLiftRatio()); + SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa); + SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail); + SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<getApproachElevator()); + float cg[3]; char buf[256]; a->getModel()->getBody()->getCG(cg); - sprintf(buf, " CG: %.1f, %.1f, %.1f", cg[0], cg[1], cg[2]); + sprintf(buf, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]); SG_LOG(SG_FLIGHT, SG_INFO, buf); if(a->getFailureMsg()) { @@ -109,7 +94,6 @@ void YASim::bind() sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf); sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf); } - } void YASim::init() @@ -122,9 +106,23 @@ void YASim::init() m->setCrashed(false); + // Figure out the initial speed type + string speed_set = fgGetString("/sim/presets/speed-set", "UVW"); + if (speed_set == "NED") + _speed_set = NED; + else if (speed_set == "UVW") + _speed_set = UVW; + else if (speed_set == "knots") + _speed_set = KNOTS; + else if (speed_set == "mach") + _speed_set = MACH; + else { + _speed_set = UVW; + SG_LOG(SG_FLIGHT, SG_ALERT, "Unknown speed type " << speed_set); + } + // Build a filename and parse it - SGPath f(globals->get_fg_root()); - f.append("Aircraft-yasim"); + SGPath f(fgGetString("/sim/aircraft-dir")); f.append(fgGetString("/sim/aero")); f.concat(".xml"); readXML(f.str(), *_fdm); @@ -146,24 +144,12 @@ void YASim::init() node->setDoubleValue("yoffset-in", pos[1]); node->setDoubleValue("zoffset-in", pos[2]); } - for(i=0; inumThrusters(); i++) { - // Sanify the initial input conditions - char buf[64]; - sprintf(buf, "/controls/throttle[%d]", i); fgSetFloat(buf, 0); - sprintf(buf, "/controls/mixture[%d]", i); fgSetFloat(buf, 1); - sprintf(buf, "/controls/propeller-pitch[%d]", i); fgSetFloat(buf, 1); - sprintf(buf, "/controls/afterburner[%d]", i); fgSetFloat(buf, 0); - } - - fgSetFloat("/controls/slats", 0); - fgSetFloat("/controls/spoilers", 0); // Are we at ground level? If so, lift the plane up so the gear // clear the ground. - double runway_altitude = - fgGetDouble("/environment/ground-elevation-m") * SG_METER_TO_FEET; - fgSetBool("/controls/gear-down", false); + double runway_altitude = get_Runway_altitude(); if(get_Altitude() - runway_altitude < 50) { + fgSetBool("/controls/gear/gear-down", false); float minGearZ = 1e18; for(i=0; inumGear(); i++) { Gear* g = a->getGear(i); @@ -173,16 +159,9 @@ void YASim::init() minGearZ = pos[2]; } _set_Altitude(runway_altitude - minGearZ*M2FT); - fgSetBool("/controls/gear-down", true); + fgSetBool("/controls/gear/gear-down", true); } - // The pilot's eyepoint - float pilot[3]; - a->getPilotPos(pilot); -// fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]); -// fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]); -// fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]); - // Blank the state, and copy in ours State s; m->setState(&s); @@ -197,21 +176,22 @@ void YASim::init() void YASim::update(double dt) { if (is_suspended()) - return; + return; int iterations = _calc_multiloop(dt); // If we're crashed, then we don't care - if(_fdm->getAirplane()->getModel()->isCrashed()) - return; + if(_fdm->getAirplane()->getModel()->isCrashed()) { + if(!fgGetBool("/sim/crashed")) + fgSetBool("/sim/crashed", true); + return; + } int i; for(i=0; iiterate(_dt); - copyFromYASim(); - - printDEBUG(); + copyToYASim(false); + _fdm->iterate(_dt); + copyFromYASim(); } } @@ -231,19 +211,14 @@ void YASim::copyToYASim(bool copyState) wind[1] = get_V_east_airmass() * FT2M * -1.0; wind[2] = get_V_down_airmass() * FT2M * -1.0; - // The ground elevation doesn't come from FGInterface; query it - // from the scenery and set it for others to find. - double ground = scenery.get_cur_elev(); - _set_Runway_altitude(ground * FT2M); + // Get ground elevation from the FGinterface's FGlocation data + double ground = getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m(); // cout << "YASIM: ground = " << ground << endl; - // You'd this this would work, but it doesn't. These values are - // always zero. Use a "standard" pressure intstead. - // - // float pressure = get_Static_pressure(); - // float temp = get_Static_temperature(); - float pressure = Atmosphere::getStdPressure(alt); - float temp = Atmosphere::getStdTemperature(alt); + float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA; + float temp = fgGetFloat("/environment/temperature-degc") + 273.15; + float dens = fgGetFloat("/environment/density-slugft3") + * SLUG2KG * M2FT*M2FT*M2FT; // Convert and set: Model* model = _fdm->getAirplane()->getModel(); @@ -252,16 +227,54 @@ void YASim::copyToYASim(bool copyState) Glue::xyz2nedMat(lat, lon, xyz2ned); // position - Glue::geod2xyz(lat, lon, alt, s.pos); + sgGeodToCart(lat, lon, alt, s.pos); // orientation Glue::euler2orient(roll, pitch, hdg, s.orient); Math::mmul33(s.orient, xyz2ned, s.orient); - // Copy in the existing velocity for now... - Math::set3(model->getState()->v, s.v); + // Velocity + string speed_set = fgGetString("/sim/presets/speed-set", "UVW"); + float v[3]; + bool needCopy = false; + switch (_speed_set) { + case NED: + v[0] = get_V_north() * FT2M * -1.0; + v[1] = get_V_east() * FT2M * -1.0; + v[2] = get_V_down() * FT2M * -1.0; + break; + case UVW: + v[0] = get_uBody() * FT2M; + v[1] = get_vBody() * FT2M; + v[2] = get_wBody() * FT2M; + Math::tmul33(s.orient, v, v); + break; + case KNOTS: + v[0] = Atmosphere::spdFromVCAS(get_V_calibrated_kts()/MPS2KTS, + pressure, temp); + v[1] = 0; + v[2] = 0; + Math::tmul33(s.orient, v, v); + needCopy = true; + break; + case MACH: + v[0] = Atmosphere::spdFromMach(get_Mach_number(), temp); + v[1] = 0; + v[2] = 0; + Math::tmul33(s.orient, v, v); + needCopy = true; + break; + default: + v[0] = 0; + v[1] = 0; + v[2] = 0; + break; + } + if (!copyState) + _speed_set = UVW; // change to this after initial setting + Math::set3(v, s.v); - if(copyState) + if(copyState || needCopy) model->setState(&s); // wind @@ -272,15 +285,15 @@ void YASim::copyToYASim(bool copyState) // us, find the (geodetic) up vector normal to the ground, then // use that to find the final (radius) term of the plane equation. double xyz[3], gplane[3]; float up[3]; - Glue::geod2xyz(lat, lon, ground, xyz); - Glue::geodUp(xyz, up); // FIXME, needless reverse computation... + sgGeodToCart(lat, lon, ground, xyz); + Glue::geodUp(lat, lon, up); // FIXME, needless reverse computation... int i; for(i=0; i<3; i++) gplane[i] = up[i]; double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2]; model->setGroundPlane(gplane, rad); // air - model->setAir(pressure, temp); + model->setAir(pressure, temp, dens); } // All the settables: @@ -336,7 +349,7 @@ void YASim::copyFromYASim() // position double lat, lon, alt; - Glue::xyz2geod(s->pos, &lat, &lon, &alt); + sgCartToGeod(s->pos, &lat, &lon, &alt); _set_Geodetic_Position(lat, lon, alt*M2FT); // UNUSED @@ -368,17 +381,15 @@ void YASim::copyFromYASim() wind[2] = get_V_down_airmass() * FT2M * -1.0; Math::tmul33(xyz2ned, wind, wind); // Wind in global Math::sub3(s->v, wind, v); // V - wind in global - Math::vmul33(s->orient, s->v, v); // to body coordinates + Math::vmul33(s->orient, v, v); // to body coordinates _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT); _set_V_rel_wind(Math::mag3(v)*M2FT); // units? - - // These don't work, use a dummy based on altitude - // float P = get_Static_pressure(); - // float T = get_Static_temperature(); - float P = Atmosphere::getStdPressure(alt); - float T = Atmosphere::getStdTemperature(alt); - _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T)*MPS2KTS); + float P = fgGetDouble("/environment/pressure-inhg") * INHG2PA; + float T = fgGetDouble("/environment/temperature-degC") + 273.15; + float D = fgGetFloat("/environment/density-slugft3") + *SLUG2KG * M2FT*M2FT*M2FT; + _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS); _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS); _set_Mach_number(Atmosphere::calcMach(v[0], T)); @@ -391,7 +402,12 @@ void YASim::copyFromYASim() _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]); _fdm->getAirplane()->getPilotAccel(v); - _set_Accels_Pilot_Body(M2FT*v[0], M2FT*v[1], -M2FT*v[2]); + _set_Accels_Pilot_Body(-M2FT*v[0], M2FT*v[1], M2FT*v[2]); + + // There is no property for pilot G's, but I need it for a panel + // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A + // REAL HOME! + fgSetFloat("/accelerations/pilot-g", -v[2]/9.8); // The one appears (!) to want inverted pilot acceleration // numbers, in G's... @@ -409,6 +425,8 @@ void YASim::copyFromYASim() Math::mmul33(s->orient, tmp, tmp); float roll, pitch, hdg; Glue::orient2euler(tmp, &roll, &pitch, &hdg); + // make heading positive value + if(hdg < 0.0) hdg += PI2; _set_Euler_Angles(roll, pitch, hdg); // rotation @@ -435,10 +453,6 @@ void YASim::copyFromYASim() node->setBoolValue("running", t->isRunning()); node->setBoolValue("cranking", t->isCranking()); - // Note: assumes all tanks have the same fuel density! - node->setDoubleValue("fuel-flow-gph", CM2GALS * t->getFuelFlow() - / airplane->getFuelDensity(0)); - float tmp[3]; t->getThrust(tmp); node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);