]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Merge branch 'next' into durk-atc
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGEngine.cpp
index a385088ae8a08a7fc354c11a63e74062b3821c06..707d425e4c7433aa72a51a4f71d8f2a0057629e1 100644 (file)
@@ -5,22 +5,22 @@
  Date started: 01/21/99
  Called by:    FGAircraft
 
- ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free Software
+ the terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 
- You should have received a copy of the GNU General Public License along with
+ You should have received a copy of the GNU Lesser General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place - Suite 330, Boston, MA  02111-1307, USA.
 
- Further information about the GNU General Public License can also be found on
+ Further information about the GNU Lesser General Public License can also be found on
  the world wide web at http://www.gnu.org.
 
 FUNCTIONAL DESCRIPTION
@@ -37,31 +37,24 @@ HISTORY
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-#ifdef FGFS
-#  include <simgear/compiler.h>
-#  ifdef SG_HAVE_STD_INCLUDES
-#    include <fstream>
-#  else
-#    include <fstream.h>
-#  endif
-#else
-#  if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
-#    include <fstream.h>
-#  else
-#    include <fstream>
-#  endif
-#endif
-
 #include "FGEngine.h"
 #include "FGTank.h"
 #include "FGPropeller.h"
 #include "FGNozzle.h"
-#include <input_output/FGXMLParse.h>
-#include <math/FGColumnVector3.h>
+#include "FGRotor.h"
+#include "models/FGPropulsion.h"
+#include "input_output/FGXMLParse.h"
+#include "math/FGColumnVector3.h"
+
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+
+using namespace std;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGEngine.cpp,v 1.42 2011/03/03 12:16:26 jberndt Exp $";
 static const char *IdHdr = ID_ENGINE;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -78,23 +71,15 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   Type = etUnknown;
   X = Y = Z = 0.0;
   EnginePitch = EngineYaw = 0.0;
-  SLFuelFlowMax = SLOxiFlowMax = 0.0;
+  SLFuelFlowMax = 0.0;
   MaxThrottle = 1.0;
   MinThrottle = 0.0;
-  Thrust = 0.0;
-  Throttle = 0.0;
-  Mixture = 1.0;
-  Starter = false;
-  FuelNeed = OxidizerNeed = 0.0;
-  Starved = Running = Cranking = false;
-  PctPower = 0.0;
-  TrimMode = false;
-  FuelFlow_gph = 0.0;
-  FuelFlow_pph = 0.0;
-  FuelFreeze = false;
+  FuelDensity = 6.0;
+  unsigned int i;
+
+  ResetToIC(); // initialize dynamic terms
 
   FDMExec = exec;
-  State = FDMExec->GetState();
   Atmosphere = FDMExec->GetAtmosphere();
   FCS = FDMExec->GetFCS();
   Propulsion = FDMExec->GetPropulsion();
@@ -106,6 +91,8 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
 
   Name = engine_element->GetAttributeValue("name");
 
+  Load(engine_element, PropertyManager, to_string(EngineNumber)); // Call ModelFunctions loader
+
 // Find and set engine location
 
   local_element = engine_element->GetParent()->FindElement("location");
@@ -113,30 +100,64 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   else      cerr << "No engine location found for this engine." << endl;
 
   local_element = engine_element->GetParent()->FindElement("orient");
-  if (local_element)  orientation = local_element->FindElementTripletConvertTo("IN");
-  else          cerr << "No engine orientation found for this engine." << endl;
+  if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
+//  else          cerr << "No engine orientation found for this engine." << endl;
+// Jon: The engine orientation has a default and is not normally used.
 
   SetPlacement(location, orientation);
 
   // Load thruster
   local_element = engine_element->GetParent()->FindElement("thruster");
   if (local_element) {
-    LoadThruster(local_element);
+    try {
+      if (!LoadThruster(local_element)) exit(-1);
+    } catch (std::string str) {
+      throw("Error loading engine " + Name + ". " + str);
+    }
   } else {
     cerr << "No thruster definition supplied with engine definition." << endl;
   }
 
+  // Build and initialize the feed tank vector.
+  for (i=0; i<(Propulsion->GetNumTanks()); i++) {
+    SourceTanks.push_back(0);
+  }
+
   // Load feed tank[s] references
   local_element = engine_element->GetParent()->FindElement("feed");
   if (local_element) {
     while (local_element) {
-      AddFeedTank((int)local_element->GetDataAsNumber());
+      int tankID = (int)local_element->GetDataAsNumber();
+      FGTank* tank = Propulsion->GetTank(tankID); 
+      if (tank) {
+        AddFeedTank(tankID, tank->GetPriority());
+        FuelDensity = tank->GetDensity();
+      } else {
+        cerr << "Feed tank " << tankID <<
+          " specified in engine definition does not exist." << endl;
+      }
       local_element = engine_element->GetParent()->FindNextElement("feed");
     }
   } else {
     cerr << "No feed tank specified in engine definition." << endl;
   }
 
+  string property_name, base_property_name;
+  base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
+
+  property_name = base_property_name + "/set-running";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
+  property_name = base_property_name + "/thrust-lbs";
+  PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
+  property_name = base_property_name + "/fuel-flow-rate-pps";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
+  property_name = base_property_name + "/fuel-used-lbs";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelUsedLbs);
+
+  PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
+
+  //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
+
   Debug(0);
 }
 
@@ -144,66 +165,95 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
 
 FGEngine::~FGEngine()
 {
-  if (Thruster) delete Thruster;
+  delete Thruster;
   Debug(1);
 }
 
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGEngine::ResetToIC(void)
+{
+  Throttle = 0.0;
+  Mixture = 1.0;
+  Starter = false;
+  FuelExpended = 0.0;
+  Starved = Running = Cranking = false;
+  PctPower = 0.0;
+  FuelFlow_gph = 0.0;
+  FuelFlow_pph = 0.0;
+  FuelFlowRate = 0.0;
+  FuelFreeze = false;
+  FuelUsedLbs = 0.0;
+}
+
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 // This base class function should be called from within the
 // derived class' Calculate() function before any other calculations are done.
 // This base class method removes fuel from the fuel tanks as appropriate,
 // and sets the starved flag if necessary.
+// This version of the fuel consumption code should never see an oxidizer tank.
 
 void FGEngine::ConsumeFuel(void)
 {
   if (FuelFreeze) return;
-  if (TrimMode) return;
+  if (FDMExec->GetTrimStatus()) return;
 
   unsigned int i;
-  double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
+  double Fshortage, FuelNeeded;
   FGTank* Tank;
-  bool haveOxTanks = false;
-  Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
-
-  // count how many assigned tanks have fuel
-  for (i=0; i<SourceTanks.size(); i++) {
-    Tank = Propulsion->GetTank(SourceTanks[i]);
-    if (Tank->GetType() == FGTank::ttFUEL){
-      if (Tank->GetContents() > 0.0) ++TanksWithFuel;
-    } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
-      haveOxTanks = true;
-      if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
-    }
-  }
-  if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) return;
-
-  for (i=0; i<SourceTanks.size(); i++) {
-    Tank = Propulsion->GetTank(SourceTanks[i]);
-    if (Tank->GetType() == FGTank::ttFUEL) {
-       Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
-    } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
-       Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
+  unsigned int TanksWithFuel = 0;
+  Fshortage = FuelNeeded = 0.0;
+  double FuelToBurn;
+  unsigned int CurrentPriority = 1;
+  vector <int> FeedList;
+  Starved = false;
+
+  FuelToBurn = CalcFuelNeed();
+  if (FuelToBurn == 0.0) return;
+
+  // Count how many fuel tanks with the current priority level have fuel.
+  // If none, then try next lower priority.  Build the feed list.
+  while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
+    for (i=0; i<Propulsion->GetNumTanks(); i++) {
+      if (SourceTanks[i] != 0) {
+        Tank = Propulsion->GetTank(i);
+        if (Tank->GetType() == FGTank::ttFUEL) {
+          if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
+             ++TanksWithFuel;
+             FeedList.push_back(i);
+           } 
+        } else {
+           cerr << "No oxidizer tanks should be used for this engine type." << endl;
+        }
+      }
     }
+    if (TanksWithFuel == 0) CurrentPriority++;
   }
 
-  if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
-  else Starved = false;
-}
+  // No fuel found at any priority!
+  if (TanksWithFuel == 0) {
+    Starved = true;
+    return;
+  }
 
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  // Remove equal amount of fuel from each feed tank.  
+  FuelNeeded = FuelToBurn/TanksWithFuel;
+  for (i=0; i<FeedList.size(); i++) {
+    Tank = Propulsion->GetTank(FeedList[i]);
+    Tank->Drain(FuelNeeded); 
+  }
+  FuelUsedLbs += FuelToBurn;
 
-double FGEngine::CalcFuelNeed(void)
-{
-  FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
-  return FuelNeed;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGEngine::CalcOxidizerNeed(void)
+double FGEngine::CalcFuelNeed(void)
 {
-  OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
-  return OxidizerNeed;
+  double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
+  FuelFlowRate = SLFuelFlowMax*PctPower;
+  FuelExpended = FuelFlowRate*dT;
+  return FuelExpended;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -219,9 +269,9 @@ void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientat
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGEngine::AddFeedTank(int tkID)
+void FGEngine::AddFeedTank(int tkID, int priority)
 {
-  SourceTanks.push_back(tkID);
+  SourceTanks[tkID] = priority;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -244,20 +294,11 @@ bool FGEngine::LoadThruster(Element *thruster_element)
 {
   string token, fullpath, localpath;
   string thruster_filename, thruster_fullpathname, thrType;
-  double xLoc, yLoc, zLoc, Pitch, Yaw;
-  double P_Factor = 0, Sense = 0.0;
   string enginePath = FDMExec->GetEnginePath();
-  string aircraftPath = FDMExec->GetAircraftPath();
-  FGXMLParse thruster_file_parser;
-  Element *document, *element;
+  string aircraftPath = FDMExec->GetFullAircraftPath();
   ifstream thruster_file;
   FGColumnVector3 location, orientation;
-
-# ifndef macintosh
   string separator = "/";
-# else
-  string separator = ";";
-# endif
 
   fullpath = enginePath + separator;
   localpath = aircraftPath + separator + "Engines" + separator;
@@ -283,8 +324,7 @@ bool FGEngine::LoadThruster(Element *thruster_element)
     return false;
   }
 
-  readXML(thruster_fullpathname, thruster_file_parser);
-  document = thruster_file_parser.GetDocument(); // document holds the thruster description
+  document = LoadXMLDocument(thruster_fullpathname);
   document->SetParent(thruster_element);
 
   thrType = document->GetName();
@@ -293,11 +333,13 @@ bool FGEngine::LoadThruster(Element *thruster_element)
     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
   } else if (thrType == "nozzle") {
     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
+  } else if (thrType == "rotor") {
+    Thruster = new FGRotor(FDMExec, document, EngineNumber);
   } else if (thrType == "direct") {
     Thruster = new FGThruster( FDMExec, document, EngineNumber);
   }
 
-  Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
+  Thruster->SetdeltaT(FDMExec->GetDeltaT() * Propulsion->GetRate());
 
   Debug(2);
   return true;
@@ -334,8 +376,8 @@ void FGEngine::Debug(int from)
       cout << "      X = " << Thruster->GetLocationX() << endl;
       cout << "      Y = " << Thruster->GetLocationY() << endl;
       cout << "      Z = " << Thruster->GetLocationZ() << endl;
-      cout << "      Pitch = " << Thruster->GetAnglesToBody(ePitch) << endl;
-      cout << "      Yaw = " << Thruster->GetAnglesToBody(eYaw) << endl;
+      cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
+      cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
     }
   }
   if (debug_lvl & 2 ) { // Instantiation/Destruction notification