]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGEngine.cpp
- fixed fuel-need calculations
[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 #  if defined(sgi) && !defined(__GNUC__)
50 #    include <fstream.h>
51 #  else
52 #    include <fstream>
53 #  endif
54 #endif
55
56 #include "FGEngine.h"
57 #include "FGTank.h"
58
59 static const char *IdSrc = "$Id$";
60 static const char *IdHdr = ID_ENGINE;
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 CLASS IMPLEMENTATION
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66
67 FGEngine::FGEngine(FGFDMExec* exec)
68   : Name(""),
69     Type(etUnknown),
70     X(0), Y(0), Z(0),
71     EnginePitch(0), EngineYaw(0),
72     SLFuelFlowMax(0), SLOxiFlowMax(0),
73     MaxThrottle(1.0), MinThrottle(0.0),
74     Thrust(0.0),
75     Throttle(0.0),
76     Mixture(1.0),
77     Magnetos(0),
78     Starter(false),
79     FuelNeed(0.0), OxidizerNeed(0.0),
80     Starved(false), Flameout(false), Running(false), Cranking(false),
81     PctPower(0.0),
82     EngineNumber(-1),
83     TrimMode(false),
84     FuelFlow_gph(0.0),
85     ManifoldPressure_inHg(0.0),
86     ExhaustGasTemp_degK(0.0),
87     CylinderHeadTemp_degK(0.0),
88     OilPressure_psi(0.0),
89     OilTemp_degK(0.0),
90     FDMExec(exec),
91     State(FDMExec->GetState()),
92     Atmosphere(FDMExec->GetAtmosphere()),
93     FCS(FDMExec->GetFCS()),
94     Propulsion(FDMExec->GetPropulsion()),
95     Aircraft(FDMExec->GetAircraft()),
96     Translation(FDMExec->GetTranslation()),
97     Rotation(FDMExec->GetRotation()),
98     Position(FDMExec->GetPosition()),
99     Auxiliary(FDMExec->GetAuxiliary()),
100     Output(FDMExec->GetOutput())
101 {
102   Debug(0);
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 FGEngine::~FGEngine()
108 {
109   Debug(1);
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 // This base class function should be called from within the
114 // derived class' Calculate() function before any other calculations are done.
115 // This base class method removes fuel from the fuel tanks as appropriate,
116 // and sets the starved flag if necessary.
117
118 void FGEngine::ConsumeFuel(void)
119 {
120   double Fshortage, Oshortage;
121   FGTank* Tank;
122
123   if (TrimMode) return;
124   Fshortage = Oshortage = 0.0;
125   for (unsigned int i=0; i<SourceTanks.size(); i++) {
126     Tank = Propulsion->GetTank(i);
127     if (Tank->GetType() == FGTank::ttFUEL) {
128       Fshortage += Tank->Reduce(CalcFuelNeed()/Propulsion->GetnumSelectedFuelTanks());
129     } else {
130       Oshortage += Tank->Reduce(CalcOxidizerNeed()/Propulsion->GetnumSelectedOxiTanks());
131     }
132   }
133
134   if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
135   else Starved = false;
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 double FGEngine::CalcFuelNeed(void) {
141   FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
142   return FuelNeed;
143 }
144
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 double FGEngine::CalcOxidizerNeed(void) {
148   OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
149   return OxidizerNeed;
150 }
151
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 void FGEngine::SetPlacement(double x, double y, double z, double pitch, double yaw) {
155   X = x;
156   Y = y;
157   Z = z;
158   EnginePitch = pitch;
159   EngineYaw = yaw;
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 void FGEngine::AddFeedTank(int tkID)
165 {
166   SourceTanks.push_back(tkID);
167 }
168
169 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 //    The bitmasked value choices are as follows:
171 //    unset: In this case (the default) JSBSim would only print
172 //       out the normally expected messages, essentially echoing
173 //       the config files as they are read. If the environment
174 //       variable is not set, debug_lvl is set to 1 internally
175 //    0: This requests JSBSim not to output any messages
176 //       whatsoever.
177 //    1: This value explicity requests the normal JSBSim
178 //       startup messages
179 //    2: This value asks for a message to be printed out when
180 //       a class is instantiated
181 //    4: When this value is set, a message is displayed when a
182 //       FGModel object executes its Run() method
183 //    8: When this value is set, various runtime state variables
184 //       are printed out periodically
185 //    16: When set various parameters are sanity checked and
186 //       a message is printed out when they go out of bounds
187
188 void FGEngine::Debug(int from)
189 {
190   if (debug_lvl <= 0) return;
191
192   if (debug_lvl & 1) { // Standard console startup message output
193     if (from == 0) { // Constructor
194
195     }
196   }
197   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
198     if (from == 0) cout << "Instantiated: FGEngine" << endl;
199     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
200   }
201   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
202   }
203   if (debug_lvl & 8 ) { // Runtime state variables
204   }
205   if (debug_lvl & 16) { // Sanity checking
206   }
207   if (debug_lvl & 64) {
208     if (from == 0) { // Constructor
209       cout << IdSrc << endl;
210       cout << IdHdr << endl;
211     }
212   }
213 }
214