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