2002-09-24 15:24:04 +00:00
|
|
|
// electrical.cxx - a flexible, generic electrical system model.
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started September 2002.
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
|
2002-09-24 15:24:04 +00:00
|
|
|
//
|
|
|
|
// 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
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-09-24 15:24:04 +00:00
|
|
|
//
|
|
|
|
// $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;
|
2005-06-14 17:57:48 +00:00
|
|
|
amp_hours = node->getFloatValue("amp-hours", 40.0);
|
|
|
|
percent_remaining = node->getFloatValue("percent-remaining", 1.0);
|
|
|
|
charge_amps = node->getFloatValue("charge-amps", 7.0);
|
2002-09-24 19:57:11 +00:00
|
|
|
} else if ( _model == "alternator" ) {
|
|
|
|
model = FG_ALTERNATOR;
|
2005-06-14 17:57:48 +00:00
|
|
|
rpm_src = node->getStringValue("rpm-source");
|
|
|
|
rpm_threshold = node->getFloatValue("rpm-threshold", 600.0);
|
|
|
|
ideal_amps = node->getFloatValue("amps", 60.0);
|
2002-09-24 19:57:11 +00:00
|
|
|
} else if ( _model == "external" ) {
|
|
|
|
model = FG_EXTERNAL;
|
2005-06-14 17:57:48 +00:00
|
|
|
ideal_amps = node->getFloatValue("amps", 60.0);
|
2002-09-24 19:57:11 +00:00
|
|
|
} else {
|
|
|
|
model = FG_UNKNOWN;
|
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
ideal_volts = node->getFloatValue("volts");
|
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 );
|
2005-06-14 17:57:48 +00:00
|
|
|
fgSetFloat( prop.c_str(), ideal_amps );
|
2002-09-26 04:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
float FGElectricalSupplier::apply_load( float amps, float dt ) {
|
|
|
|
if ( model == FG_BATTERY ) {
|
|
|
|
// calculate amp hours used
|
|
|
|
float amphrs_used = amps * dt / 3600.0;
|
|
|
|
|
|
|
|
// calculate percent of total available capacity
|
|
|
|
float percent_used = amphrs_used / amp_hours;
|
|
|
|
percent_remaining -= percent_used;
|
|
|
|
if ( percent_remaining < 0.0 ) {
|
|
|
|
percent_remaining = 0.0;
|
|
|
|
} else if ( percent_remaining > 1.0 ) {
|
|
|
|
percent_remaining = 1.0;
|
|
|
|
}
|
|
|
|
// cout << "battery percent = " << percent_remaining << endl;
|
|
|
|
return amp_hours * percent_remaining;
|
|
|
|
} else if ( model == FG_ALTERNATOR ) {
|
|
|
|
// scale alternator output for rpms < 600. For rpms >= 600
|
|
|
|
// give full output. This is just a WAG, and probably not how
|
|
|
|
// it really works but I'm keeping things "simple" to start.
|
|
|
|
float rpm = _rpm_node->getFloatValue();
|
|
|
|
float factor = rpm / rpm_threshold;
|
|
|
|
if ( factor > 1.0 ) {
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
|
|
|
// cout << "alternator amps = " << amps * factor << endl;
|
|
|
|
float available_amps = ideal_amps * factor;
|
|
|
|
return available_amps - amps;
|
|
|
|
} else if ( model == FG_EXTERNAL ) {
|
|
|
|
// cout << "external amps = " << 0.0 << endl;
|
2005-08-22 17:49:50 +00:00
|
|
|
float available_amps = ideal_amps;
|
2005-06-14 17:57:48 +00:00
|
|
|
return available_amps - amps;
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float FGElectricalSupplier::get_output_volts() {
|
2002-09-24 23:45:56 +00:00
|
|
|
if ( model == FG_BATTERY ) {
|
|
|
|
// cout << "battery amps = " << amps << endl;
|
2005-06-14 17:57:48 +00:00
|
|
|
float x = 1.0 - percent_remaining;
|
|
|
|
float tmp = -(3.0 * x - 1.0);
|
|
|
|
float factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
|
|
|
|
// cout << "battery % = " << percent_remaining <<
|
|
|
|
// " factor = " << factor << endl;
|
|
|
|
// percent_remaining -= 0.001;
|
|
|
|
return ideal_volts * factor;
|
2002-09-24 23:45:56 +00:00
|
|
|
} 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.
|
2005-06-14 17:57:48 +00:00
|
|
|
float rpm = _rpm_node->getFloatValue();
|
|
|
|
float factor = rpm / rpm_threshold;
|
2002-09-24 23:45:56 +00:00
|
|
|
if ( factor > 1.0 ) {
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
|
|
|
// cout << "alternator amps = " << amps * factor << endl;
|
2005-06-14 17:57:48 +00:00
|
|
|
return ideal_volts * factor;
|
2002-09-24 23:45:56 +00:00
|
|
|
} else if ( model == FG_EXTERNAL ) {
|
|
|
|
// cout << "external amps = " << 0.0 << endl;
|
2005-06-14 17:57:48 +00:00
|
|
|
return ideal_volts;
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float FGElectricalSupplier::get_output_amps() {
|
|
|
|
if ( model == FG_BATTERY ) {
|
|
|
|
// cout << "battery amp_hours = " << amp_hours << endl;
|
|
|
|
|
|
|
|
// This is a WAG, but produce enough amps to burn the entire
|
|
|
|
// battery in one minute.
|
|
|
|
return amp_hours * 60.0;
|
|
|
|
} else if ( model == FG_ALTERNATOR ) {
|
|
|
|
// scale alternator output for rpms < 600. For rpms >= 600
|
|
|
|
// give full output. This is just a WAG, and probably not how
|
|
|
|
// it really works but I'm keeping things "simple" to start.
|
|
|
|
float rpm = _rpm_node->getFloatValue();
|
|
|
|
float factor = rpm / rpm_threshold;
|
|
|
|
if ( factor > 1.0 ) {
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
|
|
|
// cout << "alternator amps = " << ideal_amps * factor << endl;
|
|
|
|
return ideal_amps * factor;
|
|
|
|
} else if ( model == FG_EXTERNAL ) {
|
|
|
|
// cout << "external amps = " << 0.0 << endl;
|
|
|
|
return ideal_amps;
|
2002-09-24 23:45:56 +00:00
|
|
|
} 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;
|
2005-06-14 17:57:48 +00:00
|
|
|
load_amps = 0.1; // arbitrary default value
|
2002-09-24 19:57:11 +00:00
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
name = node->getStringValue("name");
|
2003-11-20 19:03:09 +00:00
|
|
|
SGPropertyNode *draw = node->getNode("rated-draw");
|
|
|
|
if ( draw != NULL ) {
|
2005-06-14 17:57:48 +00:00
|
|
|
load_amps = draw->getFloatValue();
|
2003-11-20 19:03:09 +00:00
|
|
|
}
|
|
|
|
// cout << "rated draw = " << output_amps << endl;
|
|
|
|
|
2002-09-26 04:51:23 +00:00
|
|
|
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 );
|
2005-06-14 17:57:48 +00:00
|
|
|
} else if ( s->get_kind() == FG_SUPPLIER &&
|
|
|
|
((FGElectricalSupplier *)s)->get_model()
|
|
|
|
== FGElectricalSupplier::FG_BATTERY ) {
|
|
|
|
s->add_output( this );
|
2002-09-26 04:51:23 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
FGElectricalSystem::FGElectricalSystem ( SGPropertyNode *node ) :
|
|
|
|
name("electrical"),
|
|
|
|
num(0),
|
|
|
|
path(""),
|
2002-09-24 19:57:11 +00:00
|
|
|
enabled(false)
|
2002-09-24 15:24:04 +00:00
|
|
|
{
|
2005-06-14 17:57:48 +00:00
|
|
|
int i;
|
|
|
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
|
|
|
SGPropertyNode *child = node->getChild(i);
|
|
|
|
string cname = child->getName();
|
|
|
|
string cval = child->getStringValue();
|
|
|
|
if ( cname == "name" ) {
|
|
|
|
name = cval;
|
|
|
|
} else if ( cname == "number" ) {
|
|
|
|
num = child->getIntValue();
|
|
|
|
} else if ( cname == "path" ) {
|
|
|
|
path = cval;
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_SYSTEMS, SG_WARN,
|
|
|
|
"Error in electrical system config logic" );
|
|
|
|
if ( name.length() ) {
|
|
|
|
SG_LOG( SG_SYSTEMS, SG_WARN, "Section = " << name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
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
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// allow the electrical system to be specified via the
|
|
|
|
// aircraft-set.xml file (for backwards compatibility) or through
|
|
|
|
// the aircraft-systems.xml file. If a -set.xml entry is
|
|
|
|
// specified, that overrides the system entry.
|
|
|
|
SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
|
|
|
|
if ( path_n ) {
|
|
|
|
if ( path.length() ) {
|
2005-06-14 20:55:46 +00:00
|
|
|
SG_LOG( SG_ALL, SG_INFO,
|
2005-06-14 17:57:48 +00:00
|
|
|
"NOTICE: System manager configuration specifies an " <<
|
2005-06-14 20:55:46 +00:00
|
|
|
"electrical system: " << path << " but it is " <<
|
2005-06-14 17:57:48 +00:00
|
|
|
"being overridden by the one specified in the -set.xml " <<
|
|
|
|
"file: " << path_n->getStringValue() );
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path_n->getStringValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( path.length() ) {
|
2002-12-31 00:56:14 +00:00
|
|
|
SGPath config( globals->get_fg_root() );
|
2005-06-14 17:57:48 +00:00
|
|
|
config.append( path );
|
2002-12-31 00:56:14 +00:00
|
|
|
|
2005-06-14 20:55:46 +00:00
|
|
|
// load an obsolete xml configuration
|
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
"Reading xml 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,
|
2005-06-14 20:55:46 +00:00
|
|
|
"Detected a logic error in the electrical system ");
|
2002-12-31 00:56:14 +00:00
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
2005-06-14 20:55:46 +00:00
|
|
|
"specification file. See earlier errors for " );
|
2002-12-31 00:56:14 +00:00
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
2005-06-14 20:55:46 +00:00
|
|
|
"details.");
|
2002-12-31 00:56:14 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} catch (const sg_exception& exc) {
|
2005-06-14 20:55:46 +00:00
|
|
|
SG_LOG( SG_ALL, SG_ALERT,
|
|
|
|
"Failed to load electrical system model: "
|
2002-12-31 00:56:14 +00:00
|
|
|
<< config.str() );
|
|
|
|
}
|
2004-01-31 19:47:45 +00:00
|
|
|
} else {
|
2005-06-14 20:55:46 +00:00
|
|
|
SG_LOG( SG_ALL, SG_INFO,
|
2005-06-14 17:57:48 +00:00
|
|
|
"No xml-based electrical model specified for this model!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !enabled ) {
|
|
|
|
_amps_out->setDoubleValue(0);
|
2004-01-31 19:47:45 +00:00
|
|
|
}
|
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 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// cout << "Updating electrical system, dt = " << dt << endl;
|
2002-09-24 23:45:56 +00:00
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// zero out the voltage before we start, but don't clear the
|
|
|
|
// requested load values.
|
2002-09-24 23:45:56 +00:00
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
2003-11-15 05:18:55 +00:00
|
|
|
suppliers[i]->set_volts( 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 );
|
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 );
|
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 );
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// for each "external" supplier, propagate the electrical current
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
|
|
|
FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
|
|
|
|
if ( node->get_model() == FGElectricalSupplier::FG_EXTERNAL ) {
|
|
|
|
float load;
|
|
|
|
// cout << "Starting propagation: " << suppliers[i]->get_name()
|
|
|
|
// << endl;
|
|
|
|
load = propagate( suppliers[i], dt,
|
|
|
|
node->get_output_volts(),
|
|
|
|
node->get_output_amps(),
|
|
|
|
" " );
|
|
|
|
|
|
|
|
if ( node->apply_load( load, dt ) < 0.0 ) {
|
|
|
|
cout << "Error drawing more current than available!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each "alternator" supplier, propagate the electrical
|
|
|
|
// current
|
2002-09-24 23:45:56 +00:00
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
2005-06-14 17:57:48 +00:00
|
|
|
FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
|
|
|
|
if ( node->get_model() == FGElectricalSupplier::FG_ALTERNATOR) {
|
|
|
|
float load;
|
|
|
|
// cout << "Starting propagation: " << suppliers[i]->get_name()
|
|
|
|
// << endl;
|
|
|
|
load = propagate( suppliers[i], dt,
|
|
|
|
node->get_output_volts(),
|
|
|
|
node->get_output_amps(),
|
|
|
|
" " );
|
|
|
|
|
|
|
|
if ( node->apply_load( load, dt ) < 0.0 ) {
|
|
|
|
cout << "Error drawing more current than available!" << endl;
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// for each "battery" supplier, propagate the electrical
|
|
|
|
// current
|
|
|
|
for ( i = 0; i < suppliers.size(); ++i ) {
|
|
|
|
FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
|
|
|
|
if ( node->get_model() == FGElectricalSupplier::FG_BATTERY ) {
|
|
|
|
float load;
|
|
|
|
// cout << "Starting propagation: " << suppliers[i]->get_name()
|
|
|
|
// << endl;
|
|
|
|
load = propagate( suppliers[i], dt,
|
|
|
|
node->get_output_volts(),
|
|
|
|
node->get_output_amps(),
|
|
|
|
" " );
|
|
|
|
// cout << "battery load = " << load << endl;
|
|
|
|
|
|
|
|
if ( node->apply_load( load, dt ) < 0.0 ) {
|
|
|
|
cout << "Error drawing more current than available!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float alt_norm
|
|
|
|
= fgGetFloat("/systems/electrical/suppliers/alternator") / 60.0;
|
2003-05-27 19:18:14 +00:00
|
|
|
|
|
|
|
// impliment an extremely simplistic voltage model (assumes
|
|
|
|
// certain naming conventions in electrical system config)
|
2004-04-02 16:20:46 +00:00
|
|
|
// FIXME: we probably want to be able to feed power from all
|
|
|
|
// engines if they are running and the master-alt is switched on
|
2005-06-14 17:57:48 +00:00
|
|
|
float volts = 0.0;
|
2004-04-02 16:20:46 +00:00
|
|
|
if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
|
2003-05-27 19:18:14 +00:00
|
|
|
volts = 24.0;
|
|
|
|
}
|
2004-06-14 21:36:10 +00:00
|
|
|
if ( fgGetBool("/controls/engines/engine[0]/master-alt") ) {
|
2005-06-14 17:57:48 +00:00
|
|
|
if ( fgGetFloat("/engines/engine[0]/rpm") > 800 ) {
|
|
|
|
float alt_contrib = 28.0;
|
2004-06-14 21:36:10 +00:00
|
|
|
if ( alt_contrib > volts ) {
|
|
|
|
volts = alt_contrib;
|
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
} else if ( fgGetFloat("/engines/engine[0]/rpm") > 200 ) {
|
|
|
|
float alt_contrib = 20.0;
|
2004-06-14 21:36:10 +00:00
|
|
|
if ( alt_contrib > volts ) {
|
|
|
|
volts = alt_contrib;
|
|
|
|
}
|
2003-05-27 19:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
_volts_out->setFloatValue( volts );
|
2003-05-27 19:18:14 +00:00
|
|
|
|
|
|
|
// impliment an extremely simplistic amps model (assumes certain
|
|
|
|
// naming conventions in the electrical system config) ... FIXME:
|
|
|
|
// make this more generic
|
2005-06-14 17:57:48 +00:00
|
|
|
float amps = 0.0;
|
2004-04-20 22:54:44 +00:00
|
|
|
if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
|
|
|
|
if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
|
2005-06-14 17:57:48 +00:00
|
|
|
fgGetFloat("/engines/engine[0]/rpm") > 800 )
|
2003-05-28 18:47:42 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
_amps_out->setFloatValue( 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.
|
2005-06-14 17:57:48 +00:00
|
|
|
float FGElectricalSystem::propagate( FGElectricalComponent *node, double dt,
|
|
|
|
float input_volts, float input_amps,
|
|
|
|
string s ) {
|
2002-09-24 23:45:56 +00:00
|
|
|
s += " ";
|
2003-11-15 05:18:55 +00:00
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
float total_load = 0.0;
|
2002-09-24 23:45:56 +00:00
|
|
|
|
|
|
|
// determine the current to carry forward
|
2005-06-14 17:57:48 +00:00
|
|
|
float 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;
|
2005-06-14 17:57:48 +00:00
|
|
|
} else if ( node->get_kind() == FGElectricalComponent::FG_SUPPLIER ) {
|
|
|
|
// cout << s << "is a supplier (" << node->get_name() << ")" << endl;
|
|
|
|
FGElectricalSupplier *supplier = (FGElectricalSupplier *)node;
|
|
|
|
if ( supplier->get_model() == FGElectricalSupplier::FG_BATTERY ) {
|
|
|
|
// cout << s << " (and is a battery)" << endl;
|
|
|
|
float battery_volts = supplier->get_output_volts();
|
|
|
|
if ( battery_volts < (input_volts - 0.1) ) {
|
|
|
|
// special handling of a battery charge condition
|
|
|
|
// cout << s << " (and is being charged) in v = "
|
|
|
|
// << input_volts << " current v = " << battery_volts
|
|
|
|
// << endl;
|
|
|
|
supplier->apply_load( -supplier->get_charge_amps(), dt );
|
|
|
|
return supplier->get_charge_amps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
volts = input_volts;
|
|
|
|
} else if ( node->get_kind() == FGElectricalComponent::FG_BUS ) {
|
|
|
|
// cout << s << "is a bus (" << node->get_name() << ")" << endl;
|
|
|
|
volts = input_volts;
|
|
|
|
} else if ( node->get_kind() == FGElectricalComponent::FG_OUTPUT ) {
|
|
|
|
// cout << s << "is an output (" << node->get_name() << ")" << endl;
|
|
|
|
volts = input_volts;
|
2003-11-15 05:18:55 +00:00
|
|
|
if ( volts > 1.0 ) {
|
|
|
|
// draw current if we have voltage
|
2005-06-14 17:57:48 +00:00
|
|
|
total_load = node->get_load_amps();
|
2003-11-15 05:18:55 +00:00
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
} else if ( node->get_kind() == FGElectricalComponent::FG_CONNECTOR ) {
|
|
|
|
// cout << s << "is a connector (" << node->get_name() << ")" << endl;
|
2002-09-24 23:45:56 +00:00
|
|
|
if ( ((FGElectricalConnector *)node)->get_state() ) {
|
2005-06-14 17:57:48 +00:00
|
|
|
volts = input_volts;
|
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
|
|
|
}
|
2005-06-14 17:57:48 +00:00
|
|
|
// cout << s << " input_volts = " << 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
|
|
|
}
|
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// if this node has found a stronger power source, update the
|
|
|
|
// value and propagate to all children
|
2003-11-15 05:18:55 +00:00
|
|
|
if ( volts > node->get_volts() ) {
|
|
|
|
node->set_volts( volts );
|
2005-06-14 17:57:48 +00:00
|
|
|
for ( i = 0; i < node->get_num_outputs(); ++i ) {
|
|
|
|
FGElectricalComponent *child = node->get_output(i);
|
|
|
|
// send current equal to load
|
|
|
|
total_load += propagate( child, dt,
|
|
|
|
volts, child->get_load_amps(),
|
|
|
|
s );
|
|
|
|
}
|
2002-09-24 23:45:56 +00:00
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// if not an output node, register the downstream current draw
|
|
|
|
// (sum of all children) with this node. If volts are zero,
|
|
|
|
// current draw should be zero.
|
|
|
|
if ( node->get_kind() != FGElectricalComponent::FG_OUTPUT ) {
|
|
|
|
node->set_load_amps( total_load );
|
|
|
|
}
|
2002-09-26 04:51:23 +00:00
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
node->set_available_amps( input_amps - total_load );
|
2002-09-24 23:45:56 +00:00
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
// publish values to specified properties
|
|
|
|
for ( i = 0; i < node->get_num_props(); ++i ) {
|
|
|
|
fgSetFloat( node->get_prop(i).c_str(), node->get_volts() );
|
|
|
|
}
|
2003-11-15 05:18:55 +00:00
|
|
|
|
2005-06-14 17:57:48 +00:00
|
|
|
/*
|
|
|
|
cout << s << node->get_name() << " -> (volts) " << node->get_volts()
|
|
|
|
<< endl;
|
|
|
|
cout << s << node->get_name() << " -> (load amps) " << total_load
|
|
|
|
<< endl;
|
|
|
|
cout << s << node->get_name() << " -> (input amps) " << input_amps
|
|
|
|
<< endl;
|
|
|
|
cout << s << node->get_name() << " -> (extra amps) "
|
|
|
|
<< node->get_available_amps() << endl;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return total_load;
|
|
|
|
} else {
|
|
|
|
// cout << s << "no further propagation" << endl;
|
|
|
|
return 0.0;
|
2003-11-15 05:18:55 +00:00
|
|
|
}
|
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
|
|
|
}
|