]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/YASim.cxx
simplify name/number handling
[flightgear.git] / src / FDM / YASim / YASim.cxx
index 8e1e1e96b59634bcf8e2f72ae52ccca8be6881d3..27fcbff6d64672baa49f822b3daa06ef74b7f9d6 100644 (file)
@@ -1,4 +1,8 @@
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/misc/sg_path.hxx>
@@ -16,6 +20,9 @@
 #include "Integrator.hpp"
 #include "Glue.hpp"
 #include "Gear.hpp"
+#include "Hook.hpp"
+#include "Launchbar.hpp"
+#include "FGGround.hpp"
 #include "PropEngine.hpp"
 #include "PistonEngine.hpp"
 
@@ -43,6 +50,7 @@ YASim::YASim(double dt)
 
     _dt = dt;
 
+    _fdm->getAirplane()->getModel()->setGroundCallback( new FGGround(this) );
     _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
 }
 
@@ -87,10 +95,11 @@ void YASim::bind()
 
     char buf[256];
     for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
-       sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
-       sprintf(buf, "/engines/engine[%d]/rpm", i);           fgUntie(buf);
-       sprintf(buf, "/engines/engine[%d]/mp-osi", i);        fgUntie(buf);
-       sprintf(buf, "/engines/engine[%d]/egt-degf", i);      fgUntie(buf);
+       sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i);        fgUntie(buf);
+       sprintf(buf, "/engines/engine[%d]/rpm", i);                  fgUntie(buf);
+       sprintf(buf, "/engines/engine[%d]/mp-osi", i);               fgUntie(buf);
+       sprintf(buf, "/engines/engine[%d]/egt-degf", i);             fgUntie(buf);
+       sprintf(buf, "/engines/engine[%d]/oil-temperature-degf", i); fgUntie(buf);
     }
 }
 
@@ -138,9 +147,9 @@ void YASim::init()
        SGPropertyNode * node = fgGetNode("gear/gear", i, true);
         float pos[3];
         g->getPosition(pos);
-       node->setDoubleValue("xoffset-in", pos[0]);
-       node->setDoubleValue("yoffset-in", pos[1]);
-       node->setDoubleValue("zoffset-in", pos[2]);
+       node->setDoubleValue("xoffset-in", pos[0] * M2FT * 12);
+       node->setDoubleValue("yoffset-in", pos[1] * M2FT * 12);
+       node->setDoubleValue("zoffset-in", pos[2] * M2FT * 12);
     }
 
     // Are we at ground level?  If so, lift the plane up so the gear
@@ -185,19 +194,39 @@ void YASim::update(double dt)
         return;
     }
 
+    // ground.  Calculate a cartesian coordinate for the ground under
+    // us, find the (geodetic) up vector normal to the ground, then
+    // use that to find the final (radius) term of the plane equation.
+    float v[3] = { get_uBody(), get_vBody(), get_wBody() };
+    float lat = get_Latitude(); float lon = get_Longitude();
+    float alt = get_Altitude() * FT2M; double xyz[3];
+    sgGeodToCart(lat, lon, alt, xyz);
+    // build the environment cache.
+    float vr = _fdm->getVehicleRadius();
+    vr += 2.0*FT2M*dt*Math::mag3(v);
+    prepare_ground_cache_m( 0.0, xyz, vr );
+
+    // Track time increments.
+    FGGround* gr
+      = (FGGround*)_fdm->getAirplane()->getModel()->getGroundCallback();
+
     int i;
     for(i=0; i<iterations; i++) {
+        gr->setTimeOffset(i*_dt);
         copyToYASim(false);
         _fdm->iterate(_dt);
         copyFromYASim();
     }
+
+    // Reset the time increment.
+    gr->setTimeOffset(0.0);
 }
 
 void YASim::copyToYASim(bool copyState)
 {
     // Physical state
-    float lat = get_Latitude();
-    float lon = get_Longitude();
+    double lat = get_Latitude();
+    double lon = get_Longitude();
     float alt = get_Altitude() * FT2M;
     float roll = get_Phi();
     float pitch = get_Theta();
@@ -209,10 +238,6 @@ void YASim::copyToYASim(bool copyState)
     wind[1] = get_V_east_airmass() * FT2M * -1.0;
     wind[2] = get_V_down_airmass() * FT2M * -1.0;
 
-    // Get ground elevation
-    double ground = fgGetDouble("/position/ground-elev-m");
-    // cout << "YASIM: ground = " << ground << endl;
-
     float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA;
     float temp = fgGetFloat("/environment/temperature-degc") + 273.15;
     float dens = fgGetFloat("/environment/density-slugft3") 
@@ -279,19 +304,16 @@ void YASim::copyToYASim(bool copyState)
     Math::tmul33(xyz2ned, wind, wind);
     model->setWind(wind);
 
-    // ground.  Calculate a cartesian coordinate for the ground under
-    // 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];
-    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, dens);
+
+    // Query a ground plane for each gear/hook/launchbar and
+    // write that value into the corresponding class.
+    _fdm->getAirplane()->getModel()->updateGround(&s);
+
+    Launchbar* l = model->getLaunchbar();
+    if (l)
+        l->setLaunchCmd(0.0<fgGetFloat("/controls/gear/catapult-launch-cmd"));
 }
 
 // All the settables:
@@ -349,12 +371,17 @@ void YASim::copyFromYASim()
     double lat, lon, alt;
     sgCartToGeod(s->pos, &lat, &lon, &alt);
     _set_Geodetic_Position(lat, lon, alt*M2FT);
+    double groundlevel_m = get_groundlevel_m(lat, lon, alt);
+    _set_Runway_altitude(groundlevel_m*SG_METER_TO_FEET);
+    _set_Altitude_AGL((alt-groundlevel_m)*SG_METER_TO_FEET);
+
+    // the smallest agl of all gears
+    fgSetFloat("/position/gear-agl-m", model->getAGL());
+    fgSetFloat("/position/gear-agl-ft", model->getAGL()*M2FT);
 
     // UNUSED
     //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
 
-    _set_Altitude_AGL(model->getAGL() * M2FT);
-
     // useful conversion matrix
     float xyz2ned[9];
     Glue::xyz2nedMat(lat, lon, xyz2ned);
@@ -414,7 +441,7 @@ void YASim::copyFromYASim()
 
     // orientation
     float alpha, beta;
-    Glue::calcAlphaBeta(s, &alpha, &beta);
+    Glue::calcAlphaBeta(s, wind, &alpha, &beta);
     _set_Alpha(alpha);
     _set_Beta(beta);
 
@@ -442,29 +469,24 @@ void YASim::copyFromYASim()
        node->setBoolValue("has-brake", g->getBrake() != 0);
        node->setBoolValue("wow", g->getCompressFraction() != 0);
        node->setFloatValue("compression-norm", g->getCompressFraction());
+       node->setFloatValue("compression-m", g->getCompressDist());
+        node->setFloatValue("caster-angle-deg", g->getCasterAngle() * RAD2DEG);
+        node->setFloatValue("rollspeed-ms", g->getRollSpeed());
     }
 
-    for(i=0; i<model->numThrusters(); i++) {
-        SGPropertyNode * node = fgGetNode("engines/engine", i, true);
-        Thruster* t = model->getThruster(i);
-
-       node->setBoolValue("running", t->isRunning());
-       node->setBoolValue("cranking", t->isCranking());
-
-        float tmp[3];
-        t->getThrust(tmp);
-       node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
-
-        PropEngine* pe = t->getPropEngine();
-        if(pe) {
-           node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
-
-            pe->getTorque(tmp);
-            float power = Math::mag3(tmp) * pe->getOmega();
-            float maxPower = pe->getPistonEngine()->getMaxPower();
+    Hook* h = airplane->getHook();
+    if(h) {
+       SGPropertyNode * node = fgGetNode("gear/tailhook", 0, true);
+       node->setFloatValue("position-norm", h->getCompressFraction());
+    }
 
-           node->setDoubleValue("max-hp", maxPower * W2HP);
-           node->setDoubleValue("power-pct", 100 * power/maxPower);
-        }
+    Launchbar* l = airplane->getLaunchbar();
+    if(l) {
+       SGPropertyNode * node = fgGetNode("gear/launchbar", 0, true);
+       node->setFloatValue("position-norm", l->getCompressFraction());
+        node->setFloatValue("holdback-position-norm", l->getHoldbackCompressFraction());
+        node->setStringValue("state", l->getState());
+        node->setBoolValue("strop", l->getStrop());
     }
+
 }