2002-09-24 15:24:04 +00:00
|
|
|
// electrical.cxx - a flexible, generic electrical system model.
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started September 2002.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2002 Curtis L. Olson - curt@flightgear.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 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#include <simgear/misc/exception.hxx>
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Main/globals.hxx>
|
|
|
|
|
|
|
|
#include "electrical.hxx"
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
|
2002-09-24 23:45:56 +00:00
|
|
|
FGElectricalComponent::FGElectricalComponent() :
|
|
|
|
kind(-1),
|
|
|
|
name(""),
|
|
|
|
value(0.0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
|
2002-09-24 19:57:11 +00:00
|
|
|
kind = FG_SUPPLIER;
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
// cout << "Creating a supplier" << endl;
|
|
|
|
name = node->getStringValue("name");
|
|
|
|
string _model = node->getStringValue("kind");
|
2002-09-24 23:45:56 +00:00
|
|
|
// cout << "_model = " << _model << endl;
|
2002-09-24 19:57:11 +00:00
|
|
|
if ( _model == "battery" ) {
|
|
|
|
model = FG_BATTERY;
|
|
|
|
} else if ( _model == "alternator" ) {
|
|
|
|
model = FG_ALTERNATOR;
|
|
|
|
} else if ( _model == "external" ) {
|
|
|
|
model = FG_EXTERNAL;
|
|
|
|
} else {
|
|
|
|
model = FG_UNKNOWN;
|
|
|
|
}
|
2002-09-26 04:51:23 +00:00
|
|
|
volts = node->getDoubleValue("volts");
|
|
|
|
amps = node->getDoubleValue("amps");
|
2002-09-24 23:45:56 +00:00
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 23:45:56 +00:00
|
|
|
|
|
|
|
_rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 15:24:04 +00:00
|
|
|
|
2002-09-24 23:45:56 +00:00
|
|
|
double FGElectricalSupplier::get_output() {
|
|
|
|
if ( model == FG_BATTERY ) {
|
|
|
|
// cout << "battery amps = " << amps << endl;
|
|
|
|
return amps;
|
|
|
|
} else if ( model == FG_ALTERNATOR ) {
|
|
|
|
// scale alternator output for rpms < 600. For rpms >= 600
|
2002-09-25 12:38:09 +00:00
|
|
|
// give full output. This is just a WAG, and probably not how
|
|
|
|
// it really works but I'm keeping things "simple" to start.
|
2002-09-24 23:45:56 +00:00
|
|
|
double rpm = _rpm_node->getDoubleValue();
|
|
|
|
double factor = rpm / 600.0;
|
|
|
|
if ( factor > 1.0 ) {
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
|
|
|
// cout << "alternator amps = " << amps * factor << endl;
|
|
|
|
return amps * factor;
|
|
|
|
} else if ( model == FG_EXTERNAL ) {
|
|
|
|
// cout << "external amps = " << 0.0 << endl;
|
|
|
|
return 0.0;
|
|
|
|
} else {
|
2002-09-25 12:38:09 +00:00
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
|
2002-09-24 19:57:11 +00:00
|
|
|
kind = FG_BUS;
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
name = node->getStringValue("name");
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
|
2002-09-24 19:57:11 +00:00
|
|
|
kind = FG_OUTPUT;
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
name = node->getStringValue("name");
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
2002-09-24 15:24:04 +00:00
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
|
|
|
|
FGElectricalSystem *es ) {
|
2002-09-24 19:57:11 +00:00
|
|
|
kind = FG_CONNECTOR;
|
2002-09-24 23:45:56 +00:00
|
|
|
name = "connector";
|
2002-09-26 04:51:23 +00:00
|
|
|
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 ) );
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 23:45:56 +00:00
|
|
|
// return true if all switches are true, false otherwise. A connector
|
|
|
|
// could have multiple switches, but they all need to be true(closed)
|
|
|
|
// for current to get through.
|
|
|
|
bool FGElectricalConnector::get_state() {
|
|
|
|
unsigned int i;
|
|
|
|
for ( i = 0; i < switches.size(); ++i ) {
|
|
|
|
if ( ! switches[i]->getBoolValue() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
FGElectricalSystem::FGElectricalSystem () :
|
|
|
|
enabled(false)
|
2002-09-24 15:24:04 +00:00
|
|
|
{
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGElectricalSystem::~FGElectricalSystem () {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FGElectricalSystem::init () {
|
2002-09-24 15:24:04 +00:00
|
|
|
config_props = new SGPropertyNode;
|
|
|
|
|
2002-12-18 21:55:33 +00:00
|
|
|
SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path", true);
|
|
|
|
|
2002-09-24 15:24:04 +00:00
|
|
|
SGPath config( globals->get_fg_root() );
|
2002-12-18 21:55:33 +00:00
|
|
|
config.append( path_n->getStringValue() );
|
2002-09-24 15:24:04 +00:00
|
|
|
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
|
|
|
|
<< config.str() );
|
|
|
|
try {
|
|
|
|
readProperties( config.str(), config_props );
|
2002-09-24 19:57:11 +00:00
|
|
|
|
|
|
|
if ( build() ) {
|
|
|
|
enabled = true;
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
"Detected an internal inconsistancy in the electrical" );
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
" system specification file. See earlier errors for" );
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
" details.");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2002-09-24 15:24:04 +00:00
|
|
|
} catch (const sg_exception& exc) {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
|
|
|
|
<< config.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
delete config_props;
|
|
|
|
}
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
|
|
|
|
void FGElectricalSystem::bind () {
|
2002-09-24 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
|
|
|
|
void FGElectricalSystem::unbind () {
|
2002-09-24 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
|
|
|
|
void FGElectricalSystem::update (double dt) {
|
2002-09-24 23:45:56 +00:00
|
|
|
if ( !enabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cout << "Updating electrical system" << endl;
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// zero everything out before we start
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
|
|
|
suppliers[i]->set_value( 0.0 );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < buses.size(); ++i ) {
|
|
|
|
buses[i]->set_value( 0.0 );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < outputs.size(); ++i ) {
|
|
|
|
outputs[i]->set_value( 0.0 );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < connectors.size(); ++i ) {
|
|
|
|
connectors[i]->set_value( 0.0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each supplier, propogate the electrical current
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
|
|
|
// cout << " Updating: " << suppliers[i]->get_name() << endl;
|
|
|
|
propogate( suppliers[i], 0.0, " " );
|
|
|
|
}
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FGElectricalSystem::build () {
|
|
|
|
SGPropertyNode *node;
|
2002-09-26 04:51:23 +00:00
|
|
|
int i;
|
2002-09-24 19:57:11 +00:00
|
|
|
|
|
|
|
int count = config_props->nChildren();
|
|
|
|
for ( i = 0; i < count; ++i ) {
|
|
|
|
node = config_props->getChild(i);
|
|
|
|
string name = node->getName();
|
|
|
|
// cout << name << endl;
|
|
|
|
if ( name == "supplier" ) {
|
|
|
|
FGElectricalSupplier *s =
|
2002-09-26 04:51:23 +00:00
|
|
|
new FGElectricalSupplier( node );
|
2002-09-24 19:57:11 +00:00
|
|
|
suppliers.push_back( s );
|
|
|
|
} else if ( name == "bus" ) {
|
|
|
|
FGElectricalBus *b =
|
2002-09-26 04:51:23 +00:00
|
|
|
new FGElectricalBus( node );
|
2002-09-24 19:57:11 +00:00
|
|
|
buses.push_back( b );
|
|
|
|
} else if ( name == "output" ) {
|
|
|
|
FGElectricalOutput *o =
|
2002-09-26 04:51:23 +00:00
|
|
|
new FGElectricalOutput( node );
|
2002-09-24 19:57:11 +00:00
|
|
|
outputs.push_back( o );
|
|
|
|
} else if ( name == "connector" ) {
|
|
|
|
FGElectricalConnector *c =
|
2002-09-26 04:51:23 +00:00
|
|
|
new FGElectricalConnector( node, this );
|
2002-09-24 19:57:11 +00:00
|
|
|
connectors.push_back( c );
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
|
|
|
|
<< name );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 23:45:56 +00:00
|
|
|
// propogate the electrical current through the network
|
|
|
|
void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
|
|
|
|
string s ) {
|
|
|
|
s += " ";
|
|
|
|
|
|
|
|
// determine the current to carry forward
|
|
|
|
double current = 0.0;
|
|
|
|
if ( node->get_kind() == FG_SUPPLIER ) {
|
|
|
|
// cout << s << " is a supplier" << endl;
|
|
|
|
current = ((FGElectricalSupplier *)node)->get_output();
|
|
|
|
} else if ( node->get_kind() == FG_BUS ) {
|
|
|
|
// cout << s << " is a bus" << endl;
|
|
|
|
current = val;
|
|
|
|
} else if ( node->get_kind() == FG_OUTPUT ) {
|
|
|
|
// cout << s << " is an output" << endl;
|
|
|
|
current = val;
|
|
|
|
} else if ( node->get_kind() == FG_CONNECTOR ) {
|
|
|
|
// cout << s << " is a connector" << endl;
|
|
|
|
if ( ((FGElectricalConnector *)node)->get_state() ) {
|
|
|
|
current = val;
|
|
|
|
} else {
|
|
|
|
current = 0.0;
|
|
|
|
}
|
|
|
|
// cout << s << " val = " << current << endl;
|
|
|
|
} else {
|
2002-09-25 12:38:09 +00:00
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( current > node->get_value() ) {
|
|
|
|
node->set_value( current );
|
|
|
|
}
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// publish values to specified properties
|
|
|
|
for ( i = 0; i < node->get_num_props(); ++i ) {
|
|
|
|
fgSetDouble( node->get_prop(i).c_str(), node->get_value() );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
// cout << s << node->get_name() << " -> " << node->get_value() << endl;
|
|
|
|
|
|
|
|
// propogate to all children
|
|
|
|
for ( i = 0; i < node->get_num_outputs(); ++i ) {
|
|
|
|
propogate( node->get_output(i), current, s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 19:57:11 +00:00
|
|
|
// search for the named component and return a pointer to it, NULL otherwise
|
|
|
|
FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
|
|
|
|
unsigned int i;
|
|
|
|
string s;
|
|
|
|
|
|
|
|
// search suppliers
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
s = suppliers[i]->get_name();
|
2002-09-24 19:57:11 +00:00
|
|
|
// cout << " " << s << endl;
|
|
|
|
if ( s == name ) {
|
|
|
|
return suppliers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then search buses
|
|
|
|
for ( i = 0; i < buses.size(); ++i ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
s = buses[i]->get_name();
|
2002-09-24 19:57:11 +00:00
|
|
|
// cout << " " << s << endl;
|
|
|
|
if ( s == name ) {
|
|
|
|
return buses[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then search outputs
|
|
|
|
for ( i = 0; i < outputs.size(); ++i ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
s = outputs[i]->get_name();
|
2002-09-24 19:57:11 +00:00
|
|
|
// cout << " " << s << endl;
|
|
|
|
if ( s == name ) {
|
|
|
|
return outputs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing found
|
|
|
|
return NULL;
|
2002-09-24 15:24:04 +00:00
|
|
|
}
|