2001-12-01 06:22:24 +00:00
|
|
|
#include "Math.hpp"
|
|
|
|
#include "Propeller.hpp"
|
2004-04-30 19:06:29 +00:00
|
|
|
#include "Engine.hpp"
|
2001-12-01 06:22:24 +00:00
|
|
|
#include "PropEngine.hpp"
|
|
|
|
namespace yasim {
|
|
|
|
|
2004-04-30 19:06:29 +00:00
|
|
|
PropEngine::PropEngine(Propeller* prop, Engine* eng, float moment)
|
2001-12-01 06:22:24 +00:00
|
|
|
{
|
|
|
|
// Start off at 500rpm, because the start code doesn't exist yet
|
2002-05-10 23:35:06 +00:00
|
|
|
_omega = 52.3f;
|
2001-12-01 06:22:24 +00:00
|
|
|
_dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
_variable = false;
|
2004-01-24 23:09:41 +00:00
|
|
|
_gearRatio = 1;
|
2001-12-06 18:13:24 +00:00
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
_prop = prop;
|
|
|
|
_eng = eng;
|
|
|
|
_moment = moment;
|
2002-11-30 06:12:20 +00:00
|
|
|
_fuel = true;
|
2004-12-13 23:48:43 +00:00
|
|
|
_contra = false;
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PropEngine::~PropEngine()
|
|
|
|
{
|
|
|
|
delete _prop;
|
|
|
|
delete _eng;
|
|
|
|
}
|
|
|
|
|
2002-02-20 04:27:22 +00:00
|
|
|
void PropEngine::setMagnetos(int pos)
|
|
|
|
{
|
|
|
|
_magnetos = pos;
|
|
|
|
}
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
void PropEngine::setAdvance(float advance)
|
2001-12-01 06:22:24 +00:00
|
|
|
{
|
2001-12-06 18:13:24 +00:00
|
|
|
_advance = Math::clamp(advance, 0, 1);
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
2003-05-16 17:27:17 +00:00
|
|
|
void PropEngine::setPropPitch(float proppitch)
|
|
|
|
{
|
|
|
|
// update Propeller property
|
|
|
|
_prop->setPropPitch(proppitch);
|
|
|
|
}
|
|
|
|
|
2004-07-20 22:17:58 +00:00
|
|
|
void PropEngine::setPropFeather(int state)
|
|
|
|
{
|
|
|
|
// toggle prop feathering on/off
|
|
|
|
_prop->setPropFeather(state);
|
|
|
|
}
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
void PropEngine::setVariableProp(float min, float max)
|
2001-12-01 06:22:24 +00:00
|
|
|
{
|
2001-12-06 18:13:24 +00:00
|
|
|
_variable = true;
|
|
|
|
_minOmega = min;
|
|
|
|
_maxOmega = max;
|
|
|
|
}
|
|
|
|
|
2002-02-20 04:27:22 +00:00
|
|
|
bool PropEngine::isRunning()
|
|
|
|
{
|
|
|
|
return _eng->isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropEngine::isCranking()
|
|
|
|
{
|
|
|
|
return _eng->isCranking();
|
|
|
|
}
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
float PropEngine::getOmega()
|
|
|
|
{
|
|
|
|
return _omega;
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
2003-10-18 20:07:06 +00:00
|
|
|
void PropEngine::setOmega (float omega)
|
|
|
|
{
|
|
|
|
_omega = omega;
|
|
|
|
}
|
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
void PropEngine::getThrust(float* out)
|
|
|
|
{
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<3; i++) out[i] = _thrust[i];
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropEngine::getTorque(float* out)
|
|
|
|
{
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<3; i++) out[i] = _torque[i];
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropEngine::getGyro(float* out)
|
|
|
|
{
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<3; i++) out[i] = _gyro[i];
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float PropEngine::getFuelFlow()
|
|
|
|
{
|
|
|
|
return _fuelFlow;
|
|
|
|
}
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
void PropEngine::stabilize()
|
|
|
|
{
|
|
|
|
float speed = -Math::dot3(_wind, _dir);
|
|
|
|
_eng->setThrottle(_throttle);
|
|
|
|
_eng->setMixture(_mixture);
|
|
|
|
|
2004-05-01 14:30:00 +00:00
|
|
|
_eng->setStarter(false);
|
2002-02-27 00:18:57 +00:00
|
|
|
_eng->setMagnetos(3);
|
2004-07-20 22:17:58 +00:00
|
|
|
|
|
|
|
bool running_state = _eng->isRunning();
|
2002-02-27 00:18:57 +00:00
|
|
|
_eng->setRunning(true);
|
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
if(_variable) {
|
|
|
|
_omega = _minOmega + _advance * (_maxOmega - _minOmega);
|
|
|
|
_prop->modPitch(1e6); // Start at maximum pitch and move down
|
|
|
|
} else {
|
|
|
|
_omega = 52;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool goingUp = false;
|
|
|
|
float step = 10;
|
2006-02-26 16:46:51 +00:00
|
|
|
|
|
|
|
// If we cannot manage this in 100 iterations, give up.
|
|
|
|
for (int n = 0; n < 100; n++) {
|
2004-05-18 01:46:36 +00:00
|
|
|
float ptau, thrust;
|
|
|
|
_prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &ptau);
|
2001-12-24 13:54:03 +00:00
|
|
|
_eng->calc(_pressure, _temp, _omega);
|
2004-05-01 00:26:33 +00:00
|
|
|
_eng->stabilize();
|
2004-05-18 01:46:36 +00:00
|
|
|
|
2006-02-27 23:35:02 +00:00
|
|
|
// Do it again -- the turbo sets the target MP in the first
|
|
|
|
// run, stabilize sets the current to the target, then we need
|
|
|
|
// to run again to get the correct output torque. Clumsy, but
|
|
|
|
// it works without side effects (other than solver
|
|
|
|
// performance). In the future, the Engine objects should
|
|
|
|
// store state to allow them to do the work themselves.
|
|
|
|
_eng->calc(_pressure, _temp, _omega);
|
|
|
|
|
2005-03-22 18:17:08 +00:00
|
|
|
// Compute torque as seen by the engine's end of the gearbox.
|
|
|
|
// The propeller will be moving more slowly (for gear ratios
|
|
|
|
// less than one), so it's torque will be higher than the
|
|
|
|
// engine's, so multiply by _gearRatio to get the engine-side
|
|
|
|
// value.
|
2004-05-18 01:46:36 +00:00
|
|
|
ptau *= _gearRatio;
|
2001-12-24 13:54:03 +00:00
|
|
|
float etau = _eng->getTorque();
|
2001-12-06 18:13:24 +00:00
|
|
|
float tdiff = etau - ptau;
|
2004-05-18 01:46:36 +00:00
|
|
|
|
|
|
|
Math::mul3(thrust, _dir, _thrust);
|
2001-12-06 18:13:24 +00:00
|
|
|
|
2004-05-18 01:46:36 +00:00
|
|
|
if(Math::abs(tdiff/(_moment * _gearRatio)) < 0.1)
|
2001-12-06 18:13:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if(tdiff > 0) {
|
2002-05-10 23:35:06 +00:00
|
|
|
if(!goingUp) step *= 0.5f;
|
2001-12-06 18:13:24 +00:00
|
|
|
goingUp = true;
|
|
|
|
if(!_variable) _omega += step;
|
2002-05-10 23:35:06 +00:00
|
|
|
else _prop->modPitch(1+(step*0.005f));
|
2001-12-06 18:13:24 +00:00
|
|
|
} else {
|
2002-05-10 23:35:06 +00:00
|
|
|
if(goingUp) step *= 0.5f;
|
2001-12-06 18:13:24 +00:00
|
|
|
goingUp = false;
|
|
|
|
if(!_variable) _omega -= step;
|
2002-05-10 23:35:06 +00:00
|
|
|
else _prop->modPitch(1-(step*0.005f));
|
2001-12-06 18:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-27 00:18:57 +00:00
|
|
|
|
|
|
|
// ...and back off
|
2004-07-20 22:17:58 +00:00
|
|
|
_eng->setRunning(running_state);
|
2001-12-06 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
2002-02-27 00:41:57 +00:00
|
|
|
void PropEngine::init()
|
|
|
|
{
|
2002-05-10 23:35:06 +00:00
|
|
|
_omega = 0.01f;
|
2002-02-27 00:41:57 +00:00
|
|
|
_eng->setStarter(false);
|
|
|
|
_eng->setMagnetos(0);
|
|
|
|
}
|
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
void PropEngine::integrate(float dt)
|
|
|
|
{
|
|
|
|
float speed = -Math::dot3(_wind, _dir);
|
|
|
|
|
|
|
|
float propTorque, engTorque, thrust;
|
|
|
|
|
|
|
|
_eng->setThrottle(_throttle);
|
2002-02-20 04:27:22 +00:00
|
|
|
_eng->setStarter(_starter);
|
|
|
|
_eng->setMagnetos(_magnetos);
|
2001-12-01 06:22:24 +00:00
|
|
|
_eng->setMixture(_mixture);
|
2002-11-30 06:12:20 +00:00
|
|
|
_eng->setFuelState(_fuel);
|
2001-12-01 06:22:24 +00:00
|
|
|
|
2004-01-24 23:09:41 +00:00
|
|
|
_prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &propTorque);
|
2005-03-23 18:54:58 +00:00
|
|
|
if(_omega == 0.0)
|
|
|
|
_omega = 0.001; // hack to get around reports of NaNs somewhere...
|
2005-03-22 18:17:08 +00:00
|
|
|
propTorque *= _gearRatio;
|
2001-12-24 13:54:03 +00:00
|
|
|
_eng->calc(_pressure, _temp, _omega);
|
2004-05-01 00:26:33 +00:00
|
|
|
_eng->integrate(dt);
|
2001-12-24 13:54:03 +00:00
|
|
|
engTorque = _eng->getTorque();
|
|
|
|
_fuelFlow = _eng->getFuelFlow();
|
2001-12-01 06:22:24 +00:00
|
|
|
|
|
|
|
// Turn the thrust into a vector and save it
|
|
|
|
Math::mul3(thrust, _dir, _thrust);
|
|
|
|
|
2004-05-18 01:46:36 +00:00
|
|
|
// We do our "RPM" computations on the engine's side of the
|
|
|
|
// world, so modify the moment value accordingly.
|
|
|
|
float momt = _moment * _gearRatio;
|
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
// Euler-integrate the RPM. This doesn't need the full-on
|
|
|
|
// Runge-Kutta stuff.
|
2004-05-18 01:46:36 +00:00
|
|
|
float rotacc = (engTorque-propTorque)/Math::abs(momt);
|
2001-12-06 18:13:24 +00:00
|
|
|
_omega += dt * rotacc;
|
2003-08-06 21:38:01 +00:00
|
|
|
if (_omega < 0)
|
|
|
|
_omega = 0 - _omega; // don't allow negative RPM
|
|
|
|
// FIXME: introduce proper windmilling
|
2001-12-06 18:13:24 +00:00
|
|
|
|
2004-12-13 23:48:43 +00:00
|
|
|
// Store the total angular momentum into _gyro, unless the
|
|
|
|
// propeller is a counter-rotating pair (which has zero net
|
|
|
|
// angular momentum, even though it *does* have an MoI for
|
|
|
|
// acceleration purposes).
|
|
|
|
Math::mul3(_contra ? 0 : _omega*momt, _dir, _gyro);
|
2001-12-01 06:22:24 +00:00
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
// Accumulate the engine torque, it acts on the body as a whole.
|
2001-12-01 06:22:24 +00:00
|
|
|
// (Note: engine torque, not propeller torque. They can be
|
|
|
|
// different, but the difference goes to accelerating the
|
|
|
|
// rotation. It is the engine torque that is felt at the shaft
|
2004-12-13 23:48:43 +00:00
|
|
|
// and works on the body.) (Note 2: contra-rotating propellers do
|
|
|
|
// not exert net torque on the aircraft).
|
2001-12-01 06:22:24 +00:00
|
|
|
float tau = _moment < 0 ? engTorque : -engTorque;
|
2004-12-13 23:48:43 +00:00
|
|
|
Math::mul3(_contra ? 0 : tau, _dir, _torque);
|
2001-12-06 18:13:24 +00:00
|
|
|
|
2001-12-10 23:13:54 +00:00
|
|
|
// Iterate the propeller governor, if we have one. Since engine
|
|
|
|
// torque is basically constant with RPM, we want to make the
|
|
|
|
// propeller torque at the target RPM equal to the engine by
|
|
|
|
// varying the pitch. Assume the the torque goes as the square of
|
|
|
|
// the RPM (roughly correct) and compute a "target" torque for the
|
|
|
|
// _current_ RPM. Seek to that. This is sort of a continuous
|
|
|
|
// Newton-Raphson, basically.
|
|
|
|
if(_variable) {
|
2005-01-26 18:30:45 +00:00
|
|
|
float targetPropSpd = _minOmega + _advance*(_maxOmega-_minOmega);
|
|
|
|
float targetOmega = targetPropSpd / _gearRatio; // -> "engine omega"
|
2001-12-10 23:13:54 +00:00
|
|
|
float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
|
|
|
|
float targetTorque = engTorque * ratio2;
|
|
|
|
|
2002-05-10 23:35:06 +00:00
|
|
|
float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
|
2001-12-10 23:13:54 +00:00
|
|
|
|
|
|
|
// Convert to an acceleration here, so that big propellers
|
|
|
|
// don't seek faster than small ones.
|
2004-05-18 01:46:36 +00:00
|
|
|
float diff = Math::abs((propTorque - targetTorque) / momt);
|
2002-05-10 23:35:06 +00:00
|
|
|
if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
|
2001-12-10 23:13:54 +00:00
|
|
|
|
2001-12-06 18:13:24 +00:00
|
|
|
_prop->modPitch(mod);
|
|
|
|
}
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace yasim
|