]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PistonEngine.cpp
Add support for a turbo prop condition lever.
[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
16     // Presume a BSFC (in lb/hour per HP) of 0.45.  In SI that becomes
17     // (2.2 lb/kg, 745.7 W/hp, 3600 sec/hour) 7.62e-08 kg/Ws.
18     _f0 = power * 7.62e-08f;
19
20     _power0 = power;
21     _omega0 = speed;
22
23     // We must be at sea level under standard conditions
24     _rho0 = Atmosphere::getStdDensity(0);
25
26     // Further presume that takeoff is (duh) full throttle and
27     // peak-power, that means that by our efficiency function, we are
28     // at 11/8 of "ideal" fuel flow.
29     float realFlow = _f0 * (11.0f/8.0f);
30     _mixCoeff = realFlow * 1.1f / _omega0;
31
32     _turbo = 1;
33     _maxMP = 1e6; // No waste gate on non-turbo engines.
34
35     // Guess at reasonable values for these guys.  Displacements run
36     // at about 2 cubic inches per horsepower or so, at least for
37     // non-turbocharged engines.
38     _compression = 8;
39     _displacement = power * (2*CIN2CM/HP2W);
40 }
41
42 void PistonEngine::setTurboParams(float turbo, float maxMP)
43 {
44     _turbo = turbo;
45     _maxMP = maxMP;
46
47     // This changes the "sea level" manifold air density
48     float P0 = Atmosphere::getStdPressure(0);
49     float P = P0 * (1 + _boost * (_turbo - 1));
50     if(P > _maxMP) P = _maxMP;
51     float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
52     _rho0 = P / (287.1f * T);
53 }
54
55 void PistonEngine::setDisplacement(float d)
56 {
57     _displacement = d;
58 }
59
60 void PistonEngine::setCompression(float c)
61 {
62     _compression = c;
63 }
64
65 float PistonEngine::getMaxPower()
66 {
67     return _power0;
68 }
69
70 bool PistonEngine::isCranking()
71 {
72     return _starter;
73 }
74
75 float PistonEngine::getTorque()
76 {
77     return _torque;
78 }
79
80 float PistonEngine::getFuelFlow()
81 {
82     return _fuelFlow;
83 }
84
85 float PistonEngine::getMP()
86 {
87     return _mp;
88 }
89
90 float PistonEngine::getEGT()
91 {
92     return _egt;
93 }
94
95 void PistonEngine::calc(float pressure, float temp, float speed)
96 {
97     if(_magnetos == 0 || speed < 60*RPM2RADPS)
98         _running = false;
99     else if(_fuel == false)
100         _running = false;
101     else
102         _running = true;
103
104     // Calculate manifold pressure as ambient pressure modified for
105     // turbocharging and reduced by the throttle setting.  According
106     // to Dave Luff, minimum throttle at sea level corresponds to 6"
107     // manifold pressure.  Assume that this means that minimum MP is
108     // always 20% of ambient pressure. (But that's too much idle
109     // power, so use 10% instead!) But we need to produce _zero_
110     // thrust at that setting, so hold onto the "output" value
111     // separately.  Ick.
112     _mp = pressure * (1 + _boost*(_turbo-1)); // turbocharger
113     float mp = _mp * (0.1f + 0.9f * _throttle); // throttle
114     _mp *= _throttle;
115     if(mp > _maxMP) mp = _maxMP;              // wastegate
116
117     // Air entering the manifold does so rapidly, and thus the
118     // pressure change can be assumed to be adiabatic.  Calculate a
119     // temperature change, and use that to get the density.
120     float T = temp * Math::pow(mp/pressure, 2.0/7.0);
121     float rho = mp / (287.1f * T);
122
123     // The actual fuel flow is determined only by engine RPM and the
124     // mixture setting.  Not all of this will burn with the same
125     // efficiency.
126     _fuelFlow = _mixture * speed * _mixCoeff;
127     if(_fuel == false) _fuelFlow = 0;
128
129     // How much fuel could be burned with ideal (i.e. uncorrected!)
130     // combustion.
131     float burnable = _f0 * (rho/_rho0) * (speed/_omega0);
132
133     // Calculate the fuel that actually burns to produce work.  The
134     // idea is that less than 5/8 of ideal, we get complete
135     // combustion.  We use up all the oxygen at 1 3/8 of ideal (that
136     // is, you need to waste fuel to use all your O2).  In between,
137     // interpolate.  This vaguely matches a curve I copied out of a
138     // book for a single engine.  Shrug.
139     float burned;
140     float r = _fuelFlow/burnable;
141     if     (burnable == 0) burned = 0;
142     else if(r < .625)      burned = _fuelFlow;
143     else if(r > 1.375)     burned = burnable;
144     else
145         burned = _fuelFlow + (burnable-_fuelFlow)*(r-0.625f)*(4.0f/3.0f);
146
147     // Correct for engine control state
148     if(!_running)
149         burned = 0;
150     if(_magnetos < 3)
151         burned *= 0.9f;
152
153     // And finally the power is just the reference power scaled by the
154     // amount of fuel burned, and torque is that divided by RPM.
155     float power = _power0 * burned/_f0;
156     _torque = power/speed;
157
158     // Figure that the starter motor produces 15% of the engine's
159     // cruise torque.  Assuming 60RPM starter speed vs. 1800RPM cruise
160     // speed on a 160HP engine, that comes out to about 160*.15/30 ==
161     // 0.8 HP starter motor.  Which sounds about right to me.  I think
162     // I've finally got this tuned. :)
163     if(_starter && !_running)
164         _torque += 0.15f * _power0/_omega0;
165
166     // Also, add a negative torque of 8% of cruise, to represent
167     // internal friction.  Propeller aerodynamic friction is too low
168     // at low RPMs to provide a good deceleration.  Interpolate it
169     // away as we approach cruise RPMs (full at 50%, zero at 100%),
170     // though, to prevent interaction with the power computations.
171     // Ugly.
172     if(speed > 0 && speed < _omega0) {
173         float interp = 2 - 2*speed/_omega0;
174         interp = (interp > 1) ? 1 : interp;
175         _torque -= 0.08f * (_power0/_omega0) * interp;
176     }
177
178     // Now EGT.  This one gets a little goofy.  We can calculate the
179     // work done by an isentropically expanding exhaust gas as the
180     // mass of the gas times the specific heat times the change in
181     // temperature.  The mass is just the engine displacement times
182     // the manifold density, plus the mass of the fuel, which we know.
183     // The change in temperature can be calculated adiabatically as a
184     // function of the exhaust gas temperature and the compression
185     // ratio (which we know).  So just rearrange the equation to get
186     // EGT as a function of engine power.  Cool.  I'm using a value of
187     // 1300 J/(kg*K) for the exhaust gas specific heat.  I found this
188     // on a web page somewhere; no idea if it's accurate.  Also,
189     // remember that four stroke engines do one combustion cycle every
190     // TWO revolutions, so the displacement per revolution is half of
191     // what we'd expect.  And diddle the work done by the gas a bit to
192     // account for non-thermodynamic losses like internal friction;
193     // 10% should do it.
194
195     float massFlow = _fuelFlow + (rho * 0.5f * _displacement * speed);
196     float specHeat = 1300;
197     float corr = 1.0f/(Math::pow(_compression, 0.4f) - 1.0f);
198     _egt = corr * (power * 1.1f) / (massFlow * specHeat);
199     if(_egt < temp) _egt = temp;
200 }
201
202 }; // namespace yasim