]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PistonEngine.cpp
Forgot to hook up the WASTEGATE control. Also, fix a cut-n-paste
[flightgear.git] / src / FDM / YASim / PistonEngine.cpp
1 #include "Atmosphere.hpp"
2 #include "Math.hpp"
3 #include "PistonEngine.hpp"
4 namespace yasim {
5
6 const static float HP2W = 745.7f;
7 const static float CIN2CM = 1.6387064e-5f;
8 const static float RPM2RADPS = 0.1047198f;
9
10 PistonEngine::PistonEngine(float power, float speed)
11 {
12     _boost = 1;
13     _running = false;
14     _fuel = true;
15     _boostPressure = 0;
16     
17     _oilTemp = Atmosphere::getStdTemperature(0);
18     _oilTempTarget = _oilTemp;
19     _dOilTempdt = 0;
20
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;
24
25     _power0 = power;
26     _omega0 = speed;
27
28     // We must be at sea level under standard conditions
29     _rho0 = Atmosphere::getStdDensity(0);
30
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;
36
37     _turbo = 1;
38     _maxMP = 1e6; // No waste gate on non-turbo engines.
39     _wastegate = 1;
40     _charge = 1;
41     _chargeTarget = 1;
42
43     // Guess at reasonable values for these guys.  Displacements run
44     // at about 2 cubic inches per horsepower or so, at least for
45     // non-turbocharged engines.
46     _compression = 8;
47     _displacement = power * (2*CIN2CM/HP2W);
48 }
49
50 void PistonEngine::setTurboParams(float turbo, float maxMP)
51 {
52     _turbo = turbo;
53     _maxMP = maxMP;
54
55     // This changes the "sea level" manifold air density
56     float P0 = Atmosphere::getStdPressure(0);
57     float P = P0 * (1 + _boost * (_turbo - 1));
58     if(P > _maxMP) P = _maxMP;
59     float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
60     _rho0 = P / (287.1f * T);
61 }
62
63 void PistonEngine::setDisplacement(float d)
64 {
65     _displacement = d;
66 }
67
68 void PistonEngine::setCompression(float c)
69 {
70     _compression = c;
71 }
72
73 float PistonEngine::getMaxPower()
74 {
75     return _power0;
76 }
77
78 bool PistonEngine::isCranking()
79 {
80     return _starter;
81 }
82
83 float PistonEngine::getTorque()
84 {
85     return _torque;
86 }
87
88 float PistonEngine::getFuelFlow()
89 {
90     return _fuelFlow;
91 }
92
93 float PistonEngine::getMP()
94 {
95     return _mp;
96 }
97
98 float PistonEngine::getEGT()
99 {
100     return _egt;
101 }
102
103 void PistonEngine::stabilize()
104 {
105     _oilTemp = _oilTempTarget;
106     _charge = _chargeTarget;
107 }
108
109 void PistonEngine::integrate(float dt) 
110 {
111     _oilTemp += (_dOilTempdt * dt);
112
113     // See comments in Jet.cpp for how this decay constant works
114     float decay = 2.3f / _turboLag;
115     _charge = (_charge + dt*decay * _chargeTarget) / (1 + dt*decay);
116 }
117
118 void PistonEngine::calc(float pressure, float temp, float speed)
119 {
120     _running = _magnetos && _fuel && (speed > 60*RPM2RADPS);
121
122     // Calculate the factor required to modify supercharger output for
123     // rpm. Assume that the normalized supercharger output ~= 1 when
124     // the engine is at the nominal peak-power rpm.  A power equation
125     // of the form (A * B^x * x^C) has been derived empirically from
126     // some representative supercharger data.  This provides
127     // near-linear output over the normal operating range, with
128     // fall-off in the over-speed situation.
129     float rpm_norm = (speed / _omega0);
130     float A = 1.795206541;
131     float B = 0.55620178;
132     float C = 1.246708471;
133     float rpm_factor = A * Math::pow(B, rpm_norm) * Math::pow(rpm_norm, C);
134     _chargeTarget = 1 + (_boost * (_turbo-1) * rpm_factor);
135
136     if(_hasSuper) {
137         // Superchargers have no lag
138         _charge = _chargeTarget;
139     } else if(!_running) {
140         // Turbochargers only work well when the engine is actually
141         // running.  The 25% number is a guesstimate from Vivian.
142         _chargeTarget = 1 + (_chargeTarget - 1) * 0.25;
143     }
144
145     // We need to adjust the minimum manifold pressure to get a
146     // reasonable idle speed (a "closed" throttle doesn't suck a total
147     // vacuum in real manifolds).  This is a hack.
148     float _minMP = (-0.008 * _turbo ) + 0.1;
149
150     _mp = pressure * _charge;
151
152     // Scale to throttle setting, clamp to wastegate
153     if(_running)
154         _mp *= _minMP + (1 -_minMP) * _throttle;
155
156     // Scale the max MP according to the WASTEGATE control input.  Use
157     // the un-supercharged MP as the bottom limit.
158     float max = _wastegate * _maxMP;
159     if(max < _mp/_charge) max = _mp/_charge;
160     if(_mp > max) _mp = max;
161     
162
163     // The "boost" is the delta above ambient
164     _boostPressure = _mp - pressure;
165
166     // Air entering the manifold does so rapidly, and thus the
167     // pressure change can be assumed to be adiabatic.  Calculate a
168     // temperature change, and use that to get the density.
169     // Note: need to model intercoolers here...
170     float T = temp * Math::pow(_mp/pressure, 2.0/7.0);
171     float rho = _mp / (287.1f * T);
172
173     // The actual fuel flow is determined only by engine RPM and the
174     // mixture setting.  Not all of this will burn with the same
175     // efficiency.
176     _fuelFlow = _mixture * speed * _mixCoeff;
177     if(_fuel == false) _fuelFlow = 0;
178
179     // How much fuel could be burned with ideal (i.e. uncorrected!)
180     // combustion.
181     float burnable = _f0 * (rho/_rho0) * (speed/_omega0);
182
183     // Calculate the fuel that actually burns to produce work.  The
184     // idea is that less than 5/8 of ideal, we get complete
185     // combustion.  We use up all the oxygen at 1 3/8 of ideal (that
186     // is, you need to waste fuel to use all your O2).  In between,
187     // interpolate.  This vaguely matches a curve I copied out of a
188     // book for a single engine.  Shrug.
189     float burned;
190     float r = _fuelFlow/burnable;
191     if     (burnable == 0) burned = 0;
192     else if(r < .625)      burned = _fuelFlow;
193     else if(r > 1.375)     burned = burnable;
194     else
195         burned = _fuelFlow + (burnable-_fuelFlow)*(r-0.625f)*(4.0f/3.0f);
196
197     // Correct for engine control state
198     if(!_running)
199         burned = 0;
200     if(_magnetos < 3)
201         burned *= 0.9f;
202
203     // And finally the power is just the reference power scaled by the
204     // amount of fuel burned, and torque is that divided by RPM.
205     float power = _power0 * burned/_f0;
206     _torque = power/speed;
207
208     // Figure that the starter motor produces 15% of the engine's
209     // cruise torque.  Assuming 60RPM starter speed vs. 1800RPM cruise
210     // speed on a 160HP engine, that comes out to about 160*.15/30 ==
211     // 0.8 HP starter motor.  Which sounds about right to me.  I think
212     // I've finally got this tuned. :)
213     if(_starter && !_running)
214         _torque += 0.15f * _power0/_omega0;
215
216     // Also, add a negative torque of 8% of cruise, to represent
217     // internal friction.  Propeller aerodynamic friction is too low
218     // at low RPMs to provide a good deceleration.  Interpolate it
219     // away as we approach cruise RPMs (full at 50%, zero at 100%),
220     // though, to prevent interaction with the power computations.
221     // Ugly.
222     if(speed > 0 && speed < _omega0) {
223         float interp = 2 - 2*speed/_omega0;
224         interp = (interp > 1) ? 1 : interp;
225         _torque -= 0.08f * (_power0/_omega0) * interp;
226     }
227
228     // Now EGT.  This one gets a little goofy.  We can calculate the
229     // work done by an isentropically expanding exhaust gas as the
230     // mass of the gas times the specific heat times the change in
231     // temperature.  The mass is just the engine displacement times
232     // the manifold density, plus the mass of the fuel, which we know.
233     // The change in temperature can be calculated adiabatically as a
234     // function of the exhaust gas temperature and the compression
235     // ratio (which we know).  So just rearrange the equation to get
236     // EGT as a function of engine power.  Cool.  I'm using a value of
237     // 1300 J/(kg*K) for the exhaust gas specific heat.  I found this
238     // on a web page somewhere; no idea if it's accurate.  Also,
239     // remember that four stroke engines do one combustion cycle every
240     // TWO revolutions, so the displacement per revolution is half of
241     // what we'd expect.  And diddle the work done by the gas a bit to
242     // account for non-thermodynamic losses like internal friction;
243     // 10% should do it.
244     float massFlow = _fuelFlow + (rho * 0.5f * _displacement * speed);
245     float specHeat = 1300;
246     float corr = 1.0f/(Math::pow(_compression, 0.4f) - 1.0f);
247     _egt = corr * (power * 1.1f) / (massFlow * specHeat);
248     if(_egt < temp) _egt = temp;
249     
250     
251     // Oil temperature.
252     // Assume a linear variation between ~90degC at idle and ~120degC
253     // at full power.  No attempt to correct for airflow over the
254     // engine is made.  Make the time constant to attain target steady-
255     // state oil temp greater at engine off than on to reflect no
256     // circulation.  Nothing fancy, but populates the guage with a
257     // plausible value.
258     float tau;  // secs 
259     if(_running) {
260         _oilTempTarget = 363.0f + (30.0f * (power/_power0));
261         tau = 600;
262         // Reduce tau linearly to 300 at max power
263         tau -= (power/_power0) * 300.0f;
264     } else {
265         _oilTempTarget = temp;
266         tau = 1500;             
267     }
268     _dOilTempdt = (_oilTempTarget - _oilTemp) / tau;
269 }
270
271 }; // namespace yasim