]> git.mxchange.org Git - flightgear.git/blob - JSBsim/FGEngine.cpp
dcc2f89f37d87cd551cbce9ecc30deb14e740df1
[flightgear.git] / 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
34 01/21/99   JSB   Created
35
36 ********************************************************************************
37 INCLUDES
38 *******************************************************************************/
39
40 #include <fstream.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "FGEngine.h"
46 #include "FGState.h"
47 #include "FGFDMExec.h"
48 #include "FGAtmosphere.h"
49 #include "FGFCS.h"
50 #include "FGAircraft.h"
51 #include "FGTranslation.h"
52 #include "FGRotation.h"
53 #include "FGPosition.h"
54 #include "FGAuxiliary.h"
55 #include "FGOutput.h"
56
57 /*******************************************************************************
58 ************************************ CODE **************************************
59 *******************************************************************************/
60
61
62 FGEngine::FGEngine(FGFDMExec* fdex, char *engineName, int num)
63 {
64   char fullpath[256];
65   char tag[256];
66
67   FDMExec = fdex;
68
69   State       = FDMExec->GetState();
70   Atmosphere  = FDMExec->GetAtmosphere();
71   FCS         = FDMExec->GetFCS();
72   Aircraft    = FDMExec->GetAircraft();
73   Translation = FDMExec->GetTranslation();
74   Rotation    = FDMExec->GetRotation();
75   Position    = FDMExec->GetPosition();
76   Auxiliary   = FDMExec->GetAuxiliary();
77   Output      = FDMExec->GetOutput();
78
79   strcpy(Name, engineName);
80   sprintf(fullpath,"/h/curt/projects/FlightGear/Simulator/FDM/JSBsim/engine/%s.dat", engineName);
81   ifstream enginefile(fullpath);
82
83   if (enginefile) {
84     enginefile >> tag;
85     if (strstr(tag,"ROCKET")) Type = 0;
86     else if (strstr(tag,"PISTON")) Type = 1;
87     else if (strstr(tag,"TURBOPROP")) Type = 2;
88     else if (strstr(tag,"TURBOJET")) Type = 3;
89     else Type = 0;
90     enginefile >> X;
91     enginefile >> Y;
92     enginefile >> Z;
93     enginefile >> SLThrustMax;
94     enginefile >> VacThrustMax;
95     enginefile >> MaxThrottle;
96     enginefile >> MinThrottle;
97     enginefile >> SLFuelFlowMax;
98     if (Type == 0)
99       enginefile >> SLOxiFlowMax;
100     enginefile.close();
101   } else {
102     cerr << "Unable to open engine definition file " << engineName << endl;
103   }
104
105   EngineNumber = num;
106   Thrust = 0.0;
107   Starved = Flameout = false;
108 }
109
110
111 FGEngine::~FGEngine(void)
112 {
113 }
114
115
116 float FGEngine::CalcRocketThrust(void)
117 {
118   float lastThrust;
119
120   Throttle = FCS->GetThrottle(EngineNumber);
121   lastThrust = Thrust;                 // last actual thrust
122
123   if (Throttle < MinThrottle || Starved) {
124     PctPower = Thrust = 0.0; // desired thrust
125     Flameout = true;
126   } else {
127     PctPower = Throttle / MaxThrottle;
128     Thrust = PctPower*((1.0 - Atmosphere->Getrho() / 0.002378)*(VacThrustMax - SLThrustMax) +
129                                SLThrustMax); // desired thrust
130     Flameout = false;
131   }
132
133   Thrust +=     0.8*(Thrust - lastThrust); // actual thrust
134
135   return Thrust;
136 }
137
138
139 float FGEngine::CalcPistonThrust(void)
140 {
141   return Thrust;
142 }
143
144
145 float FGEngine::CalcThrust(void)
146 {
147   switch(Type) {
148   case 0: // Rocket
149     return CalcRocketThrust();
150     // break;
151   case 1: // Piston
152     return CalcPistonThrust();
153     // break;
154   default:
155     return 9999.0;
156     // break;
157   }
158 }
159
160 float FGEngine::CalcFuelNeed() {
161   FuelNeed = SLFuelFlowMax*PctPower;
162   return FuelNeed;
163 }
164
165
166 float FGEngine::CalcOxidizerNeed() {
167   OxidizerNeed = SLOxiFlowMax*PctPower;
168   return OxidizerNeed;
169 }
170