]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Sync. with JSBSim CVS
[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   char property_name[80];
119   snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
120   PropertyManager->Tie( property_name, this, &FGEngine::GetRunning, &FGEngine::SetRunning );
121   snprintf(property_name, 80, "propulsion/engine[%u]/thrust-lbs", EngineNumber);
122   PropertyManager->Tie( property_name, this, &FGEngine::GetThrust);
123   snprintf(property_name, 80, "propulsion/engine[%u]/fuel-flow-rate-pps", EngineNumber);
124   PropertyManager->Tie( property_name, this, &FGEngine::GetFuelFlowRate);
125
126   Debug(0);
127 }
128
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 FGEngine::~FGEngine()
132 {
133   delete Thruster;
134   Debug(1);
135 }
136
137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138
139 void FGEngine::ResetToIC(void)
140 {
141   Thrust = 0.0;
142   Throttle = 0.0;
143   Mixture = 1.0;
144   Starter = false;
145   FuelExpended = 0.0;
146   Starved = Running = Cranking = false;
147   PctPower = 0.0;
148   TrimMode = false;
149   FuelFlow_gph = 0.0;
150   FuelFlow_pph = 0.0;
151   FuelFreeze = false;
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 // This base class function should be called from within the
156 // derived class' Calculate() function before any other calculations are done.
157 // This base class method removes fuel from the fuel tanks as appropriate,
158 // and sets the starved flag if necessary.
159 // This version of the fuel consumption code should never see an oxidizer tank.
160
161 void FGEngine::ConsumeFuel(void)
162 {
163   if (FuelFreeze) return;
164   if (TrimMode) return;
165
166   unsigned int i;
167   double Fshortage, TanksWithFuel;
168   FGTank* Tank;
169   Fshortage = TanksWithFuel = 0.0;
170
171   // count how many assigned tanks have fuel
172   for (i=0; i<SourceTanks.size(); i++) {
173     Tank = Propulsion->GetTank(SourceTanks[i]);
174     if (Tank->GetType() == FGTank::ttFUEL){
175       if (Tank->GetContents() > 0.0) ++TanksWithFuel;
176     } else {
177        cerr << "No oxidizer tanks should be used for this engine type." << endl;
178     }
179   }
180   if (TanksWithFuel==0) {
181     Starved = true;
182     return;
183   }
184
185   for (i=0; i<SourceTanks.size(); i++) {
186     Tank = Propulsion->GetTank(SourceTanks[i]);
187     if (Tank->GetType() == FGTank::ttFUEL) {
188        Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
189     } else {
190        cerr << "No oxidizer tanks should be used for this engine type." << endl;
191     }
192   }
193
194   if (Fshortage < 0.00) Starved = true;
195   else Starved = false;
196 }
197
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199
200 double FGEngine::CalcFuelNeed(void)
201 {
202   double dT = State->Getdt()*Propulsion->GetRate();
203   FuelFlowRate = SLFuelFlowMax*PctPower;
204   FuelExpended = FuelFlowRate*dT;
205   return FuelExpended;
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
211 {
212   X = location(eX);
213   Y = location(eY);
214   Z = location(eZ);
215   EnginePitch = orientation(ePitch);
216   EngineYaw = orientation (eYaw);
217 }
218
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221 void FGEngine::AddFeedTank(int tkID)
222 {
223   SourceTanks.push_back(tkID);
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 FGColumnVector3& FGEngine::GetBodyForces(void)
229 {
230   return Thruster->GetBodyForces();
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 FGColumnVector3& FGEngine::GetMoments(void)
236 {
237   return Thruster->GetMoments();
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242 bool FGEngine::LoadThruster(Element *thruster_element)
243 {
244   string token, fullpath, localpath;
245   string thruster_filename, thruster_fullpathname, thrType;
246   double P_Factor = 0, Sense = 0.0;
247   string enginePath = FDMExec->GetEnginePath();
248   string aircraftPath = FDMExec->GetFullAircraftPath();
249   ifstream thruster_file;
250   FGColumnVector3 location, orientation;
251   string separator = "/";
252
253   fullpath = enginePath + separator;
254   localpath = aircraftPath + separator + "Engines" + separator;
255
256   thruster_filename = thruster_element->GetAttributeValue("file");
257   if ( !thruster_filename.empty()) {
258     thruster_fullpathname = fullpath + thruster_filename + ".xml";
259     thruster_file.open(thruster_fullpathname.c_str());
260     if ( !thruster_file.is_open()) {
261       thruster_fullpathname = localpath + thruster_filename + ".xml";
262       thruster_file.open(thruster_fullpathname.c_str());
263       if ( !thruster_file.is_open()) {
264         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
265         return false;
266       } else {
267         thruster_file.close();
268       }
269     } else {
270       thruster_file.close();
271     }
272   } else {
273     cerr << "No thruster filename given." << endl;
274     return false;
275   }
276
277   document = LoadXMLDocument(thruster_fullpathname);
278   document->SetParent(thruster_element);
279
280   thrType = document->GetName();
281
282   if (thrType == "propeller") {
283     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
284   } else if (thrType == "nozzle") {
285     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
286   } else if (thrType == "direct") {
287     Thruster = new FGThruster( FDMExec, document, EngineNumber);
288   }
289
290   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
291
292   Debug(2);
293   return true;
294 }
295
296 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 //    The bitmasked value choices are as follows:
298 //    unset: In this case (the default) JSBSim would only print
299 //       out the normally expected messages, essentially echoing
300 //       the config files as they are read. If the environment
301 //       variable is not set, debug_lvl is set to 1 internally
302 //    0: This requests JSBSim not to output any messages
303 //       whatsoever.
304 //    1: This value explicity requests the normal JSBSim
305 //       startup messages
306 //    2: This value asks for a message to be printed out when
307 //       a class is instantiated
308 //    4: When this value is set, a message is displayed when a
309 //       FGModel object executes its Run() method
310 //    8: When this value is set, various runtime state variables
311 //       are printed out periodically
312 //    16: When set various parameters are sanity checked and
313 //       a message is printed out when they go out of bounds
314
315 void FGEngine::Debug(int from)
316 {
317   if (debug_lvl <= 0) return;
318
319   if (debug_lvl & 1) { // Standard console startup message output
320     if (from == 0) { // Constructor
321
322     }
323     if (from == 2) { // After thruster loading
324       cout << "      X = " << Thruster->GetLocationX() << endl;
325       cout << "      Y = " << Thruster->GetLocationY() << endl;
326       cout << "      Z = " << Thruster->GetLocationZ() << endl;
327       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
328       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
329     }
330   }
331   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
332     if (from == 0) cout << "Instantiated: FGEngine" << endl;
333     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
334   }
335   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
336   }
337   if (debug_lvl & 8 ) { // Runtime state variables
338   }
339   if (debug_lvl & 16) { // Sanity checking
340   }
341   if (debug_lvl & 64) {
342     if (from == 0) { // Constructor
343       cout << IdSrc << endl;
344       cout << IdHdr << endl;
345     }
346   }
347 }
348 }