1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 09/12/2000
6 Purpose: This module models a Piston engine
8 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.org) --------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class descends from the FGEngine class and models a Piston engine based on
31 parameters given in the engine config file for this class
34 --------------------------------------------------------------------------------
35 09/12/2000 JSB Created
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGPropulsion.h"
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_PISTON;
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 FGPiston::FGPiston(FGFDMExec* exec, FGConfigFile* Eng_cfg)
54 MinManifoldPressure_inHg(6.5),
55 MaxManifoldPressure_inHg(28.5),
61 CONVERT_CUBIC_INCHES_TO_METERS_CUBED(1.638706e-5),
63 rho_fuel(800), // estimate
64 calorific_value_fuel(47.3e6),
70 Name = Eng_cfg->GetValue("NAME");
71 Eng_cfg->GetNextConfigLine();
72 while (Eng_cfg->GetValue() != "/FG_PISTON") {
74 if (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
75 else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
76 else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
77 else if (token == "MAXHP") *Eng_cfg >> MaxHP;
78 else if (token == "CYCLES") *Eng_cfg >> Cycles;
79 else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
80 else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
81 else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
82 else if (token == "SLFUELFLOWMAX") *Eng_cfg >> SLFuelFlowMax;
83 else cerr << "Unhandled token in Engine config file: " << token << endl;
87 cout << "\n Engine Name: " << Name << endl;
88 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
89 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
90 cout << " Displacement: " << Displacement << endl;
91 cout << " MaxHP: " << MaxHP << endl;
92 cout << " Cycles: " << Cycles << endl;
93 cout << " IdleRPM: " << IdleRPM << endl;
94 cout << " MaxThrottle: " << MaxThrottle << endl;
95 cout << " MinThrottle: " << MinThrottle << endl;
96 cout << " SLFuelFlowMax: " << SLFuelFlowMax << endl;
100 EngineNumber = 0; // FIXME: this should be the actual number
101 OilTemp_degK = 298; // FIXME: should be initialized in FGEngine
106 volumetric_efficiency = 0.8; // Actually f(speed, load) but this will get us running
108 // First column is thi, second is neta (combustion efficiency)
109 Lookup_Combustion_Efficiency = new FGTable(12);
110 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
111 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
112 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
113 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
114 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
115 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
116 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
117 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
118 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
119 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
120 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
121 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
124 cout << " Combustion Efficiency table:" << endl;
125 Lookup_Combustion_Efficiency->Print();
128 Power_Mixture_Correlation = new FGTable(13);
129 *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
130 *Power_Mixture_Correlation << 10 << 86.0;
131 *Power_Mixture_Correlation << 11 << 93.5;
132 *Power_Mixture_Correlation << 12 << 98.0;
133 *Power_Mixture_Correlation << 13 << 100.0;
134 *Power_Mixture_Correlation << 14 << 99.0;
135 *Power_Mixture_Correlation << 15 << 96.4;
136 *Power_Mixture_Correlation << 16 << 92.5;
137 *Power_Mixture_Correlation << 17 << 88.0;
138 *Power_Mixture_Correlation << 18 << 83.0;
139 *Power_Mixture_Correlation << 19 << 78.5;
140 *Power_Mixture_Correlation << 20 << 74.0;
141 *Power_Mixture_Correlation << (14.7/0.6) << 58;
144 cout << " Power Mixture Correlation table:" << endl;
145 Power_Mixture_Correlation->Print();
148 if (debug_lvl & 2) cout << "Instantiated: FGPiston" << endl;
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 FGPiston::~FGPiston()
155 if (debug_lvl & 2) cout << "Destroyed: FGPiston" << endl;
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 float FGPiston::Calculate(float PowerRequired)
162 float h,EngineMaxPower;
164 // FIXME: calculate from actual fuel flow
167 Throttle = FCS->GetThrottlePos(EngineNumber);
168 Mixture = FCS->GetMixturePos(EngineNumber);
174 p_amb = Atmosphere->GetPressure() * 48; // convert from lbs/ft2 to Pa
175 p_amb_sea_level = Atmosphere->GetPressureSL() * 48;
176 T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0); // convert from Rankine to Kelvin
178 RPM = Propulsion->GetThruster(EngineNumber)->GetRPM();
179 //if (RPM < IdleRPM) RPM = IdleRPM; // kludge
181 IAS = Auxiliary->GetVcalibratedKTS();
183 if (Mixture >= 0.5) {
185 doManifoldPressure();
197 PowerAvailable = (HP * HPTOFTLBSSEC) - PowerRequired;
198 return PowerAvailable;
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 * Start or stop the engine.
206 void FGPiston::doEngineStartup(void)
208 // TODO: check magnetos, spark, starter, etc. and decide whether
211 // Check parameters that may alter the operating state of the engine.
212 // (spark, fuel, starter motor etc)
215 static int crank_counter = 0;
218 Magneto_Left = false;
219 Magneto_Right = false;
220 // Magneto positions:
229 } // neglects battery voltage, master on switch, etc for now.
231 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
232 if (Magnetos > 1) Magneto_Right = true;
234 // Assume we have fuel for now
237 // Check if we are turning the starter motor
238 if (Cranking != Starter) {
239 // This check saves .../cranking from getting updated every loop - they
240 // only update when changed.
245 //Check mode of engine operation
246 // ACK - unfortunately this hack doesn't work in JSBSim since the RPM is reset
247 // each iteration by the propeller :-(
255 // consider making a horrible noise if the starter is engaged with
256 // the engine running
258 // TODO - find a better guess at cranking speed
261 // if ((!Running) && (spark) && (fuel) && (crank_counter > 120)) {
263 if ((!Running) && (spark) && (fuel)) {
264 // start the engine if revs high enough
266 // For now just instantaneously start but later we should maybe crank for
273 if ( (Running) && ((!spark)||(!fuel)) ) {
275 // note that we only cut the power - the engine may continue to
276 // spin if the prop is in a moving airstream
280 // And finally a last check for stalling
282 //Check if we have stalled the engine
285 } else if ((RPM <= 480) && (Cranking)) {
286 // Make sure the engine noise dosn't play if the engine won't
287 // start due to eg mixture lever pulled out.
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 * Calculate the nominal manifold pressure in inches hg
298 * This function calculates nominal manifold pressure directly
299 * from the throttle position, and does not adjust it for the
300 * difference between the pressure at sea level and the pressure
301 * at the current altitude (that adjustment takes place in
302 * {@link #doEnginePower}).
304 * TODO: changes in MP should not be instantaneous -- introduce
305 * a lag between throttle changes and MP changes, to allow pressure
306 * to build up or disperse.
308 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
310 * Outputs: ManifoldPressure_inHg
313 void FGPiston::doManifoldPressure(void)
315 ManifoldPressure_inHg = MinManifoldPressure_inHg +
316 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 * Calculate the air flow through the engine.
323 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
324 * RPM, volumetric_efficiency
326 * Outputs: rho_air, m_dot_air
329 void FGPiston::doAirFlow(void)
331 rho_air = p_amb / (R_air * T_amb);
332 float rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
333 float displacement_SI = Displacement * CONVERT_CUBIC_INCHES_TO_METERS_CUBED;
334 float swept_volume = (displacement_SI * (RPM/60)) / 2;
335 float v_dot_air = swept_volume * volumetric_efficiency;
336 m_dot_air = v_dot_air * rho_air_manifold;
339 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 * Calculate the fuel flow into the engine.
343 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
345 * Outputs: equivalence_ratio, m_dot_fuel
348 void FGPiston::doFuelFlow(void)
350 float thi_sea_level = 1.3 * Mixture;
351 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
352 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
355 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 * Calculate the power produced by the engine.
359 * <p>Currently, the JSBSim propellor model does not allow the
360 * engine to produce enough RPMs to get up to a high horsepower.
361 * When tested with sufficient RPM, it has no trouble reaching
364 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
365 * equivalence_ratio, Cycles, MaxHP
367 * Outputs: Percentage_Power, HP
370 void FGPiston::doEnginePower(void)
372 float True_ManifoldPressure_inHg = ManifoldPressure_inHg * p_amb / p_amb_sea_level;
373 float ManXRPM = True_ManifoldPressure_inHg * RPM;
374 // FIXME: this needs to be generalized
375 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
376 float T_amb_degF = (T_amb * 1.8) - 459.67;
377 float T_amb_sea_lev_degF = (288 * 1.8) - 459.67;
379 Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
380 float Percentage_of_best_power_mixture_power =
381 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
383 Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
384 if (Percentage_Power < 0.0)
385 Percentage_Power = 0.0;
386 else if (Percentage_Power > 100.0)
387 Percentage_Power = 100.0;
388 HP = Percentage_Power * MaxHP / 100.0;
394 HP = 3.0 + ((480 - RPM) / 10.0);
399 // Quick hack until we port the FMEP stuff
408 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 * Calculate the exhaust gas temperature.
412 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
413 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
415 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
418 void FGPiston::doEGT(void)
420 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
421 float enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
422 combustion_efficiency * 0.33;
423 float heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
424 float delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
425 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
426 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
429 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 * Calculate the cylinder head temperature.
433 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
434 * combustion_efficiency, RPM
436 * Outputs: CylinderHeadTemp_degK
439 void FGPiston::doCHT(void)
445 float arbitary_area = 1.0;
446 float CpCylinderHead = 800.0;
447 float MassCylinderHead = 8.0;
449 float temperature_difference = CylinderHeadTemp_degK - T_amb;
450 float v_apparent = IAS * 0.5144444;
451 float v_dot_cooling_air = arbitary_area * v_apparent;
452 float m_dot_cooling_air = v_dot_cooling_air * rho_air;
453 float dqdt_from_combustion =
454 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
455 float dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
456 (h3 * RPM * temperature_difference);
457 float dqdt_free = h1 * temperature_difference;
458 float dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
460 float HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
462 CylinderHeadTemp_degK = dqdt_cylinder_head / HeatCapacityCylinderHead;
465 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 * Calculate the oil temperature.
469 * Inputs: Percentage_Power, running flag.
471 * Outputs: OilTemp_degK
474 void FGPiston::doOilTemperature(void)
476 float idle_percentage_power = 2.3; // approximately
477 float target_oil_temp; // Steady state oil temp at the current engine conditions
478 float time_constant; // The time constant for the differential equation
481 target_oil_temp = 363;
482 time_constant = 500; // Time constant for engine-on idling.
483 if (Percentage_Power > idle_percentage_power) {
484 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
487 target_oil_temp = 298;
488 time_constant = 1000; // Time constant for engine-off; reflects the fact
489 // that oil is no longer getting circulated
492 float dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
494 OilTemp_degK += (dOilTempdt * dt);
497 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 * Calculate the oil pressure.
503 * Outputs: OilPressure_psi
506 void FGPiston::doOilPressure(void)
508 float Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
509 float Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
510 float Design_Oil_Temp = 85; // FIXME: may vary by engine
511 // FIXME: WRONG!!! (85 degK???)
512 float Oil_Viscosity_Index = 0.25;
514 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
516 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
517 OilPressure_psi = Oil_Press_Relief_Valve;
520 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
523 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 void FGPiston::Debug(void)
527 //TODO: Add your source code here