]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGEngine.cpp
Sync w. 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 #include "FGPropeller.h"
59 #include "FGNozzle.h"
60
61 namespace JSBSim {
62
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_ENGINE;
65
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 CLASS IMPLEMENTATION
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69
70
71 FGEngine::FGEngine(FGFDMExec* exec)
72 {
73   Name = "";
74   Type = etUnknown;
75   X = Y = Z = 0.0;
76   EnginePitch = EngineYaw = 0.0;
77   SLFuelFlowMax = SLOxiFlowMax = 0.0;
78   MaxThrottle = 1.0;
79   MinThrottle = 0.0;
80   Thrust = 0.0;
81   Throttle = 0.0;
82   Mixture = 1.0;
83   Starter = false;
84   FuelNeed = OxidizerNeed = 0.0;
85   Starved = Running = Cranking = false;
86   PctPower = 0.0;
87   EngineNumber = -1;
88   TrimMode = false;
89   FuelFlow_gph = 0.0;
90   FuelFlow_pph = 0.0;
91
92   FDMExec = exec;
93   State = FDMExec->GetState();
94   Atmosphere = FDMExec->GetAtmosphere();
95   FCS = FDMExec->GetFCS();
96   Propulsion = FDMExec->GetPropulsion();
97   Aircraft = FDMExec->GetAircraft();
98   Propagate = FDMExec->GetPropagate();
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, TanksWithFuel;
123   FGTank* Tank;
124
125   if (TrimMode) return;
126   Fshortage = Oshortage = TanksWithFuel = 0.0;
127  
128   // count how many assigned tanks have fuel
129   for (unsigned int i=0; i<SourceTanks.size(); i++) {
130     Tank = Propulsion->GetTank(SourceTanks[i]);
131     if (Tank->GetContents() > 0.0) {
132       ++TanksWithFuel;
133     }
134   }
135   if (!TanksWithFuel) return;
136
137   for (unsigned int i=0; i<SourceTanks.size(); i++) {
138     Tank = Propulsion->GetTank(SourceTanks[i]);
139     if (Tank->GetType() == FGTank::ttFUEL) {
140        Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
141     } else {
142        Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithFuel);
143     }
144   }
145
146   if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
147   else Starved = false;
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 double FGEngine::CalcFuelNeed(void)
153 {
154   FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
155   return FuelNeed;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 double FGEngine::CalcOxidizerNeed(void)
161 {
162   OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
163   return OxidizerNeed;
164 }
165
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 void FGEngine::SetPlacement(double x, double y, double z, double pitch, double yaw)
169 {
170   X = x;
171   Y = y;
172   Z = z;
173   EnginePitch = pitch;
174   EngineYaw = yaw;
175 }
176
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178
179 void FGEngine::AddFeedTank(int tkID)
180 {
181   SourceTanks.push_back(tkID);
182 }
183
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 FGColumnVector3& FGEngine::GetBodyForces(void)
187 {
188   return Thruster->GetBodyForces();
189 }
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 FGColumnVector3& FGEngine::GetMoments(void)
194 {
195   return Thruster->GetMoments();
196 }
197
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199
200 bool FGEngine::LoadThruster(FGConfigFile* AC_cfg)
201 {
202   string token, fullpath, localpath;
203   string thrusterFileName, thrType, engineFileName;
204   FGConfigFile* Cfg_ptr = 0;
205   double xLoc, yLoc, zLoc, Pitch, Yaw;
206   double P_Factor = 0, Sense = 0.0;
207   string enginePath = FDMExec->GetEnginePath();
208   string aircraftPath = FDMExec->GetAircraftPath();
209   thrusterFileName = AC_cfg->GetValue("FILE");
210
211 # ifndef macintosh
212       fullpath = enginePath + "/";
213       localpath = aircraftPath + "/"  + "/Engines/";
214 # else
215       fullpath = enginePath + ";";
216       localpath = aircraftPath + ";" + ";Engines;";
217 # endif
218
219   // Look in the Aircraft/Engines directory first
220   FGConfigFile Local_Thruster_cfg(localpath + thrusterFileName + ".xml");
221   FGConfigFile Thruster_cfg(fullpath + thrusterFileName + ".xml");
222
223   if (Local_Thruster_cfg.IsOpen()) {
224     Cfg_ptr = &Local_Thruster_cfg;
225     if (debug_lvl > 0) cout << "\n    Reading thruster from file: " << localpath
226                                       + thrusterFileName + ".xml"<< endl;
227   } else {
228     if (Thruster_cfg.IsOpen()) {
229       Cfg_ptr = &Thruster_cfg;
230       if (debug_lvl > 0) cout << "\n    Reading thruster from file: " << fullpath
231                                         + thrusterFileName + ".xml"<< endl;
232     }
233   }
234
235   if (Cfg_ptr) {
236     Cfg_ptr->GetNextConfigLine();
237     thrType = Cfg_ptr->GetValue();
238
239     if (thrType == "FG_PROPELLER") {
240       Thruster = new FGPropeller(FDMExec, Cfg_ptr);
241     } else if (thrType == "FG_NOZZLE") {
242       Thruster = new FGNozzle(FDMExec, Cfg_ptr);
243     } else if (thrType == "FG_DIRECT") {
244       Thruster = new FGThruster( FDMExec, Cfg_ptr);
245     }
246
247     AC_cfg->GetNextConfigLine();
248     while ((token = AC_cfg->GetValue()) != string("/AC_THRUSTER")) {
249       *AC_cfg >> token;
250       if (token == "XLOC") *AC_cfg >> xLoc;
251       else if (token == "YLOC") *AC_cfg >> yLoc;
252       else if (token == "ZLOC") *AC_cfg >> zLoc;
253       else if (token == "PITCH") *AC_cfg >> Pitch;
254       else if (token == "YAW") *AC_cfg >> Yaw;
255       else if (token == "P_FACTOR") *AC_cfg >> P_Factor;
256       else if (token == "SENSE")   *AC_cfg >> Sense;
257       else cerr << "Unknown identifier: " << token << " in engine file: "
258                 << engineFileName << endl;
259     }
260
261     Thruster->SetLocation(xLoc, yLoc, zLoc);
262     Thruster->SetAnglesToBody(0, Pitch, Yaw);
263     if (thrType == "FG_PROPELLER" && P_Factor > 0.001) {
264       ((FGPropeller*)Thruster)->SetPFactor(P_Factor);
265       if (debug_lvl > 0) cout << "      P-Factor: " << P_Factor << endl;
266       ((FGPropeller*)Thruster)->SetSense(fabs(Sense)/Sense);
267       if (debug_lvl > 0) cout << "      Sense: " << Sense <<  endl;
268     }
269     Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
270     return true;
271   } else {
272
273     cerr << "Could not read thruster config file: " << fullpath
274               + thrusterFileName + ".xml" << endl;
275     return false;
276   }
277
278 }
279
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 //    The bitmasked value choices are as follows:
282 //    unset: In this case (the default) JSBSim would only print
283 //       out the normally expected messages, essentially echoing
284 //       the config files as they are read. If the environment
285 //       variable is not set, debug_lvl is set to 1 internally
286 //    0: This requests JSBSim not to output any messages
287 //       whatsoever.
288 //    1: This value explicity requests the normal JSBSim
289 //       startup messages
290 //    2: This value asks for a message to be printed out when
291 //       a class is instantiated
292 //    4: When this value is set, a message is displayed when a
293 //       FGModel object executes its Run() method
294 //    8: When this value is set, various runtime state variables
295 //       are printed out periodically
296 //    16: When set various parameters are sanity checked and
297 //       a message is printed out when they go out of bounds
298
299 void FGEngine::Debug(int from)
300 {
301   if (debug_lvl <= 0) return;
302
303   if (debug_lvl & 1) { // Standard console startup message output
304     if (from == 0) { // Constructor
305
306     }
307   }
308   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
309     if (from == 0) cout << "Instantiated: FGEngine" << endl;
310     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
311   }
312   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
313   }
314   if (debug_lvl & 8 ) { // Runtime state variables
315   }
316   if (debug_lvl & 16) { // Sanity checking
317   }
318   if (debug_lvl & 64) {
319     if (from == 0) { // Constructor
320       cout << IdSrc << endl;
321       cout << IdHdr << endl;
322     }
323   }
324 }
325 }