+
+void FGElectricalSystem::update (double dt) {
+ if ( !enabled ) {
+ return;
+ }
+
+ // cout << "Updating electrical system, dt = " << dt << endl;
+
+ unsigned int i;
+
+ // 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_volts( 0.0 );
+ }
+ for ( i = 0; i < buses.size(); ++i ) {
+ buses[i]->set_volts( 0.0 );
+ }
+ for ( i = 0; i < outputs.size(); ++i ) {
+ outputs[i]->set_volts( 0.0 );
+ }
+ for ( i = 0; i < connectors.size(); ++i ) {
+ connectors[i]->set_volts( 0.0 );
+ }
+
+ // 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 ) {
+ SG_LOG(SG_SYSTEMS, SG_ALERT,
+ "Error drawing more current than available!");
+ }
+ }
+ }
+
+ // 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)
+ // 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/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->setFloatValue( volts );
+
+ // impliment an extremely simplistic amps model (assumes certain
+ // naming conventions in the electrical system config) ... FIXME:
+ // make this more generic
+ 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;
+ }
+ 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->setFloatValue( amps );
+}
+
+
+bool FGElectricalSystem::build (SGPropertyNode* config_props) {
+ SGPropertyNode *node;
+ int i;
+
+ 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 =
+ new FGElectricalSupplier( node );
+ suppliers.push_back( s );
+ } else if ( name == "bus" ) {
+ FGElectricalBus *b =
+ new FGElectricalBus( node );
+ buses.push_back( b );
+ } else if ( name == "output" ) {
+ FGElectricalOutput *o =
+ new FGElectricalOutput( node );
+ outputs.push_back( o );
+ } else if ( name == "connector" ) {
+ FGElectricalConnector *c =
+ new FGElectricalConnector( node, this );
+ connectors.push_back( c );
+ } else {
+ SG_LOG( SG_SYSTEMS, SG_ALERT, "Unknown component type specified: "
+ << name );
+ return false;
+ }
+ }
+
+ return true;
+}
+
+
+// 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
+ float volts = 0.0;
+ if ( !fgGetBool("/systems/electrical/serviceable") ) {
+ 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() ) {
+ volts = input_volts;
+ } else {
+ volts = 0.0;
+ }
+ // cout << s << " input_volts = " << volts << endl;
+ } else {
+ SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown node type" );
+ }
+
+ int i;
+
+ // 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() );
+ }
+
+ /*
+ 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;
+ }
+}
+
+
+// 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 ) {
+ s = suppliers[i]->get_name();
+ // cout << " " << s << endl;
+ if ( s == name ) {
+ return suppliers[i];
+ }
+ }
+
+ // then search buses
+ for ( i = 0; i < buses.size(); ++i ) {
+ s = buses[i]->get_name();
+ // cout << " " << s << endl;
+ if ( s == name ) {
+ return buses[i];
+ }
+ }
+
+ // then search outputs
+ for ( i = 0; i < outputs.size(); ++i ) {
+ s = outputs[i]->get_name();
+ // cout << " " << s << endl;
+ if ( s == name ) {
+ return outputs[i];
+ }
+ }
+
+ // nothing found
+ return NULL;