]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Update to the latest version of JSBSim which supports Lighter Than Air craft
[flightgear.git] / src / FDM / JSBSim / models / propulsion / 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 Lesser 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 Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28 See header file.
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 01/21/99   JSB   Created
33 09/03/99   JSB   Changed Rocket thrust equation to correct -= Thrust instead of
34                  += Thrust (thanks to Tony Peden)
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #ifdef FGFS
41 #  include <simgear/compiler.h>
42 #  ifdef SG_HAVE_STD_INCLUDES
43 #    include <fstream>
44 #  else
45 #    include <fstream.h>
46 #  endif
47 #else
48 #  if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
49 #    include <fstream.h>
50 #  else
51 #    include <fstream>
52 #  endif
53 #endif
54
55 #include "FGEngine.h"
56 #include "FGTank.h"
57 #include "FGPropeller.h"
58 #include "FGNozzle.h"
59 #include <input_output/FGXMLParse.h>
60 #include <math/FGColumnVector3.h>
61
62 namespace JSBSim {
63
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_ENGINE;
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 CLASS IMPLEMENTATION
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
72                       : EngineNumber(engine_number)
73 {
74   Element* local_element;
75   FGColumnVector3 location, orientation;
76
77   Name = "";
78   Type = etUnknown;
79   X = Y = Z = 0.0;
80   EnginePitch = EngineYaw = 0.0;
81   SLFuelFlowMax = SLOxiFlowMax = 0.0;
82   MaxThrottle = 1.0;
83   MinThrottle = 0.0;
84
85   ResetToIC(); // initialize dynamic terms
86
87   FDMExec = exec;
88   State = FDMExec->GetState();
89   Atmosphere = FDMExec->GetAtmosphere();
90   FCS = FDMExec->GetFCS();
91   Propulsion = FDMExec->GetPropulsion();
92   Aircraft = FDMExec->GetAircraft();
93   Propagate = FDMExec->GetPropagate();
94   Auxiliary = FDMExec->GetAuxiliary();
95
96   PropertyManager = FDMExec->GetPropertyManager();
97
98   Name = engine_element->GetAttributeValue("name");
99
100 // Find and set engine location
101
102   local_element = engine_element->GetParent()->FindElement("location");
103   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
104   else      cerr << "No engine location found for this engine." << endl;
105
106   local_element = engine_element->GetParent()->FindElement("orient");
107   if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
108 //  else          cerr << "No engine orientation found for this engine." << endl;
109 // Jon: The engine orientation has a default and is not normally used.
110
111   SetPlacement(location, orientation);
112
113   // Load thruster
114   local_element = engine_element->GetParent()->FindElement("thruster");
115   if (local_element) {
116     if (!LoadThruster(local_element)) exit(-1);
117   } else {
118     cerr << "No thruster definition supplied with engine definition." << endl;
119   }
120
121   // Load feed tank[s] references
122   local_element = engine_element->GetParent()->FindElement("feed");
123   if (local_element) {
124     while (local_element) {
125       AddFeedTank((int)local_element->GetDataAsNumber());
126       local_element = engine_element->GetParent()->FindNextElement("feed");
127     }
128   } else {
129     cerr << "No feed tank specified in engine definition." << endl;
130   }
131
132   char property_name[80];
133   snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
134   PropertyManager->Tie( property_name, (FGEngine*)this, &FGEngine::GetRunning,
135                                        &FGEngine::SetRunning );
136
137   Debug(0);
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 FGEngine::~FGEngine()
143 {
144   delete Thruster;
145   Debug(1);
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 void FGEngine::ResetToIC(void)
151 {
152   Thrust = 0.0;
153   Throttle = 0.0;
154   Mixture = 1.0;
155   Starter = false;
156   FuelNeed = OxidizerNeed = 0.0;
157   Starved = Running = Cranking = false;
158   PctPower = 0.0;
159   TrimMode = false;
160   FuelFlow_gph = 0.0;
161   FuelFlow_pph = 0.0;
162   FuelFreeze = false;
163 }
164
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 // This base class function should be called from within the
167 // derived class' Calculate() function before any other calculations are done.
168 // This base class method removes fuel from the fuel tanks as appropriate,
169 // and sets the starved flag if necessary.
170
171 void FGEngine::ConsumeFuel(void)
172 {
173   if (FuelFreeze) return;
174   if (TrimMode) return;
175
176   unsigned int i;
177   double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
178   FGTank* Tank;
179   bool haveOxTanks = false;
180   Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
181
182   // count how many assigned tanks have fuel
183   for (i=0; i<SourceTanks.size(); i++) {
184     Tank = Propulsion->GetTank(SourceTanks[i]);
185     if (Tank->GetType() == FGTank::ttFUEL){
186       if (Tank->GetContents() > 0.0) ++TanksWithFuel;
187     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
188       haveOxTanks = true;
189       if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
190     }
191   }
192   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
193     Starved = true;
194     return;
195   }
196
197   for (i=0; i<SourceTanks.size(); i++) {
198     Tank = Propulsion->GetTank(SourceTanks[i]);
199     if (Tank->GetType() == FGTank::ttFUEL) {
200        Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
201     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
202        Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
203     }
204   }
205
206   if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
207   else Starved = false;
208 }
209
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 double FGEngine::CalcFuelNeed(void)
213 {
214   FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
215   return FuelNeed;
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 double FGEngine::CalcOxidizerNeed(void)
221 {
222   OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
223   return OxidizerNeed;
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
229 {
230   X = location(eX);
231   Y = location(eY);
232   Z = location(eZ);
233   EnginePitch = orientation(ePitch);
234   EngineYaw = orientation (eYaw);
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 void FGEngine::AddFeedTank(int tkID)
240 {
241   SourceTanks.push_back(tkID);
242 }
243
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245
246 FGColumnVector3& FGEngine::GetBodyForces(void)
247 {
248   return Thruster->GetBodyForces();
249 }
250
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252
253 FGColumnVector3& FGEngine::GetMoments(void)
254 {
255   return Thruster->GetMoments();
256 }
257
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259
260 bool FGEngine::LoadThruster(Element *thruster_element)
261 {
262   string token, fullpath, localpath;
263   string thruster_filename, thruster_fullpathname, thrType;
264   double P_Factor = 0, Sense = 0.0;
265   string enginePath = FDMExec->GetEnginePath();
266   string aircraftPath = FDMExec->GetFullAircraftPath();
267   ifstream thruster_file;
268   FGColumnVector3 location, orientation;
269   string separator = "/";
270
271 # ifdef macintosh
272     separator = ";";
273 # endif
274
275   fullpath = enginePath + separator;
276   localpath = aircraftPath + separator + "Engines" + separator;
277
278   thruster_filename = thruster_element->GetAttributeValue("file");
279   if ( !thruster_filename.empty()) {
280     thruster_fullpathname = fullpath + thruster_filename + ".xml";
281     thruster_file.open(thruster_fullpathname.c_str());
282     if ( !thruster_file.is_open()) {
283       thruster_fullpathname = localpath + thruster_filename + ".xml";
284       thruster_file.open(thruster_fullpathname.c_str());
285       if ( !thruster_file.is_open()) {
286         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
287         return false;
288       } else {
289         thruster_file.close();
290       }
291     } else {
292       thruster_file.close();
293     }
294   } else {
295     cerr << "No thruster filename given." << endl;
296     return false;
297   }
298
299   document = LoadXMLDocument(thruster_fullpathname);
300   document->SetParent(thruster_element);
301
302   thrType = document->GetName();
303
304   if (thrType == "propeller") {
305     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
306   } else if (thrType == "nozzle") {
307     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
308   } else if (thrType == "direct") {
309     Thruster = new FGThruster( FDMExec, document, EngineNumber);
310   }
311
312   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
313
314   Debug(2);
315   return true;
316 }
317
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 //    The bitmasked value choices are as follows:
320 //    unset: In this case (the default) JSBSim would only print
321 //       out the normally expected messages, essentially echoing
322 //       the config files as they are read. If the environment
323 //       variable is not set, debug_lvl is set to 1 internally
324 //    0: This requests JSBSim not to output any messages
325 //       whatsoever.
326 //    1: This value explicity requests the normal JSBSim
327 //       startup messages
328 //    2: This value asks for a message to be printed out when
329 //       a class is instantiated
330 //    4: When this value is set, a message is displayed when a
331 //       FGModel object executes its Run() method
332 //    8: When this value is set, various runtime state variables
333 //       are printed out periodically
334 //    16: When set various parameters are sanity checked and
335 //       a message is printed out when they go out of bounds
336
337 void FGEngine::Debug(int from)
338 {
339   if (debug_lvl <= 0) return;
340
341   if (debug_lvl & 1) { // Standard console startup message output
342     if (from == 0) { // Constructor
343
344     }
345     if (from == 2) { // After thruster loading
346       cout << "      X = " << Thruster->GetLocationX() << endl;
347       cout << "      Y = " << Thruster->GetLocationY() << endl;
348       cout << "      Z = " << Thruster->GetLocationZ() << endl;
349       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
350       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
351     }
352   }
353   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
354     if (from == 0) cout << "Instantiated: FGEngine" << endl;
355     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
356   }
357   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
358   }
359   if (debug_lvl & 8 ) { // Runtime state variables
360   }
361   if (debug_lvl & 16) { // Sanity checking
362   }
363   if (debug_lvl & 64) {
364     if (from == 0) { // Constructor
365       cout << IdSrc << endl;
366       cout << IdHdr << endl;
367     }
368   }
369 }
370 }