1 #include "Atmosphere.hpp"
3 #include "PistonEngine.hpp"
6 const static float HP2W = 745.7f;
7 const static float CIN2CM = 1.6387064e-5f;
8 const static float RPM2RADPS = 0.1047198f;
10 PistonEngine::PistonEngine(float power, float speed)
18 _oilTemp = Atmosphere::getStdTemperature(0);
19 _oilTempTarget = _oilTemp;
22 // Presume a BSFC (in lb/hour per HP) of 0.45. In SI that becomes
23 // (2.2 lb/kg, 745.7 W/hp, 3600 sec/hour) 7.62e-08 kg/Ws.
24 _f0 = power * 7.62e-08f;
29 // We must be at sea level under standard conditions
30 _rho0 = Atmosphere::getStdDensity(0);
32 // Further presume that takeoff is (duh) full throttle and
33 // peak-power, that means that by our efficiency function, we are
34 // at 11/8 of "ideal" fuel flow.
35 float realFlow = _f0 * (11.0f/8.0f);
36 _mixCoeff = realFlow * 1.1f / _omega0;
40 _maxMP = 1e6; // No waste gate on non-turbo engines.
46 // Guess at reasonable values for these guys. Displacements run
47 // at about 2 cubic inches per horsepower or so, at least for
48 // non-turbocharged engines.
50 _displacement = power * (2*CIN2CM/HP2W);
53 void PistonEngine::setTurboParams(float turbo, float maxMP)
58 // This changes the "sea level" manifold air density
59 float P0 = Atmosphere::getStdPressure(0);
60 float P = P0 * (1 + _boost * (_turbo - 1));
61 if(P > _maxMP) P = _maxMP;
62 float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
63 _rho0 = P / (287.1f * T);
66 void PistonEngine::setDisplacement(float d)
71 void PistonEngine::setCompression(float c)
76 void PistonEngine::setMinThrottle(float m)
81 float PistonEngine::getMaxPower()
86 bool PistonEngine::isCranking()
91 float PistonEngine::getTorque()
96 float PistonEngine::getFuelFlow()
101 float PistonEngine::getMP()
106 float PistonEngine::getEGT()
111 void PistonEngine::stabilize()
113 _oilTemp = _oilTempTarget;
114 _charge = _chargeTarget;
117 void PistonEngine::integrate(float dt)
119 _oilTemp += (_dOilTempdt * dt);
121 // See comments in Jet.cpp for how this decay constant works
122 float decay = 2.3f / _turboLag;
123 _charge = (_charge + dt*decay * _chargeTarget) / (1 + dt*decay);
126 void PistonEngine::calc(float pressure, float temp, float speed)
128 _running = _magnetos && _fuel && (speed > 60*RPM2RADPS);
130 // Calculate the factor required to modify supercharger output for
131 // rpm. Assume that the normalized supercharger output ~= 1 when
132 // the engine is at the nominal peak-power rpm. A power equation
133 // of the form (A * B^x * x^C) has been derived empirically from
134 // some representative supercharger data. This provides
135 // near-linear output over the normal operating range, with
136 // fall-off in the over-speed situation.
137 float rpm_norm = (speed / _omega0);
138 float A = 1.795206541;
139 float B = 0.55620178;
140 float C = 1.246708471;
141 float rpm_factor = A * Math::pow(B, rpm_norm) * Math::pow(rpm_norm, C);
142 _chargeTarget = 1 + (_boost * (_turbo-1) * rpm_factor);
145 // Superchargers have no lag
146 _charge = _chargeTarget;
147 } else if(!_running) {
148 // Turbochargers only work well when the engine is actually
149 // running. The 25% number is a guesstimate from Vivian.
150 _chargeTarget = 1 + (_chargeTarget - 1) * 0.25;
153 // We need to adjust the minimum manifold pressure to get a
154 // reasonable idle speed (a "closed" throttle doesn't suck a total
155 // vacuum in real manifolds). This is a hack.
156 float _minMP = (-0.008 * _turbo ) + _minthrottle;
158 _mp = pressure * _charge;
160 // Scale to throttle setting, clamp to wastegate
162 _mp *= _minMP + (1 -_minMP) * _throttle;
164 // Scale the max MP according to the WASTEGATE control input. Use
165 // the un-supercharged MP as the bottom limit.
166 float max = _wastegate * _maxMP;
167 if(max < _mp/_charge) max = _mp/_charge;
168 if(_mp > max) _mp = max;
171 // The "boost" is the delta above ambient
172 _boostPressure = _mp - pressure;
174 // Air entering the manifold does so rapidly, and thus the
175 // pressure change can be assumed to be adiabatic. Calculate a
176 // temperature change, and use that to get the density.
177 // Note: need to model intercoolers here...
178 float T = temp * Math::pow((_mp*_mp)/(pressure*pressure), 1.0/7.0);
179 float rho = _mp / (287.1f * T);
181 // The actual fuel flow is determined only by engine RPM and the
182 // mixture setting. Not all of this will burn with the same
184 _fuelFlow = _mixture * speed * _mixCoeff;
185 if(_fuel == false) _fuelFlow = 0;
187 // How much fuel could be burned with ideal (i.e. uncorrected!)
189 float burnable = _f0 * (rho/_rho0) * (speed/_omega0);
191 // Calculate the fuel that actually burns to produce work. The
192 // idea is that less than 5/8 of ideal, we get complete
193 // combustion. We use up all the oxygen at 1 3/8 of ideal (that
194 // is, you need to waste fuel to use all your O2). In between,
195 // interpolate. This vaguely matches a curve I copied out of a
196 // book for a single engine. Shrug.
198 float r = _fuelFlow/burnable;
199 if (burnable == 0) burned = 0;
200 else if(r < .625) burned = _fuelFlow;
201 else if(r > 1.375) burned = burnable;
203 burned = _fuelFlow + (burnable-_fuelFlow)*(r-0.625f)*(4.0f/3.0f);
205 // Correct for engine control state
211 // And finally the power is just the reference power scaled by the
212 // amount of fuel burned, and torque is that divided by RPM.
213 float power = _power0 * burned/_f0;
214 _torque = power/speed;
216 // Figure that the starter motor produces 15% of the engine's
217 // cruise torque. Assuming 60RPM starter speed vs. 1800RPM cruise
218 // speed on a 160HP engine, that comes out to about 160*.15/30 ==
219 // 0.8 HP starter motor. Which sounds about right to me. I think
220 // I've finally got this tuned. :)
221 if(_starter && !_running)
222 _torque += 0.15f * _power0/_omega0;
224 // Also, add a negative torque of 8% of cruise, to represent
225 // internal friction. Propeller aerodynamic friction is too low
226 // at low RPMs to provide a good deceleration. Interpolate it
227 // away as we approach cruise RPMs (full at 50%, zero at 100%),
228 // though, to prevent interaction with the power computations.
230 if(speed > 0 && speed < _omega0) {
231 float interp = 2 - 2*speed/_omega0;
232 interp = (interp > 1) ? 1 : interp;
233 _torque -= 0.08f * (_power0/_omega0) * interp;
236 // Now EGT. This one gets a little goofy. We can calculate the
237 // work done by an isentropically expanding exhaust gas as the
238 // mass of the gas times the specific heat times the change in
239 // temperature. The mass is just the engine displacement times
240 // the manifold density, plus the mass of the fuel, which we know.
241 // The change in temperature can be calculated adiabatically as a
242 // function of the exhaust gas temperature and the compression
243 // ratio (which we know). So just rearrange the equation to get
244 // EGT as a function of engine power. Cool. I'm using a value of
245 // 1300 J/(kg*K) for the exhaust gas specific heat. I found this
246 // on a web page somewhere; no idea if it's accurate. Also,
247 // remember that four stroke engines do one combustion cycle every
248 // TWO revolutions, so the displacement per revolution is half of
249 // what we'd expect. And diddle the work done by the gas a bit to
250 // account for non-thermodynamic losses like internal friction;
252 float massFlow = _fuelFlow + (rho * 0.5f * _displacement * speed);
253 float specHeat = 1300;
254 float corr = 1.0f/(Math::pow(_compression, 0.4f) - 1.0f);
255 _egt = corr * (power * 1.1f) / (massFlow * specHeat);
256 if(_egt < temp) _egt = temp;
260 // Assume a linear variation between ~90degC at idle and ~120degC
261 // at full power. No attempt to correct for airflow over the
262 // engine is made. Make the time constant to attain target steady-
263 // state oil temp greater at engine off than on to reflect no
264 // circulation. Nothing fancy, but populates the guage with a
268 _oilTempTarget = 363.0f + (30.0f * (power/_power0));
270 // Reduce tau linearly to 300 at max power
271 tau -= (power/_power0) * 300.0f;
273 _oilTempTarget = temp;
276 _dOilTempdt = (_oilTempTarget - _oilTemp) / tau;
279 }; // namespace yasim