Skip to content

Commit

Permalink
Made propulsion systems independent from other models by passing in i…
Browse files Browse the repository at this point in the history
…nput data rather than pulling it in from external sources.
  • Loading branch information
jberndt committed Aug 3, 2011
1 parent cfd7492 commit 4e47d8d
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 360 deletions.
4 changes: 3 additions & 1 deletion src/FGFDMExec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ using namespace std;

namespace JSBSim {

static const char *IdSrc = "$Id: FGFDMExec.cpp,v 1.105 2011/07/28 12:48:18 jberndt Exp $";
static const char *IdSrc = "$Id: FGFDMExec.cpp,v 1.106 2011/08/03 03:21:05 jberndt Exp $";
static const char *IdHdr = ID_FDMEXEC;

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -438,6 +438,8 @@ void FGFDMExec::LoadInputs(unsigned int idx)
Propulsion->in.MixtureCmd = FCS->GetMixtureCmd();
Propulsion->in.PropAdvance = FCS->GetPropAdvance();
Propulsion->in.PropFeather = FCS->GetPropFeather();
Propulsion->in.H_agl = Propagate->GetDistanceAGL();
Propulsion->in.PQR = Propagate->GetPQR();
break;
case eAerodynamics:
Aerodynamics->in.Alpha = Auxiliary->Getalpha();
Expand Down
114 changes: 108 additions & 6 deletions src/models/FGPropulsion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ INCLUDES
#include <sstream>
#include <cstdlib>
#include <iomanip>

#include "FGFDMExec.h"
#include "FGPropulsion.h"
#include "models/FGMassBalance.h"
#include "models/propulsion/FGThruster.h"
#include "models/propulsion/FGRocket.h"
#include "models/propulsion/FGTurbine.h"
#include "models/propulsion/FGPiston.h"
Expand All @@ -65,7 +66,7 @@ using namespace std;

namespace JSBSim {

static const char *IdSrc = "$Id: FGPropulsion.cpp,v 1.49 2011/07/28 12:48:19 jberndt Exp $";
static const char *IdSrc = "$Id: FGPropulsion.cpp,v 1.50 2011/08/03 03:21:06 jberndt Exp $";
static const char *IdHdr = ID_PROPULSION;

extern short debug_lvl;
Expand All @@ -87,7 +88,7 @@ FGPropulsion::FGPropulsion(FGFDMExec* exec) : FGModel(exec)
tankJ.InitMatrix();
refuel = dump = false;
DumpRate = 0.0;
fuel_freeze = false;
FuelFreeze = false;
TotalFuelQuantity = 0.0;
IsBound =
HavePistonEngine =
Expand Down Expand Up @@ -163,13 +164,14 @@ bool FGPropulsion::Run(bool Holding)

for (i=0; i<numEngines; i++) {
Engines[i]->Calculate();
ConsumeFuel(Engines[i]);
vForces += Engines[i]->GetBodyForces(); // sum body frame forces
vMoments += Engines[i]->GetMoments(); // sum body frame moments
}

TotalFuelQuantity = 0.0;
for (i=0; i<numTanks; i++) {
Tanks[i]->Calculate( in.TotalDeltaT );
Tanks[i]->Calculate( in.TotalDeltaT, in.TAT_c);
if (Tanks[i]->GetType() == FGTank::ttFUEL) {
TotalFuelQuantity += Tanks[i]->GetContents();
}
Expand All @@ -183,6 +185,106 @@ bool FGPropulsion::Run(bool Holding)
return false;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// The engine can tell us how much fuel it needs, but it is up to the propulsion
// subsystem manager class FGPropulsion to manage fuel flow amongst tanks. Engines
// May burn fuel from more than one tank at a time, and may burn from one tank
// before another - that is, may burn from one tank until the tank is depleted,
// then burn from the next highest priority tank. This can be accompished
// by defining a fuel management system, but this way of specifying priorities
// is more automatic from a user perspective.

void FGPropulsion::ConsumeFuel(FGEngine* engine)
{
if (FuelFreeze) return;
if (FDMExec->GetTrimStatus()) return;

unsigned int TanksWithFuel=0, CurrentFuelTankPriority=1;
unsigned int TanksWithOxidizer=0, CurrentOxidizerTankPriority=1;
vector <int> FeedListFuel, FeedListOxi;
bool Starved = true; // Initially set Starved to true. Set to false in code below.
// bool hasOxTanks = false;

// For this engine,
// 1) Count how many fuel tanks with the current priority level have fuel
// 2) If there none, then try next lower priority (higher number) - that is,
// increment CurrentPriority.
// 3) Build the feed list.
// 4) Do the same for oxidizer tanks, if needed.

// Process fuel tanks, if any
while ((TanksWithFuel == 0) && (CurrentFuelTankPriority <= numTanks)) {
for (unsigned int i=0; i<engine->GetNumSourceTanks(); i++) {
unsigned int TankId = engine->GetSourceTank(i);
FGTank* Tank = Tanks[TankId];
unsigned int TankPriority = Tank->GetPriority();
if (TankPriority != 0) {
switch(Tank->GetType()) {
case FGTank::ttFUEL:
if ((Tank->GetContents() > 0.0) && Tank->GetSelected() && (TankPriority == CurrentFuelTankPriority)) {
TanksWithFuel++;
Starved = false;
FeedListFuel.push_back(TankId);
}
break;
case FGTank::ttOXIDIZER:
// Skip this here (done below)
break;
}
}
}
if (TanksWithFuel == 0) CurrentFuelTankPriority++; // No tanks at this priority, try next priority
}

// Process Oxidizer tanks, if any
if (engine->GetType() == FGEngine::etRocket) {
while ((TanksWithOxidizer == 0) && (CurrentOxidizerTankPriority <= numTanks)) {
for (unsigned int i=0; i<engine->GetNumSourceTanks(); i++) {
unsigned int TankId = engine->GetSourceTank(i);
FGTank* Tank = Tanks[TankId];
unsigned int TankPriority = Tank->GetPriority();
if (TankPriority != 0) {
switch(Tank->GetType()) {
case FGTank::ttFUEL:
// Skip this here (done above)
break;
case FGTank::ttOXIDIZER:
// hasOxTanks = true;
if (Tank->GetContents() > 0.0 && Tank->GetSelected() && TankPriority == CurrentOxidizerTankPriority) {
TanksWithOxidizer++;
if (TanksWithFuel > 0) Starved = false;
FeedListOxi.push_back(TankId);
}
break;
}
}
}
if (TanksWithOxidizer == 0) CurrentOxidizerTankPriority++; // No tanks at this priority, try next priority
}
}

engine->SetStarved(Starved); // Tanks can be refilled, so be sure to reset engine Starved flag here.

// No fuel or fuel/oxidizer found at any priority!
if (Starved) return;

double FuelToBurn = engine->CalcFuelNeed(); // How much fuel does this engine need?
double FuelNeededPerTank = FuelToBurn / TanksWithFuel; // Determine fuel needed per tank.
for (unsigned int i=0; i<FeedListFuel.size(); i++) {
Tanks[FeedListFuel[i]]->Drain(FuelNeededPerTank);
}

if (engine->GetType() == FGEngine::etRocket) {
double OxidizerToBurn = engine->CalcOxidizerNeed(); // How much fuel does this engine need?
double OxidizerNeededPerTank = OxidizerToBurn / TanksWithOxidizer; // Determine fuel needed per tank.
for (unsigned int i=0; i<FeedListOxi.size(); i++) {
Tanks[FeedListOxi[i]]->Drain(OxidizerNeededPerTank);
}
}

}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bool FGPropulsion::GetSteadyState(void)
Expand All @@ -205,7 +307,7 @@ bool FGPropulsion::GetSteadyState(void)
while (!steady && j < 6000) {
Engines[i]->Calculate();
lastThrust = currentThrust;
currentThrust = Engines[i]->GetThruster()->GetThrust();
currentThrust = Engines[i]->GetThrust();
if (fabs(lastThrust-currentThrust) < 0.0001) {
steady_count++;
if (steady_count > 120) {
Expand Down Expand Up @@ -657,7 +759,7 @@ void FGPropulsion::DumpFuel(double time_slice)

void FGPropulsion::SetFuelFreeze(bool f)
{
fuel_freeze = f;
FuelFreeze = f;
for (unsigned int i=0; i<numEngines; i++) {
Engines[i]->SetFuelFreeze(f);
}
Expand Down
9 changes: 5 additions & 4 deletions src/models/FGPropulsion.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ INCLUDES
DEFINITIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#define ID_PROPULSION "$Id: FGPropulsion.h,v 1.29 2011/07/28 12:48:19 jberndt Exp $"
#define ID_PROPULSION "$Id: FGPropulsion.h,v 1.30 2011/08/03 03:21:06 jberndt Exp $"

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FORWARD DECLARATIONS
Expand Down Expand Up @@ -92,7 +92,7 @@ CLASS DOCUMENTATION
@endcode
@author Jon S. Berndt
@version $Id: FGPropulsion.h,v 1.29 2011/07/28 12:48:19 jberndt Exp $
@version $Id: FGPropulsion.h,v 1.30 2011/08/03 03:21:06 jberndt Exp $
@see
FGEngine
FGTank
Expand Down Expand Up @@ -186,7 +186,7 @@ class FGPropulsion : public FGModel, public FGXMLFileRead
std::ifstream* FindEngineFile(const std::string& filename);
std::string FindEngineFullPathname(const std::string& engine_filename);
inline int GetActiveEngine(void) const {return ActiveEngine;}
inline bool GetFuelFreeze(void) {return fuel_freeze;}
inline bool GetFuelFreeze(void) {return FuelFreeze;}
double GetTotalFuelQuantity(void) const {return TotalFuelQuantity;}

void SetMagnetos(int setting);
Expand Down Expand Up @@ -215,7 +215,7 @@ class FGPropulsion : public FGModel, public FGXMLFileRead
FGMatrix33 tankJ;
bool refuel;
bool dump;
bool fuel_freeze;
bool FuelFreeze;
double TotalFuelQuantity;
double DumpRate;
bool IsBound;
Expand All @@ -224,6 +224,7 @@ class FGPropulsion : public FGModel, public FGXMLFileRead
bool HaveTurboPropEngine;
bool HaveRocketEngine;
bool HaveElectricEngine;
void ConsumeFuel(FGEngine* engine);

int InitializedEngines;
bool HasInitializedEngines;
Expand Down
3 changes: 2 additions & 1 deletion src/models/propulsion/FGElectric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using namespace std;

namespace JSBSim {

static const char *IdSrc = "$Id: FGElectric.cpp,v 1.12 2011/07/28 12:48:19 jberndt Exp $";
static const char *IdSrc = "$Id: FGElectric.cpp,v 1.13 2011/08/03 03:21:06 jberndt Exp $";
static const char *IdHdr = ID_ELECTRIC;

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -98,6 +98,7 @@ void FGElectric::Calculate(void)

HP = PowerWatts * in.ThrottlePos[EngineNumber] / hptowatts;

LoadThrusterInputs();
Thruster->Calculate(HP * hptoftlbssec);

RunPostFunctions();
Expand Down
Loading

0 comments on commit 4e47d8d

Please sign in to comment.