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