1 // electrical.cxx - a flexible, generic electrical system model.
3 // Written by Curtis Olson, started September 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/props/props_io.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
38 #include "electrical.hxx"
41 FGElectricalComponent::FGElectricalComponent() :
50 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
53 // cout << "Creating a supplier" << endl;
54 name = node->getStringValue("name");
55 string _model = node->getStringValue("kind");
56 // cout << "_model = " << _model << endl;
57 if ( _model == "battery" ) {
59 amp_hours = node->getFloatValue("amp-hours", 40.0);
60 percent_remaining = node->getFloatValue("percent-remaining", 1.0);
61 charge_amps = node->getFloatValue("charge-amps", 7.0);
62 } else if ( _model == "alternator" ) {
63 model = FG_ALTERNATOR;
64 rpm_src = node->getStringValue("rpm-source");
65 rpm_threshold = node->getFloatValue("rpm-threshold", 600.0);
66 ideal_amps = node->getFloatValue("amps", 60.0);
67 } else if ( _model == "external" ) {
69 ideal_amps = node->getFloatValue("amps", 60.0);
73 ideal_volts = node->getFloatValue("volts");
76 for ( i = 0; i < node->nChildren(); ++i ) {
77 SGPropertyNode *child = node->getChild(i);
78 // cout << " scanning: " << child->getName() << endl;
79 if ( !strcmp(child->getName(), "prop") ) {
80 string prop = child->getStringValue();
81 // cout << " Adding prop = " << prop << endl;
83 fgSetFloat( prop.c_str(), ideal_amps );
87 _rpm_node = fgGetNode( rpm_src.c_str(), true);
91 float FGElectricalSupplier::apply_load( float amps, float dt ) {
92 if ( model == FG_BATTERY ) {
93 // calculate amp hours used
94 float amphrs_used = amps * dt / 3600.0;
96 // calculate percent of total available capacity
97 float percent_used = amphrs_used / amp_hours;
98 percent_remaining -= percent_used;
99 if ( percent_remaining < 0.0 ) {
100 percent_remaining = 0.0;
101 } else if ( percent_remaining > 1.0 ) {
102 percent_remaining = 1.0;
104 // cout << "battery percent = " << percent_remaining << endl;
105 return amp_hours * percent_remaining;
106 } else if ( model == FG_ALTERNATOR ) {
107 // scale alternator output for rpms < 600. For rpms >= 600
108 // give full output. This is just a WAG, and probably not how
109 // it really works but I'm keeping things "simple" to start.
110 float rpm = _rpm_node->getFloatValue();
111 float factor = rpm / rpm_threshold;
112 if ( factor > 1.0 ) {
115 // cout << "alternator amps = " << amps * factor << endl;
116 float available_amps = ideal_amps * factor;
117 return available_amps - amps;
118 } else if ( model == FG_EXTERNAL ) {
119 // cout << "external amps = " << 0.0 << endl;
120 float available_amps = ideal_amps;
121 return available_amps - amps;
123 SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown supplier type" );
130 float FGElectricalSupplier::get_output_volts() {
131 if ( model == FG_BATTERY ) {
132 // cout << "battery amps = " << amps << endl;
133 float x = 1.0 - percent_remaining;
134 float tmp = -(3.0 * x - 1.0);
135 float factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
136 // cout << "battery % = " << percent_remaining <<
137 // " factor = " << factor << endl;
138 // percent_remaining -= 0.001;
139 return ideal_volts * factor;
140 } else if ( model == FG_ALTERNATOR ) {
141 // scale alternator output for rpms < 600. For rpms >= 600
142 // give full output. This is just a WAG, and probably not how
143 // it really works but I'm keeping things "simple" to start.
144 float rpm = _rpm_node->getFloatValue();
145 float factor = rpm / rpm_threshold;
146 if ( factor > 1.0 ) {
149 // cout << "alternator amps = " << amps * factor << endl;
150 return ideal_volts * factor;
151 } else if ( model == FG_EXTERNAL ) {
152 // cout << "external amps = " << 0.0 << endl;
155 SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown supplier type" );
162 float FGElectricalSupplier::get_output_amps() {
163 if ( model == FG_BATTERY ) {
164 // cout << "battery amp_hours = " << amp_hours << endl;
166 // This is a WAG, but produce enough amps to burn the entire
167 // battery in one minute.
168 return amp_hours * 60.0;
169 } else if ( model == FG_ALTERNATOR ) {
170 // scale alternator output for rpms < 600. For rpms >= 600
171 // give full output. This is just a WAG, and probably not how
172 // it really works but I'm keeping things "simple" to start.
173 float rpm = _rpm_node->getFloatValue();
174 float factor = rpm / rpm_threshold;
175 if ( factor > 1.0 ) {
178 // cout << "alternator amps = " << ideal_amps * factor << endl;
179 return ideal_amps * factor;
180 } else if ( model == FG_EXTERNAL ) {
181 // cout << "external amps = " << 0.0 << endl;
184 SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown supplier type" );
191 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
194 name = node->getStringValue("name");
196 for ( i = 0; i < node->nChildren(); ++i ) {
197 SGPropertyNode *child = node->getChild(i);
198 if ( !strcmp(child->getName(), "prop") ) {
199 string prop = child->getStringValue();
206 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
208 load_amps = 0.1; // arbitrary default value
210 name = node->getStringValue("name");
211 SGPropertyNode *draw = node->getNode("rated-draw");
212 if ( draw != NULL ) {
213 load_amps = draw->getFloatValue();
215 // cout << "rated draw = " << output_amps << endl;
218 for ( i = 0; i < node->nChildren(); ++i ) {
219 SGPropertyNode *child = node->getChild(i);
220 if ( !strcmp(child->getName(), "prop") ) {
221 string prop = child->getStringValue();
228 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
231 circuit_breaker( false )
233 bool initial_state = true;
235 for ( i = 0; i < node->nChildren(); ++i ) {
236 SGPropertyNode *child = node->getChild(i);
237 string cname = child->getName();
238 string cval = child->getStringValue();
239 if ( cname == "prop" ) {
240 switch_node = fgGetNode( cval.c_str(), true );
241 // cout << "switch node = " << cval << endl;
242 } else if ( cname == "initial-state" ) {
243 if ( cval == "off" || cval == "false" ) {
244 initial_state = false;
246 // cout << "initial state = " << initial_state << endl;
247 } else if ( cname == "rating-amps" ) {
248 rating_amps = atof( cval.c_str() );
249 circuit_breaker = true;
250 // cout << "initial state = " << initial_state << endl;
254 switch_node->setBoolValue( initial_state );
255 // cout << " value = " << switch_node->getBoolValue() << endl;
259 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
260 FGElectricalSystem *es ) {
264 for ( i = 0; i < node->nChildren(); ++i ) {
265 SGPropertyNode *child = node->getChild(i);
266 string cname = child->getName();
267 string cval = child->getStringValue();
268 // cout << " " << cname << " = " << cval << endl;
269 if ( cname == "input" ) {
270 FGElectricalComponent *s = es->find( child->getStringValue() );
273 if ( s->get_kind() == FG_SUPPLIER ) {
274 s->add_output( this );
275 } else if ( s->get_kind() == FG_BUS ) {
276 s->add_output( this );
278 SG_LOG( SG_SYSTEMS, SG_ALERT,
279 "Attempt to connect to something that can't provide an output: "
280 << child->getStringValue() );
283 SG_LOG( SG_SYSTEMS, SG_ALERT, "Can't find named source: "
284 << child->getStringValue() );
286 } else if ( cname == "output" ) {
287 FGElectricalComponent *s = es->find( child->getStringValue() );
290 if ( s->get_kind() == FG_BUS ) {
291 s->add_input( this );
292 } else if ( s->get_kind() == FG_OUTPUT ) {
293 s->add_input( this );
294 } else if ( s->get_kind() == FG_SUPPLIER &&
295 ((FGElectricalSupplier *)s)->get_model()
296 == FGElectricalSupplier::FG_BATTERY ) {
297 s->add_output( this );
299 SG_LOG( SG_SYSTEMS, SG_ALERT,
300 "Attempt to connect to something that can't provide an input: "
301 << child->getStringValue() );
304 SG_LOG( SG_SYSTEMS, SG_ALERT, "Can't find named source: "
305 << child->getStringValue() );
307 } else if ( cname == "switch" ) {
308 // cout << "Switch = " << child->getStringValue() << endl;
309 FGElectricalSwitch s( child );
316 // set all switches to the specified state
317 void FGElectricalConnector::set_switches( bool state ) {
318 // cout << "setting switch state to " << state << endl;
319 for ( unsigned int i = 0; i < switches.size(); ++i ) {
320 switches[i].set_state( state );
325 // return true if all switches are true, false otherwise. A connector
326 // could have multiple switches, but they all need to be true(closed)
327 // for current to get through.
328 bool FGElectricalConnector::get_state() {
330 for ( i = 0; i < switches.size(); ++i ) {
331 if ( ! switches[i].get_state() ) {
340 FGElectricalSystem::FGElectricalSystem ( SGPropertyNode *node ) :
341 name(node->getStringValue("name", "electrical")),
342 num(node->getIntValue("number", 0)),
343 path(node->getStringValue("path")),
349 FGElectricalSystem::~FGElectricalSystem () {
353 void FGElectricalSystem::init () {
354 SGPropertyNode_ptr config_props = new SGPropertyNode;
356 _volts_out = fgGetNode( "/systems/electrical/volts", true );
357 _amps_out = fgGetNode( "/systems/electrical/amps", true );
359 // allow the electrical system to be specified via the
360 // aircraft-set.xml file (for backwards compatibility) or through
361 // the aircraft-systems.xml file. If a -set.xml entry is
362 // specified, that overrides the system entry.
363 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
365 if ( path.length() ) {
366 SG_LOG( SG_SYSTEMS, SG_INFO,
367 "NOTICE: System manager configuration specifies an " <<
368 "electrical system: " << path << " but it is " <<
369 "being overridden by the one specified in the -set.xml " <<
370 "file: " << path_n->getStringValue() );
373 path = path_n->getStringValue();
376 if ( path.length() ) {
377 SGPath config = globals->resolve_aircraft_path(path);
379 // load an obsolete xml configuration
380 SG_LOG( SG_SYSTEMS, SG_WARN,
381 "Reading deprecated xml electrical system model from\n "
384 readProperties( config.str(), config_props );
386 if ( build(config_props) ) {
389 SG_LOG( SG_SYSTEMS, SG_ALERT,
390 "Detected a logic error in the electrical system ");
391 SG_LOG( SG_SYSTEMS, SG_ALERT,
392 "specification file. See earlier errors for " );
393 SG_LOG( SG_SYSTEMS, SG_ALERT,
397 } catch (const sg_exception&) {
398 SG_LOG( SG_SYSTEMS, SG_ALERT,
399 "Failed to load electrical system model: "
403 SG_LOG( SG_SYSTEMS, SG_INFO,
404 "No xml-based electrical model specified for this model!");
408 _amps_out->setDoubleValue(0);
414 void FGElectricalSystem::bind () {
418 void FGElectricalSystem::unbind () {
422 void FGElectricalSystem::update (double dt) {
427 // cout << "Updating electrical system, dt = " << dt << endl;
431 // zero out the voltage before we start, but don't clear the
432 // requested load values.
433 for ( i = 0; i < suppliers.size(); ++i ) {
434 suppliers[i]->set_volts( 0.0 );
436 for ( i = 0; i < buses.size(); ++i ) {
437 buses[i]->set_volts( 0.0 );
439 for ( i = 0; i < outputs.size(); ++i ) {
440 outputs[i]->set_volts( 0.0 );
442 for ( i = 0; i < connectors.size(); ++i ) {
443 connectors[i]->set_volts( 0.0 );
446 // for each "external" supplier, propagate the electrical current
447 for ( i = 0; i < suppliers.size(); ++i ) {
448 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
449 if ( node->get_model() == FGElectricalSupplier::FG_EXTERNAL ) {
451 // cout << "Starting propagation: " << suppliers[i]->get_name()
453 load = propagate( suppliers[i], dt,
454 node->get_output_volts(),
455 node->get_output_amps(),
458 if ( node->apply_load( load, dt ) < 0.0 ) {
459 SG_LOG(SG_SYSTEMS, SG_ALERT,
460 "Error drawing more current than available!");
465 // for each "alternator" supplier, propagate the electrical
467 for ( i = 0; i < suppliers.size(); ++i ) {
468 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
469 if ( node->get_model() == FGElectricalSupplier::FG_ALTERNATOR) {
471 // cout << "Starting propagation: " << suppliers[i]->get_name()
473 load = propagate( suppliers[i], dt,
474 node->get_output_volts(),
475 node->get_output_amps(),
478 if ( node->apply_load( load, dt ) < 0.0 ) {
479 SG_LOG(SG_SYSTEMS, SG_ALERT,
480 "Error drawing more current than available!");
485 // for each "battery" supplier, propagate the electrical
487 for ( i = 0; i < suppliers.size(); ++i ) {
488 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
489 if ( node->get_model() == FGElectricalSupplier::FG_BATTERY ) {
491 // cout << "Starting propagation: " << suppliers[i]->get_name()
493 load = propagate( suppliers[i], dt,
494 node->get_output_volts(),
495 node->get_output_amps(),
497 // cout << "battery load = " << load << endl;
499 if ( node->apply_load( load, dt ) < 0.0 ) {
500 SG_LOG(SG_SYSTEMS, SG_ALERT,
501 "Error drawing more current than available!");
507 = fgGetFloat("/systems/electrical/suppliers/alternator") / 60.0;
509 // impliment an extremely simplistic voltage model (assumes
510 // certain naming conventions in electrical system config)
511 // FIXME: we probably want to be able to feed power from all
512 // engines if they are running and the master-alt is switched on
514 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
517 if ( fgGetBool("/controls/engines/engine[0]/master-alt") ) {
518 if ( fgGetFloat("/engines/engine[0]/rpm") > 800 ) {
519 float alt_contrib = 28.0;
520 if ( alt_contrib > volts ) {
523 } else if ( fgGetFloat("/engines/engine[0]/rpm") > 200 ) {
524 float alt_contrib = 20.0;
525 if ( alt_contrib > volts ) {
530 _volts_out->setFloatValue( volts );
532 // impliment an extremely simplistic amps model (assumes certain
533 // naming conventions in the electrical system config) ... FIXME:
534 // make this more generic
536 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
537 if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
538 fgGetFloat("/engines/engine[0]/rpm") > 800 )
540 amps += 40.0 * alt_norm;
542 amps -= 15.0; // normal load
543 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
546 if ( fgGetBool("/controls/switches/nav-lights") ) {
553 _amps_out->setFloatValue( amps );
557 bool FGElectricalSystem::build (SGPropertyNode* config_props) {
558 SGPropertyNode *node;
561 int count = config_props->nChildren();
562 for ( i = 0; i < count; ++i ) {
563 node = config_props->getChild(i);
564 string name = node->getName();
565 // cout << name << endl;
566 if ( name == "supplier" ) {
567 FGElectricalSupplier *s =
568 new FGElectricalSupplier( node );
569 suppliers.push_back( s );
570 } else if ( name == "bus" ) {
572 new FGElectricalBus( node );
573 buses.push_back( b );
574 } else if ( name == "output" ) {
575 FGElectricalOutput *o =
576 new FGElectricalOutput( node );
577 outputs.push_back( o );
578 } else if ( name == "connector" ) {
579 FGElectricalConnector *c =
580 new FGElectricalConnector( node, this );
581 connectors.push_back( c );
583 SG_LOG( SG_SYSTEMS, SG_ALERT, "Unknown component type specified: "
593 // propagate the electrical current through the network, returns the
594 // total current drawn by the children of this node.
595 float FGElectricalSystem::propagate( FGElectricalComponent *node, double dt,
596 float input_volts, float input_amps,
600 float total_load = 0.0;
602 // determine the current to carry forward
604 if ( !fgGetBool("/systems/electrical/serviceable") ) {
606 } else if ( node->get_kind() == FGElectricalComponent::FG_SUPPLIER ) {
607 // cout << s << "is a supplier (" << node->get_name() << ")" << endl;
608 FGElectricalSupplier *supplier = (FGElectricalSupplier *)node;
609 if ( supplier->get_model() == FGElectricalSupplier::FG_BATTERY ) {
610 // cout << s << " (and is a battery)" << endl;
611 float battery_volts = supplier->get_output_volts();
612 if ( battery_volts < (input_volts - 0.1) ) {
613 // special handling of a battery charge condition
614 // cout << s << " (and is being charged) in v = "
615 // << input_volts << " current v = " << battery_volts
617 supplier->apply_load( -supplier->get_charge_amps(), dt );
618 return supplier->get_charge_amps();
622 } else if ( node->get_kind() == FGElectricalComponent::FG_BUS ) {
623 // cout << s << "is a bus (" << node->get_name() << ")" << endl;
625 } else if ( node->get_kind() == FGElectricalComponent::FG_OUTPUT ) {
626 // cout << s << "is an output (" << node->get_name() << ")" << endl;
629 // draw current if we have voltage
630 total_load = node->get_load_amps();
632 } else if ( node->get_kind() == FGElectricalComponent::FG_CONNECTOR ) {
633 // cout << s << "is a connector (" << node->get_name() << ")" << endl;
634 if ( ((FGElectricalConnector *)node)->get_state() ) {
639 // cout << s << " input_volts = " << volts << endl;
641 SG_LOG( SG_SYSTEMS, SG_ALERT, "unknown node type" );
646 // if this node has found a stronger power source, update the
647 // value and propagate to all children
648 if ( volts > node->get_volts() ) {
649 node->set_volts( volts );
650 for ( i = 0; i < node->get_num_outputs(); ++i ) {
651 FGElectricalComponent *child = node->get_output(i);
652 // send current equal to load
653 total_load += propagate( child, dt,
654 volts, child->get_load_amps(),
658 // if not an output node, register the downstream current draw
659 // (sum of all children) with this node. If volts are zero,
660 // current draw should be zero.
661 if ( node->get_kind() != FGElectricalComponent::FG_OUTPUT ) {
662 node->set_load_amps( total_load );
665 node->set_available_amps( input_amps - total_load );
667 // publish values to specified properties
668 for ( i = 0; i < node->get_num_props(); ++i ) {
669 fgSetFloat( node->get_prop(i).c_str(), node->get_volts() );
673 cout << s << node->get_name() << " -> (volts) " << node->get_volts()
675 cout << s << node->get_name() << " -> (load amps) " << total_load
677 cout << s << node->get_name() << " -> (input amps) " << input_amps
679 cout << s << node->get_name() << " -> (extra amps) "
680 << node->get_available_amps() << endl;
685 // cout << s << "no further propagation" << endl;
691 // search for the named component and return a pointer to it, NULL otherwise
692 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
697 for ( i = 0; i < suppliers.size(); ++i ) {
698 s = suppliers[i]->get_name();
699 // cout << " " << s << endl;
706 for ( i = 0; i < buses.size(); ++i ) {
707 s = buses[i]->get_name();
708 // cout << " " << s << endl;
714 // then search outputs
715 for ( i = 0; i < outputs.size(); ++i ) {
716 s = outputs[i]->get_name();
717 // cout << " " << s << endl;