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