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;
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->setMagnetos(3);
101 _eng->setRunning(true);
104 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
105 _prop->modPitch(1e6); // Start at maximum pitch and move down
110 bool goingUp = false;
114 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
115 _eng->calc(_pressure, _temp, _omega);
116 float etau = _eng->getTorque();
117 float tdiff = etau - ptau;
119 if(Math::abs(tdiff/_moment) < 0.1)
123 if(!goingUp) step *= 0.5f;
125 if(!_variable) _omega += step;
126 else _prop->modPitch(1+(step*0.005f));
128 if(goingUp) step *= 0.5f;
130 if(!_variable) _omega -= step;
131 else _prop->modPitch(1-(step*0.005f));
136 _eng->setRunning(false);
139 void PropEngine::init()
142 _eng->setStarter(false);
143 _eng->setMagnetos(0);
146 void PropEngine::integrate(float dt)
148 float speed = -Math::dot3(_wind, _dir);
150 float propTorque, engTorque, thrust;
152 _eng->setThrottle(_throttle);
153 _eng->setStarter(_starter);
154 _eng->setMagnetos(_magnetos);
155 _eng->setMixture(_mixture);
156 _eng->setFuelState(_fuel);
158 _prop->calc(_rho, speed, _omega * _gearRatio, &thrust, &propTorque);
159 _eng->calc(_pressure, _temp, _omega);
160 engTorque = _eng->getTorque();
161 _fuelFlow = _eng->getFuelFlow();
163 // Turn the thrust into a vector and save it
164 Math::mul3(thrust, _dir, _thrust);
166 // Euler-integrate the RPM. This doesn't need the full-on
167 // Runge-Kutta stuff.
168 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
169 _omega += dt * rotacc;
171 _omega = 0 - _omega; // don't allow negative RPM
172 // FIXME: introduce proper windmilling
174 // Store the total angular momentum into _gyro
175 Math::mul3(_omega*_moment, _dir, _gyro);
177 // Accumulate the engine torque, it acts on the body as a whole.
178 // (Note: engine torque, not propeller torque. They can be
179 // different, but the difference goes to accelerating the
180 // rotation. It is the engine torque that is felt at the shaft
181 // and works on the body.)
182 float tau = _moment < 0 ? engTorque : -engTorque;
183 Math::mul3(tau, _dir, _torque);
185 // Iterate the propeller governor, if we have one. Since engine
186 // torque is basically constant with RPM, we want to make the
187 // propeller torque at the target RPM equal to the engine by
188 // varying the pitch. Assume the the torque goes as the square of
189 // the RPM (roughly correct) and compute a "target" torque for the
190 // _current_ RPM. Seek to that. This is sort of a continuous
191 // Newton-Raphson, basically.
193 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
194 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
195 float targetTorque = engTorque * ratio2;
197 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
199 // Convert to an acceleration here, so that big propellers
200 // don't seek faster than small ones.
201 float diff = Math::abs((propTorque - targetTorque) / _moment);
202 if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
204 _prop->modPitch(mod);
208 }; // namespace yasim