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::setMagnetos(int pos)
31 void PropEngine::setAdvance(float advance)
33 _advance = Math::clamp(advance, 0, 1);
36 void PropEngine::setVariableProp(float min, float max)
43 bool PropEngine::isRunning()
45 return _eng->isRunning();
48 bool PropEngine::isCranking()
50 return _eng->isCranking();
53 float PropEngine::getOmega()
58 void PropEngine::getThrust(float* out)
61 for(i=0; i<3; i++) out[i] = _thrust[i];
64 void PropEngine::getTorque(float* out)
67 for(i=0; i<3; i++) out[i] = _torque[i];
70 void PropEngine::getGyro(float* out)
73 for(i=0; i<3; i++) out[i] = _gyro[i];
76 float PropEngine::getFuelFlow()
81 void PropEngine::stabilize()
83 float speed = -Math::dot3(_wind, _dir);
84 _eng->setThrottle(_throttle);
85 _eng->setMixture(_mixture);
88 _eng->setRunning(true);
91 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
92 _prop->modPitch(1e6); // Start at maximum pitch and move down
101 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
102 _eng->calc(_pressure, _temp, _omega);
103 float etau = _eng->getTorque();
104 float tdiff = etau - ptau;
106 if(Math::abs(tdiff/_moment) < 0.1)
110 if(!goingUp) step *= 0.5;
112 if(!_variable) _omega += step;
113 else _prop->modPitch(1+(step*0.005));
115 if(goingUp) step *= 0.5;
117 if(!_variable) _omega -= step;
118 else _prop->modPitch(1-(step*0.005));
123 _eng->setRunning(false);
126 void PropEngine::integrate(float dt)
128 float speed = -Math::dot3(_wind, _dir);
130 float propTorque, engTorque, thrust;
132 _eng->setThrottle(_throttle);
133 _eng->setStarter(_starter);
134 _eng->setMagnetos(_magnetos);
135 _eng->setMixture(_mixture);
137 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
138 _eng->calc(_pressure, _temp, _omega);
139 engTorque = _eng->getTorque();
140 _fuelFlow = _eng->getFuelFlow();
142 // Turn the thrust into a vector and save it
143 Math::mul3(thrust, _dir, _thrust);
145 // Euler-integrate the RPM. This doesn't need the full-on
146 // Runge-Kutta stuff.
147 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
148 _omega += dt * rotacc;
150 // Store the total angular momentum into _gyro
151 Math::mul3(_omega*_moment, _dir, _gyro);
153 // Accumulate the engine torque, it acts on the body as a whole.
154 // (Note: engine torque, not propeller torque. They can be
155 // different, but the difference goes to accelerating the
156 // rotation. It is the engine torque that is felt at the shaft
157 // and works on the body.)
158 float tau = _moment < 0 ? engTorque : -engTorque;
159 Math::mul3(tau, _dir, _torque);
161 // Iterate the propeller governor, if we have one. Since engine
162 // torque is basically constant with RPM, we want to make the
163 // propeller torque at the target RPM equal to the engine by
164 // varying the pitch. Assume the the torque goes as the square of
165 // the RPM (roughly correct) and compute a "target" torque for the
166 // _current_ RPM. Seek to that. This is sort of a continuous
167 // Newton-Raphson, basically.
169 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
170 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
171 float targetTorque = engTorque * ratio2;
173 float mod = propTorque < targetTorque ? 1.04 : (1/1.04);
175 // Convert to an acceleration here, so that big propellers
176 // don't seek faster than small ones.
177 float diff = Math::abs((propTorque - targetTorque) / _moment);
178 if(diff < 10) mod = 1 + (mod-1)*(0.1*diff);
180 _prop->modPitch(mod);
184 }; // namespace yasim