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;
21 PropEngine::~PropEngine()
27 void PropEngine::setMagnetos(int pos)
32 void PropEngine::setAdvance(float advance)
34 _advance = Math::clamp(advance, 0, 1);
37 void PropEngine::setPropPitch(float proppitch)
39 // update Propeller property
40 _prop->setPropPitch(proppitch);
43 void PropEngine::setVariableProp(float min, float max)
50 bool PropEngine::isRunning()
52 return _eng->isRunning();
55 bool PropEngine::isCranking()
57 return _eng->isCranking();
60 float PropEngine::getOmega()
65 void PropEngine::setOmega (float omega)
70 void PropEngine::getThrust(float* out)
73 for(i=0; i<3; i++) out[i] = _thrust[i];
76 void PropEngine::getTorque(float* out)
79 for(i=0; i<3; i++) out[i] = _torque[i];
82 void PropEngine::getGyro(float* out)
85 for(i=0; i<3; i++) out[i] = _gyro[i];
88 float PropEngine::getFuelFlow()
93 void PropEngine::stabilize()
95 float speed = -Math::dot3(_wind, _dir);
96 _eng->setThrottle(_throttle);
97 _eng->setMixture(_mixture);
100 _eng->setRunning(true);
103 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
104 _prop->modPitch(1e6); // Start at maximum pitch and move down
109 bool goingUp = false;
113 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
114 _eng->calc(_pressure, _temp, _omega);
115 float etau = _eng->getTorque();
116 float tdiff = etau - ptau;
118 if(Math::abs(tdiff/_moment) < 0.1)
122 if(!goingUp) step *= 0.5f;
124 if(!_variable) _omega += step;
125 else _prop->modPitch(1+(step*0.005f));
127 if(goingUp) step *= 0.5f;
129 if(!_variable) _omega -= step;
130 else _prop->modPitch(1-(step*0.005f));
135 _eng->setRunning(false);
138 void PropEngine::init()
141 _eng->setStarter(false);
142 _eng->setMagnetos(0);
145 void PropEngine::integrate(float dt)
147 float speed = -Math::dot3(_wind, _dir);
149 float propTorque, engTorque, thrust;
151 _eng->setThrottle(_throttle);
152 _eng->setStarter(_starter);
153 _eng->setMagnetos(_magnetos);
154 _eng->setMixture(_mixture);
155 _eng->setFuelState(_fuel);
157 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
158 _eng->calc(_pressure, _temp, _omega);
159 engTorque = _eng->getTorque();
160 _fuelFlow = _eng->getFuelFlow();
162 // Turn the thrust into a vector and save it
163 Math::mul3(thrust, _dir, _thrust);
165 // Euler-integrate the RPM. This doesn't need the full-on
166 // Runge-Kutta stuff.
167 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
168 _omega += dt * rotacc;
170 _omega = 0 - _omega; // don't allow negative RPM
171 // FIXME: introduce proper windmilling
173 // Store the total angular momentum into _gyro
174 Math::mul3(_omega*_moment, _dir, _gyro);
176 // Accumulate the engine torque, it acts on the body as a whole.
177 // (Note: engine torque, not propeller torque. They can be
178 // different, but the difference goes to accelerating the
179 // rotation. It is the engine torque that is felt at the shaft
180 // and works on the body.)
181 float tau = _moment < 0 ? engTorque : -engTorque;
182 Math::mul3(tau, _dir, _torque);
184 // Iterate the propeller governor, if we have one. Since engine
185 // torque is basically constant with RPM, we want to make the
186 // propeller torque at the target RPM equal to the engine by
187 // varying the pitch. Assume the the torque goes as the square of
188 // the RPM (roughly correct) and compute a "target" torque for the
189 // _current_ RPM. Seek to that. This is sort of a continuous
190 // Newton-Raphson, basically.
192 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
193 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
194 float targetTorque = engTorque * ratio2;
196 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
198 // Convert to an acceleration here, so that big propellers
199 // don't seek faster than small ones.
200 float diff = Math::abs((propTorque - targetTorque) / _moment);
201 if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
203 _prop->modPitch(mod);
207 }; // namespace yasim