2 #include "Propeller.hpp"
3 #include "PistonEngine.hpp"
4 #include "PropEngine.hpp"
7 PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment)
9 // Start off at 500rpm, because the start code doesn't exist yet
11 _dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
20 PropEngine::~PropEngine()
26 void PropEngine::setAdvance(float advance)
28 _advance = Math::clamp(advance, 0, 1);
31 void PropEngine::setVariableProp(float min, float max)
38 float PropEngine::getOmega()
43 void PropEngine::getThrust(float* out)
45 for(int i=0; i<3; i++) out[i] = _thrust[i];
48 void PropEngine::getTorque(float* out)
50 for(int i=0; i<3; i++) out[i] = _torque[i];
53 void PropEngine::getGyro(float* out)
55 for(int i=0; i<3; i++) out[i] = _gyro[i];
58 float PropEngine::getFuelFlow()
63 void PropEngine::stabilize()
65 float speed = -Math::dot3(_wind, _dir);
66 _eng->setThrottle(_throttle);
67 _eng->setMixture(_mixture);
70 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
71 _prop->modPitch(1e6); // Start at maximum pitch and move down
79 float etau, ptau, dummy;
80 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
81 _eng->calc(_P, _T, _omega, &etau, &dummy);
82 float tdiff = etau - ptau;
84 if(Math::abs(tdiff/_moment) < 0.1)
88 if(!goingUp) step *= 0.5;
90 if(!_variable) _omega += step;
91 else _prop->modPitch(1+(step*0.005));
93 if(goingUp) step *= 0.5;
95 if(!_variable) _omega -= step;
96 else _prop->modPitch(1-(step*0.005));
101 void PropEngine::integrate(float dt)
103 float speed = -Math::dot3(_wind, _dir);
105 float propTorque, engTorque, thrust;
107 _eng->setThrottle(_throttle);
108 _eng->setMixture(_mixture);
110 _prop->calc(_rho, speed, _omega,
111 &thrust, &propTorque);
112 _eng->calc(_P, _T, _omega, &engTorque, &_fuelFlow);
114 // Turn the thrust into a vector and save it
115 Math::mul3(thrust, _dir, _thrust);
117 // Euler-integrate the RPM. This doesn't need the full-on
118 // Runge-Kutta stuff.
119 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
120 _omega += dt * rotacc;
122 // Clamp to a 500 rpm idle. This should probably be settable, and
123 // needs to go away when the startup code gets written.
124 if(_omega < 52.3) _omega = 52.3;
126 // FIXME: Integrate the propeller governor here, when that gets
129 // Store the total angular momentum into _gyro
130 Math::mul3(_omega*_moment, _dir, _gyro);
132 // Accumulate the engine torque, it acts on the body as a whole.
133 // (Note: engine torque, not propeller torque. They can be
134 // different, but the difference goes to accelerating the
135 // rotation. It is the engine torque that is felt at the shaft
136 // and works on the body.)
137 float tau = _moment < 0 ? engTorque : -engTorque;
138 Math::mul3(tau, _dir, _torque);
140 // Play with the variable propeller, but only if the propeller is
141 // vaguely stable alread (accelerating less than 100 rpm/s)
142 if(_variable && Math::abs(rotacc) < 20) {
143 float target = _minOmega + _advance*(_maxOmega-_minOmega);
145 if(target > _omega) mod = 1/mod;
146 float diff = Math::abs(target - _omega);
147 if(diff < 1) mod = 1 + (mod-1)*diff;
148 if(thrust < 0) mod = 1;
149 _prop->modPitch(mod);
153 }; // namespace yasim