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)
17 _oilTemp = Atmosphere::getStdTemperature(0);
18 _oilTempTarget = _oilTemp;
21 // Presume a BSFC (in lb/hour per HP) of 0.45. In SI that becomes
22 // (2.2 lb/kg, 745.7 W/hp, 3600 sec/hour) 7.62e-08 kg/Ws.
23 _f0 = power * 7.62e-08f;
28 // We must be at sea level under standard conditions
29 _rho0 = Atmosphere::getStdDensity(0);
31 // Further presume that takeoff is (duh) full throttle and
32 // peak-power, that means that by our efficiency function, we are
33 // at 11/8 of "ideal" fuel flow.
34 float realFlow = _f0 * (11.0f/8.0f);
35 _mixCoeff = realFlow * 1.1f / _omega0;
38 _maxMP = 1e6; // No waste gate on non-turbo engines.
40 // Guess at reasonable values for these guys. Displacements run
41 // at about 2 cubic inches per horsepower or so, at least for
42 // non-turbocharged engines.
44 _displacement = power * (2*CIN2CM/HP2W);
47 void PistonEngine::setTurboParams(float turbo, float maxMP)
52 // This changes the "sea level" manifold air density
53 float P0 = Atmosphere::getStdPressure(0);
54 float P = P0 * (1 + _boost * (_turbo - 1));
55 if(P > _maxMP) P = _maxMP;
56 float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
57 _rho0 = P / (287.1f * T);
60 void PistonEngine::setDisplacement(float d)
65 void PistonEngine::setCompression(float c)
70 float PistonEngine::getMaxPower()
75 bool PistonEngine::isCranking()
80 float PistonEngine::getTorque()
85 float PistonEngine::getFuelFlow()
90 float PistonEngine::getMP()
95 float PistonEngine::getEGT()
100 void PistonEngine::stabilize()
102 _oilTemp = _oilTempTarget;
105 void PistonEngine::integrate(float dt)
107 _oilTemp += (_dOilTempdt * dt);
110 void PistonEngine::calc(float pressure, float temp, float speed)
112 if(_magnetos == 0 || speed < 60*RPM2RADPS)
114 else if(_fuel == false)
119 // Calculate the factor required to modify supercharger output for
120 // rpm. Assume that the normalized supercharger output ~= 1 when
121 // the engine is at the nominated peak-power rpm (normalised).
122 // A power equation of the form (A * B^x * x^C) has been
123 // derived empirically from some representative supercharger data.
124 // This provides near-linear output over the normal operating range,
125 // with fall-off in the over-speed situation.
126 float rpm_norm = (speed / _omega0);
127 float A = 1.795206541;
128 float B = 0.55620178;
129 float C = 1.246708471;
130 float rpm_factor = A * Math::pow(B, rpm_norm) * Math::pow(rpm_norm, C);
132 // We need to adjust the minimum manifold pressure to get a
133 // reasonable idle speed (a "closed" throttle doesn't suck a total
134 // vacuum in real manifolds). This is a hack.
135 float _minMP = (-0.008 * _turbo ) + 0.1;
137 // Scale to throttle setting, clamp to wastegate
139 _mp = pressure * (1 + (_boost * (_turbo-1) * rpm_factor));
140 _mp *= _minMP + (1 -_minMP) * _throttle;
142 if(_mp > _maxMP) _mp = _maxMP;
144 // The "boost" is the delta above ambient
145 _boostPressure = _mp - pressure;
147 // Air entering the manifold does so rapidly, and thus the
148 // pressure change can be assumed to be adiabatic. Calculate a
149 // temperature change, and use that to get the density.
150 // Note: need to model intercoolers here...
151 float T = temp * Math::pow(_mp/pressure, 2.0/7.0);
152 float rho = _mp / (287.1f * T);
154 // The actual fuel flow is determined only by engine RPM and the
155 // mixture setting. Not all of this will burn with the same
157 _fuelFlow = _mixture * speed * _mixCoeff;
158 if(_fuel == false) _fuelFlow = 0;
160 // How much fuel could be burned with ideal (i.e. uncorrected!)
162 float burnable = _f0 * (rho/_rho0) * (speed/_omega0);
164 // Calculate the fuel that actually burns to produce work. The
165 // idea is that less than 5/8 of ideal, we get complete
166 // combustion. We use up all the oxygen at 1 3/8 of ideal (that
167 // is, you need to waste fuel to use all your O2). In between,
168 // interpolate. This vaguely matches a curve I copied out of a
169 // book for a single engine. Shrug.
171 float r = _fuelFlow/burnable;
172 if (burnable == 0) burned = 0;
173 else if(r < .625) burned = _fuelFlow;
174 else if(r > 1.375) burned = burnable;
176 burned = _fuelFlow + (burnable-_fuelFlow)*(r-0.625f)*(4.0f/3.0f);
178 // Correct for engine control state
184 // And finally the power is just the reference power scaled by the
185 // amount of fuel burned, and torque is that divided by RPM.
186 float power = _power0 * burned/_f0;
187 _torque = power/speed;
189 // Figure that the starter motor produces 15% of the engine's
190 // cruise torque. Assuming 60RPM starter speed vs. 1800RPM cruise
191 // speed on a 160HP engine, that comes out to about 160*.15/30 ==
192 // 0.8 HP starter motor. Which sounds about right to me. I think
193 // I've finally got this tuned. :)
194 if(_starter && !_running)
195 _torque += 0.15f * _power0/_omega0;
197 // Also, add a negative torque of 8% of cruise, to represent
198 // internal friction. Propeller aerodynamic friction is too low
199 // at low RPMs to provide a good deceleration. Interpolate it
200 // away as we approach cruise RPMs (full at 50%, zero at 100%),
201 // though, to prevent interaction with the power computations.
203 if(speed > 0 && speed < _omega0) {
204 float interp = 2 - 2*speed/_omega0;
205 interp = (interp > 1) ? 1 : interp;
206 _torque -= 0.08f * (_power0/_omega0) * interp;
209 // Now EGT. This one gets a little goofy. We can calculate the
210 // work done by an isentropically expanding exhaust gas as the
211 // mass of the gas times the specific heat times the change in
212 // temperature. The mass is just the engine displacement times
213 // the manifold density, plus the mass of the fuel, which we know.
214 // The change in temperature can be calculated adiabatically as a
215 // function of the exhaust gas temperature and the compression
216 // ratio (which we know). So just rearrange the equation to get
217 // EGT as a function of engine power. Cool. I'm using a value of
218 // 1300 J/(kg*K) for the exhaust gas specific heat. I found this
219 // on a web page somewhere; no idea if it's accurate. Also,
220 // remember that four stroke engines do one combustion cycle every
221 // TWO revolutions, so the displacement per revolution is half of
222 // what we'd expect. And diddle the work done by the gas a bit to
223 // account for non-thermodynamic losses like internal friction;
225 float massFlow = _fuelFlow + (rho * 0.5f * _displacement * speed);
226 float specHeat = 1300;
227 float corr = 1.0f/(Math::pow(_compression, 0.4f) - 1.0f);
228 _egt = corr * (power * 1.1f) / (massFlow * specHeat);
229 if(_egt < temp) _egt = temp;
233 // Assume a linear variation between ~90degC at idle and ~120degC
234 // at full power. No attempt to correct for airflow over the
235 // engine is made. Make the time constant to attain target steady-
236 // state oil temp greater at engine off than on to reflect no
237 // circulation. Nothing fancy, but populates the guage with a
241 _oilTempTarget = 363.0f + (30.0f * (power/_power0));
243 // Reduce tau linearly to 300 at max power
244 tau -= (power/_power0) * 300.0f;
246 _oilTempTarget = temp;
249 _dOilTempdt = (_oilTempTarget - _oilTemp) / tau;
252 }; // namespace yasim