From b1c964030d7cb8fc5a161cd40c5ea0300021290e Mon Sep 17 00:00:00 2001 From: andy Date: Fri, 30 Apr 2004 19:06:29 +0000 Subject: [PATCH] Refactoring in preparation to add a turbine engine to YASim. The PistonEngine class has grown an "Engine" superclass. Some other stuff moved around too, and I cleaned up some property naming while I was in there. This hasn't been tested very thorougly, hopefully I didn't break anything. --- src/FDM/YASim/ControlMap.cpp | 2 +- src/FDM/YASim/Engine.hpp | 45 +++++++++++++++++++++++++++ src/FDM/YASim/FGFDM.cpp | 57 ++++++++++++++++++---------------- src/FDM/YASim/PistonEngine.cpp | 35 --------------------- src/FDM/YASim/PistonEngine.hpp | 36 ++++++--------------- src/FDM/YASim/PropEngine.cpp | 4 +-- src/FDM/YASim/PropEngine.hpp | 8 ++--- src/FDM/YASim/Thruster.hpp | 4 +-- src/FDM/YASim/YASim.cxx | 24 -------------- src/FDM/YASim/yasim-test.cpp | 1 + 10 files changed, 95 insertions(+), 121 deletions(-) create mode 100644 src/FDM/YASim/Engine.hpp diff --git a/src/FDM/YASim/ControlMap.cpp b/src/FDM/YASim/ControlMap.cpp index 9854cac28..51f527594 100644 --- a/src/FDM/YASim/ControlMap.cpp +++ b/src/FDM/YASim/ControlMap.cpp @@ -206,7 +206,7 @@ void ControlMap::applyControls(float dt) case ROTORENGINEON: ((Rotor*)obj)->setEngineOn((int)lval); break; case REVERSE_THRUST: ((Jet*)obj)->setReverse(lval != 0); break; case BOOST: - ((Thruster*)obj)->getPistonEngine()->setBoost(lval); + ((PistonEngine*)((Thruster*)obj)->getEngine())->setBoost(lval); break; } } diff --git a/src/FDM/YASim/Engine.hpp b/src/FDM/YASim/Engine.hpp new file mode 100644 index 000000000..edbe15d3d --- /dev/null +++ b/src/FDM/YASim/Engine.hpp @@ -0,0 +1,45 @@ +#ifndef _ENGINE_HPP +#define _ENGINE_HPP + +namespace yasim { + +class PistonEngine; + +// +// Interface for the "Engine" part of a PropEngine object. This is a +// virtual class, intended to be implemented by stuff like +// PistonEngine and TurbineEngine, and maybe exotics like +// SolarElectricEngine, etc... +// + +class Engine { +public: + virtual PistonEngine* isPistonEngine() { return 0; } + + void setThrottle(float throttle) { _throttle = throttle; } + void setStarter(bool starter) { _starter = starter; } + void setMagnetos(int magnetos) { _magnetos = magnetos; } + void setMixture(float mixture) { _mixture = mixture; } + void setBoost(float boost) { _boost = boost; } + void setFuelState(bool hasFuel) { _fuel = hasFuel; } + void setRunning(bool r) { _running = r; } + + bool isRunning() { return _running; } + virtual bool isCranking() { return false; } + + virtual void calc(float pressure, float temp, float speed) = 0; + virtual float getTorque() = 0; + virtual float getFuelFlow() = 0; + +protected: + float _throttle; + bool _starter; // true=engaged, false=disengaged + int _magnetos; // 0=off, 1=right, 2=left, 3=both + float _mixture; + float _boost; + bool _fuel; + bool _running; +}; + +}; // namespace yasim +#endif // _ENGINE_HPP diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp index 4524c35b4..6454f848e 100644 --- a/src/FDM/YASim/FGFDM.cpp +++ b/src/FDM/YASim/FGFDM.cpp @@ -3,6 +3,7 @@ #include
+#include "Math.hpp" #include "Jet.hpp" #include "SimpleJet.hpp" #include "Gear.hpp" @@ -436,41 +437,43 @@ void FGFDM::setOutputProperties() for(i=0; i<_thrusters.size(); i++) { EngRec* er = (EngRec*)_thrusters.get(i); Thruster* t = er->eng; + SGPropertyNode * node = fgGetNode("engines/engine", i, true); - sprintf(buf, "%s/fuel-flow-gph", er->prefix); - fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS); + // Set: running, cranking, prop-thrust, max-hp, power-pct + node->setBoolValue("running", t->isRunning()); + node->setBoolValue("cranking", t->isCranking()); - if(t->getPropEngine()) { - PropEngine* p = t->getPropEngine(); - - sprintf(buf, "%s/rpm", er->prefix); - fgSetFloat(buf, p->getOmega() / RPM2RAD); - } + float tmp[3]; + t->getThrust(tmp); + float lbs = Math::mag3(tmp) * (KG2LBS/9.8); + node->setFloatValue("prop-thrust", lbs); // Deprecated name + node->setFloatValue("thrust-lbs", lbs); - if(t->getPistonEngine()) { - PistonEngine* p = t->getPistonEngine(); - - sprintf(buf, "%s/mp-osi", er->prefix); - fgSetFloat(buf, p->getMP() * (1/INHG2PA)); + node->setFloatValue("fuel-flow-gph", + (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS); - sprintf(buf, "%s/egt-degf", er->prefix); - fgSetFloat(buf, p->getEGT() * K2DEGF + K2DEGFOFFSET); + if(t->getPropEngine()) { + PropEngine* p = t->getPropEngine(); + node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD)); + + if(p->getEngine()->isPistonEngine()) { + PistonEngine* pe = p->getEngine()->isPistonEngine(); + node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA)); + node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA)); + node->setFloatValue("egt-degf", + pe->getEGT() * K2DEGF + K2DEGFOFFSET); +// } else if(p->isTurbineEngine()) { +// TurbineEngine* te = p->isTurbineEngine(); + } } if(t->getJet()) { Jet* j = t->getJet(); - - sprintf(buf, "%s/n1", er->prefix); - fgSetFloat(buf, j->getN1()); - - sprintf(buf, "%s/n2", er->prefix); - fgSetFloat(buf, j->getN2()); - - sprintf(buf, "%s/epr", er->prefix); - fgSetFloat(buf, j->getEPR()); - - sprintf(buf, "%s/egt-degf", er->prefix); - fgSetFloat(buf, j->getEGT() * K2DEGF + K2DEGFOFFSET); + node->setFloatValue("n1", j->getN1()); + node->setFloatValue("n2", j->getN2()); + node->setFloatValue("epr", j->getEPR()); + node->setFloatValue("egr-degf", + j->getEGT() * K2DEGF + K2DEGFOFFSET); } } } diff --git a/src/FDM/YASim/PistonEngine.cpp b/src/FDM/YASim/PistonEngine.cpp index e88178fb2..938909c62 100644 --- a/src/FDM/YASim/PistonEngine.cpp +++ b/src/FDM/YASim/PistonEngine.cpp @@ -68,41 +68,6 @@ float PistonEngine::getMaxPower() return _power0; } -void PistonEngine::setThrottle(float t) -{ - _throttle = t; -} - -void PistonEngine::setRunning(bool r) -{ - _running = r; -} - -void PistonEngine::setStarter(bool s) -{ - _cranking = s; -} - -void PistonEngine::setMagnetos(int m) -{ - _magnetos = m; -} - -void PistonEngine::setMixture(float m) -{ - _mixture = m; -} - -void PistonEngine::setBoost(float boost) -{ - _boost = boost; -} - -bool PistonEngine::isRunning() -{ - return _running; -} - bool PistonEngine::isCranking() { return _cranking; diff --git a/src/FDM/YASim/PistonEngine.hpp b/src/FDM/YASim/PistonEngine.hpp index bd7ffb362..96aac1190 100644 --- a/src/FDM/YASim/PistonEngine.hpp +++ b/src/FDM/YASim/PistonEngine.hpp @@ -1,35 +1,28 @@ #ifndef _PISTONENGINE_HPP #define _PISTONENGINE_HPP +#include "Engine.hpp" + namespace yasim { -class PistonEngine { +class PistonEngine : public Engine { public: + virtual PistonEngine* isPistonEngine() { return this; } + // Initializes an engine from known "takeoff" parameters. PistonEngine(float power, float spd); void setTurboParams(float mul, float maxMP); void setDisplacement(float d); void setCompression(float c); - void setThrottle(float throttle); - void setStarter(bool starter); - void setMagnetos(int magnetos); - void setMixture(float mixture); - void setBoost(float boost); // fraction of turbo-mul used - void setFuelState(bool hasFuel) { _fuel = hasFuel; } - - // For solver use - void setRunning(bool r); - - float getMaxPower(); // max sea-level power - - void calc(float pressure, float temp, float speed); - bool isRunning(); bool isCranking(); - float getTorque(); - float getFuelFlow(); float getMP(); float getEGT(); + float getMaxPower(); // max sea-level power + + virtual void calc(float pressure, float temp, float speed); + virtual float getTorque(); + virtual float getFuelFlow(); private: // Static configuration: @@ -43,16 +36,7 @@ private: float _displacement; // piston stroke volume float _compression; // compression ratio (>1) - // Runtime settables: - float _throttle; - bool _starter; // true=engaged, false=disengaged - int _magnetos; // 0=off, 1=right, 2=left, 3=both - float _mixture; - float _boost; - bool _fuel; - // Runtime state/output: - bool _running; bool _cranking; float _mp; float _torque; diff --git a/src/FDM/YASim/PropEngine.cpp b/src/FDM/YASim/PropEngine.cpp index f5f90236a..9b7afdc67 100644 --- a/src/FDM/YASim/PropEngine.cpp +++ b/src/FDM/YASim/PropEngine.cpp @@ -1,10 +1,10 @@ #include "Math.hpp" #include "Propeller.hpp" -#include "PistonEngine.hpp" +#include "Engine.hpp" #include "PropEngine.hpp" namespace yasim { -PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment) +PropEngine::PropEngine(Propeller* prop, Engine* eng, float moment) { // Start off at 500rpm, because the start code doesn't exist yet _omega = 52.3f; diff --git a/src/FDM/YASim/PropEngine.hpp b/src/FDM/YASim/PropEngine.hpp index 603dee162..92ad8549e 100644 --- a/src/FDM/YASim/PropEngine.hpp +++ b/src/FDM/YASim/PropEngine.hpp @@ -6,11 +6,11 @@ namespace yasim { class Propeller; -class PistonEngine; +class Engine; class PropEngine : public Thruster { public: - PropEngine(Propeller* prop, PistonEngine* eng, float moment); + PropEngine(Propeller* prop, Engine* eng, float moment); virtual ~PropEngine(); void setMagnetos(int magnetos); @@ -20,7 +20,7 @@ public: void setGearRatio(float ratio) { _gearRatio = ratio; } virtual PropEngine* getPropEngine() { return this; } - virtual PistonEngine* getPistonEngine() { return _eng; } + virtual Engine* getEngine() { return _eng; } virtual Propeller* getPropeller() { return _prop; } // Dynamic output @@ -42,7 +42,7 @@ public: private: float _moment; Propeller* _prop; - PistonEngine* _eng; + Engine* _eng; bool _variable; int _magnetos; // 0=off, 1=right, 2=left, 3=both diff --git a/src/FDM/YASim/Thruster.hpp b/src/FDM/YASim/Thruster.hpp index 7d6e0c9cd..c022ebaa4 100644 --- a/src/FDM/YASim/Thruster.hpp +++ b/src/FDM/YASim/Thruster.hpp @@ -6,7 +6,7 @@ namespace yasim { class Jet; class PropEngine; class Propeller; -class PistonEngine; +class Engine; class Thruster { public: @@ -19,7 +19,7 @@ public: virtual Jet* getJet() { return 0; } virtual PropEngine* getPropEngine() { return 0; } virtual Propeller* getPropeller() { return 0; } - virtual PistonEngine* getPistonEngine() { return 0; } + virtual Engine* getEngine() { return 0; } // Static data void getPosition(float* out); diff --git a/src/FDM/YASim/YASim.cxx b/src/FDM/YASim/YASim.cxx index 8e1e1e96b..9f7594b2e 100644 --- a/src/FDM/YASim/YASim.cxx +++ b/src/FDM/YASim/YASim.cxx @@ -443,28 +443,4 @@ void YASim::copyFromYASim() node->setBoolValue("wow", g->getCompressFraction() != 0); node->setFloatValue("compression-norm", g->getCompressFraction()); } - - for(i=0; inumThrusters(); i++) { - SGPropertyNode * node = fgGetNode("engines/engine", i, true); - Thruster* t = model->getThruster(i); - - node->setBoolValue("running", t->isRunning()); - node->setBoolValue("cranking", t->isCranking()); - - float tmp[3]; - t->getThrust(tmp); - node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8); - - PropEngine* pe = t->getPropEngine(); - if(pe) { - node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM); - - pe->getTorque(tmp); - float power = Math::mag3(tmp) * pe->getOmega(); - float maxPower = pe->getPistonEngine()->getMaxPower(); - - node->setDoubleValue("max-hp", maxPower * W2HP); - node->setDoubleValue("power-pct", 100 * power/maxPower); - } - } } diff --git a/src/FDM/YASim/yasim-test.cpp b/src/FDM/YASim/yasim-test.cpp index faf7520e4..7f578b6a9 100644 --- a/src/FDM/YASim/yasim-test.cpp +++ b/src/FDM/YASim/yasim-test.cpp @@ -13,6 +13,7 @@ bool fgSetFloat (const char * name, float val) { return false; } bool fgSetBool(char const * name, bool val) { return false; } bool fgGetBool(char const * name, bool def) { return false; } SGPropertyNode* fgGetNode (const char * path, bool create) { return 0; } +SGPropertyNode* fgGetNode (const char * path, int i, bool create) { return 0; } float fgGetFloat (const char * name, float defaultValue) { return 0; } float fgGetDouble (const char * name, double defaultValue) { return 0; } float fgSetDouble (const char * name, double defaultValue) { return 0; } -- 2.39.5