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