]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPiston.cpp
Initial revision.
[flightgear.git] / src / FDM / JSBSim / FGPiston.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPiston.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/12/2000
6  Purpose:      This module models a Piston engine
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) --------------
9
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
13  version.
14
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
18  details.
19
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.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
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
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 09/12/2000  JSB  Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGPiston.h"
42 #include "FGPropulsion.h"
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_PISTON;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGPiston::FGPiston(FGFDMExec* exec, FGConfigFile* Eng_cfg)
52   : FGEngine(exec),
53     MinManifoldPressure_inHg(6.5),
54     MaxManifoldPressure_inHg(28.5),
55     Displacement(360),
56     MaxHP(200),
57     Cycles(2),
58     IdleRPM(600),
59     // Set constants
60     CONVERT_CUBIC_INCHES_TO_METERS_CUBED(1.638706e-5),
61     R_air(287.3),
62     rho_fuel(800),                 // estimate
63     calorific_value_fuel(47.3e6),
64     Cp_air(1005),
65     Cp_fuel(1700)
66 {
67   string token;
68
69   Name = Eng_cfg->GetValue("NAME");
70   Eng_cfg->GetNextConfigLine();
71   while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
72     *Eng_cfg >> token;
73     if      (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
74     else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
75     else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
76     else if (token == "MAXHP") *Eng_cfg >> MaxHP;
77     else if (token == "CYCLES") *Eng_cfg >> Cycles;
78     else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
79     else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
80     else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
81     else if (token == "SLFUELFLOWMAX") *Eng_cfg >> SLFuelFlowMax;
82     else cerr << "Unhandled token in Engine config file: " << token << endl;
83   }
84
85   if (debug_lvl > 0) {
86     cout << "\n    Engine Name: " << Name << endl;
87     cout << "      MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
88     cout << "      MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
89     cout << "      Displacement: " << Displacement << endl;
90     cout << "      MaxHP: " << MaxHP << endl;
91     cout << "      Cycles: " << Cycles << endl;
92     cout << "      IdleRPM: " << IdleRPM << endl;
93     cout << "      MaxThrottle: " << MaxThrottle << endl;
94     cout << "      MinThrottle: " << MinThrottle << endl;
95     cout << "      SLFuelFlowMax: " << SLFuelFlowMax << endl;
96   }
97
98   Type = etPiston;
99   crank_counter = 0;
100   EngineNumber = 0;    // FIXME: this should be the actual number
101   OilTemp_degK = 298;  // FIXME: should be initialized in FGEngine
102
103   dt = State->Getdt();
104
105   // Initialisation
106   volumetric_efficiency = 0.8;  // Actually f(speed, load) but this will get us running
107
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;
122
123   cout << endl;
124   cout << "      Combustion Efficiency table:" << endl;
125   Lookup_Combustion_Efficiency->Print();
126   cout << endl;
127
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;
142
143   cout << endl;
144   cout << "      Power Mixture Correlation table:" << endl;
145   Power_Mixture_Correlation->Print();
146   cout << endl;
147
148   if (debug_lvl & 2) cout << "Instantiated: FGPiston" << endl;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152
153 FGPiston::~FGPiston()
154 {
155   if (debug_lvl & 2) cout << "Destroyed:    FGPiston" << endl;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 double FGPiston::Calculate(double PowerRequired)
161 {
162   double h,EngineMaxPower;
163
164         // FIXME: calculate from actual fuel flow
165   ConsumeFuel();
166
167   Throttle = FCS->GetThrottlePos(EngineNumber);
168   Mixture = FCS->GetMixturePos(EngineNumber);
169
170   //
171   // Input values.
172   //
173
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
177
178   RPM = Propulsion->GetThruster(EngineNumber)->GetRPM();
179   //if (RPM < IdleRPM) RPM = IdleRPM;  // kludge
180     
181   IAS = Auxiliary->GetVcalibratedKTS();
182
183   if (Mixture >= 0.5) {
184     doEngineStartup();
185     doManifoldPressure();
186     doAirFlow();
187     doFuelFlow();
188     doEnginePower();
189     doEGT();
190     doCHT();
191     doOilTemperature();
192     doOilPressure();
193   } else {
194     HP = 0;
195   }
196
197   PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
198   return PowerAvailable;
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 /**
203  * Start or stop the engine.
204  */
205
206 void FGPiston::doEngineStartup(void)
207 {
208   // TODO: check magnetos, spark, starter, etc. and decide whether
209   // engine is running
210
211   // Check parameters that may alter the operating state of the engine. 
212   // (spark, fuel, starter motor etc)
213   bool spark;
214   bool fuel;
215
216   // Check for spark
217   Magneto_Left = false;
218   Magneto_Right = false;
219   // Magneto positions:
220   // 0 -> off
221   // 1 -> left only
222   // 2 -> right only
223   // 3 -> both
224   if (Magnetos != 0) {
225     spark = true;
226   } else {
227     spark = false;
228   }  // neglects battery voltage, master on switch, etc for now.
229   
230   if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
231   if (Magnetos > 1)  Magneto_Right = true;
232
233   // Assume we have fuel for now
234   fuel = true;
235
236   // Check if we are turning the starter motor
237   if (Cranking != Starter) {
238     // This check saves .../cranking from getting updated every loop - they
239     // only update when changed.
240     Cranking = Starter;
241     crank_counter = 0;
242   }
243
244   //Check mode of engine operation
245   // ACK - unfortunately this hack doesn't work in JSBSim since the RPM is reset
246   // each iteration by the propeller :-(
247   if (Cranking) {
248     crank_counter++;
249     if (RPM <= 480) {
250       RPM += 100;
251       if (RPM > 480)
252         RPM = 480;
253     } else {
254       // consider making a horrible noise if the starter is engaged with
255       // the engine running
256     }
257     // TODO - find a better guess at cranking speed
258   }
259   
260   // if ((!Running) && (spark) && (fuel) && (crank_counter > 120)) {
261
262   if ((!Running) && (spark) && (fuel)) {
263   // start the engine if revs high enough
264     if (RPM > 450) {
265       // For now just instantaneously start but later we should maybe crank for
266       // a bit
267       Running = true;
268       // RPM = 600;
269     }
270   }
271
272   if ( (Running) && ((!spark)||(!fuel)) ) {
273     // Cut the engine
274     // note that we only cut the power - the engine may continue to
275     // spin if the prop is in a moving airstream
276     Running = false;
277   }
278
279   // And finally a last check for stalling
280   if (Running) { 
281     //Check if we have stalled the engine
282     if (RPM == 0) {
283       Running = false;
284     } else if ((RPM <= 480) && (Cranking)) {
285       // Make sure the engine noise dosn't play if the engine won't
286             // start due to eg mixture lever pulled out.
287       Running = false;
288     }
289   }
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 /**
295  * Calculate the nominal manifold pressure in inches hg
296  *
297  * This function calculates nominal manifold pressure directly
298  * from the throttle position, and does not adjust it for the
299  * difference between the pressure at sea level and the pressure
300  * at the current altitude (that adjustment takes place in
301  * {@link #doEnginePower}).
302  *
303  * TODO: changes in MP should not be instantaneous -- introduce
304  * a lag between throttle changes and MP changes, to allow pressure
305  * to build up or disperse.
306  *
307  * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
308  *
309  * Outputs: ManifoldPressure_inHg
310  */
311
312 void FGPiston::doManifoldPressure(void)
313 {
314   ManifoldPressure_inHg = MinManifoldPressure_inHg +
315     (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
316 }
317
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 /**
320  * Calculate the air flow through the engine.
321  *
322  * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
323  *   RPM, volumetric_efficiency
324  *
325  * Outputs: rho_air, m_dot_air
326  */
327
328 void FGPiston::doAirFlow(void)
329 {
330   rho_air = p_amb / (R_air * T_amb);
331   double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
332   double displacement_SI = Displacement * CONVERT_CUBIC_INCHES_TO_METERS_CUBED;
333   double swept_volume = (displacement_SI * (RPM/60)) / 2;
334   double v_dot_air = swept_volume * volumetric_efficiency;
335   m_dot_air = v_dot_air * rho_air_manifold;
336 }
337
338 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 /**
340  * Calculate the fuel flow into the engine.
341  *
342  * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
343  *
344  * Outputs: equivalence_ratio, m_dot_fuel
345  */
346
347 void FGPiston::doFuelFlow(void)
348 {
349   double thi_sea_level = 1.3 * Mixture;
350   equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
351   m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
352 }
353
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 /**
356  * Calculate the power produced by the engine.
357  *
358  * <p>Currently, the JSBSim propellor model does not allow the
359  * engine to produce enough RPMs to get up to a high horsepower.
360  * When tested with sufficient RPM, it has no trouble reaching
361  * 200HP.</p>
362  *
363  * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb, 
364  *   equivalence_ratio, Cycles, MaxHP
365  *
366  * Outputs: Percentage_Power, HP
367  */
368
369 void FGPiston::doEnginePower(void)
370 {
371   double True_ManifoldPressure_inHg = ManifoldPressure_inHg * p_amb / p_amb_sea_level;
372   double ManXRPM = True_ManifoldPressure_inHg * RPM;
373         // FIXME: this needs to be generalized
374   Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
375   double T_amb_degF = (T_amb * 1.8) - 459.67;
376   double T_amb_sea_lev_degF = (288 * 1.8) - 459.67; 
377   Percentage_Power =
378     Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
379   double Percentage_of_best_power_mixture_power =
380     Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
381   Percentage_Power =
382     Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
383   if (Percentage_Power < 0.0)
384     Percentage_Power = 0.0;
385   else if (Percentage_Power > 100.0)
386     Percentage_Power = 100.0;
387   HP = Percentage_Power * MaxHP / 100.0;
388
389   //Hack
390   if (!Running) {
391     if (Cranking) {
392       if (RPM < 480) {
393         HP = 3.0 + ((480 - RPM) / 10.0);
394       } else {
395         HP = 3.0;
396       }
397     } else {
398       // Quick hack until we port the FMEP stuff
399       if (RPM > 0.0)
400         HP = -1.5;
401       else
402         HP = 0.0;
403     }
404   }
405 }
406
407 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 /**
409  * Calculate the exhaust gas temperature.
410  *
411  * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel, 
412  *   Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
413  *
414  * Outputs: combustion_efficiency, ExhaustGasTemp_degK
415  */
416
417 void FGPiston::doEGT(void)
418 {
419   combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
420   double enthalpy_exhaust = m_dot_fuel * calorific_value_fuel * 
421     combustion_efficiency * 0.33;
422   double heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
423   double delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
424   ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
425   ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
426 }
427
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 /**
430  * Calculate the cylinder head temperature.
431  *
432  * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
433  *   combustion_efficiency, RPM
434  *
435  * Outputs: CylinderHeadTemp_degK
436  */
437
438 void FGPiston::doCHT(void)
439 {
440   double h1 = -95.0;
441   double h2 = -3.95;
442   double h3 = -0.05;
443
444   double arbitary_area = 1.0;
445   double CpCylinderHead = 800.0;
446   double MassCylinderHead = 8.0;
447
448   double temperature_difference = CylinderHeadTemp_degK - T_amb;
449   double v_apparent = IAS * 0.5144444;
450   double v_dot_cooling_air = arbitary_area * v_apparent;
451   double m_dot_cooling_air = v_dot_cooling_air * rho_air;
452   double dqdt_from_combustion = 
453     m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
454   double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) + 
455     (h3 * RPM * temperature_difference);
456   double dqdt_free = h1 * temperature_difference;
457   double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
458     
459   double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
460     
461   CylinderHeadTemp_degK = dqdt_cylinder_head / HeatCapacityCylinderHead;
462 }
463
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 /**
466  * Calculate the oil temperature.
467  *
468  * Inputs: Percentage_Power, running flag.
469  *
470  * Outputs: OilTemp_degK
471  */
472
473 void FGPiston::doOilTemperature(void)
474 {
475   double idle_percentage_power = 2.3;        // approximately
476   double target_oil_temp;        // Steady state oil temp at the current engine conditions
477   double time_constant;          // The time constant for the differential equation
478
479   if (Running) {
480     target_oil_temp = 363;
481     time_constant = 500;        // Time constant for engine-on idling.
482     if (Percentage_Power > idle_percentage_power) {
483       time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power 
484     }
485   } else {
486     target_oil_temp = 298;
487     time_constant = 1000;  // Time constant for engine-off; reflects the fact
488                            // that oil is no longer getting circulated
489   }
490
491   double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
492
493   OilTemp_degK += (dOilTempdt * dt);
494 }
495
496 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 /**
498  * Calculate the oil pressure.
499  *
500  * Inputs: RPM
501  *
502  * Outputs: OilPressure_psi
503  */
504
505 void FGPiston::doOilPressure(void)
506 {
507   double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
508   double Oil_Press_RPM_Max = 1800;    // FIXME: may vary by engine
509   double Design_Oil_Temp = 85;        // FIXME: may vary by engine
510              // FIXME: WRONG!!! (85 degK???)
511   double Oil_Viscosity_Index = 0.25;
512
513   OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
514
515   if (OilPressure_psi >= Oil_Press_Relief_Valve) {
516     OilPressure_psi = Oil_Press_Relief_Valve;
517   }
518
519   OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
520 }
521
522 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523
524 void FGPiston::Debug(void)
525 {
526   //TODO: Add your source code here
527 }
528