]> git.mxchange.org Git - flightgear.git/blobdiff - src/Systems/electrical.cxx
Fix stray back-button in Qt launcher
[flightgear.git] / src / Systems / electrical.cxx
index b069407c4774d09abe49081e41e2849b581b8bf9..379841f5a6b6eeaaf62a10b90675d183590d73d4 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started September 2002.
 //
-// Copyright (C) 2002  Curtis L. Olson  - curt@flightgear.org
+// Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 //
 // 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.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <cstdlib>
+#include <cstring>
 
 #include <simgear/structure/exception.hxx>
 #include <simgear/misc/sg_path.hxx>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/props/props_io.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
@@ -33,7 +41,8 @@
 FGElectricalComponent::FGElectricalComponent() :
     kind(-1),
     name(""),
-    value(0.0)
+    volts(0.0),
+    load_amps(0.0)
 {
 }
 
@@ -47,26 +56,31 @@ FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
     // cout << "_model = " << _model << endl;
     if ( _model == "battery" ) {
         model = FG_BATTERY;
+        amp_hours = node->getFloatValue("amp-hours", 40.0);
+        percent_remaining = node->getFloatValue("percent-remaining", 1.0);
+        charge_amps = node->getFloatValue("charge-amps", 7.0);
     } else if ( _model == "alternator" ) {
         model = FG_ALTERNATOR;
+        rpm_src = node->getStringValue("rpm-source");
+        rpm_threshold = node->getFloatValue("rpm-threshold", 600.0);
+        ideal_amps = node->getFloatValue("amps", 60.0);
     } else if ( _model == "external" ) {
         model = FG_EXTERNAL;
+        ideal_amps = node->getFloatValue("amps", 60.0);
     } else {
         model = FG_UNKNOWN;
     }
-    volts = node->getDoubleValue("volts");
-    amps = node->getDoubleValue("amps");
-    rpm_src = node->getStringValue("rpm-source");
+    ideal_volts = node->getFloatValue("volts");
 
     int i;
     for ( i = 0; i < node->nChildren(); ++i ) {
         SGPropertyNode *child = node->getChild(i);
         // cout << " scanning: " << child->getName() << endl;
-        if ( (string)child->getName() == "prop" ) {
+        if ( !strcmp(child->getName(), "prop") ) {
             string prop = child->getStringValue();
             // cout << "  Adding prop = " << prop << endl;
             add_prop( prop );
-            fgSetDouble( prop.c_str(), amps );
+            fgSetFloat( prop.c_str(), ideal_amps );
         }
     }
 
@@ -74,26 +88,100 @@ FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
 }  
 
 
-double FGElectricalSupplier::get_output() {
+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;
+        float available_amps = ideal_amps;
+        return available_amps - amps;
+    } else {
+        SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown supplier type" );
+    }
+
+    return 0.0;
+}
+
+
+float FGElectricalSupplier::get_output_volts() {
     if ( model == FG_BATTERY ) {
         // cout << "battery amps = " << amps << endl;
-        return amps;
+        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;
     } 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.
-        double rpm = _rpm_node->getDoubleValue();
-        double factor = rpm / 600.0;
+        float rpm = _rpm_node->getFloatValue();
+        float factor = rpm / rpm_threshold;
         if ( factor > 1.0 ) {
             factor = 1.0;
         }
         // cout << "alternator amps = " << amps * factor << endl;
-        return amps * factor;
+        return ideal_volts * factor;
     } else if ( model == FG_EXTERNAL ) {
         // cout << "external amps = " << 0.0 << endl;
-        return 0.0;
+        return ideal_volts;
+    } else {
+        SG_LOG( SG_SYSTEMS, 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;
     } else {
-        SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
+        SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown supplier type" );
     }
 
     return 0.0;
@@ -107,7 +195,7 @@ FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
     int i;
     for ( i = 0; i < node->nChildren(); ++i ) {
         SGPropertyNode *child = node->getChild(i);
-        if ( (string)child->getName() == "prop" ) {
+        if ( !strcmp(child->getName(), "prop") ) {
             string prop = child->getStringValue();
             add_prop( prop );
         }
@@ -117,12 +205,19 @@ FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
 
 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
     kind = FG_OUTPUT;
+    load_amps = 0.1;            // arbitrary default value
 
     name = node->getStringValue("name");
+    SGPropertyNode *draw = node->getNode("rated-draw");
+    if ( draw != NULL ) {
+        load_amps = draw->getFloatValue();
+    }
+    // cout << "rated draw = " << output_amps << endl;
+
     int i;
     for ( i = 0; i < node->nChildren(); ++i ) {
         SGPropertyNode *child = node->getChild(i);
-        if ( (string)child->getName() == "prop" ) {
+        if ( !strcmp(child->getName(), "prop") ) {
             string prop = child->getStringValue();
             add_prop( prop );
         }
@@ -130,6 +225,37 @@ FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
 }  
 
 
+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 );
+            // cout << "switch node = " << cval << endl;
+        } else if ( cname == "initial-state" ) {
+            if ( cval == "off" || cval == "false" ) {
+                initial_state = false;
+            }
+            // cout << "initial state = " << initial_state << endl;
+        } else if ( cname == "rating-amps" ) {
+            rating_amps = atof( cval.c_str() );
+            circuit_breaker = true;
+            // cout << "initial state = " << initial_state << endl;
+        }            
+    }
+
+    switch_node->setBoolValue( initial_state );
+    // cout << "  value = " << switch_node->getBoolValue() << endl;
+}
+
+
 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
                                                FGElectricalSystem *es ) {
     kind = FG_CONNECTOR;
@@ -149,12 +275,12 @@ FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
                 } else if ( s->get_kind() == FG_BUS ) {
                     s->add_output( this );
                 } else {
-                    SG_LOG( SG_ALL, SG_ALERT,
+                    SG_LOG( SG_SYSTEMS, 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: " 
+                SG_LOG( SG_SYSTEMS, SG_ALERT, "Can't find named source: " 
                         << child->getStringValue() );
             }
         } else if ( cname == "output" ) {
@@ -165,35 +291,23 @@ FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
                     s->add_input( this );
                 } else if ( s->get_kind() == FG_OUTPUT ) {
                     s->add_input( this );
+                } else if ( s->get_kind() == FG_SUPPLIER &&
+                            ((FGElectricalSupplier *)s)->get_model()
+                            == FGElectricalSupplier::FG_BATTERY ) {
+                    s->add_output( this );
                 } else {
-                    SG_LOG( SG_ALL, SG_ALERT,
+                    SG_LOG( SG_SYSTEMS, 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: " 
+                SG_LOG( SG_SYSTEMS, 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 ) );
-        }
-    }
-
-    // do a 2nd pass to pick up starting switch value if specified
-    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 == "initial-state" ) {
-            if ( cval == "off" ) {
-                set_switches( false );
-            } else {
-                set_switches( true );
-            }
+             // cout << "Switch = " << child->getStringValue() << endl;
+            FGElectricalSwitch s( child );
+            add_switch( s );
         }
     }
 }  
@@ -203,7 +317,7 @@ FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
 void FGElectricalConnector::set_switches( bool state ) {
     // cout << "setting switch state to " << state << endl;
     for ( unsigned int i = 0; i < switches.size(); ++i ) {
-        switches[i]->setBoolValue( state );
+        switches[i].set_state( state );
     }
 }
 
@@ -214,7 +328,7 @@ void FGElectricalConnector::set_switches( bool state ) {
 bool FGElectricalConnector::get_state() {
     unsigned int i;
     for ( i = 0; i < switches.size(); ++i ) {
-        if ( ! switches[i]->getBoolValue() ) {
+        if ( ! switches[i].get_state() ) {
             return false;
         }
     }
@@ -223,7 +337,10 @@ bool FGElectricalConnector::get_state() {
 }
 
 
-FGElectricalSystem::FGElectricalSystem () :
+FGElectricalSystem::FGElectricalSystem ( SGPropertyNode *node ) :
+    name(node->getStringValue("name", "electrical")),
+    num(node->getIntValue("number", 0)),
+    path(node->getStringValue("path")),
     enabled(false)
 {
 }
@@ -234,42 +351,58 @@ FGElectricalSystem::~FGElectricalSystem () {
 
 
 void FGElectricalSystem::init () {
-    config_props = new SGPropertyNode;
+    SGPropertyNode_ptr config_props = new SGPropertyNode;
 
-    SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
     _volts_out = fgGetNode( "/systems/electrical/volts", true );
     _amps_out = fgGetNode( "/systems/electrical/amps", true );
 
-    if (path_n) {
-        SGPath config( globals->get_fg_root() );
-        config.append( path_n->getStringValue() );
+    // 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() ) {
+            SG_LOG( SG_SYSTEMS, SG_INFO,
+                    "NOTICE: System manager configuration specifies an " <<
+                    "electrical system: " << path << " but it is " <<
+                    "being overridden by the one specified in the -set.xml " <<
+                    "file: " << path_n->getStringValue() );
+        }
+
+        path = path_n->getStringValue();
+    }
 
-        SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
+    if ( path.length() ) {
+        SGPath config = globals->resolve_aircraft_path(path);
+#if defined(ENABLE_DEV_WARNINGS)
+        // load an obsolete xml configuration
+        SG_LOG( SG_SYSTEMS, SG_WARN,
+                "Reading deprecated xml electrical system model from\n    "
                 << config.str() );
+#endif
         try {
             readProperties( config.str(), config_props );
 
-            if ( build() ) {
+            if ( build(config_props) ) {
                 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);
+                throw sg_exception("Logic error in electrical system file.");
             }        
-        } catch (const sg_exception& exc) {
-            SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
+        } catch (const sg_exception&) {
+            SG_LOG( SG_SYSTEMS, SG_ALERT,
+                    "Failed to load electrical system model: "
                     << config.str() );
         }
+    } else {
+        SG_LOG( SG_SYSTEMS, SG_INFO,
+                "No xml-based electrical model specified for this model!");
+    }
 
-    } else
-        SG_LOG( SG_ALL, SG_ALERT,
-                "No electrical model specified for this model!");
+    if ( !enabled ) {
+        _amps_out->setDoubleValue(0);
+    }
 
-    delete config_props;
 }
 
 
@@ -283,60 +416,121 @@ void FGElectricalSystem::unbind () {
 
 void FGElectricalSystem::update (double dt) {
     if ( !enabled ) {
-        _amps_out->setDoubleValue(0);
         return;
     }
 
-    // cout << "Updating electrical system" << endl;
+    // cout << "Updating electrical system, dt = " << dt << endl;
 
     unsigned int i;
 
-    // zero everything out before we start
+    // zero out the voltage before we start, but don't clear the
+    // requested load values.
     for ( i = 0; i < suppliers.size(); ++i ) {
-        suppliers[i]->set_value( 0.0 );
+        suppliers[i]->set_volts( 0.0 );
     }
     for ( i = 0; i < buses.size(); ++i ) {
-        buses[i]->set_value( 0.0 );
+        buses[i]->set_volts( 0.0 );
     }
     for ( i = 0; i < outputs.size(); ++i ) {
-        outputs[i]->set_value( 0.0 );
+        outputs[i]->set_volts( 0.0 );
     }
     for ( i = 0; i < connectors.size(); ++i ) {
-        connectors[i]->set_value( 0.0 );
+        connectors[i]->set_volts( 0.0 );
     }
 
-    // for each supplier, propagate the electrical current
+    // for each "external" supplier, propagate the electrical current
     for ( i = 0; i < suppliers.size(); ++i ) {
-        // cout << " Updating: " << suppliers[i]->get_name() << endl;
-        propagate( suppliers[i], 0.0, " " );
+        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 ) {
+                SG_LOG(SG_SYSTEMS, SG_ALERT,
+                       "Error drawing more current than available!");
+            }
+        }     
     }
 
-    double alt_norm
-        = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
+    // for each "alternator" supplier, propagate the electrical
+    // current
+    for ( i = 0; i < suppliers.size(); ++i ) {
+        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 ) {
+                SG_LOG(SG_SYSTEMS, SG_ALERT,
+                       "Error drawing more current than available!");
+            }
+        }     
+    }
+
+    // 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 ) {
+                SG_LOG(SG_SYSTEMS, SG_ALERT,
+                       "Error drawing more current than available!");
+            }
+        }     
+    }
+
+    float alt_norm
+        = fgGetFloat("/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") ) {
+    // FIXME: we probably want to be able to feed power from all
+    // engines if they are running and the master-alt is switched on
+    float volts = 0.0;
+    if ( fgGetBool("/controls/engines/engine[0]/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;
+    if ( fgGetBool("/controls/engines/engine[0]/master-alt") ) {
+        if ( fgGetFloat("/engines/engine[0]/rpm") > 800 ) {
+            float alt_contrib = 28.0;
+            if ( alt_contrib > volts ) {
+                volts = alt_contrib;
+            }
+        } else if ( fgGetFloat("/engines/engine[0]/rpm") > 200 ) {
+            float alt_contrib = 20.0;
+            if ( alt_contrib > volts ) {
+                volts = alt_contrib;
+            }
         }
     }
-    _volts_out->setDoubleValue( volts );
+    _volts_out->setFloatValue( 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") ) {
-        if ( fgGetBool("/controls/switches/master-alt") &&
-             fgGetDouble("/engines/engine[0]/rpm") > 800 )
+    float amps = 0.0;
+    if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
+        if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
+             fgGetFloat("/engines/engine[0]/rpm") > 800 )
         {
             amps += 40.0 * alt_norm;
         }
@@ -351,11 +545,11 @@ void FGElectricalSystem::update (double dt) {
             amps = 7.0;
         }
     }
-    _amps_out->setDoubleValue( amps );
+    _amps_out->setFloatValue( amps );
 }
 
 
-bool FGElectricalSystem::build () {
+bool FGElectricalSystem::build (SGPropertyNode* config_props) {
     SGPropertyNode *node;
     int i;
 
@@ -381,7 +575,7 @@ bool FGElectricalSystem::build () {
                 new FGElectricalConnector( node, this );
             connectors.push_back( c );
         } else {
-            SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: " 
+            SG_LOG( SG_SYSTEMS, SG_ALERT, "Unknown component type specified: " 
                     << name );
             return false;
         }
@@ -391,51 +585,100 @@ bool FGElectricalSystem::build () {
 }
 
 
-// propagate the electrical current through the network
-void FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
-                                    string s ) {
+// propagate the electrical current through the network, returns the
+// total current drawn by the children of this node.
+float FGElectricalSystem::propagate( FGElectricalComponent *node, double dt,
+                                     float input_volts, float input_amps,
+                                     string s ) {
     s += " ";
+    
+    float total_load = 0.0;
 
     // determine the current to carry forward
-    double current = 0.0;
+    float volts = 0.0;
     if ( !fgGetBool("/systems/electrical/serviceable") ) {
-        current = 0;
-    } else 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;
+        volts = 0;
+    } 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;
+        if ( volts > 1.0 ) {
+            // draw current if we have voltage
+            total_load = node->get_load_amps();
+        }
+    } else if ( node->get_kind() == FGElectricalComponent::FG_CONNECTOR ) {
+        // cout << s << "is a connector (" << node->get_name() << ")" << endl;
         if ( ((FGElectricalConnector *)node)->get_state() ) {
-            current = val;
+            volts = input_volts;
         } else {
-            current = 0.0;
+            volts = 0.0;
         }
-        // cout << s << "  val = " << current << endl;
+        // cout << s << "  input_volts = " << volts << endl;
     } else {
-        SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
-    }
-
-    if ( current > node->get_value() ) {
-        node->set_value( current );
+        SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown node type" );
     }
 
     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() );
-    }
-    // cout << s << node->get_name() << " -> " << node->get_value() << endl;
+    // if this node has found a stronger power source, update the
+    // value and propagate to all children
+    if ( volts > node->get_volts() ) {
+        node->set_volts( volts );
+        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 );
+        }
+
+        // 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 );
+        }
+
+        node->set_available_amps( input_amps - total_load );
+
+        // publish values to specified properties
+        for ( i = 0; i < node->get_num_props(); ++i ) {
+            fgSetFloat( node->get_prop(i).c_str(), node->get_volts() );
+        }
 
-    // propagate to all children
-    for ( i = 0; i < node->get_num_outputs(); ++i ) {
-        propagate( node->get_output(i), current, s );
+        /*
+        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;
     }
 }