2 #include "Propeller.hpp"
4 #include "PropEngine.hpp"
7 PropEngine::PropEngine(Propeller* prop, Engine* 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;
23 PropEngine::~PropEngine()
29 void PropEngine::setMagnetos(int pos)
34 void PropEngine::setAdvance(float advance)
36 _advance = Math::clamp(advance, 0, 1);
39 void PropEngine::setPropPitch(float proppitch)
41 // update Propeller property
42 _prop->setPropPitch(proppitch);
45 void PropEngine::setPropFeather(int state)
47 // toggle prop feathering on/off
48 _prop->setPropFeather(state);
51 void PropEngine::setVariableProp(float min, float max)
58 bool PropEngine::isRunning()
60 return _eng->isRunning();
63 bool PropEngine::isCranking()
65 return _eng->isCranking();
68 float PropEngine::getOmega()
73 void PropEngine::setOmega (float omega)
78 void PropEngine::getThrust(float* out)
81 for(i=0; i<3; i++) out[i] = _thrust[i];
84 void PropEngine::getTorque(float* out)
87 for(i=0; i<3; i++) out[i] = _torque[i];
90 void PropEngine::getGyro(float* out)
93 for(i=0; i<3; i++) out[i] = _gyro[i];
96 float PropEngine::getFuelFlow()
101 void PropEngine::stabilize()
103 float speed = -Math::dot3(_wind, _dir);
104 _eng->setThrottle(_throttle);
105 _eng->setMixture(_mixture);
107 _eng->setStarter(false);
108 _eng->setMagnetos(3);
110 bool running_state = _eng->isRunning();
111 _eng->setRunning(true);
114 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
115 _prop->modPitch(1e6); // Start at maximum pitch and move down
120 bool goingUp = false;
123 // If we cannot manage this in 100 iterations, give up.
124 for (int n = 0; n < 100; n++) {
126 _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &ptau);
127 _eng->calc(_pressure, _temp, _omega);
130 // Compute torque as seen by the engine's end of the gearbox.
131 // The propeller will be moving more slowly (for gear ratios
132 // less than one), so it's torque will be higher than the
133 // engine's, so multiply by _gearRatio to get the engine-side
136 float etau = _eng->getTorque();
137 float tdiff = etau - ptau;
139 Math::mul3(thrust, _dir, _thrust);
141 if(Math::abs(tdiff/(_moment * _gearRatio)) < 0.1)
145 if(!goingUp) step *= 0.5f;
147 if(!_variable) _omega += step;
148 else _prop->modPitch(1+(step*0.005f));
150 if(goingUp) step *= 0.5f;
152 if(!_variable) _omega -= step;
153 else _prop->modPitch(1-(step*0.005f));
158 _eng->setRunning(running_state);
161 void PropEngine::init()
164 _eng->setStarter(false);
165 _eng->setMagnetos(0);
168 void PropEngine::integrate(float dt)
170 float speed = -Math::dot3(_wind, _dir);
172 float propTorque, engTorque, thrust;
174 _eng->setThrottle(_throttle);
175 _eng->setStarter(_starter);
176 _eng->setMagnetos(_magnetos);
177 _eng->setMixture(_mixture);
178 _eng->setFuelState(_fuel);
180 _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &propTorque);
182 _omega = 0.001; // hack to get around reports of NaNs somewhere...
183 propTorque *= _gearRatio;
184 _eng->calc(_pressure, _temp, _omega);
186 engTorque = _eng->getTorque();
187 _fuelFlow = _eng->getFuelFlow();
189 // Turn the thrust into a vector and save it
190 Math::mul3(thrust, _dir, _thrust);
192 // We do our "RPM" computations on the engine's side of the
193 // world, so modify the moment value accordingly.
194 float momt = _moment * _gearRatio;
196 // Euler-integrate the RPM. This doesn't need the full-on
197 // Runge-Kutta stuff.
198 float rotacc = (engTorque-propTorque)/Math::abs(momt);
199 _omega += dt * rotacc;
201 _omega = 0 - _omega; // don't allow negative RPM
202 // FIXME: introduce proper windmilling
204 // Store the total angular momentum into _gyro, unless the
205 // propeller is a counter-rotating pair (which has zero net
206 // angular momentum, even though it *does* have an MoI for
207 // acceleration purposes).
208 Math::mul3(_contra ? 0 : _omega*momt, _dir, _gyro);
210 // Accumulate the engine torque, it acts on the body as a whole.
211 // (Note: engine torque, not propeller torque. They can be
212 // different, but the difference goes to accelerating the
213 // rotation. It is the engine torque that is felt at the shaft
214 // and works on the body.) (Note 2: contra-rotating propellers do
215 // not exert net torque on the aircraft).
216 float tau = _moment < 0 ? engTorque : -engTorque;
217 Math::mul3(_contra ? 0 : tau, _dir, _torque);
219 // Iterate the propeller governor, if we have one. Since engine
220 // torque is basically constant with RPM, we want to make the
221 // propeller torque at the target RPM equal to the engine by
222 // varying the pitch. Assume the the torque goes as the square of
223 // the RPM (roughly correct) and compute a "target" torque for the
224 // _current_ RPM. Seek to that. This is sort of a continuous
225 // Newton-Raphson, basically.
227 float targetPropSpd = _minOmega + _advance*(_maxOmega-_minOmega);
228 float targetOmega = targetPropSpd / _gearRatio; // -> "engine omega"
229 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
230 float targetTorque = engTorque * ratio2;
232 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
234 // Convert to an acceleration here, so that big propellers
235 // don't seek faster than small ones.
236 float diff = Math::abs((propTorque - targetTorque) / momt);
237 if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
239 _prop->modPitch(mod);
243 }; // namespace yasim