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