]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Merge branch 'curt/navradio'
[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 (jon@jsbsim.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 #include "FGEngine.h"
41 #include "FGTank.h"
42 #include "FGPropeller.h"
43 #include "FGNozzle.h"
44 #include "FGState.h"
45 #include "models/FGPropulsion.h"
46 #include "input_output/FGXMLParse.h"
47 #include "math/FGColumnVector3.h"
48
49 #include <iostream>
50 #include <fstream>
51 #include <cstdlib>
52
53 using namespace std;
54
55 namespace JSBSim {
56
57 static const char *IdSrc = "$Id$";
58 static const char *IdHdr = ID_ENGINE;
59
60 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 CLASS IMPLEMENTATION
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63
64 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
65                       : EngineNumber(engine_number)
66 {
67   Element* local_element;
68   FGColumnVector3 location, orientation;
69
70   Name = "";
71   Type = etUnknown;
72   X = Y = Z = 0.0;
73   EnginePitch = EngineYaw = 0.0;
74   SLFuelFlowMax = 0.0;
75   MaxThrottle = 1.0;
76   MinThrottle = 0.0;
77   FuelDensity = 6.0;
78   unsigned int i;
79
80   ResetToIC(); // initialize dynamic terms
81
82   FDMExec = exec;
83   State = FDMExec->GetState();
84   Atmosphere = FDMExec->GetAtmosphere();
85   FCS = FDMExec->GetFCS();
86   Propulsion = FDMExec->GetPropulsion();
87   Aircraft = FDMExec->GetAircraft();
88   Propagate = FDMExec->GetPropagate();
89   Auxiliary = FDMExec->GetAuxiliary();
90
91   PropertyManager = FDMExec->GetPropertyManager();
92
93   Name = engine_element->GetAttributeValue("name");
94
95 // Find and set engine location
96
97   local_element = engine_element->GetParent()->FindElement("location");
98   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
99   else      cerr << "No engine location found for this engine." << endl;
100
101   local_element = engine_element->GetParent()->FindElement("orient");
102   if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
103 //  else          cerr << "No engine orientation found for this engine." << endl;
104 // Jon: The engine orientation has a default and is not normally used.
105
106   SetPlacement(location, orientation);
107
108   // Load thruster
109   local_element = engine_element->GetParent()->FindElement("thruster");
110   if (local_element) {
111     if (!LoadThruster(local_element)) exit(-1);
112   } else {
113     cerr << "No thruster definition supplied with engine definition." << endl;
114   }
115
116   // Build and initialize the feed tank vector.
117   for (i=0; i<(Propulsion->GetNumTanks()); i++) {
118     SourceTanks.push_back(0);
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       int tankID = (int)local_element->GetDataAsNumber();
126       FGTank* tank = Propulsion->GetTank(tankID); 
127       AddFeedTank( tankID , tank->GetPriority());
128       FuelDensity = tank->GetDensity();
129       local_element = engine_element->GetParent()->FindNextElement("feed");
130     }
131   } else {
132     cerr << "No feed tank specified in engine definition." << endl;
133   }
134
135   string property_name, base_property_name;
136   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
137
138   property_name = base_property_name + "/set-running";
139   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
140   property_name = base_property_name + "/thrust-lbs";
141   PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
142   property_name = base_property_name + "/fuel-flow-rate-pps";
143   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
144
145   //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
146
147   Debug(0);
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 FGEngine::~FGEngine()
153 {
154   delete Thruster;
155   Debug(1);
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 void FGEngine::ResetToIC(void)
161 {
162   Throttle = 0.0;
163   Mixture = 1.0;
164   Starter = false;
165   FuelExpended = 0.0;
166   Starved = Running = Cranking = false;
167   PctPower = 0.0;
168   TrimMode = false;
169   FuelFlow_gph = 0.0;
170   FuelFlow_pph = 0.0;
171   FuelFreeze = false;
172 }
173
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 // This base class function should be called from within the
176 // derived class' Calculate() function before any other calculations are done.
177 // This base class method removes fuel from the fuel tanks as appropriate,
178 // and sets the starved flag if necessary.
179 // This version of the fuel consumption code should never see an oxidizer tank.
180
181 void FGEngine::ConsumeFuel(void)
182 {
183   if (FuelFreeze) return;
184   if (TrimMode) return;
185
186   unsigned int i;
187   double Fshortage, FuelNeeded;
188   FGTank* Tank;
189   unsigned int TanksWithFuel = 0;
190   Fshortage = FuelNeeded = 0.0;
191   double FuelToBurn;
192   unsigned int CurrentPriority = 1;
193   vector <int> FeedList;
194   Starved = false;
195
196   FuelToBurn = CalcFuelNeed();
197   if (FuelToBurn == 0.0) return;
198
199   // Count how many fuel tanks with the current priority level have fuel.
200   // If none, then try next lower priority.  Build the feed list.
201   while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
202     for (i=0; i<Propulsion->GetNumTanks(); i++) {
203       if (SourceTanks[i] != 0) {
204         Tank = Propulsion->GetTank(i);
205         if (Tank->GetType() == FGTank::ttFUEL) {
206           if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
207              ++TanksWithFuel;
208              FeedList.push_back(i);
209            } 
210         } else {
211            cerr << "No oxidizer tanks should be used for this engine type." << endl;
212         }
213       }
214     }
215     if (TanksWithFuel == 0) CurrentPriority++;
216   }
217
218   // No fuel found at any priority!
219   if (TanksWithFuel == 0) {
220     Starved = true;
221     return;
222   }
223
224   // Remove equal amount of fuel from each feed tank.  
225   FuelNeeded = FuelToBurn/TanksWithFuel;
226   for (i=0; i<FeedList.size(); i++) {
227     Tank = Propulsion->GetTank(FeedList[i]);
228     Tank->Drain(FuelNeeded); 
229   }
230
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 double FGEngine::CalcFuelNeed(void)
236 {
237   double dT = State->Getdt()*Propulsion->GetRate();
238   FuelFlowRate = SLFuelFlowMax*PctPower;
239   FuelExpended = FuelFlowRate*dT;
240   return FuelExpended;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
246 {
247   X = location(eX);
248   Y = location(eY);
249   Z = location(eZ);
250   EnginePitch = orientation(ePitch);
251   EngineYaw = orientation (eYaw);
252 }
253
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255
256 void FGEngine::AddFeedTank(int tkID, int priority)
257 {
258   SourceTanks[tkID] = priority;
259 }
260
261 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262
263 FGColumnVector3& FGEngine::GetBodyForces(void)
264 {
265   return Thruster->GetBodyForces();
266 }
267
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269
270 FGColumnVector3& FGEngine::GetMoments(void)
271 {
272   return Thruster->GetMoments();
273 }
274
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276
277 bool FGEngine::LoadThruster(Element *thruster_element)
278 {
279   string token, fullpath, localpath;
280   string thruster_filename, thruster_fullpathname, thrType;
281   string enginePath = FDMExec->GetEnginePath();
282   string aircraftPath = FDMExec->GetFullAircraftPath();
283   ifstream thruster_file;
284   FGColumnVector3 location, orientation;
285   string separator = "/";
286
287   fullpath = enginePath + separator;
288   localpath = aircraftPath + separator + "Engines" + separator;
289
290   thruster_filename = thruster_element->GetAttributeValue("file");
291   if ( !thruster_filename.empty()) {
292     thruster_fullpathname = fullpath + thruster_filename + ".xml";
293     thruster_file.open(thruster_fullpathname.c_str());
294     if ( !thruster_file.is_open()) {
295       thruster_fullpathname = localpath + thruster_filename + ".xml";
296       thruster_file.open(thruster_fullpathname.c_str());
297       if ( !thruster_file.is_open()) {
298         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
299         return false;
300       } else {
301         thruster_file.close();
302       }
303     } else {
304       thruster_file.close();
305     }
306   } else {
307     cerr << "No thruster filename given." << endl;
308     return false;
309   }
310
311   document = LoadXMLDocument(thruster_fullpathname);
312   document->SetParent(thruster_element);
313
314   thrType = document->GetName();
315
316   if (thrType == "propeller") {
317     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
318   } else if (thrType == "nozzle") {
319     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
320   } else if (thrType == "direct") {
321     Thruster = new FGThruster( FDMExec, document, EngineNumber);
322   }
323
324   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
325
326   Debug(2);
327   return true;
328 }
329
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 //    The bitmasked value choices are as follows:
332 //    unset: In this case (the default) JSBSim would only print
333 //       out the normally expected messages, essentially echoing
334 //       the config files as they are read. If the environment
335 //       variable is not set, debug_lvl is set to 1 internally
336 //    0: This requests JSBSim not to output any messages
337 //       whatsoever.
338 //    1: This value explicity requests the normal JSBSim
339 //       startup messages
340 //    2: This value asks for a message to be printed out when
341 //       a class is instantiated
342 //    4: When this value is set, a message is displayed when a
343 //       FGModel object executes its Run() method
344 //    8: When this value is set, various runtime state variables
345 //       are printed out periodically
346 //    16: When set various parameters are sanity checked and
347 //       a message is printed out when they go out of bounds
348
349 void FGEngine::Debug(int from)
350 {
351   if (debug_lvl <= 0) return;
352
353   if (debug_lvl & 1) { // Standard console startup message output
354     if (from == 0) { // Constructor
355
356     }
357     if (from == 2) { // After thruster loading
358       cout << "      X = " << Thruster->GetLocationX() << endl;
359       cout << "      Y = " << Thruster->GetLocationY() << endl;
360       cout << "      Z = " << Thruster->GetLocationZ() << endl;
361       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
362       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
363     }
364   }
365   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
366     if (from == 0) cout << "Instantiated: FGEngine" << endl;
367     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
368   }
369   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
370   }
371   if (debug_lvl & 8 ) { // Runtime state variables
372   }
373   if (debug_lvl & 16) { // Sanity checking
374   }
375   if (debug_lvl & 64) {
376     if (from == 0) { // Constructor
377       cout << IdSrc << endl;
378       cout << IdHdr << endl;
379     }
380   }
381 }
382 }