]> git.mxchange.org Git - flightgear.git/blob - src/FDM/IO360.hxx
Latest YASim changes.
[flightgear.git] / src / FDM / IO360.hxx
1 // IO360.hxx - a piston engine model currently for the IO360 engine fitted to the C172
2 //             but with the potential to model other naturally aspirated piston engines
3 //             given appropriate config input.
4 //
5 // Written by David Luff, started 2000.
6 // Based on code by Phil Schubert, started 1999.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23 #ifndef _IO360_HXX_
24 #define _IO360_HXX_
25
26 #include <simgear/compiler.h>
27
28 #include <math.h>
29
30 #include STL_IOSTREAM
31 #include STL_FSTREAM
32
33 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
34 SG_USING_STD(ofstream);
35 #endif
36
37 class FGNewEngine {
38
39 private:
40
41     // These constants should probably be moved eventually
42     float CONVERT_CUBIC_INCHES_TO_METERS_CUBED;
43     float CONVERT_HP_TO_WATTS;
44
45     // Properties of working fluids
46     float Cp_air;               // J/KgK
47     float Cp_fuel;              // J/KgK
48     float calorific_value_fuel; // W/Kg
49     float rho_fuel;             // kg/m^3
50     float rho_air;              // kg/m^3
51
52     // environment inputs
53     float p_amb_sea_level;      // Sea level ambient pressure in Pascals
54     float p_amb;                // Ambient pressure at current altitude in Pascals
55     float T_amb;                // ditto deg Kelvin
56
57     // Control inputs
58     float Throttle_Lever_Pos;   // 0 = Closed, 100 = Fully Open
59     float Propeller_Lever_Pos;  // 0 = Full Course 100 = Full Fine
60     float Mixture_Lever_Pos;    // 0 = Idle Cut Off 100 = Full Rich
61     int mag_pos;                // 0=off, 1=left, 2=right, 3=both.
62     bool starter;
63
64     //misc
65     float IAS;
66     double time_step;
67
68     // Engine Specific Variables that should be read in from a config file
69     float MaxHP;                // Horsepower
70     float displacement;         // Cubic inches
71     float displacement_SI;      //m^3 (derived from above rather than read in)
72     float engine_inertia;       //kg.m^2
73     float prop_inertia;         //kg.m^2
74     float Max_Fuel_Flow;        // Units??? Do we need this variable any more??
75
76     // Engine specific variables that maybe should be read in from config but are pretty generic and won't vary much for a naturally aspirated piston engine.
77     float Max_Manifold_Pressure;    // inches Hg - typical manifold pressure at full throttle and typical max rpm
78     float Min_Manifold_Pressure;    // inches Hg - typical manifold pressure at idle (closed throttle)
79     float Max_RPM;              // rpm - this is really a bit of a hack and could be make redundant if the prop model works properly and takes tips at speed of sound into account.
80     float Min_RPM;              // rpm - possibly redundant ???
81     float Mag_Derate_Percent;   // Percentage reduction in power when mags are switched from 'both' to either 'L' or 'R'
82     float Gear_Ratio;           // Gearing between engine and propellor
83     float n_R;                  // Number of cycles per power stroke - 2 for a 4 stroke engine.
84
85     // Engine Variables not read in from config file
86     float RPM;                  // rpm
87     float Percentage_Power;     // Power output as percentage of maximum power output
88     float Manifold_Pressure;    // Inches Hg
89     float Fuel_Flow_gals_hr;    // USgals/hour
90     float Torque_lbft;          // lb-ft                
91     float Torque_SI;            // Nm
92     float CHT_degK;             // Cylinder head temperature deg K
93     float CHT_degF;             // Ditto in deg Fahrenheit
94     float Mixture;
95     float Oil_Pressure;         // PSI
96     float Oil_Temp;             // Deg C
97     float current_oil_temp;     // deg K
98     /**** one of these is superfluous !!!!***/
99     float HP;                   // Current power output in HP
100     float Power_SI;             // Current power output in Watts
101     float RPS;
102     float angular_velocity_SI;  // rad/s
103     float Torque_FMEP;          // The component of Engine torque due to FMEP (Nm)
104     float Torque_Imbalance;     // difference between engine and prop torque
105     float EGT;                  // Exhaust gas temperature deg K
106     float EGT_degF;             // Exhaust gas temperature deg Fahrenheit
107     float volumetric_efficiency;
108     float combustion_efficiency;
109     float equivalence_ratio;    // ratio of stoichiometric AFR over actual AFR 
110     float thi_sea_level;        // the equivalence ratio we would be running at assuming sea level air denisity in the manifold
111     float v_dot_air;            // volume flow rate of air into engine  - m^3/s
112     float m_dot_air;            // mass flow rate of air into engine - kg/s
113     float m_dot_fuel;           // mass flow rate of fuel into engine - kg/s
114     float swept_volume;         // total engine swept volume - m^3
115     /********* swept volume or the geometry used to calculate it should be in the config read section surely ??? ******/
116     float True_Manifold_Pressure;   //in Hg - actual manifold gauge pressure
117     float rho_air_manifold;     // denisty of air in the manifold - kg/m^3
118     float R_air;                // Gas constant of air (287) UNITS???
119     float delta_T_exhaust;      // Temperature change of exhaust this time step - degK
120     float heat_capacity_exhaust;    // exhaust gas specific heat capacity - J/kgK
121     float enthalpy_exhaust;         // Enthalpy at current exhaust gas conditions - UNITS???
122     float Percentage_of_best_power_mixture_power;   // Current power as a percentage of what power we would have at the same conditions but at best power mixture
123     float abstract_mixture;     //temporary hack
124     float angular_acceleration; //rad/s^2
125     float FMEP;                 //Friction Mean Effective Pressure (Pa)
126
127     // Various bits of housekeeping describing the engines state.
128     bool  running;              // flag to indicate the engine is running self sustaining
129     bool  cranking;             // flag to indicate the engine is being cranked
130     int   crank_counter;        // Number of iterations that the engine has been cranked non-stop
131     bool  spark;                // flag to indicate a spark is available
132     bool  fuel;                 // flag to indicate fuel is available
133
134     // Propellor Variables
135     float FGProp1_RPS;          // rps
136     float prop_torque;          // Nm
137     float prop_thrust;          // Newtons
138     double prop_diameter;       // meters
139     double blade_angle;         // degrees
140
141
142 // MEMBER FUNCTIONS
143     
144     // Calculate Engine RPM based on Propellor Lever Position
145     float Calc_Engine_RPM(float Position);
146
147     // Calculate Manifold Pressure based on throttle lever position
148     // Note that this is simplistic and needs altering to include engine speed effects
149     float Calc_Manifold_Pressure( float LeverPosn, float MaxMan, float MinMan);
150
151     // Calculate combustion efficiency based on equivalence ratio
152     float Lookup_Combustion_Efficiency(float thi_actual);
153
154     // Calculate percentage of best power mixture power based on equivalence ratio
155     float Power_Mixture_Correlation(float thi_actual);
156
157     // Calculate exhaust gas temperature change
158     float Calculate_Delta_T_Exhaust(void);
159
160     // Calculate cylinder head temperature
161     float FGNewEngine::Calc_CHT(float CHT);
162
163     void FGNewEngine::Calc_EGT(void);
164
165     // Calculate fuel flow in gals/hr
166     void FGNewEngine::Calc_Fuel_Flow_Gals_Hr(void);
167
168     // Calculate current percentage power
169     void FGNewEngine::Calc_Percentage_Power(bool mag_left, bool mag_right);
170
171     // Calculate Oil Temperature
172     float Calc_Oil_Temp (float oil_temp);
173     
174     // Calculate Oil Pressure
175     float Calc_Oil_Press (float Oil_Temp, float Engine_RPM);
176
177     // Propeller calculations.
178     void FGNewEngine::Do_Prop_Calcs(void);
179
180 public:
181
182 //    ofstream outfile;
183
184     //constructor
185     FGNewEngine() {
186 //      outfile.open("FGNewEngine.dat", ios::out|ios::trunc);
187     }
188
189     //destructor
190     ~FGNewEngine() {
191 //      outfile.close();
192     }
193
194     // set initial default values
195     void init(double dt);
196
197     // update the engine model based on current control positions
198     void update();
199
200     inline void set_IAS( float value ) { IAS = value; }
201     inline void set_Throttle_Lever_Pos( float value ) {
202         Throttle_Lever_Pos = value;
203     }
204     inline void set_Propeller_Lever_Pos( float value ) {
205         Propeller_Lever_Pos = value;
206     }
207     inline void set_Mixture_Lever_Pos( float value ) {
208         Mixture_Lever_Pos = value;
209     }
210     // set the magneto switch position
211     inline void set_Magneto_Switch_Pos( int value ) {
212         mag_pos = value;
213     }
214     inline void setStarterFlag( bool flag ) {
215         starter = flag;
216     }
217     // set ambient pressure - takes pounds per square foot
218     inline void set_p_amb( float value ) { 
219         p_amb = value * 47.88026;
220         // Convert to Pascals
221     }
222     // set ambient temperature - takes degrees Rankine
223     inline void set_T_amb( float value ) { 
224         T_amb = value * 0.555555555556;
225         // Convert to degrees Kelvin
226     }
227
228     // accessors
229     inline float get_RPM() const { return RPM; }
230     inline float get_Manifold_Pressure() const { return True_Manifold_Pressure; }
231  //   inline float get_Rho() const { return Rho; }
232     inline float get_MaxHP() const { return MaxHP; }
233     inline float get_Percentage_Power() const { return Percentage_Power; }
234     inline float get_EGT() const { return EGT_degF; }    // Returns EGT in Fahrenheit
235     inline float get_CHT() const { return CHT_degF; }    // Note this returns CHT in Fahrenheit
236     inline float get_prop_thrust_SI() const { return prop_thrust; }
237     inline float get_prop_thrust_lbs() const { return (prop_thrust * 0.2248); }
238     inline float get_fuel_flow_gals_hr() const { return (Fuel_Flow_gals_hr); }
239     inline float get_oil_temp() const { return ((current_oil_temp * 1.8) - 459.67); }
240     inline bool getRunningFlag() const { return running; }
241     inline bool getCrankingFlag() const { return cranking; }
242     inline bool getStarterFlag() const { return starter; }
243 };
244
245
246 #endif // _IO360_HXX_