1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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.
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
33 09/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
34 += Thrust (thanks to Tony Peden)
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGPropeller.h"
44 #include "input_output/FGXMLParse.h"
45 #include "math/FGColumnVector3.h"
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_ENGINE;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
58 : EngineNumber(engine_number)
60 Element* local_element;
61 FGColumnVector3 location, orientation;
66 EnginePitch = EngineYaw = 0.0;
72 ResetToIC(); // initialize dynamic terms
75 State = FDMExec->GetState();
76 Atmosphere = FDMExec->GetAtmosphere();
77 FCS = FDMExec->GetFCS();
78 Propulsion = FDMExec->GetPropulsion();
79 Aircraft = FDMExec->GetAircraft();
80 Propagate = FDMExec->GetPropagate();
81 Auxiliary = FDMExec->GetAuxiliary();
83 PropertyManager = FDMExec->GetPropertyManager();
85 Name = engine_element->GetAttributeValue("name");
87 // Find and set engine location
89 local_element = engine_element->GetParent()->FindElement("location");
90 if (local_element) location = local_element->FindElementTripletConvertTo("IN");
91 else cerr << "No engine location found for this engine." << endl;
93 local_element = engine_element->GetParent()->FindElement("orient");
94 if (local_element) orientation = local_element->FindElementTripletConvertTo("RAD");
95 // else cerr << "No engine orientation found for this engine." << endl;
96 // Jon: The engine orientation has a default and is not normally used.
98 SetPlacement(location, orientation);
101 local_element = engine_element->GetParent()->FindElement("thruster");
103 if (!LoadThruster(local_element)) exit(-1);
105 cerr << "No thruster definition supplied with engine definition." << endl;
108 // Build and initialize the feed tank vector.
109 for (i=0; i<(Propulsion->GetNumTanks()); i++) {
110 SourceTanks.push_back(0);
113 // Load feed tank[s] references
114 local_element = engine_element->GetParent()->FindElement("feed");
116 while (local_element) {
117 int tankID = (int)local_element->GetDataAsNumber();
118 AddFeedTank( tankID , Propulsion->GetTank(tankID)->GetPriority());
119 local_element = engine_element->GetParent()->FindNextElement("feed");
122 cerr << "No feed tank specified in engine definition." << endl;
125 string property_name, base_property_name;
126 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
128 property_name = base_property_name + "/set-running";
129 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
130 property_name = base_property_name + "/thrust-lbs";
131 PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
132 property_name = base_property_name + "/fuel-flow-rate-pps";
133 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 FGEngine::~FGEngine()
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 void FGEngine::ResetToIC(void)
154 Starved = Running = Cranking = false;
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 // This base class function should be called from within the
164 // derived class' Calculate() function before any other calculations are done.
165 // This base class method removes fuel from the fuel tanks as appropriate,
166 // and sets the starved flag if necessary.
167 // This version of the fuel consumption code should never see an oxidizer tank.
169 void FGEngine::ConsumeFuel(void)
171 if (FuelFreeze) return;
172 if (TrimMode) return;
175 double Fshortage, FuelNeeded;
177 unsigned int TanksWithFuel = 0;
178 Fshortage = FuelNeeded = 0.0;
180 unsigned int CurrentPriority = 1;
181 vector <int> FeedList;
184 FuelToBurn = CalcFuelNeed();
186 while (FuelToBurn > 0.0) {
188 // Count how many fuel tanks with the current priority level have fuel.
189 // If none, then try next lower priority. Build the feed list.
190 while ((TanksWithFuel == 0.0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
191 for (i=0; i<Propulsion->GetNumTanks(); i++) {
192 if (SourceTanks[i] != 0) {
193 Tank = Propulsion->GetTank(i);
194 if (Tank->GetType() == FGTank::ttFUEL) {
195 if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
197 FeedList.push_back(i);
200 cerr << "No oxidizer tanks should be used for this engine type." << endl;
204 if (TanksWithFuel == 0) CurrentPriority++;
207 // No fuel found at any priority!
208 if (TanksWithFuel == 0) {
213 // Remove equal amount of fuel from each feed tank.
214 FuelNeeded = FuelToBurn/TanksWithFuel;
215 for (i=0; i<FeedList.size(); i++) {
216 Tank = Propulsion->GetTank(FeedList[i]);
217 Tank->Drain(FuelNeeded);
218 FuelToBurn -= FuelNeeded;
221 // check if we were not able to burn all the fuel we needed to at this priority level
222 if (FuelToBurn > 0.0) {
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 double FGEngine::CalcFuelNeed(void)
235 double dT = State->Getdt()*Propulsion->GetRate();
236 FuelFlowRate = SLFuelFlowMax*PctPower;
237 FuelExpended = FuelFlowRate*dT;
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
248 EnginePitch = orientation(ePitch);
249 EngineYaw = orientation (eYaw);
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 void FGEngine::AddFeedTank(int tkID, int priority)
256 SourceTanks[tkID] = priority;
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 FGColumnVector3& FGEngine::GetBodyForces(void)
263 return Thruster->GetBodyForces();
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 FGColumnVector3& FGEngine::GetMoments(void)
270 return Thruster->GetMoments();
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 bool FGEngine::LoadThruster(Element *thruster_element)
277 string token, fullpath, localpath;
278 string thruster_filename, thruster_fullpathname, thrType;
279 string enginePath = FDMExec->GetEnginePath();
280 string aircraftPath = FDMExec->GetFullAircraftPath();
281 ifstream thruster_file;
282 FGColumnVector3 location, orientation;
283 string separator = "/";
285 fullpath = enginePath + separator;
286 localpath = aircraftPath + separator + "Engines" + separator;
288 thruster_filename = thruster_element->GetAttributeValue("file");
289 if ( !thruster_filename.empty()) {
290 thruster_fullpathname = fullpath + thruster_filename + ".xml";
291 thruster_file.open(thruster_fullpathname.c_str());
292 if ( !thruster_file.is_open()) {
293 thruster_fullpathname = localpath + thruster_filename + ".xml";
294 thruster_file.open(thruster_fullpathname.c_str());
295 if ( !thruster_file.is_open()) {
296 cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
299 thruster_file.close();
302 thruster_file.close();
305 cerr << "No thruster filename given." << endl;
309 document = LoadXMLDocument(thruster_fullpathname);
310 document->SetParent(thruster_element);
312 thrType = document->GetName();
314 if (thrType == "propeller") {
315 Thruster = new FGPropeller(FDMExec, document, EngineNumber);
316 } else if (thrType == "nozzle") {
317 Thruster = new FGNozzle(FDMExec, document, EngineNumber);
318 } else if (thrType == "direct") {
319 Thruster = new FGThruster( FDMExec, document, EngineNumber);
322 Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 // The bitmasked value choices are as follows:
330 // unset: In this case (the default) JSBSim would only print
331 // out the normally expected messages, essentially echoing
332 // the config files as they are read. If the environment
333 // variable is not set, debug_lvl is set to 1 internally
334 // 0: This requests JSBSim not to output any messages
336 // 1: This value explicity requests the normal JSBSim
338 // 2: This value asks for a message to be printed out when
339 // a class is instantiated
340 // 4: When this value is set, a message is displayed when a
341 // FGModel object executes its Run() method
342 // 8: When this value is set, various runtime state variables
343 // are printed out periodically
344 // 16: When set various parameters are sanity checked and
345 // a message is printed out when they go out of bounds
347 void FGEngine::Debug(int from)
349 if (debug_lvl <= 0) return;
351 if (debug_lvl & 1) { // Standard console startup message output
352 if (from == 0) { // Constructor
355 if (from == 2) { // After thruster loading
356 cout << " X = " << Thruster->GetLocationX() << endl;
357 cout << " Y = " << Thruster->GetLocationY() << endl;
358 cout << " Z = " << Thruster->GetLocationZ() << endl;
359 cout << " Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
360 cout << " Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
363 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
364 if (from == 0) cout << "Instantiated: FGEngine" << endl;
365 if (from == 1) cout << "Destroyed: FGEngine" << endl;
367 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
369 if (debug_lvl & 8 ) { // Runtime state variables
371 if (debug_lvl & 16) { // Sanity checking
373 if (debug_lvl & 64) {
374 if (from == 0) { // Constructor
375 cout << IdSrc << endl;
376 cout << IdHdr << endl;