]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/FGFDM.cpp
Add support for a turbo prop condition lever.
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
index abe8843513fc500565dcfff9f10bc1abba7ca33d..57d95c041571e5e58d2b296c7d922f31f80f655c 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <Main/fg_props.hxx>
 
+#include "Math.hpp"
 #include "Jet.hpp"
 #include "SimpleJet.hpp"
 #include "Gear.hpp"
@@ -10,6 +11,7 @@
 #include "PropEngine.hpp"
 #include "Propeller.hpp"
 #include "PistonEngine.hpp"
+#include "TurbineEngine.hpp"
 #include "Rotor.hpp"
 #include "Rotorpart.hpp"
 #include "Rotorblade.hpp"
@@ -34,6 +36,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?
 
@@ -48,6 +52,10 @@ FGFDM::FGFDM()
     // should probably be settable, but there are very few aircraft
     // who trim their approaches using things other than elevator.
     _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
+
+    // FIXME: read seed from somewhere?
+    int seed = 0;
+    _turb = new Turbulence(10, seed);
 }
 
 FGFDM::~FGFDM()
@@ -78,9 +86,24 @@ void FGFDM::iterate(float dt)
     getExternalInput(dt);
     _airplane.iterate(dt);
 
-    if(fgGetBool("/sim/freeze/fuel") != true)
-        _airplane.consumeFuel(dt);
+    // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
+    char buf[256];
+    for(int i=0; i<_airplane.numThrusters(); i++) {
+        Thruster* t = _airplane.getThruster(i);
+
+        sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
+        t->setFuelState(!fgGetBool(buf));
 
+        sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
+        double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
+        fgSetDouble(buf, consumed);
+    }
+    for(int i=0; i<_airplane.numTanks(); i++) {
+        sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
+        _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
+    } 
+    _airplane.calcFuelWeights();
+    
     setOutputProperties();
 }
 
@@ -94,9 +117,28 @@ void FGFDM::init()
     // Allows the user to start with something other than full fuel
     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
 
+    // Read out the resulting fuel state
+    char buf[256];
+    for(int i=0; i<_airplane.numTanks(); i++) {
+        sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
+        fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
+
+        double density = _airplane.getFuelDensity(i);
+        sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
+        fgSetDouble(buf, density * (KG2LBS/CM2GALS));
+
+        sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
+        fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
+
+        sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
+        fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
+    }    
+
     // This has a nasty habit of being false at startup.  That's not
     // good.
     fgSetBool("/controls/gear/gear-down", true);
+
+    _airplane.getModel()->setTurbulence(_turb);
 }
 
 // Not the worlds safest parser.  But it's short & sweet.
@@ -112,13 +154,17 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        float spd = attrf(a, "speed") * KTS2MPS;
        float alt = attrf(a, "alt", 0) * FT2M;
        float aoa = attrf(a, "aoa", 0) * DEG2RAD;
-       _airplane.setApproach(spd, alt, aoa);
+       _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2));
        _cruiseCurr = false;
     } else if(eq(name, "cruise")) {
        float spd = attrf(a, "speed") * KTS2MPS;
        float alt = attrf(a, "alt") * FT2M;
-       _airplane.setCruise(spd, alt);
+       _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
        _cruiseCurr = true;
+    } else if(eq(name, "solve-weight")) {
+        int idx = attri(a, "idx");
+        float wgt = attrf(a, "weight") * LBS2KG;
+        _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
     } else if(eq(name, "cockpit")) {
        v[0] = attrf(a, "x");
        v[1] = attrf(a, "y");
@@ -130,10 +176,12 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        _airplane.setWing(parseWing(a, name));
     } else if(eq(name, "hstab")) {
        _airplane.setTail(parseWing(a, name));
-    } else if(eq(name, "vstab")) {
-       _airplane.addVStab(parseWing(a, name));
-    } else if(eq(name, "mstab")) {
+    } 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")) {
@@ -155,6 +203,7 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
        j->setMaxThrust(attrf(a, "thrust") * LBS2N,
                        attrf(a, "afterburner", 0) * LBS2N);
        j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
+        j->setReverseThrust(attrf(a, "reverse", 0.2));
 
        float n1min = attrf(a, "n1-idle", 55);
        float n1max = attrf(a, "n1-max", 102);
@@ -310,6 +359,9 @@ void FGFDM::getExternalInput(float dt)
 {
     char buf[256];
 
+    _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
+    _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
+
     // The control axes
     ControlMap* cm = _airplane.getControlMap();
     cm->reset();
@@ -334,14 +386,14 @@ void FGFDM::getExternalInput(float dt)
        if(t->getPropEngine()) {
             PropEngine* p = t->getPropEngine();
             sprintf(buf, "%s/rpm", er->prefix);
-            p->setOmega(fgGetFloat(buf) * RPM2RAD);
+            p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
         }
     }
 }
 
 void FGFDM::setOutputProperties()
 {
-    char buf[256];
+    // char buf[256];
     int i;
 
     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
@@ -360,105 +412,77 @@ void FGFDM::setOutputProperties()
         p->prop->setFloatValue(val);
     }
 
-    float totalFuel = 0, totalCap = 0;
-    float fuelDensity = 720; // in kg/m^3, default to gasoline: ~6 lb/gal
-    for(i=0; i<_airplane.numTanks(); i++) {
-        fuelDensity = _airplane.getFuelDensity(i);
-       sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
-       fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
-       sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
-       fgSetFloat(buf, KG2LBS*_airplane.getFuel(i));
-        totalFuel += _airplane.getFuel(i);
-        totalCap += _airplane.getTankCapacity(i);
-    }
-    if(totalCap != 0) {
-        fgSetFloat("/consumables/fuel/total-fuel-lbs", KG2LBS*totalFuel);
-        fgSetFloat("/consumables/fuel/total-fuel-gals",
-                   CM2GALS*totalFuel/fuelDensity);
-        fgSetFloat("/consumables/fuel/total-fuel-norm", totalFuel/totalCap);
-    }
-
     for(i=0; i<_airplane.getNumRotors(); i++) {
         Rotor*r=(Rotor*)_airplane.getRotor(i);
-        int j=0;
+        int j = 0;
         float f;
         char b[256];
-        while(j=r->getValueforFGSet(j,b,&f))
-        {
-              if (b[0])
-              {
-                 fgSetFloat(b,f);
-              }
-        }
+        while(j = r->getValueforFGSet(j, b, &f))
+            if(b[0]) fgSetFloat(b,f);
         
-        for(j=0; j<r->numRotorparts(); j++) {
+        for(j=0; j < r->numRotorparts(); j++) {
             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
             char *b;
             int k;
-            for (k=0;k<2;k++)
-            {
-              b=s->getAlphaoutput(k);
-              if (b[0])
-              {
-                 fgSetFloat(b,s->getAlpha(k));
-                 //printf("setting [%s]\n",b);
-              }
+            for(k=0; k<2; k++) {
+                b=s->getAlphaoutput(k);
+                if(b[0]) fgSetFloat(b, s->getAlpha(k));
             }
         }
-        for(j=0; j<r->numRotorblades(); j++) {
+        for(j=0; j < r->numRotorblades(); j++) {
             Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
             char *b;
             int k;
-            for (k=0;k<2;k++)
-            {
-              b=s->getAlphaoutput(k);
-              if (b[0])
-              {
-                 fgSetFloat(b,s->getAlpha(k));
-              }
+            for (k=0; k<2; k++) {
+                b = s->getAlphaoutput(k);
+                if(b[0]) fgSetFloat(b, s->getAlpha(k));
             }
         }
-     }
-
+    }
 
+    float fuelDensity = _airplane.getFuelDensity(0); // HACK
     for(i=0; i<_thrusters.size(); i++) {
        EngRec* er = (EngRec*)_thrusters.get(i);
         Thruster* t = er->eng;
+        SGPropertyNode * node = fgGetNode("engines/engine", i, true);
+
+        // Set: running, cranking, prop-thrust, max-hp, power-pct
+       node->setBoolValue("running", t->isRunning());
+       node->setBoolValue("cranking", t->isCranking());
 
-       sprintf(buf, "%s/fuel-flow-gph", er->prefix);
-        fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
+        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);
+            } 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("egr-degf",
+                                j->getEGT() * K2DEGF + K2DEGFOFFSET);
         }
     }
 }
@@ -498,11 +522,12 @@ Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
     _currObj = w;
     return w;
 }
+
 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");
@@ -522,8 +547,6 @@ Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
     forward[2] = attrf(a, "fz");
     w->setForward(forward);
 
-
-
     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
@@ -553,7 +576,7 @@ Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
     void setAlphamax(float f);
     void setAlpha0factor(float f);
 
-    if(attristrue(a,"ccw"))
+    if(attrb(a,"ccw"))
        w->setCcw(1); 
     
     if(a->hasAttribute("name"))
@@ -572,21 +595,79 @@ Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
     w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
     w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
     w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
-    if(attristrue(a,"notorque"))
+    if(attrb(a,"notorque"))
        w->setNotorque(1); 
-    if(attristrue(a,"simblades"))
+    if(attrb(a,"simblades"))
        w->setSimBlades(1); 
 
+    _currObj = w;
+    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);
 
-    _currObj = w;
-    return w;
+    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);
+    }
+
+    ((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");
@@ -599,27 +680,10 @@ 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);
-    }
-
     if(a->hasAttribute("takeoff-power")) {
        float power0 = attrf(a, "takeoff-power") * HP2W;
        float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
@@ -636,6 +700,8 @@ void FGFDM::parsePropeller(XMLAttributes* a)
        prop->setManualPitch();
     }
 
+    thruster->setGearRatio(attrf(a, "gear-ratio", 1));
+
     char buf[64];
     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
     EngRec* er = new EngRec();
@@ -661,6 +727,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;
@@ -670,6 +737,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;
@@ -687,10 +755,12 @@ 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;
     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
            << name << "' in YASim aircraft description.");
     exit(1);
@@ -765,11 +835,29 @@ float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
     else         return (float)atof(val);    
 }
 
-bool FGFDM::attristrue(XMLAttributes* atts, char* attr)
+// ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
+// when I have a chance. :).  Unless you have a parser that can check
+// symbol constants (we don't), this kind of coding is just a Bad
+// Idea.  This implementation, for example, silently returns a boolean
+// falsehood for values of "1", "yes", "True", and "TRUE".  Which is
+// especially annoying preexisting boolean attributes in the same
+// parser want to see "1" and will choke on a "true"...
+//
+// Unfortunately, this usage creeped into existing configuration files
+// while I wasn't active, and it's going to be hard to remove.  Issue
+// a warning to nag people into changing their ways for now...
+bool FGFDM::attrb(XMLAttributes* atts, char* attr)
 {
     const char* val = atts->getValue(attr);
     if(val == 0) return false;
-    else         return eq(val,"true");    
+
+    if(eq(val,"true")) {
+        SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
+               "deprecated 'true' boolean in YASim configuration file.  " <<
+               "Use numeric booleans (attribute=\"1\") instead");
+        return true;
+    }
+    return attri(atts, attr, 0) ? true : false;
 }
 
 }; // namespace yasim