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