]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/FGFDM.cpp
Use bool where the source and destination variable is bool.
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
index aa04eea8b3d3c380e1bf9b493e89bdbc7f571aca..426a35ed43a92d1e26e4547a144366be4a3bb31b 100644 (file)
@@ -3,13 +3,17 @@
 
 #include <Main/fg_props.hxx>
 
+#include "Math.hpp"
 #include "Jet.hpp"
 #include "SimpleJet.hpp"
 #include "Gear.hpp"
+#include "Hook.hpp"
+#include "Launchbar.hpp"
 #include "Atmosphere.hpp"
 #include "PropEngine.hpp"
 #include "Propeller.hpp"
 #include "PistonEngine.hpp"
+#include "TurbineEngine.hpp"
 #include "Rotor.hpp"
 #include "Rotorpart.hpp"
 #include "Rotorblade.hpp"
@@ -34,6 +38,8 @@ static const float K2DEGFOFFSET = -459.4;
 static const float CIN2CM = 1.6387064e-5;
 static const float YASIM_PI = 3.14159265358979323846;
 
+static const float NM2FTLB = (1/(LBS2N*FT2M));
+
 // Stubs, so that this can be compiled without the FlightGear
 // binary.  What's the best way to handle this?
 
@@ -42,6 +48,8 @@ static const float YASIM_PI = 3.14159265358979323846;
 
 FGFDM::FGFDM()
 {
+    _vehicle_radius = 0.0f;
+
     _nextEngine = 0;
 
     // Map /controls/flight/elevator to the approach elevator control.  This
@@ -75,6 +83,7 @@ FGFDM::~FGFDM()
     }
     for(i=0; i<_controlProps.size(); i++)
         delete (PropOut*)_controlProps.get(i);
+    delete _turb;
 }
 
 void FGFDM::iterate(float dt)
@@ -91,7 +100,7 @@ void FGFDM::iterate(float dt)
         t->setFuelState(!fgGetBool(buf));
 
         sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
-        double consumed = fgGetDouble(buf) + dt * t->getFuelFlow();
+        double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
         fgSetDouble(buf, consumed);
     }
     for(int i=0; i<_airplane.numTanks(); i++) {
@@ -100,7 +109,7 @@ void FGFDM::iterate(float dt)
     } 
     _airplane.calcFuelWeights();
     
-    setOutputProperties();
+    setOutputProperties(dt);
 }
 
 Airplane* FGFDM::getAirplane()
@@ -174,6 +183,10 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        _airplane.setTail(parseWing(a, name));
     } else if(eq(name, "vstab") || eq(name, "mstab")) {
        _airplane.addVStab(parseWing(a, name));
+    } else if(eq(name, "piston-engine")) {
+        parsePistonEngine(a);
+    } else if(eq(name, "turbine-engine")) {
+        parseTurbineEngine(a);
     } else if(eq(name, "propeller")) {
        parsePropeller(a);
     } else if(eq(name, "thruster")) {
@@ -208,6 +221,8 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
        if(a->hasAttribute("exhaust-speed"))
            j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
+       if(a->hasAttribute("spool-time"))
+           j->setSpooling(attrf(a, "spool-time"));
        
        j->setPosition(v);
        _airplane.addThruster(j, mass, v);
@@ -223,9 +238,21 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        v[1] = attrf(a, "y");
        v[2] = attrf(a, "z");
        g->setPosition(v);
-       v[0] = 0;
-       v[1] = 0;
-       v[2] = attrf(a, "compression", 1);
+        float nrm = Math::mag3(v);
+        if (_vehicle_radius < nrm)
+            _vehicle_radius = nrm;
+        if(a->hasAttribute("upx")) {
+            v[0] = attrf(a, "upx");
+            v[1] = attrf(a, "upy");
+            v[2] = attrf(a, "upz");
+            Math::unit3(v, v);
+        } else {
+            v[0] = 0;
+            v[1] = 0;
+            v[2] = 1;
+        }
+        for(int i=0; i<3; i++)
+            v[i] *= attrf(a, "compression", 1);
        g->setCompression(v);
         g->setBrake(attrf(a, "skid", 0));
        g->setStaticFriction(attrf(a, "sfric", 0.8));
@@ -233,6 +260,38 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
         g->setSpring(attrf(a, "spring", 1));
         g->setDamping(attrf(a, "damp", 1));
        _airplane.addGear(g);
+    } else if(eq(name, "hook")) {
+       Hook* h = new Hook();
+       _currObj = h;
+       v[0] = attrf(a, "x");
+       v[1] = attrf(a, "y");
+       v[2] = attrf(a, "z");
+       h->setPosition(v);
+        float length = attrf(a, "length", 1.0);
+        h->setLength(length);
+        float nrm = length+Math::mag3(v);
+        if (_vehicle_radius < nrm)
+            _vehicle_radius = nrm;
+        h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
+        h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
+       _airplane.addHook(h);
+    } else if(eq(name, "launchbar")) {
+       Launchbar* l = new Launchbar();
+       _currObj = l;
+       v[0] = attrf(a, "x");
+       v[1] = attrf(a, "y");
+       v[2] = attrf(a, "z");
+       l->setLaunchbarMount(v);
+       v[0] = attrf(a, "holdback-x", v[0]);
+       v[1] = attrf(a, "holdback-y", v[1]);
+       v[2] = attrf(a, "holdback-z", v[2]);
+       l->setHoldbackMount(v);
+        float length = attrf(a, "length", 1.0);
+        l->setLength(length);
+        l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
+        l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
+        l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
+       _airplane.addLaunchbar(l);
     } else if(eq(name, "fuselage")) {
        float b[3];
        v[0] = attrf(a, "ax");
@@ -383,9 +442,21 @@ void FGFDM::getExternalInput(float dt)
     }
 }
 
-void FGFDM::setOutputProperties()
+// Linearly "seeks" a property by the specified fraction of the way to
+// the target value.  Used to emulate "slowly changing" output values.
+static void moveprop(SGPropertyNode* node, const char* prop,
+                    float target, float frac)
 {
-    char buf[256];
+    float val = node->getFloatValue(prop);
+    if(frac > 1) frac = 1;
+    if(frac < 0) frac = 0;
+    val += (target - val) * frac;
+    node->setFloatValue(prop, val);
+}
+
+void FGFDM::setOutputProperties(float dt)
+{
+    // char buf[256];
     int i;
 
     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
@@ -436,41 +507,58 @@ void FGFDM::setOutputProperties()
     for(i=0; i<_thrusters.size(); i++) {
        EngRec* er = (EngRec*)_thrusters.get(i);
         Thruster* t = er->eng;
+        SGPropertyNode * node = fgGetNode("engines/engine", i, true);
 
-       sprintf(buf, "%s/fuel-flow-gph", er->prefix);
-        fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
+        // Set: running, cranking, prop-thrust, max-hp, power-pct
+       node->setBoolValue("running", t->isRunning());
+       node->setBoolValue("cranking", t->isCranking());
+
+        float tmp[3];
+        t->getThrust(tmp);
+        float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
+       node->setFloatValue("prop-thrust", lbs); // Deprecated name
+       node->setFloatValue("thrust-lbs", lbs);
+        node->setFloatValue("fuel-flow-gph",
+                            (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
 
        if(t->getPropEngine()) {
             PropEngine* p = t->getPropEngine();
-
-            sprintf(buf, "%s/rpm", er->prefix);
-            fgSetFloat(buf, p->getOmega() / RPM2RAD);
-        }
-
-        if(t->getPistonEngine()) {
-            PistonEngine* p = t->getPistonEngine();
-           
-            sprintf(buf, "%s/mp-osi", er->prefix);
-           fgSetFloat(buf, p->getMP() * (1/INHG2PA));
-
-           sprintf(buf, "%s/egt-degf", er->prefix);
-           fgSetFloat(buf, p->getEGT() * K2DEGF + K2DEGFOFFSET);
+            node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
+            node->setFloatValue("torque-ftlb",
+                                p->getEngine()->getTorque() * NM2FTLB);
+        
+            if(p->getEngine()->isPistonEngine()) {
+                PistonEngine* pe = p->getEngine()->isPistonEngine();
+                node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
+                node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
+                node->setFloatValue("egt-degf",
+                                    pe->getEGT() * K2DEGF + K2DEGFOFFSET);
+                node->setFloatValue("oil-temperature-degf",
+                                    pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
+                node->setFloatValue("boost-gauge-inhg",
+                                    pe->getBoost() * (1/INHG2PA));
+            } else if(p->getEngine()->isTurbineEngine()) {
+                TurbineEngine* te = p->getEngine()->isTurbineEngine();
+                node->setFloatValue("n2", te->getN2());
+            }
         }
 
         if(t->getJet()) {
             Jet* j = t->getJet();
-
-            sprintf(buf, "%s/n1", er->prefix);
-            fgSetFloat(buf, j->getN1());
-
-            sprintf(buf, "%s/n2", er->prefix);
-            fgSetFloat(buf, j->getN2());
-
-            sprintf(buf, "%s/epr", er->prefix);
-            fgSetFloat(buf, j->getEPR());
-
-            sprintf(buf, "%s/egt-degf", er->prefix);
-            fgSetFloat(buf, j->getEGT() * K2DEGF + K2DEGFOFFSET);
+            node->setFloatValue("n1", j->getN1());
+            node->setFloatValue("n2", j->getN2());
+            node->setFloatValue("epr", j->getEPR());
+            node->setFloatValue("egt-degf",
+                                j->getEGT() * K2DEGF + K2DEGFOFFSET);
+
+            // These are "unmodeled" values that are still needed for
+            // many cockpits.  Tie them all to the N1 speed, but
+            // normalize the numbers to the range [0:1] so the
+            // cockpit code can scale them to the right values.
+            float pnorm = j->getPerfNorm();
+            moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
+            moveprop(node, "oilt-norm", pnorm, dt/30); // 30s 
+            moveprop(node, "itt-norm", pnorm, dt/1); // 1s
         }
     }
 }
@@ -497,8 +585,12 @@ Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
     w->setTaper(attrf(a, "taper", 1));
     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
     w->setCamber(attrf(a, "camber", 0));
-    w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
-    w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
+
+    // These come in with positive indicating positive AoA, but the
+    // internals expect a rotation about the left-pointing Y axis, so
+    // invert the sign.
+    w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
+    w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
 
     // The 70% is a magic number that sorta kinda seems to match known
     // throttle settings to approach speed.
@@ -515,7 +607,7 @@ Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
 {
     Rotor* w = new Rotor();
 
-    float defDihed = 0;
+    // float defDihed = 0;
 
     float pos[3];
     pos[0] = attrf(a, "x");
@@ -592,8 +684,74 @@ Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
     return w;
 }
 
+void FGFDM::parsePistonEngine(XMLAttributes* a)
+{
+    float engP = attrf(a, "eng-power") * HP2W;
+    float engS = attrf(a, "eng-rpm") * RPM2RAD;
+
+    PistonEngine* eng = new PistonEngine(engP, engS);
+
+    if(a->hasAttribute("displacement"))
+        eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
+
+    if(a->hasAttribute("compression"))
+        eng->setCompression(attrf(a, "compression"));        
+
+    if(a->hasAttribute("turbo-mul")) {
+        float mul = attrf(a, "turbo-mul");
+        float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
+        eng->setTurboParams(mul, mp);
+        eng->setTurboLag(attrf(a, "turbo-lag", 2));
+    }
+
+    if(a->hasAttribute("supercharger"))
+        eng->setSupercharger(attrb(a, "supercharger"));
+
+    ((PropEngine*)_currObj)->setEngine(eng);
+}
+
+void FGFDM::parseTurbineEngine(XMLAttributes* a)
+{
+    float power = attrf(a, "eng-power") * HP2W;
+    float omega = attrf(a, "eng-rpm") * RPM2RAD;
+    float alt = attrf(a, "alt") * FT2M;
+    float flatRating = attrf(a, "flat-rating") * HP2W;
+    TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
+
+    if(a->hasAttribute("n2-low-idle"))
+        eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
+                        attrf(a, "n2-max"));
+
+    // Nasty units conversion: lbs/hr per hp -> kg/s per watt
+    if(a->hasAttribute("bsfc"))
+        eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
+
+    ((PropEngine*)_currObj)->setEngine(eng);
+}
+
 void FGFDM::parsePropeller(XMLAttributes* a)
 {
+    // Legacy Handling for the old engines syntax:
+    PistonEngine* eng = 0;
+    if(a->hasAttribute("eng-power")) {
+        SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
+               << "Legacy engine definition in YASim configuration file.  "
+               << "Please fix.");
+        float engP = attrf(a, "eng-power") * HP2W;
+        float engS = attrf(a, "eng-rpm") * RPM2RAD;
+        eng = new PistonEngine(engP, engS);
+        if(a->hasAttribute("displacement"))
+            eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
+        if(a->hasAttribute("compression"))
+            eng->setCompression(attrf(a, "compression"));        
+        if(a->hasAttribute("turbo-mul")) {
+            float mul = attrf(a, "turbo-mul");
+            float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
+            eng->setTurboParams(mul, mp);
+        }
+    }
+
+    // Now parse the actual propeller definition:
     float cg[3];
     cg[0] = attrf(a, "x");
     cg[1] = attrf(a, "y");
@@ -606,26 +764,14 @@ void FGFDM::parsePropeller(XMLAttributes* a)
     float power = attrf(a, "cruise-power") * HP2W;
     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
 
-    // Hack, fix this pronto:
-    float engP = attrf(a, "eng-power") * HP2W;
-    float engS = attrf(a, "eng-rpm") * RPM2RAD;
-
     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
-    PistonEngine* eng = new PistonEngine(engP, engS);
     PropEngine* thruster = new PropEngine(prop, eng, moment);
     _airplane.addThruster(thruster, mass, cg);
 
-    if(a->hasAttribute("displacement"))
-        eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
-
-    if(a->hasAttribute("compression"))
-        eng->setCompression(attrf(a, "compression"));        
-
-    if(a->hasAttribute("turbo-mul")) {
-        float mul = attrf(a, "turbo-mul");
-        float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
-        eng->setTurboParams(mul, mp);
-    }
+    // Set the stops (fine = minimum pitch, coarse = maximum pitch)
+    float fine_stop = attrf(a, "fine-stop", 0.25f);
+    float coarse_stop = attrf(a, "coarse-stop", 4.0f);
+    prop->setStops(fine_stop, coarse_stop);
 
     if(a->hasAttribute("takeoff-power")) {
        float power0 = attrf(a, "takeoff-power") * HP2W;
@@ -639,6 +785,9 @@ void FGFDM::parsePropeller(XMLAttributes* a)
        thruster->setVariableProp(min, max);
     }
 
+    if(attrb(a, "contra"))
+        thruster->setContraPair(true);
+
     if(a->hasAttribute("manual-pitch")) {
        prop->setManualPitch();
     }
@@ -670,6 +819,7 @@ int FGFDM::parseAxis(const char* name)
     // Not there, make a new one.
     AxisRec* a = new AxisRec();
     a->name = dup(name);
+    fgGetNode( a->name, true ); // make sure the property name exists
     a->handle = _airplane.getControlMap()->newInput();
     _axes.add(a);
     return a->handle;
@@ -679,6 +829,7 @@ int FGFDM::parseOutput(const char* name)
 {
     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
+    if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
     if(eq(name, "STARTER"))   return ControlMap::STARTER;
     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
@@ -689,6 +840,8 @@ int FGFDM::parseOutput(const char* name)
     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
     if(eq(name, "STEER"))     return ControlMap::STEER;
     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
+    if(eq(name, "HEXTEND"))   return ControlMap::HEXTEND;
+    if(eq(name, "LEXTEND"))   return ControlMap::LEXTEND;
     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
@@ -696,11 +849,13 @@ int FGFDM::parseOutput(const char* name)
     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
+    if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
     if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
+    if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
            << name << "' in YASim aircraft description.");
     exit(1);