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$
|
|
|
|
|
|
|
|
|
2003-09-24 17:20:55 +00:00
|
|
|
#include <simgear/structure/exception.hxx>
|
2002-09-24 15:24:04 +00:00
|
|
|
#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(""),
|
2003-11-15 05:18:55 +00:00
|
|
|
volts(0.0),
|
|
|
|
load_amps(0.0)
|
2002-09-24 23:45:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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");
|
2003-02-03 22:35:24 +00:00
|
|
|
rpm_src = node->getStringValue("rpm-source");
|
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;
|
2003-10-16 19:51:59 +00:00
|
|
|
if ( !strcmp(child->getName(), "prop") ) {
|
2002-09-26 04:51:23 +00:00
|
|
|
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
|
|
|
|
2003-02-03 22:35:24 +00:00
|
|
|
_rpm_node = fgGetNode( rpm_src.c_str(), 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);
|
2003-10-16 19:51:59 +00:00
|
|
|
if ( !strcmp(child->getName(), "prop") ) {
|
2002-09-26 04:51:23 +00:00
|
|
|
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;
|
2003-11-15 05:18:55 +00:00
|
|
|
output_amps = 0.1;
|
2002-09-24 19:57:11 +00:00
|
|
|
|
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);
|
2003-10-16 19:51:59 +00:00
|
|
|
if ( !strcmp(child->getName(), "prop") ) {
|
2002-09-26 04:51:23 +00:00
|
|
|
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
|
|
|
|
2003-11-17 21:10:47 +00:00
|
|
|
FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
|
|
|
|
switch_node( NULL ),
|
|
|
|
rating_amps( 0.0f ),
|
|
|
|
circuit_breaker( false )
|
|
|
|
{
|
|
|
|
bool initial_state = true;
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
|
|
|
SGPropertyNode *child = node->getChild(i);
|
|
|
|
string cname = child->getName();
|
|
|
|
string cval = child->getStringValue();
|
|
|
|
if ( cname == "prop" ) {
|
|
|
|
switch_node = fgGetNode( cval.c_str(), true );
|
2003-11-17 21:19:37 +00:00
|
|
|
// cout << "switch node = " << cval << endl;
|
2003-11-17 21:10:47 +00:00
|
|
|
} else if ( cname == "initial-state" ) {
|
|
|
|
if ( cval == "off" || cval == "false" ) {
|
|
|
|
initial_state = false;
|
|
|
|
}
|
2003-11-17 21:19:37 +00:00
|
|
|
// cout << "initial state = " << initial_state << endl;
|
2003-11-18 21:17:33 +00:00
|
|
|
} else if ( cname == "rating-amps" ) {
|
|
|
|
rating_amps = atof( cval.c_str() );
|
|
|
|
circuit_breaker = true;
|
|
|
|
// cout << "initial state = " << initial_state << endl;
|
2003-11-17 21:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_node->setBoolValue( initial_state );
|
2003-11-17 21:19:37 +00:00
|
|
|
// cout << " value = " << switch_node->getBoolValue() << endl;
|
2003-11-17 21:10:47 +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" ) {
|
2003-11-17 21:19:37 +00:00
|
|
|
// cout << "Switch = " << child->getStringValue() << endl;
|
2003-11-17 21:10:47 +00:00
|
|
|
FGElectricalSwitch s( child );
|
2003-11-15 05:18:55 +00:00
|
|
|
add_switch( s );
|
2002-09-26 04:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-03 22:15:36 +00:00
|
|
|
// set all switches to the specified state
|
|
|
|
void FGElectricalConnector::set_switches( bool state ) {
|
2003-02-13 21:31:44 +00:00
|
|
|
// cout << "setting switch state to " << state << endl;
|
2003-02-03 22:15:36 +00:00
|
|
|
for ( unsigned int i = 0; i < switches.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
switches[i].set_state( state );
|
2003-02-03 22:15:36 +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 ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
if ( ! switches[i].get_state() ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
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-31 00:56:14 +00:00
|
|
|
SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
|
2003-05-27 19:18:14 +00:00
|
|
|
_volts_out = fgGetNode( "/systems/electrical/volts", true );
|
|
|
|
_amps_out = fgGetNode( "/systems/electrical/amps", true );
|
2002-12-31 00:56:14 +00:00
|
|
|
|
|
|
|
if (path_n) {
|
|
|
|
SGPath config( globals->get_fg_root() );
|
|
|
|
config.append( path_n->getStringValue() );
|
|
|
|
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
|
2002-09-24 15:24:04 +00:00
|
|
|
<< config.str() );
|
2002-12-31 00:56:14 +00:00
|
|
|
try {
|
|
|
|
readProperties( config.str(), config_props );
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} catch (const sg_exception& exc) {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
|
|
|
|
<< config.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
"No electrical model specified for this model!");
|
2002-09-24 15:24:04 +00:00
|
|
|
|
|
|
|
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 ) {
|
2003-05-27 19:18:14 +00:00
|
|
|
_amps_out->setDoubleValue(0);
|
2002-09-24 23:45:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cout << "Updating electrical system" << endl;
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// zero everything out before we start
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
suppliers[i]->set_volts( 0.0 );
|
|
|
|
suppliers[i]->set_load_amps( 0.0 );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
for ( i = 0; i < buses.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
buses[i]->set_volts( 0.0 );
|
|
|
|
buses[i]->set_load_amps( 0.0 );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
for ( i = 0; i < outputs.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
outputs[i]->set_volts( 0.0 );
|
|
|
|
outputs[i]->set_load_amps( 0.0 );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
for ( i = 0; i < connectors.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
connectors[i]->set_volts( 0.0 );
|
|
|
|
connectors[i]->set_load_amps( 0.0 );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
2003-02-03 22:22:02 +00:00
|
|
|
// for each supplier, propagate the electrical current
|
2002-09-24 23:45:56 +00:00
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
|
|
|
// cout << " Updating: " << suppliers[i]->get_name() << endl;
|
2003-02-03 22:22:02 +00:00
|
|
|
propagate( suppliers[i], 0.0, " " );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
2003-05-27 19:18:14 +00:00
|
|
|
double alt_norm
|
|
|
|
= fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
|
|
|
|
|
|
|
|
// impliment an extremely simplistic voltage model (assumes
|
|
|
|
// certain naming conventions in electrical system config)
|
|
|
|
double volts = 0.0;
|
|
|
|
if ( fgGetBool("/controls/switches/master-bat") ) {
|
|
|
|
volts = 24.0;
|
|
|
|
}
|
|
|
|
if ( fgGetBool("/controls/switches/master-alt") &&
|
|
|
|
fgGetDouble("/engines/engine[0]/rpm") > 800 )
|
|
|
|
{
|
|
|
|
double alt_contrib = 28.0;
|
|
|
|
if ( alt_contrib > volts ) {
|
|
|
|
volts = alt_contrib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_volts_out->setDoubleValue( volts );
|
|
|
|
|
|
|
|
// impliment an extremely simplistic amps model (assumes certain
|
|
|
|
// naming conventions in the electrical system config) ... FIXME:
|
|
|
|
// make this more generic
|
|
|
|
double amps = 0.0;
|
|
|
|
if ( fgGetBool("/controls/switches/master-bat") ) {
|
2003-05-28 18:47:42 +00:00
|
|
|
if ( fgGetBool("/controls/switches/master-alt") &&
|
|
|
|
fgGetDouble("/engines/engine[0]/rpm") > 800 )
|
|
|
|
{
|
2003-05-27 19:18:14 +00:00
|
|
|
amps += 40.0 * alt_norm;
|
|
|
|
}
|
|
|
|
amps -= 15.0; // normal load
|
|
|
|
if ( fgGetBool("/controls/switches/flashing-beacon") ) {
|
|
|
|
amps -= 7.5;
|
|
|
|
}
|
|
|
|
if ( fgGetBool("/controls/switches/nav-lights") ) {
|
|
|
|
amps -= 7.5;
|
|
|
|
}
|
|
|
|
if ( amps > 7.0 ) {
|
|
|
|
amps = 7.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_amps_out->setDoubleValue( amps );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:18:55 +00:00
|
|
|
// propagate the electrical current through the network, returns the
|
|
|
|
// total current drawn by the children of this node.
|
|
|
|
float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
|
2002-09-24 23:45:56 +00:00
|
|
|
string s ) {
|
|
|
|
s += " ";
|
2003-11-15 05:18:55 +00:00
|
|
|
|
|
|
|
float current_amps = 0.0;
|
2002-09-24 23:45:56 +00:00
|
|
|
|
|
|
|
// determine the current to carry forward
|
2003-11-15 05:18:55 +00:00
|
|
|
double volts = 0.0;
|
2003-01-25 21:49:57 +00:00
|
|
|
if ( !fgGetBool("/systems/electrical/serviceable") ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = 0;
|
2003-01-25 21:49:57 +00:00
|
|
|
} else if ( node->get_kind() == FG_SUPPLIER ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
// cout << s << " is a supplier" << endl;
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = ((FGElectricalSupplier *)node)->get_output();
|
2002-09-24 23:45:56 +00:00
|
|
|
} else if ( node->get_kind() == FG_BUS ) {
|
|
|
|
// cout << s << " is a bus" << endl;
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = val;
|
2002-09-24 23:45:56 +00:00
|
|
|
} else if ( node->get_kind() == FG_OUTPUT ) {
|
|
|
|
// cout << s << " is an output" << endl;
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = val;
|
|
|
|
if ( volts > 1.0 ) {
|
|
|
|
// draw current if we have voltage
|
|
|
|
current_amps = ((FGElectricalOutput *)node)->get_output_amps();
|
|
|
|
}
|
2002-09-24 23:45:56 +00:00
|
|
|
} else if ( node->get_kind() == FG_CONNECTOR ) {
|
|
|
|
// cout << s << " is a connector" << endl;
|
|
|
|
if ( ((FGElectricalConnector *)node)->get_state() ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = val;
|
2002-09-24 23:45:56 +00:00
|
|
|
} else {
|
2003-11-15 05:18:55 +00:00
|
|
|
volts = 0.0;
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
2003-11-15 05:18:55 +00:00
|
|
|
// cout << s << " val = " << volts << endl;
|
2002-09-24 23:45:56 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2003-11-15 05:18:55 +00:00
|
|
|
if ( volts > node->get_volts() ) {
|
|
|
|
node->set_volts( volts );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// publish values to specified properties
|
|
|
|
for ( i = 0; i < node->get_num_props(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
// cout << s << node->get_name() << " -> " << node->get_value() << endl;
|
|
|
|
|
2003-02-03 22:22:02 +00:00
|
|
|
// propagate to all children
|
2002-09-24 23:45:56 +00:00
|
|
|
for ( i = 0; i < node->get_num_outputs(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
current_amps += propagate( node->get_output(i), volts, s );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
2003-11-15 05:18:55 +00:00
|
|
|
|
|
|
|
// if not an output node, register the downstream current draw
|
|
|
|
// with this node. If volts are zero, current draw should be zero.
|
|
|
|
if ( node->get_kind() != FG_OUTPUT ) {
|
|
|
|
node->set_load_amps( current_amps );
|
|
|
|
}
|
|
|
|
// cout << s << node->get_name() << " -> " << current_amps << endl;
|
|
|
|
|
|
|
|
return current_amps;
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|