]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGEngine.cpp
FG_HAVE_STD_INCLUDES -> SG_HAVE_STD_INCLUDES
[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 SG_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 static const char *IdSrc = "$Header$";
66 static const char *IdHdr = "ID_ENGINE";
67
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 CLASS IMPLEMENTATION
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71
72
73 FGEngine::FGEngine(FGFDMExec* fdex, string enginePath, string engineName, int num) {
74   string fullpath;
75   string tag;
76
77   FDMExec = fdex;
78
79   State       = FDMExec->GetState();
80   Atmosphere  = FDMExec->GetAtmosphere();
81   FCS         = FDMExec->GetFCS();
82   Aircraft    = FDMExec->GetAircraft();
83   Translation = FDMExec->GetTranslation();
84   Rotation    = FDMExec->GetRotation();
85   Position    = FDMExec->GetPosition();
86   Auxiliary   = FDMExec->GetAuxiliary();
87   Output      = FDMExec->GetOutput();
88
89   Name = engineName;
90
91 # ifndef macintosh  
92     fullpath = enginePath + "/" + engineName + ".xml";
93 # else
94     fullpath = enginePath + ";" + engineName + ".xml";
95 # endif
96       
97   cout << "    Reading engine: " << engineName << " from file: " << fullpath << endl;
98   ifstream enginefile(fullpath.c_str());
99
100   if (enginefile) {
101     enginefile >> tag;
102
103     if      (tag == "ROCKET")    Type = etRocket;
104     else if (tag == "PISTON")    Type = etPiston;
105     else if (tag == "TURBOPROP") Type = etTurboProp;
106     else if (tag == "TURBOJET")  Type = etTurboJet;
107     else                         Type = etUnknown;
108
109     switch(Type) {
110     case etTurboProp:
111     case etTurboJet: 
112       cerr << "Unsupported Engine type" << tag << endl;
113       break;
114     case etUnknown:
115       cerr << "Unknown engine type: " << tag << endl;
116       break;
117     case etPiston:
118       enginefile >> X;
119       enginefile >> Y;
120       enginefile >> Z;
121       enginefile >> EnginePitch;
122       enginefile >> EngineYaw;
123       enginefile >> BrakeHorsePower;
124       enginefile >> MaxThrottle;
125       enginefile >> MinThrottle;
126       enginefile >> SLFuelFlowMax;
127       enginefile >> SpeedSlope;
128       enginefile >> SpeedIntercept;
129       enginefile >> AltitudeSlope;
130
131       break;
132     case etRocket:
133       enginefile >> X;
134       enginefile >> Y;
135       enginefile >> Z;
136       enginefile >> EnginePitch;
137       enginefile >> EngineYaw;
138       enginefile >> SLThrustMax;
139       enginefile >> VacThrustMax;
140       enginefile >> MaxThrottle;
141       enginefile >> MinThrottle;
142       enginefile >> SLFuelFlowMax;
143       enginefile >> SLOxiFlowMax;
144       break;
145     }
146
147     enginefile.close();
148   } else {
149     cerr << "Unable to open engine definition file " << fullpath.c_str() << endl;
150   }
151
152   EngineNumber = num;
153   Thrust = PctPower = 0.0;
154   Starved = Flameout = false;
155   Running = true;
156 }
157
158
159 FGEngine::~FGEngine(void) {}
160
161
162 float FGEngine::CalcRocketThrust(void) {
163   float lastThrust;
164
165   Throttle = FCS->GetThrottlePos(EngineNumber);
166   lastThrust = Thrust;                 // last actual thrust
167
168   if (Throttle < MinThrottle || Starved) {
169     PctPower = Thrust = 0.0; // desired thrust
170     Flameout = true;
171   } else {
172     PctPower = Throttle / MaxThrottle;
173     Thrust = PctPower*((1.0 - Atmosphere->GetDensityRatio())*(VacThrustMax - SLThrustMax) +
174                        SLThrustMax); // desired thrust
175     Flameout = false;
176   }
177
178
179   if(State->Getdt() > 0.0) {
180     Thrust -= 0.8*(Thrust - lastThrust); // actual thrust
181   }
182
183   return Thrust;
184 }
185
186
187 float FGEngine::CalcPistonThrust(void) {
188   float v,h,pa;
189
190   Throttle = FCS->GetThrottlePos(EngineNumber);
191   Throttle /= 100;
192
193   v = Translation->GetVt();
194   h = Position->Geth();
195
196   if (v < 10)
197     v = 10;
198   if (h < 0)
199     h = 0;
200
201   pa=(SpeedSlope*v + SpeedIntercept)*(1 +AltitudeSlope*h)*BrakeHorsePower;
202
203   Thrust = Throttle*(pa*HPTOFTLBSSEC)/v;
204
205   return Thrust;
206 }
207
208
209 float FGEngine::CalcThrust(void) {
210   if(Running) {
211     switch(Type) {
212     case etRocket:
213       return CalcRocketThrust();
214       // break;
215     case etPiston:
216       return CalcPistonThrust();
217       // break;
218     default:
219       return 9999.0;
220       // break;
221     }
222   } else {
223     return 0;
224   }
225 }
226
227 float FGEngine::CalcFuelNeed() {
228   FuelNeed = SLFuelFlowMax*PctPower;
229   return FuelNeed;
230 }
231
232
233 float FGEngine::CalcOxidizerNeed() {
234   OxidizerNeed = SLOxiFlowMax*PctPower;
235   return OxidizerNeed;
236 }
237