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.
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/props/props_io.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
37 #include "electrical.hxx"
40 FGElectricalComponent::FGElectricalComponent() :
49 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
52 // cout << "Creating a supplier" << endl;
53 name = node->getStringValue("name");
54 string _model = node->getStringValue("kind");
55 // cout << "_model = " << _model << endl;
56 if ( _model == "battery" ) {
58 amp_hours = node->getFloatValue("amp-hours", 40.0);
59 percent_remaining = node->getFloatValue("percent-remaining", 1.0);
60 charge_amps = node->getFloatValue("charge-amps", 7.0);
61 } else if ( _model == "alternator" ) {
62 model = FG_ALTERNATOR;
63 rpm_src = node->getStringValue("rpm-source");
64 rpm_threshold = node->getFloatValue("rpm-threshold", 600.0);
65 ideal_amps = node->getFloatValue("amps", 60.0);
66 } else if ( _model == "external" ) {
68 ideal_amps = node->getFloatValue("amps", 60.0);
72 ideal_volts = node->getFloatValue("volts");
75 for ( i = 0; i < node->nChildren(); ++i ) {
76 SGPropertyNode *child = node->getChild(i);
77 // cout << " scanning: " << child->getName() << endl;
78 if ( !strcmp(child->getName(), "prop") ) {
79 string prop = child->getStringValue();
80 // cout << " Adding prop = " << prop << endl;
82 fgSetFloat( prop.c_str(), ideal_amps );
86 _rpm_node = fgGetNode( rpm_src.c_str(), true);
90 float FGElectricalSupplier::apply_load( float amps, float dt ) {
91 if ( model == FG_BATTERY ) {
92 // calculate amp hours used
93 float amphrs_used = amps * dt / 3600.0;
95 // calculate percent of total available capacity
96 float percent_used = amphrs_used / amp_hours;
97 percent_remaining -= percent_used;
98 if ( percent_remaining < 0.0 ) {
99 percent_remaining = 0.0;
100 } else if ( percent_remaining > 1.0 ) {
101 percent_remaining = 1.0;
103 // cout << "battery percent = " << percent_remaining << endl;
104 return amp_hours * percent_remaining;
105 } else if ( model == FG_ALTERNATOR ) {
106 // scale alternator output for rpms < 600. For rpms >= 600
107 // give full output. This is just a WAG, and probably not how
108 // it really works but I'm keeping things "simple" to start.
109 float rpm = _rpm_node->getFloatValue();
110 float factor = rpm / rpm_threshold;
111 if ( factor > 1.0 ) {
114 // cout << "alternator amps = " << amps * factor << endl;
115 float available_amps = ideal_amps * factor;
116 return available_amps - amps;
117 } else if ( model == FG_EXTERNAL ) {
118 // cout << "external amps = " << 0.0 << endl;
119 float available_amps = ideal_amps;
120 return available_amps - amps;
122 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
129 float FGElectricalSupplier::get_output_volts() {
130 if ( model == FG_BATTERY ) {
131 // cout << "battery amps = " << amps << endl;
132 float x = 1.0 - percent_remaining;
133 float tmp = -(3.0 * x - 1.0);
134 float factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
135 // cout << "battery % = " << percent_remaining <<
136 // " factor = " << factor << endl;
137 // percent_remaining -= 0.001;
138 return ideal_volts * factor;
139 } else if ( model == FG_ALTERNATOR ) {
140 // scale alternator output for rpms < 600. For rpms >= 600
141 // give full output. This is just a WAG, and probably not how
142 // it really works but I'm keeping things "simple" to start.
143 float rpm = _rpm_node->getFloatValue();
144 float factor = rpm / rpm_threshold;
145 if ( factor > 1.0 ) {
148 // cout << "alternator amps = " << amps * factor << endl;
149 return ideal_volts * factor;
150 } else if ( model == FG_EXTERNAL ) {
151 // cout << "external amps = " << 0.0 << endl;
154 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
161 float FGElectricalSupplier::get_output_amps() {
162 if ( model == FG_BATTERY ) {
163 // cout << "battery amp_hours = " << amp_hours << endl;
165 // This is a WAG, but produce enough amps to burn the entire
166 // battery in one minute.
167 return amp_hours * 60.0;
168 } else if ( model == FG_ALTERNATOR ) {
169 // scale alternator output for rpms < 600. For rpms >= 600
170 // give full output. This is just a WAG, and probably not how
171 // it really works but I'm keeping things "simple" to start.
172 float rpm = _rpm_node->getFloatValue();
173 float factor = rpm / rpm_threshold;
174 if ( factor > 1.0 ) {
177 // cout << "alternator amps = " << ideal_amps * factor << endl;
178 return ideal_amps * factor;
179 } else if ( model == FG_EXTERNAL ) {
180 // cout << "external amps = " << 0.0 << endl;
183 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
190 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
193 name = node->getStringValue("name");
195 for ( i = 0; i < node->nChildren(); ++i ) {
196 SGPropertyNode *child = node->getChild(i);
197 if ( !strcmp(child->getName(), "prop") ) {
198 string prop = child->getStringValue();
205 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
207 load_amps = 0.1; // arbitrary default value
209 name = node->getStringValue("name");
210 SGPropertyNode *draw = node->getNode("rated-draw");
211 if ( draw != NULL ) {
212 load_amps = draw->getFloatValue();
214 // cout << "rated draw = " << output_amps << endl;
217 for ( i = 0; i < node->nChildren(); ++i ) {
218 SGPropertyNode *child = node->getChild(i);
219 if ( !strcmp(child->getName(), "prop") ) {
220 string prop = child->getStringValue();
227 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
230 circuit_breaker( false )
232 bool initial_state = true;
234 for ( i = 0; i < node->nChildren(); ++i ) {
235 SGPropertyNode *child = node->getChild(i);
236 string cname = child->getName();
237 string cval = child->getStringValue();
238 if ( cname == "prop" ) {
239 switch_node = fgGetNode( cval.c_str(), true );
240 // cout << "switch node = " << cval << endl;
241 } else if ( cname == "initial-state" ) {
242 if ( cval == "off" || cval == "false" ) {
243 initial_state = false;
245 // cout << "initial state = " << initial_state << endl;
246 } else if ( cname == "rating-amps" ) {
247 rating_amps = atof( cval.c_str() );
248 circuit_breaker = true;
249 // cout << "initial state = " << initial_state << endl;
253 switch_node->setBoolValue( initial_state );
254 // cout << " value = " << switch_node->getBoolValue() << endl;
258 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
259 FGElectricalSystem *es ) {
263 for ( i = 0; i < node->nChildren(); ++i ) {
264 SGPropertyNode *child = node->getChild(i);
265 string cname = child->getName();
266 string cval = child->getStringValue();
267 // cout << " " << cname << " = " << cval << endl;
268 if ( cname == "input" ) {
269 FGElectricalComponent *s = es->find( child->getStringValue() );
272 if ( s->get_kind() == FG_SUPPLIER ) {
273 s->add_output( this );
274 } else if ( s->get_kind() == FG_BUS ) {
275 s->add_output( this );
277 SG_LOG( SG_ALL, SG_ALERT,
278 "Attempt to connect to something that can't provide an output: "
279 << child->getStringValue() );
282 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
283 << child->getStringValue() );
285 } else if ( cname == "output" ) {
286 FGElectricalComponent *s = es->find( child->getStringValue() );
289 if ( s->get_kind() == FG_BUS ) {
290 s->add_input( this );
291 } else if ( s->get_kind() == FG_OUTPUT ) {
292 s->add_input( this );
293 } else if ( s->get_kind() == FG_SUPPLIER &&
294 ((FGElectricalSupplier *)s)->get_model()
295 == FGElectricalSupplier::FG_BATTERY ) {
296 s->add_output( this );
298 SG_LOG( SG_ALL, SG_ALERT,
299 "Attempt to connect to something that can't provide an input: "
300 << child->getStringValue() );
303 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
304 << child->getStringValue() );
306 } else if ( cname == "switch" ) {
307 // cout << "Switch = " << child->getStringValue() << endl;
308 FGElectricalSwitch s( child );
315 // set all switches to the specified state
316 void FGElectricalConnector::set_switches( bool state ) {
317 // cout << "setting switch state to " << state << endl;
318 for ( unsigned int i = 0; i < switches.size(); ++i ) {
319 switches[i].set_state( state );
324 // return true if all switches are true, false otherwise. A connector
325 // could have multiple switches, but they all need to be true(closed)
326 // for current to get through.
327 bool FGElectricalConnector::get_state() {
329 for ( i = 0; i < switches.size(); ++i ) {
330 if ( ! switches[i].get_state() ) {
339 FGElectricalSystem::FGElectricalSystem ( SGPropertyNode *node ) :
340 name(node->getStringValue("name", "electrical")),
341 num(node->getIntValue("number", 0)),
342 path(node->getStringValue("path")),
348 FGElectricalSystem::~FGElectricalSystem () {
352 void FGElectricalSystem::init () {
353 SGPropertyNode_ptr config_props = new SGPropertyNode;
355 _volts_out = fgGetNode( "/systems/electrical/volts", true );
356 _amps_out = fgGetNode( "/systems/electrical/amps", true );
358 // allow the electrical system to be specified via the
359 // aircraft-set.xml file (for backwards compatibility) or through
360 // the aircraft-systems.xml file. If a -set.xml entry is
361 // specified, that overrides the system entry.
362 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
364 if ( path.length() ) {
365 SG_LOG( SG_ALL, SG_INFO,
366 "NOTICE: System manager configuration specifies an " <<
367 "electrical system: " << path << " but it is " <<
368 "being overridden by the one specified in the -set.xml " <<
369 "file: " << path_n->getStringValue() );
372 path = path_n->getStringValue();
375 if ( path.length() ) {
376 SGPath config = globals->resolve_aircraft_path(path);
378 // load an obsolete xml configuration
379 SG_LOG( SG_ALL, SG_WARN,
380 "Reading deprecated xml electrical system model from\n "
383 readProperties( config.str(), config_props );
385 if ( build(config_props) ) {
388 SG_LOG( SG_ALL, SG_ALERT,
389 "Detected a logic error in the electrical system ");
390 SG_LOG( SG_ALL, SG_ALERT,
391 "specification file. See earlier errors for " );
392 SG_LOG( SG_ALL, SG_ALERT,
396 } catch (const sg_exception&) {
397 SG_LOG( SG_ALL, SG_ALERT,
398 "Failed to load electrical system model: "
402 SG_LOG( SG_ALL, SG_INFO,
403 "No xml-based electrical model specified for this model!");
407 _amps_out->setDoubleValue(0);
413 void FGElectricalSystem::bind () {
417 void FGElectricalSystem::unbind () {
421 void FGElectricalSystem::update (double dt) {
426 // cout << "Updating electrical system, dt = " << dt << endl;
430 // zero out the voltage before we start, but don't clear the
431 // requested load values.
432 for ( i = 0; i < suppliers.size(); ++i ) {
433 suppliers[i]->set_volts( 0.0 );
435 for ( i = 0; i < buses.size(); ++i ) {
436 buses[i]->set_volts( 0.0 );
438 for ( i = 0; i < outputs.size(); ++i ) {
439 outputs[i]->set_volts( 0.0 );
441 for ( i = 0; i < connectors.size(); ++i ) {
442 connectors[i]->set_volts( 0.0 );
445 // for each "external" supplier, propagate the electrical current
446 for ( i = 0; i < suppliers.size(); ++i ) {
447 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
448 if ( node->get_model() == FGElectricalSupplier::FG_EXTERNAL ) {
450 // cout << "Starting propagation: " << suppliers[i]->get_name()
452 load = propagate( suppliers[i], dt,
453 node->get_output_volts(),
454 node->get_output_amps(),
457 if ( node->apply_load( load, dt ) < 0.0 ) {
458 SG_LOG(SG_ALL, SG_ALERT,
459 "Error drawing more current than available!");
464 // for each "alternator" supplier, propagate the electrical
466 for ( i = 0; i < suppliers.size(); ++i ) {
467 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
468 if ( node->get_model() == FGElectricalSupplier::FG_ALTERNATOR) {
470 // cout << "Starting propagation: " << suppliers[i]->get_name()
472 load = propagate( suppliers[i], dt,
473 node->get_output_volts(),
474 node->get_output_amps(),
477 if ( node->apply_load( load, dt ) < 0.0 ) {
478 SG_LOG(SG_ALL, SG_ALERT,
479 "Error drawing more current than available!");
484 // for each "battery" supplier, propagate the electrical
486 for ( i = 0; i < suppliers.size(); ++i ) {
487 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
488 if ( node->get_model() == FGElectricalSupplier::FG_BATTERY ) {
490 // cout << "Starting propagation: " << suppliers[i]->get_name()
492 load = propagate( suppliers[i], dt,
493 node->get_output_volts(),
494 node->get_output_amps(),
496 // cout << "battery load = " << load << endl;
498 if ( node->apply_load( load, dt ) < 0.0 ) {
499 SG_LOG(SG_ALL, SG_ALERT,
500 "Error drawing more current than available!");
506 = fgGetFloat("/systems/electrical/suppliers/alternator") / 60.0;
508 // impliment an extremely simplistic voltage model (assumes
509 // certain naming conventions in electrical system config)
510 // FIXME: we probably want to be able to feed power from all
511 // engines if they are running and the master-alt is switched on
513 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
516 if ( fgGetBool("/controls/engines/engine[0]/master-alt") ) {
517 if ( fgGetFloat("/engines/engine[0]/rpm") > 800 ) {
518 float alt_contrib = 28.0;
519 if ( alt_contrib > volts ) {
522 } else if ( fgGetFloat("/engines/engine[0]/rpm") > 200 ) {
523 float alt_contrib = 20.0;
524 if ( alt_contrib > volts ) {
529 _volts_out->setFloatValue( volts );
531 // impliment an extremely simplistic amps model (assumes certain
532 // naming conventions in the electrical system config) ... FIXME:
533 // make this more generic
535 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
536 if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
537 fgGetFloat("/engines/engine[0]/rpm") > 800 )
539 amps += 40.0 * alt_norm;
541 amps -= 15.0; // normal load
542 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
545 if ( fgGetBool("/controls/switches/nav-lights") ) {
552 _amps_out->setFloatValue( amps );
556 bool FGElectricalSystem::build (SGPropertyNode* config_props) {
557 SGPropertyNode *node;
560 int count = config_props->nChildren();
561 for ( i = 0; i < count; ++i ) {
562 node = config_props->getChild(i);
563 string name = node->getName();
564 // cout << name << endl;
565 if ( name == "supplier" ) {
566 FGElectricalSupplier *s =
567 new FGElectricalSupplier( node );
568 suppliers.push_back( s );
569 } else if ( name == "bus" ) {
571 new FGElectricalBus( node );
572 buses.push_back( b );
573 } else if ( name == "output" ) {
574 FGElectricalOutput *o =
575 new FGElectricalOutput( node );
576 outputs.push_back( o );
577 } else if ( name == "connector" ) {
578 FGElectricalConnector *c =
579 new FGElectricalConnector( node, this );
580 connectors.push_back( c );
582 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
592 // propagate the electrical current through the network, returns the
593 // total current drawn by the children of this node.
594 float FGElectricalSystem::propagate( FGElectricalComponent *node, double dt,
595 float input_volts, float input_amps,
599 float total_load = 0.0;
601 // determine the current to carry forward
603 if ( !fgGetBool("/systems/electrical/serviceable") ) {
605 } else if ( node->get_kind() == FGElectricalComponent::FG_SUPPLIER ) {
606 // cout << s << "is a supplier (" << node->get_name() << ")" << endl;
607 FGElectricalSupplier *supplier = (FGElectricalSupplier *)node;
608 if ( supplier->get_model() == FGElectricalSupplier::FG_BATTERY ) {
609 // cout << s << " (and is a battery)" << endl;
610 float battery_volts = supplier->get_output_volts();
611 if ( battery_volts < (input_volts - 0.1) ) {
612 // special handling of a battery charge condition
613 // cout << s << " (and is being charged) in v = "
614 // << input_volts << " current v = " << battery_volts
616 supplier->apply_load( -supplier->get_charge_amps(), dt );
617 return supplier->get_charge_amps();
621 } else if ( node->get_kind() == FGElectricalComponent::FG_BUS ) {
622 // cout << s << "is a bus (" << node->get_name() << ")" << endl;
624 } else if ( node->get_kind() == FGElectricalComponent::FG_OUTPUT ) {
625 // cout << s << "is an output (" << node->get_name() << ")" << endl;
628 // draw current if we have voltage
629 total_load = node->get_load_amps();
631 } else if ( node->get_kind() == FGElectricalComponent::FG_CONNECTOR ) {
632 // cout << s << "is a connector (" << node->get_name() << ")" << endl;
633 if ( ((FGElectricalConnector *)node)->get_state() ) {
638 // cout << s << " input_volts = " << volts << endl;
640 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
645 // if this node has found a stronger power source, update the
646 // value and propagate to all children
647 if ( volts > node->get_volts() ) {
648 node->set_volts( volts );
649 for ( i = 0; i < node->get_num_outputs(); ++i ) {
650 FGElectricalComponent *child = node->get_output(i);
651 // send current equal to load
652 total_load += propagate( child, dt,
653 volts, child->get_load_amps(),
657 // if not an output node, register the downstream current draw
658 // (sum of all children) with this node. If volts are zero,
659 // current draw should be zero.
660 if ( node->get_kind() != FGElectricalComponent::FG_OUTPUT ) {
661 node->set_load_amps( total_load );
664 node->set_available_amps( input_amps - total_load );
666 // publish values to specified properties
667 for ( i = 0; i < node->get_num_props(); ++i ) {
668 fgSetFloat( node->get_prop(i).c_str(), node->get_volts() );
672 cout << s << node->get_name() << " -> (volts) " << node->get_volts()
674 cout << s << node->get_name() << " -> (load amps) " << total_load
676 cout << s << node->get_name() << " -> (input amps) " << input_amps
678 cout << s << node->get_name() << " -> (extra amps) "
679 << node->get_available_amps() << endl;
684 // cout << s << "no further propagation" << endl;
690 // search for the named component and return a pointer to it, NULL otherwise
691 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
696 for ( i = 0; i < suppliers.size(); ++i ) {
697 s = suppliers[i]->get_name();
698 // cout << " " << s << endl;
705 for ( i = 0; i < buses.size(); ++i ) {
706 s = buses[i]->get_name();
707 // cout << " " << s << endl;
713 // then search outputs
714 for ( i = 0; i < outputs.size(); ++i ) {
715 s = outputs[i]->get_name();
716 // cout << " " << s << endl;