]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Syn.c w. JSBSim.
[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 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 details.
18
19  You should have received a copy of the GNU 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 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   Thrust = 0.0;
85   Throttle = 0.0;
86   Mixture = 1.0;
87   Starter = false;
88   FuelNeed = OxidizerNeed = 0.0;
89   Starved = Running = Cranking = false;
90   PctPower = 0.0;
91   TrimMode = false;
92   FuelFlow_gph = 0.0;
93   FuelFlow_pph = 0.0;
94   FuelFreeze = false;
95
96   FDMExec = exec;
97   State = FDMExec->GetState();
98   Atmosphere = FDMExec->GetAtmosphere();
99   FCS = FDMExec->GetFCS();
100   Propulsion = FDMExec->GetPropulsion();
101   Aircraft = FDMExec->GetAircraft();
102   Propagate = FDMExec->GetPropagate();
103   Auxiliary = FDMExec->GetAuxiliary();
104
105   PropertyManager = FDMExec->GetPropertyManager();
106
107   Name = engine_element->GetAttributeValue("name");
108
109 // Find and set engine location
110
111   local_element = engine_element->GetParent()->FindElement("location");
112   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
113   else      cerr << "No engine location found for this engine." << endl;
114
115   local_element = engine_element->GetParent()->FindElement("orient");
116   if (local_element)  orientation = local_element->FindElementTripletConvertTo("IN");
117   else          cerr << "No engine orientation found for this engine." << endl;
118
119   SetPlacement(location, orientation);
120
121   // Load thruster
122   local_element = engine_element->GetParent()->FindElement("thruster");
123   if (local_element) {
124     LoadThruster(local_element);
125   } else {
126     cerr << "No thruster definition supplied with engine definition." << endl;
127   }
128
129   // Load feed tank[s] references
130   local_element = engine_element->GetParent()->FindElement("feed");
131   if (local_element) {
132     while (local_element) {
133       AddFeedTank((int)local_element->GetDataAsNumber());
134       local_element = engine_element->GetParent()->FindNextElement("feed");
135     }
136   } else {
137     cerr << "No feed tank specified in engine definition." << endl;
138   }
139
140   Debug(0);
141 }
142
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144
145 FGEngine::~FGEngine()
146 {
147   if (Thruster) delete Thruster;
148   Debug(1);
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 // This base class function should be called from within the
153 // derived class' Calculate() function before any other calculations are done.
154 // This base class method removes fuel from the fuel tanks as appropriate,
155 // and sets the starved flag if necessary.
156
157 void FGEngine::ConsumeFuel(void)
158 {
159   if (FuelFreeze) return;
160   if (TrimMode) return;
161
162   unsigned int i;
163   double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
164   FGTank* Tank;
165   bool haveOxTanks = false;
166   Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
167
168   // count how many assigned tanks have fuel
169   for (i=0; i<SourceTanks.size(); i++) {
170     Tank = Propulsion->GetTank(SourceTanks[i]);
171     if (Tank->GetType() == FGTank::ttFUEL){
172       if (Tank->GetContents() > 0.0) ++TanksWithFuel;
173     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
174       haveOxTanks = true;
175       if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
176     }
177   }
178   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) return;
179
180   for (i=0; i<SourceTanks.size(); i++) {
181     Tank = Propulsion->GetTank(SourceTanks[i]);
182     if (Tank->GetType() == FGTank::ttFUEL) {
183        Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
184     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
185        Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
186     }
187   }
188
189   if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
190   else Starved = false;
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195 double FGEngine::CalcFuelNeed(void)
196 {
197   FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
198   return FuelNeed;
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 double FGEngine::CalcOxidizerNeed(void)
204 {
205   OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
206   return OxidizerNeed;
207 }
208
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210
211 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
212 {
213   X = location(eX);
214   Y = location(eY);
215   Z = location(eZ);
216   EnginePitch = orientation(ePitch);
217   EngineYaw = orientation (eYaw);
218 }
219
220 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221
222 void FGEngine::AddFeedTank(int tkID)
223 {
224   SourceTanks.push_back(tkID);
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 FGColumnVector3& FGEngine::GetBodyForces(void)
230 {
231   return Thruster->GetBodyForces();
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 FGColumnVector3& FGEngine::GetMoments(void)
237 {
238   return Thruster->GetMoments();
239 }
240
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242
243 bool FGEngine::LoadThruster(Element *thruster_element)
244 {
245   string token, fullpath, localpath;
246   string thruster_filename, thruster_fullpathname, thrType;
247   double xLoc, yLoc, zLoc, Pitch, Yaw;
248   double P_Factor = 0, Sense = 0.0;
249   string enginePath = FDMExec->GetEnginePath();
250   string aircraftPath = FDMExec->GetAircraftPath();
251   FGXMLParse thruster_file_parser;
252   Element *document, *element;
253   ifstream thruster_file;
254   FGColumnVector3 location, orientation;
255
256 # ifndef macintosh
257   string separator = "/";
258 # else
259   string separator = ";";
260 # endif
261
262   fullpath = enginePath + separator;
263   localpath = aircraftPath + separator + "Engines" + separator;
264
265   thruster_filename = thruster_element->GetAttributeValue("file");
266   if ( !thruster_filename.empty()) {
267     thruster_fullpathname = fullpath + thruster_filename + ".xml";
268     thruster_file.open(thruster_fullpathname.c_str());
269     if ( !thruster_file.is_open()) {
270       thruster_fullpathname = localpath + thruster_filename + ".xml";
271       thruster_file.open(thruster_fullpathname.c_str());
272       if ( !thruster_file.is_open()) {
273         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
274         return false;
275       } else {
276         thruster_file.close();
277       }
278     } else {
279       thruster_file.close();
280     }
281   } else {
282     cerr << "No thruster filename given." << endl;
283     return false;
284   }
285
286   readXML(thruster_fullpathname, thruster_file_parser);
287   document = thruster_file_parser.GetDocument(); // document holds the thruster description
288   document->SetParent(thruster_element);
289
290   thrType = document->GetName();
291
292   if (thrType == "propeller") {
293     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
294   } else if (thrType == "nozzle") {
295     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
296   } else if (thrType == "direct") {
297     Thruster = new FGThruster( FDMExec, document, EngineNumber);
298   }
299
300   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
301
302   Debug(2);
303   return true;
304 }
305
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 //    The bitmasked value choices are as follows:
308 //    unset: In this case (the default) JSBSim would only print
309 //       out the normally expected messages, essentially echoing
310 //       the config files as they are read. If the environment
311 //       variable is not set, debug_lvl is set to 1 internally
312 //    0: This requests JSBSim not to output any messages
313 //       whatsoever.
314 //    1: This value explicity requests the normal JSBSim
315 //       startup messages
316 //    2: This value asks for a message to be printed out when
317 //       a class is instantiated
318 //    4: When this value is set, a message is displayed when a
319 //       FGModel object executes its Run() method
320 //    8: When this value is set, various runtime state variables
321 //       are printed out periodically
322 //    16: When set various parameters are sanity checked and
323 //       a message is printed out when they go out of bounds
324
325 void FGEngine::Debug(int from)
326 {
327   if (debug_lvl <= 0) return;
328
329   if (debug_lvl & 1) { // Standard console startup message output
330     if (from == 0) { // Constructor
331
332     }
333     if (from == 2) { // After thruster loading
334       cout << "      X = " << Thruster->GetLocationX() << endl;
335       cout << "      Y = " << Thruster->GetLocationY() << endl;
336       cout << "      Z = " << Thruster->GetLocationZ() << endl;
337       cout << "      Pitch = " << Thruster->GetAnglesToBody(ePitch) << endl;
338       cout << "      Yaw = " << Thruster->GetAnglesToBody(eYaw) << endl;
339     }
340   }
341   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
342     if (from == 0) cout << "Instantiated: FGEngine" << endl;
343     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
344   }
345   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
346   }
347   if (debug_lvl & 8 ) { // Runtime state variables
348   }
349   if (debug_lvl & 16) { // Sanity checking
350   }
351   if (debug_lvl & 64) {
352     if (from == 0) { // Constructor
353       cout << IdSrc << endl;
354       cout << IdHdr << endl;
355     }
356   }
357 }
358 }