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;
22 PropEngine::~PropEngine()
28 void PropEngine::setMagnetos(int pos)
33 void PropEngine::setAdvance(float advance)
35 _advance = Math::clamp(advance, 0, 1);
38 void PropEngine::setPropPitch(float proppitch)
40 // update Propeller property
41 _prop->setPropPitch(proppitch);
44 void PropEngine::setVariableProp(float min, float max)
51 bool PropEngine::isRunning()
53 return _eng->isRunning();
56 bool PropEngine::isCranking()
58 return _eng->isCranking();
61 float PropEngine::getOmega()
66 void PropEngine::setOmega (float omega)
71 void PropEngine::getThrust(float* out)
74 for(i=0; i<3; i++) out[i] = _thrust[i];
77 void PropEngine::getTorque(float* out)
80 for(i=0; i<3; i++) out[i] = _torque[i];
83 void PropEngine::getGyro(float* out)
86 for(i=0; i<3; i++) out[i] = _gyro[i];
89 float PropEngine::getFuelFlow()
94 void PropEngine::stabilize()
96 float speed = -Math::dot3(_wind, _dir);
97 _eng->setThrottle(_throttle);
98 _eng->setMixture(_mixture);
100 _eng->setStarter(false);
101 _eng->setMagnetos(3);
102 _eng->setRunning(true);
105 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
106 _prop->modPitch(1e6); // Start at maximum pitch and move down
111 bool goingUp = false;
115 _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &ptau);
116 _eng->calc(_pressure, _temp, _omega);
119 // Compute torque as seen by the engine's end of the
122 float etau = _eng->getTorque();
123 float tdiff = etau - ptau;
125 Math::mul3(thrust, _dir, _thrust);
127 if(Math::abs(tdiff/(_moment * _gearRatio)) < 0.1)
131 if(!goingUp) step *= 0.5f;
133 if(!_variable) _omega += step;
134 else _prop->modPitch(1+(step*0.005f));
136 if(goingUp) step *= 0.5f;
138 if(!_variable) _omega -= step;
139 else _prop->modPitch(1-(step*0.005f));
144 _eng->setRunning(false);
147 void PropEngine::init()
150 _eng->setStarter(false);
151 _eng->setMagnetos(0);
154 void PropEngine::integrate(float dt)
156 float speed = -Math::dot3(_wind, _dir);
158 float propTorque, engTorque, thrust;
160 _eng->setThrottle(_throttle);
161 _eng->setStarter(_starter);
162 _eng->setMagnetos(_magnetos);
163 _eng->setMixture(_mixture);
164 _eng->setFuelState(_fuel);
166 _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &propTorque);
167 _eng->calc(_pressure, _temp, _omega);
169 engTorque = _eng->getTorque();
170 _fuelFlow = _eng->getFuelFlow();
172 // Turn the thrust into a vector and save it
173 Math::mul3(thrust, _dir, _thrust);
175 // We do our "RPM" computations on the engine's side of the
176 // world, so modify the moment value accordingly.
177 float momt = _moment * _gearRatio;
179 // Euler-integrate the RPM. This doesn't need the full-on
180 // Runge-Kutta stuff.
181 float rotacc = (engTorque-propTorque)/Math::abs(momt);
182 _omega += dt * rotacc;
184 _omega = 0 - _omega; // don't allow negative RPM
185 // FIXME: introduce proper windmilling
187 // Store the total angular momentum into _gyro
188 Math::mul3(_omega*momt, _dir, _gyro);
190 // Accumulate the engine torque, it acts on the body as a whole.
191 // (Note: engine torque, not propeller torque. They can be
192 // different, but the difference goes to accelerating the
193 // rotation. It is the engine torque that is felt at the shaft
194 // and works on the body.)
195 float tau = _moment < 0 ? engTorque : -engTorque;
196 Math::mul3(tau, _dir, _torque);
198 // Iterate the propeller governor, if we have one. Since engine
199 // torque is basically constant with RPM, we want to make the
200 // propeller torque at the target RPM equal to the engine by
201 // varying the pitch. Assume the the torque goes as the square of
202 // the RPM (roughly correct) and compute a "target" torque for the
203 // _current_ RPM. Seek to that. This is sort of a continuous
204 // Newton-Raphson, basically.
206 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
207 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
208 float targetTorque = engTorque * ratio2;
210 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
212 // Convert to an acceleration here, so that big propellers
213 // don't seek faster than small ones.
214 float diff = Math::abs((propTorque - targetTorque) / momt);
215 if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
217 _prop->modPitch(mod);
221 }; // namespace yasim