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