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