Each node in the electrical system graph can now publish to an arbitrary
number of properties which will reflect that node's state.
This commit is contained in:
parent
911e7dbfa8
commit
cc4821e43f
2 changed files with 119 additions and 100 deletions
|
@ -33,20 +33,17 @@
|
||||||
FGElectricalComponent::FGElectricalComponent() :
|
FGElectricalComponent::FGElectricalComponent() :
|
||||||
kind(-1),
|
kind(-1),
|
||||||
name(""),
|
name(""),
|
||||||
prop(""),
|
|
||||||
value(0.0)
|
value(0.0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FGElectricalSupplier::FGElectricalSupplier ( string _name, string _prop,
|
FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
|
||||||
string _model,
|
|
||||||
double _volts, double _amps )
|
|
||||||
{
|
|
||||||
kind = FG_SUPPLIER;
|
kind = FG_SUPPLIER;
|
||||||
|
|
||||||
name = _name;
|
// cout << "Creating a supplier" << endl;
|
||||||
prop = _prop;
|
name = node->getStringValue("name");
|
||||||
|
string _model = node->getStringValue("kind");
|
||||||
// cout << "_model = " << _model << endl;
|
// cout << "_model = " << _model << endl;
|
||||||
if ( _model == "battery" ) {
|
if ( _model == "battery" ) {
|
||||||
model = FG_BATTERY;
|
model = FG_BATTERY;
|
||||||
|
@ -57,10 +54,20 @@ FGElectricalSupplier::FGElectricalSupplier ( string _name, string _prop,
|
||||||
} else {
|
} else {
|
||||||
model = FG_UNKNOWN;
|
model = FG_UNKNOWN;
|
||||||
}
|
}
|
||||||
volts = _volts;
|
volts = node->getDoubleValue("volts");
|
||||||
amps = _amps;
|
amps = node->getDoubleValue("amps");
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
||||||
|
SGPropertyNode *child = node->getChild(i);
|
||||||
|
// cout << " scanning: " << child->getName() << endl;
|
||||||
|
if ( (string)child->getName() == "prop" ) {
|
||||||
|
string prop = child->getStringValue();
|
||||||
|
// cout << " Adding prop = " << prop << endl;
|
||||||
|
add_prop( prop );
|
||||||
fgSetDouble( prop.c_str(), amps );
|
fgSetDouble( prop.c_str(), amps );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
|
_rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
|
||||||
}
|
}
|
||||||
|
@ -92,28 +99,87 @@ double FGElectricalSupplier::get_output() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FGElectricalBus::FGElectricalBus ( string _name, string _prop )
|
FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
|
||||||
{
|
|
||||||
kind = FG_BUS;
|
kind = FG_BUS;
|
||||||
|
|
||||||
name = _name;
|
name = node->getStringValue("name");
|
||||||
prop = _prop;
|
int i;
|
||||||
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
||||||
|
SGPropertyNode *child = node->getChild(i);
|
||||||
|
if ( (string)child->getName() == "prop" ) {
|
||||||
|
string prop = child->getStringValue();
|
||||||
|
add_prop( prop );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FGElectricalOutput::FGElectricalOutput ( string _name, string _prop )
|
FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
|
||||||
{
|
|
||||||
kind = FG_OUTPUT;
|
kind = FG_OUTPUT;
|
||||||
|
|
||||||
name = _name;
|
name = node->getStringValue("name");
|
||||||
prop = _prop;
|
int i;
|
||||||
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
||||||
|
SGPropertyNode *child = node->getChild(i);
|
||||||
|
if ( (string)child->getName() == "prop" ) {
|
||||||
|
string prop = child->getStringValue();
|
||||||
|
add_prop( prop );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FGElectricalConnector::FGElectricalConnector ()
|
FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
|
||||||
{
|
FGElectricalSystem *es ) {
|
||||||
kind = FG_CONNECTOR;
|
kind = FG_CONNECTOR;
|
||||||
name = "connector";
|
name = "connector";
|
||||||
|
int i;
|
||||||
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
||||||
|
SGPropertyNode *child = node->getChild(i);
|
||||||
|
string cname = child->getName();
|
||||||
|
string cval = child->getStringValue();
|
||||||
|
// cout << " " << cname << " = " << cval << endl;
|
||||||
|
if ( cname == "input" ) {
|
||||||
|
FGElectricalComponent *s = es->find( child->getStringValue() );
|
||||||
|
if ( s != NULL ) {
|
||||||
|
add_input( s );
|
||||||
|
if ( s->get_kind() == FG_SUPPLIER ) {
|
||||||
|
s->add_output( this );
|
||||||
|
} else if ( s->get_kind() == FG_BUS ) {
|
||||||
|
s->add_output( this );
|
||||||
|
} else {
|
||||||
|
SG_LOG( SG_ALL, SG_ALERT,
|
||||||
|
"Attempt to connect to something that can't provide an output: "
|
||||||
|
<< child->getStringValue() );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
|
||||||
|
<< child->getStringValue() );
|
||||||
|
}
|
||||||
|
} else if ( cname == "output" ) {
|
||||||
|
FGElectricalComponent *s = es->find( child->getStringValue() );
|
||||||
|
if ( s != NULL ) {
|
||||||
|
add_output( s );
|
||||||
|
if ( s->get_kind() == FG_BUS ) {
|
||||||
|
s->add_input( this );
|
||||||
|
} else if ( s->get_kind() == FG_OUTPUT ) {
|
||||||
|
s->add_input( this );
|
||||||
|
} else {
|
||||||
|
SG_LOG( SG_ALL, SG_ALERT,
|
||||||
|
"Attempt to connect to something that can't provide an input: "
|
||||||
|
<< child->getStringValue() );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
|
||||||
|
<< child->getStringValue() );
|
||||||
|
}
|
||||||
|
} else if ( cname == "switch" ) {
|
||||||
|
// set default value of switch to true
|
||||||
|
// cout << "Switch = " << child->getStringValue() << endl;
|
||||||
|
fgSetBool( child->getStringValue(), true );
|
||||||
|
add_switch( fgGetNode( child->getStringValue(), true ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -215,7 +281,7 @@ void FGElectricalSystem::update (double dt) {
|
||||||
|
|
||||||
bool FGElectricalSystem::build () {
|
bool FGElectricalSystem::build () {
|
||||||
SGPropertyNode *node;
|
SGPropertyNode *node;
|
||||||
int i, j;
|
int i;
|
||||||
|
|
||||||
int count = config_props->nChildren();
|
int count = config_props->nChildren();
|
||||||
for ( i = 0; i < count; ++i ) {
|
for ( i = 0; i < count; ++i ) {
|
||||||
|
@ -224,80 +290,20 @@ bool FGElectricalSystem::build () {
|
||||||
// cout << name << endl;
|
// cout << name << endl;
|
||||||
if ( name == "supplier" ) {
|
if ( name == "supplier" ) {
|
||||||
FGElectricalSupplier *s =
|
FGElectricalSupplier *s =
|
||||||
new FGElectricalSupplier( node->getStringValue("name"),
|
new FGElectricalSupplier( node );
|
||||||
node->getStringValue("prop"),
|
|
||||||
node->getStringValue("kind"),
|
|
||||||
node->getDoubleValue("volts"),
|
|
||||||
node->getDoubleValue("amps") );
|
|
||||||
suppliers.push_back( s );
|
suppliers.push_back( s );
|
||||||
} else if ( name == "bus" ) {
|
} else if ( name == "bus" ) {
|
||||||
FGElectricalBus *b =
|
FGElectricalBus *b =
|
||||||
new FGElectricalBus( node->getStringValue("name"),
|
new FGElectricalBus( node );
|
||||||
node->getStringValue("prop") );
|
|
||||||
buses.push_back( b );
|
buses.push_back( b );
|
||||||
} else if ( name == "output" ) {
|
} else if ( name == "output" ) {
|
||||||
FGElectricalOutput *o =
|
FGElectricalOutput *o =
|
||||||
new FGElectricalOutput( node->getStringValue("name"),
|
new FGElectricalOutput( node );
|
||||||
node->getStringValue("prop") );
|
|
||||||
outputs.push_back( o );
|
outputs.push_back( o );
|
||||||
} else if ( name == "connector" ) {
|
} else if ( name == "connector" ) {
|
||||||
FGElectricalConnector *c =
|
FGElectricalConnector *c =
|
||||||
new FGElectricalConnector();
|
new FGElectricalConnector( node, this );
|
||||||
connectors.push_back( c );
|
connectors.push_back( c );
|
||||||
SGPropertyNode *child;
|
|
||||||
int ccount = node->nChildren();
|
|
||||||
for ( j = 0; j < ccount; ++j ) {
|
|
||||||
child = node->getChild(j);
|
|
||||||
string cname = child->getName();
|
|
||||||
string cval = child->getStringValue();
|
|
||||||
// cout << " " << cname << " = " << cval << endl;
|
|
||||||
if ( cname == "input" ) {
|
|
||||||
FGElectricalComponent *s = find( child->getStringValue() );
|
|
||||||
if ( s != NULL ) {
|
|
||||||
c->add_input( s );
|
|
||||||
if ( s->get_kind() == FG_SUPPLIER ) {
|
|
||||||
s->add_output( c );
|
|
||||||
} else if ( s->get_kind() == FG_BUS ) {
|
|
||||||
s->add_output( c );
|
|
||||||
} else {
|
|
||||||
SG_LOG( SG_ALL, SG_ALERT,
|
|
||||||
"Attempt to connect to something that can't provide an output: "
|
|
||||||
<< child->getStringValue() );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SG_LOG( SG_ALL, SG_ALERT,
|
|
||||||
"Can't find named source: "
|
|
||||||
<< child->getStringValue() );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if ( cname == "output" ) {
|
|
||||||
FGElectricalComponent *s = find( child->getStringValue() );
|
|
||||||
if ( s != NULL ) {
|
|
||||||
c->add_output( s );
|
|
||||||
if ( s->get_kind() == FG_BUS ) {
|
|
||||||
s->add_input( c );
|
|
||||||
} else if ( s->get_kind() == FG_OUTPUT ) {
|
|
||||||
s->add_input( c );
|
|
||||||
} else {
|
|
||||||
SG_LOG( SG_ALL, SG_ALERT,
|
|
||||||
"Attempt to connect to something that can't provide an input: "
|
|
||||||
<< child->getStringValue() );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SG_LOG( SG_ALL, SG_ALERT,
|
|
||||||
"Can't find named source: "
|
|
||||||
<< child->getStringValue() );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if ( cname == "switch" ) {
|
|
||||||
// set default value of switch to true
|
|
||||||
// cout << "Switch = " << child->getStringValue() << endl;
|
|
||||||
fgSetBool( child->getStringValue(), true );
|
|
||||||
c->add_switch( fgGetNode( child->getStringValue(), true ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
|
SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
|
||||||
<< name );
|
<< name );
|
||||||
|
@ -341,13 +347,15 @@ void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
|
||||||
node->set_value( current );
|
node->set_value( current );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! node->get_prop().empty() ) {
|
int i;
|
||||||
fgSetDouble( node->get_prop().c_str(), node->get_value() );
|
|
||||||
|
// publish values to specified properties
|
||||||
|
for ( i = 0; i < node->get_num_props(); ++i ) {
|
||||||
|
fgSetDouble( node->get_prop(i).c_str(), node->get_value() );
|
||||||
}
|
}
|
||||||
// cout << s << node->get_name() << " -> " << node->get_value() << endl;
|
// cout << s << node->get_name() << " -> " << node->get_value() << endl;
|
||||||
|
|
||||||
// propogate to all children
|
// propogate to all children
|
||||||
int i;
|
|
||||||
for ( i = 0; i < node->get_num_outputs(); ++i ) {
|
for ( i = 0; i < node->get_num_outputs(); ++i ) {
|
||||||
propogate( node->get_output(i), current, s );
|
propogate( node->get_output(i), current, s );
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,10 @@ SG_USING_STD(vector);
|
||||||
#include <Main/fgfs.hxx>
|
#include <Main/fgfs.hxx>
|
||||||
|
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
class FGElectricalSystem;
|
||||||
|
|
||||||
|
|
||||||
#define FG_UNKNOWN -1
|
#define FG_UNKNOWN -1
|
||||||
#define FG_SUPPLIER 0
|
#define FG_SUPPLIER 0
|
||||||
#define FG_BUS 1
|
#define FG_BUS 1
|
||||||
|
@ -58,11 +62,11 @@ protected:
|
||||||
|
|
||||||
int kind;
|
int kind;
|
||||||
string name;
|
string name;
|
||||||
string prop;
|
|
||||||
double value;
|
double value;
|
||||||
|
|
||||||
comp_list inputs;
|
comp_list inputs;
|
||||||
comp_list outputs;
|
comp_list outputs;
|
||||||
|
string_list props;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -70,12 +74,19 @@ public:
|
||||||
virtual ~FGElectricalComponent() {}
|
virtual ~FGElectricalComponent() {}
|
||||||
|
|
||||||
inline string get_name() { return name; }
|
inline string get_name() { return name; }
|
||||||
inline string get_prop() { return prop; }
|
|
||||||
|
|
||||||
inline int get_kind() const { return kind; }
|
inline int get_kind() const { return kind; }
|
||||||
inline double get_value() const { return value; }
|
inline double get_value() const { return value; }
|
||||||
inline void set_value( double val ) { value = val; }
|
inline void set_value( double val ) { value = val; }
|
||||||
|
|
||||||
|
inline int get_num_inputs() const { return outputs.size(); }
|
||||||
|
inline FGElectricalComponent *get_input( const int i ) {
|
||||||
|
return inputs[i];
|
||||||
|
}
|
||||||
|
inline void add_input( FGElectricalComponent *c ) {
|
||||||
|
inputs.push_back( c );
|
||||||
|
}
|
||||||
|
|
||||||
inline int get_num_outputs() const { return outputs.size(); }
|
inline int get_num_outputs() const { return outputs.size(); }
|
||||||
inline FGElectricalComponent *get_output( const int i ) {
|
inline FGElectricalComponent *get_output( const int i ) {
|
||||||
return outputs[i];
|
return outputs[i];
|
||||||
|
@ -84,13 +95,14 @@ public:
|
||||||
outputs.push_back( c );
|
outputs.push_back( c );
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int get_num_inputs() const { return outputs.size(); }
|
inline int get_num_props() const { return props.size(); }
|
||||||
inline FGElectricalComponent *get_input( const int i ) {
|
inline string get_prop( const int i ) {
|
||||||
return inputs[i];
|
return props[i];
|
||||||
}
|
}
|
||||||
inline void add_input( FGElectricalComponent *c ) {
|
inline void add_prop( const string &s ) {
|
||||||
inputs.push_back( c );
|
props.push_back( s );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -111,8 +123,7 @@ class FGElectricalSupplier : public FGElectricalComponent {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FGElectricalSupplier ( string _name, string _prop, string _model,
|
FGElectricalSupplier ( SGPropertyNode *node );
|
||||||
double _volts, double _amps );
|
|
||||||
~FGElectricalSupplier () {}
|
~FGElectricalSupplier () {}
|
||||||
|
|
||||||
double get_output();
|
double get_output();
|
||||||
|
@ -125,7 +136,7 @@ class FGElectricalBus : public FGElectricalComponent {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FGElectricalBus ( string _name, string _prop );
|
FGElectricalBus ( SGPropertyNode *node );
|
||||||
~FGElectricalBus () {}
|
~FGElectricalBus () {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -136,7 +147,7 @@ class FGElectricalOutput : public FGElectricalComponent {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FGElectricalOutput ( string _name, string _prop );
|
FGElectricalOutput ( SGPropertyNode *node );
|
||||||
~FGElectricalOutput () {}
|
~FGElectricalOutput () {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,7 +163,7 @@ class FGElectricalConnector : public FGElectricalComponent {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FGElectricalConnector ();
|
FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
|
||||||
~FGElectricalConnector () {}
|
~FGElectricalConnector () {}
|
||||||
|
|
||||||
void add_switch( SGPropertyNode *node ) {
|
void add_switch( SGPropertyNode *node ) {
|
||||||
|
|
Loading…
Reference in a new issue