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