]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGEngine.cpp
Updates from the Jon and Tony show.
[flightgear.git] / src / FDM / JSBSim / FGEngine.cpp
1 /*******************************************************************************
2  
3  Module:       FGEngine.cpp
4  Author:       Jon Berndt
5  Date started: 01/21/99
6  Called by:    FGAircraft
7  
8  ------------- Copyright (C) 1999  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 See header file.
30  
31 HISTORY
32 --------------------------------------------------------------------------------
33 01/21/99   JSB   Created
34 09/03/99   JSB   Changed Rocket thrust equation to correct -= Thrust instead of
35                  += Thrust (thanks to Tony Peden)
36  
37 ********************************************************************************
38 INCLUDES
39 *******************************************************************************/
40
41 #ifdef FGFS
42 #  include <simgear/compiler.h>
43 #  ifdef FG_HAVE_STD_INCLUDES
44 #    include <fstream>
45 #  else
46 #    include <fstream.h>
47 #  endif
48 #else
49 #  include <fstream>
50 #endif
51
52 #include "FGEngine.h"
53 #include "FGState.h"
54 #include "FGFDMExec.h"
55 #include "FGAtmosphere.h"
56 #include "FGFCS.h"
57 #include "FGAircraft.h"
58 #include "FGTranslation.h"
59 #include "FGRotation.h"
60 #include "FGPosition.h"
61 #include "FGAuxiliary.h"
62 #include "FGOutput.h"
63 #include "FGDefs.h"
64
65 /*******************************************************************************
66 ************************************ CODE **************************************
67 *******************************************************************************/
68
69
70 FGEngine::FGEngine(FGFDMExec* fdex, string enginePath, string engineName, int num) {
71   string fullpath;
72   string tag;
73
74   FDMExec = fdex;
75
76   State       = FDMExec->GetState();
77   Atmosphere  = FDMExec->GetAtmosphere();
78   FCS         = FDMExec->GetFCS();
79   Aircraft    = FDMExec->GetAircraft();
80   Translation = FDMExec->GetTranslation();
81   Rotation    = FDMExec->GetRotation();
82   Position    = FDMExec->GetPosition();
83   Auxiliary   = FDMExec->GetAuxiliary();
84   Output      = FDMExec->GetOutput();
85
86   Name = engineName;
87   fullpath = enginePath + "/" + engineName + ".dat";
88   ifstream enginefile(fullpath.c_str());
89
90   if (enginefile) {
91     enginefile >> tag;
92
93     if      (tag == "ROCKET")    Type = etRocket;
94     else if (tag == "PISTON")    Type = etPiston;
95     else if (tag == "TURBOPROP") Type = etTurboProp;
96     else if (tag == "TURBOJET")  Type = etTurboJet;
97     else                         Type = etUnknown;
98
99     switch(Type) {
100     case etUnknown:
101       cerr << "Unknown engine type: " << tag << endl;
102       break;
103     case etPiston:
104       enginefile >> X;
105       enginefile >> Y;
106       enginefile >> Z;
107       enginefile >> BrakeHorsePower;
108       enginefile >> MaxThrottle;
109       enginefile >> MinThrottle;
110       enginefile >> SLFuelFlowMax;
111       enginefile >> SpeedSlope;
112       enginefile >> SpeedIntercept;
113       enginefile >> AltitudeSlope;
114
115       break;
116     case etRocket:
117       enginefile >> X;
118       enginefile >> Y;
119       enginefile >> Z;
120       enginefile >> SLThrustMax;
121       enginefile >> VacThrustMax;
122       enginefile >> MaxThrottle;
123       enginefile >> MinThrottle;
124       enginefile >> SLFuelFlowMax;
125       enginefile >> SLOxiFlowMax;
126       break;
127     }
128
129     enginefile.close();
130   } else {
131     cerr << "Unable to open engine definition file " << fullpath << endl;
132   }
133
134   EngineNumber = num;
135   Thrust = PctPower = 0.0;
136   Starved = Flameout = false;
137 }
138
139
140 FGEngine::~FGEngine(void) {}
141
142
143
144 float FGEngine::CalcRocketThrust(void) {
145   float lastThrust;
146
147   Throttle = FCS->GetThrottlePos(EngineNumber);
148   lastThrust = Thrust;                 // last actual thrust
149
150   if (Throttle < MinThrottle || Starved) {
151     PctPower = Thrust = 0.0; // desired thrust
152     Flameout = true;
153   } else {
154     PctPower = Throttle / MaxThrottle;
155     Thrust = PctPower*((1.0 - Atmosphere->GetDensityRatio())*(VacThrustMax - SLThrustMax) +
156                        SLThrustMax); // desired thrust
157     Flameout = false;
158   }
159
160
161   Thrust -= 0.8*(Thrust - lastThrust); // actual thrust
162
163   return Thrust;
164 }
165
166
167 float FGEngine::CalcPistonThrust(void) {
168   float v,h,pa;
169
170   Throttle = FCS->GetThrottlePos(EngineNumber);
171   Throttle /= 100;
172
173   v=Translation->GetVt();
174   h=Position->Geth();
175   if(v < 10)
176     v=10;
177   if(h < 0)
178     h=0;
179   
180   pa=(SpeedSlope*v + SpeedIntercept)*(1 +AltitudeSlope*h)*BrakeHorsePower;
181   
182   Thrust= Throttle*(pa*HPTOFTLBSSEC)/v;
183
184   return Thrust;
185 }
186
187
188 float FGEngine::CalcThrust(void) {
189   switch(Type) {
190   case etRocket:
191     return CalcRocketThrust();
192     // break;
193   case etPiston:
194     return CalcPistonThrust();
195     // break;
196   default:
197     return 9999.0;
198     // break;
199   }
200
201 }
202
203 float FGEngine::CalcFuelNeed() {
204   FuelNeed = SLFuelFlowMax*PctPower;
205   return FuelNeed;
206 }
207
208
209 float FGEngine::CalcOxidizerNeed() {
210   OxidizerNeed = SLOxiFlowMax*PctPower;
211   return OxidizerNeed;
212 }
213