]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/PropEngine.cpp
Add support for a turbo prop condition lever.
[flightgear.git] / src / FDM / YASim / PropEngine.cpp
index 219734760a999a7b818d927b1d932abadf24eeb8..74c29f317bda064cfc4eba408e8117abea680d02 100644 (file)
@@ -1,20 +1,22 @@
 #include "Math.hpp"
 #include "Propeller.hpp"
-#include "PistonEngine.hpp"
+#include "Engine.hpp"
 #include "PropEngine.hpp"
 namespace yasim {
 
-PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment)
+PropEngine::PropEngine(Propeller* prop, Engine* eng, float moment)
 {
     // Start off at 500rpm, because the start code doesn't exist yet
     _omega = 52.3f;
     _dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
 
     _variable = false;
+    _gearRatio = 1;
 
     _prop = prop;
     _eng = eng;
     _moment = moment;
+    _fuel = true;
 }
 
 PropEngine::~PropEngine()
@@ -33,6 +35,18 @@ void PropEngine::setAdvance(float advance)
     _advance = Math::clamp(advance, 0, 1);
 }
 
+void PropEngine::setPropPitch(float proppitch)
+{
+    // update Propeller property
+    _prop->setPropPitch(proppitch);
+}
+
+void PropEngine::setPropFeather(int state)
+{
+    // toggle prop feathering on/off
+    _prop->setPropFeather(state);
+}
+
 void PropEngine::setVariableProp(float min, float max)
 {
     _variable = true;
@@ -55,6 +69,11 @@ float PropEngine::getOmega()
     return _omega;
 }
 
+void PropEngine::setOmega (float omega)
+{
+    _omega = omega;
+}
+
 void PropEngine::getThrust(float* out)
 {
     int i;
@@ -84,7 +103,10 @@ void PropEngine::stabilize()
     _eng->setThrottle(_throttle);
     _eng->setMixture(_mixture);
 
+    _eng->setStarter(false);
     _eng->setMagnetos(3);
+
+    bool running_state = _eng->isRunning();
     _eng->setRunning(true);
 
     if(_variable) {
@@ -97,13 +119,20 @@ void PropEngine::stabilize()
     bool goingUp = false;
     float step = 10;
     while(true) {
-       float ptau, dummy;
-       _prop->calc(_rho, speed, _omega, &dummy, &ptau);
+       float ptau, thrust;
+       _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &ptau);
        _eng->calc(_pressure, _temp, _omega);
+        _eng->stabilize();
+
+        // Compute torque as seen by the engine's end of the
+        // gearbox.
+        ptau *= _gearRatio;
         float etau = _eng->getTorque();
        float tdiff = etau - ptau;
+        
+        Math::mul3(thrust, _dir, _thrust);
 
-       if(Math::abs(tdiff/_moment) < 0.1)
+       if(Math::abs(tdiff/(_moment * _gearRatio)) < 0.1)
            break;
 
        if(tdiff > 0) {
@@ -120,7 +149,7 @@ void PropEngine::stabilize()
     }
 
     // ...and back off
-    _eng->setRunning(false);
+    _eng->setRunning(running_state);
 }
 
 void PropEngine::init()
@@ -140,22 +169,31 @@ void PropEngine::integrate(float dt)
     _eng->setStarter(_starter);
     _eng->setMagnetos(_magnetos);
     _eng->setMixture(_mixture);
+    _eng->setFuelState(_fuel);
     
-    _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
+    _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &propTorque);
     _eng->calc(_pressure, _temp, _omega);
+    _eng->integrate(dt);
     engTorque = _eng->getTorque();
     _fuelFlow = _eng->getFuelFlow();
 
     // Turn the thrust into a vector and save it
     Math::mul3(thrust, _dir, _thrust);
 
+    // We do our "RPM" computations on the engine's side of the
+    // world, so modify the moment value accordingly.
+    float momt = _moment * _gearRatio;
+
     // Euler-integrate the RPM.  This doesn't need the full-on
     // Runge-Kutta stuff.
-    float rotacc = (engTorque-propTorque)/Math::abs(_moment);
+    float rotacc = (engTorque-propTorque)/Math::abs(momt);
     _omega += dt * rotacc;
+    if (_omega < 0)
+        _omega = 0 - _omega;    // don't allow negative RPM
+                                // FIXME: introduce proper windmilling
 
     // Store the total angular momentum into _gyro
-    Math::mul3(_omega*_moment, _dir, _gyro);
+    Math::mul3(_omega*momt, _dir, _gyro);
 
     // Accumulate the engine torque, it acts on the body as a whole.
     // (Note: engine torque, not propeller torque.  They can be
@@ -181,7 +219,7 @@ void PropEngine::integrate(float dt)
 
        // Convert to an acceleration here, so that big propellers
        // don't seek faster than small ones.
-       float diff = Math::abs((propTorque - targetTorque) / _moment);
+       float diff = Math::abs((propTorque - targetTorque) / momt);
        if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
 
        _prop->modPitch(mod);