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.
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/debug/logstream.hxx>
31 #include <Main/fg_props.hxx>
32 #include <Main/globals.hxx>
34 #include "electrical.hxx"
37 FGElectricalComponent::FGElectricalComponent() :
46 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
49 // cout << "Creating a supplier" << endl;
50 name = node->getStringValue("name");
51 string _model = node->getStringValue("kind");
52 // cout << "_model = " << _model << endl;
53 if ( _model == "battery" ) {
55 amp_hours = node->getFloatValue("amp-hours", 40.0);
56 percent_remaining = node->getFloatValue("percent-remaining", 1.0);
57 charge_amps = node->getFloatValue("charge-amps", 7.0);
58 } else if ( _model == "alternator" ) {
59 model = FG_ALTERNATOR;
60 rpm_src = node->getStringValue("rpm-source");
61 rpm_threshold = node->getFloatValue("rpm-threshold", 600.0);
62 ideal_amps = node->getFloatValue("amps", 60.0);
63 } else if ( _model == "external" ) {
65 ideal_amps = node->getFloatValue("amps", 60.0);
69 ideal_volts = node->getFloatValue("volts");
72 for ( i = 0; i < node->nChildren(); ++i ) {
73 SGPropertyNode *child = node->getChild(i);
74 // cout << " scanning: " << child->getName() << endl;
75 if ( !strcmp(child->getName(), "prop") ) {
76 string prop = child->getStringValue();
77 // cout << " Adding prop = " << prop << endl;
79 fgSetFloat( prop.c_str(), ideal_amps );
83 _rpm_node = fgGetNode( rpm_src.c_str(), true);
87 float FGElectricalSupplier::apply_load( float amps, float dt ) {
88 if ( model == FG_BATTERY ) {
89 // calculate amp hours used
90 float amphrs_used = amps * dt / 3600.0;
92 // calculate percent of total available capacity
93 float percent_used = amphrs_used / amp_hours;
94 percent_remaining -= percent_used;
95 if ( percent_remaining < 0.0 ) {
96 percent_remaining = 0.0;
97 } else if ( percent_remaining > 1.0 ) {
98 percent_remaining = 1.0;
100 // cout << "battery percent = " << percent_remaining << endl;
101 return amp_hours * percent_remaining;
102 } else if ( model == FG_ALTERNATOR ) {
103 // scale alternator output for rpms < 600. For rpms >= 600
104 // give full output. This is just a WAG, and probably not how
105 // it really works but I'm keeping things "simple" to start.
106 float rpm = _rpm_node->getFloatValue();
107 float factor = rpm / rpm_threshold;
108 if ( factor > 1.0 ) {
111 // cout << "alternator amps = " << amps * factor << endl;
112 float available_amps = ideal_amps * factor;
113 return available_amps - amps;
114 } else if ( model == FG_EXTERNAL ) {
115 // cout << "external amps = " << 0.0 << endl;
116 float available_amps = ideal_amps;
117 return available_amps - amps;
119 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
126 float FGElectricalSupplier::get_output_volts() {
127 if ( model == FG_BATTERY ) {
128 // cout << "battery amps = " << amps << endl;
129 float x = 1.0 - percent_remaining;
130 float tmp = -(3.0 * x - 1.0);
131 float factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
132 // cout << "battery % = " << percent_remaining <<
133 // " factor = " << factor << endl;
134 // percent_remaining -= 0.001;
135 return ideal_volts * factor;
136 } else if ( model == FG_ALTERNATOR ) {
137 // scale alternator output for rpms < 600. For rpms >= 600
138 // give full output. This is just a WAG, and probably not how
139 // it really works but I'm keeping things "simple" to start.
140 float rpm = _rpm_node->getFloatValue();
141 float factor = rpm / rpm_threshold;
142 if ( factor > 1.0 ) {
145 // cout << "alternator amps = " << amps * factor << endl;
146 return ideal_volts * factor;
147 } else if ( model == FG_EXTERNAL ) {
148 // cout << "external amps = " << 0.0 << endl;
151 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
158 float FGElectricalSupplier::get_output_amps() {
159 if ( model == FG_BATTERY ) {
160 // cout << "battery amp_hours = " << amp_hours << endl;
162 // This is a WAG, but produce enough amps to burn the entire
163 // battery in one minute.
164 return amp_hours * 60.0;
165 } else if ( model == FG_ALTERNATOR ) {
166 // scale alternator output for rpms < 600. For rpms >= 600
167 // give full output. This is just a WAG, and probably not how
168 // it really works but I'm keeping things "simple" to start.
169 float rpm = _rpm_node->getFloatValue();
170 float factor = rpm / rpm_threshold;
171 if ( factor > 1.0 ) {
174 // cout << "alternator amps = " << ideal_amps * factor << endl;
175 return ideal_amps * factor;
176 } else if ( model == FG_EXTERNAL ) {
177 // cout << "external amps = " << 0.0 << endl;
180 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
187 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
190 name = node->getStringValue("name");
192 for ( i = 0; i < node->nChildren(); ++i ) {
193 SGPropertyNode *child = node->getChild(i);
194 if ( !strcmp(child->getName(), "prop") ) {
195 string prop = child->getStringValue();
202 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
204 load_amps = 0.1; // arbitrary default value
206 name = node->getStringValue("name");
207 SGPropertyNode *draw = node->getNode("rated-draw");
208 if ( draw != NULL ) {
209 load_amps = draw->getFloatValue();
211 // cout << "rated draw = " << output_amps << endl;
214 for ( i = 0; i < node->nChildren(); ++i ) {
215 SGPropertyNode *child = node->getChild(i);
216 if ( !strcmp(child->getName(), "prop") ) {
217 string prop = child->getStringValue();
224 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
227 circuit_breaker( false )
229 bool initial_state = true;
231 for ( i = 0; i < node->nChildren(); ++i ) {
232 SGPropertyNode *child = node->getChild(i);
233 string cname = child->getName();
234 string cval = child->getStringValue();
235 if ( cname == "prop" ) {
236 switch_node = fgGetNode( cval.c_str(), true );
237 // cout << "switch node = " << cval << endl;
238 } else if ( cname == "initial-state" ) {
239 if ( cval == "off" || cval == "false" ) {
240 initial_state = false;
242 // cout << "initial state = " << initial_state << endl;
243 } else if ( cname == "rating-amps" ) {
244 rating_amps = atof( cval.c_str() );
245 circuit_breaker = true;
246 // cout << "initial state = " << initial_state << endl;
250 switch_node->setBoolValue( initial_state );
251 // cout << " value = " << switch_node->getBoolValue() << endl;
255 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
256 FGElectricalSystem *es ) {
260 for ( i = 0; i < node->nChildren(); ++i ) {
261 SGPropertyNode *child = node->getChild(i);
262 string cname = child->getName();
263 string cval = child->getStringValue();
264 // cout << " " << cname << " = " << cval << endl;
265 if ( cname == "input" ) {
266 FGElectricalComponent *s = es->find( child->getStringValue() );
269 if ( s->get_kind() == FG_SUPPLIER ) {
270 s->add_output( this );
271 } else if ( s->get_kind() == FG_BUS ) {
272 s->add_output( this );
274 SG_LOG( SG_ALL, SG_ALERT,
275 "Attempt to connect to something that can't provide an output: "
276 << child->getStringValue() );
279 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
280 << child->getStringValue() );
282 } else if ( cname == "output" ) {
283 FGElectricalComponent *s = es->find( child->getStringValue() );
286 if ( s->get_kind() == FG_BUS ) {
287 s->add_input( this );
288 } else if ( s->get_kind() == FG_OUTPUT ) {
289 s->add_input( this );
290 } else if ( s->get_kind() == FG_SUPPLIER &&
291 ((FGElectricalSupplier *)s)->get_model()
292 == FGElectricalSupplier::FG_BATTERY ) {
293 s->add_output( this );
295 SG_LOG( SG_ALL, SG_ALERT,
296 "Attempt to connect to something that can't provide an input: "
297 << child->getStringValue() );
300 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
301 << child->getStringValue() );
303 } else if ( cname == "switch" ) {
304 // cout << "Switch = " << child->getStringValue() << endl;
305 FGElectricalSwitch s( child );
312 // set all switches to the specified state
313 void FGElectricalConnector::set_switches( bool state ) {
314 // cout << "setting switch state to " << state << endl;
315 for ( unsigned int i = 0; i < switches.size(); ++i ) {
316 switches[i].set_state( state );
321 // return true if all switches are true, false otherwise. A connector
322 // could have multiple switches, but they all need to be true(closed)
323 // for current to get through.
324 bool FGElectricalConnector::get_state() {
326 for ( i = 0; i < switches.size(); ++i ) {
327 if ( ! switches[i].get_state() ) {
336 FGElectricalSystem::FGElectricalSystem ( SGPropertyNode *node ) :
337 name(node->getStringValue("name", "electrical")),
338 num(node->getIntValue("number", 0)),
339 path(node->getStringValue("path")),
345 FGElectricalSystem::~FGElectricalSystem () {
349 void FGElectricalSystem::init () {
350 config_props = new SGPropertyNode;
352 _volts_out = fgGetNode( "/systems/electrical/volts", true );
353 _amps_out = fgGetNode( "/systems/electrical/amps", true );
355 // allow the electrical system to be specified via the
356 // aircraft-set.xml file (for backwards compatibility) or through
357 // the aircraft-systems.xml file. If a -set.xml entry is
358 // specified, that overrides the system entry.
359 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
361 if ( path.length() ) {
362 SG_LOG( SG_ALL, SG_INFO,
363 "NOTICE: System manager configuration specifies an " <<
364 "electrical system: " << path << " but it is " <<
365 "being overridden by the one specified in the -set.xml " <<
366 "file: " << path_n->getStringValue() );
369 path = path_n->getStringValue();
372 if ( path.length() ) {
373 SGPath config( globals->get_fg_root() );
374 config.append( path );
376 // load an obsolete xml configuration
377 SG_LOG( SG_ALL, SG_WARN,
378 "Reading deprecated xml electrical system model from\n "
381 readProperties( config.str(), config_props );
386 SG_LOG( SG_ALL, SG_ALERT,
387 "Detected a logic error in the electrical system ");
388 SG_LOG( SG_ALL, SG_ALERT,
389 "specification file. See earlier errors for " );
390 SG_LOG( SG_ALL, SG_ALERT,
394 } catch (const sg_exception& exc) {
395 SG_LOG( SG_ALL, SG_ALERT,
396 "Failed to load electrical system model: "
400 SG_LOG( SG_ALL, SG_INFO,
401 "No xml-based electrical model specified for this model!");
405 _amps_out->setDoubleValue(0);
412 void FGElectricalSystem::bind () {
416 void FGElectricalSystem::unbind () {
420 void FGElectricalSystem::update (double dt) {
425 // cout << "Updating electrical system, dt = " << dt << endl;
429 // zero out the voltage before we start, but don't clear the
430 // requested load values.
431 for ( i = 0; i < suppliers.size(); ++i ) {
432 suppliers[i]->set_volts( 0.0 );
434 for ( i = 0; i < buses.size(); ++i ) {
435 buses[i]->set_volts( 0.0 );
437 for ( i = 0; i < outputs.size(); ++i ) {
438 outputs[i]->set_volts( 0.0 );
440 for ( i = 0; i < connectors.size(); ++i ) {
441 connectors[i]->set_volts( 0.0 );
444 // for each "external" supplier, propagate the electrical current
445 for ( i = 0; i < suppliers.size(); ++i ) {
446 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
447 if ( node->get_model() == FGElectricalSupplier::FG_EXTERNAL ) {
449 // cout << "Starting propagation: " << suppliers[i]->get_name()
451 load = propagate( suppliers[i], dt,
452 node->get_output_volts(),
453 node->get_output_amps(),
456 if ( node->apply_load( load, dt ) < 0.0 ) {
457 SG_LOG(SG_ALL, SG_ALERT,
458 "Error drawing more current than available!");
463 // for each "alternator" supplier, propagate the electrical
465 for ( i = 0; i < suppliers.size(); ++i ) {
466 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
467 if ( node->get_model() == FGElectricalSupplier::FG_ALTERNATOR) {
469 // cout << "Starting propagation: " << suppliers[i]->get_name()
471 load = propagate( suppliers[i], dt,
472 node->get_output_volts(),
473 node->get_output_amps(),
476 if ( node->apply_load( load, dt ) < 0.0 ) {
477 SG_LOG(SG_ALL, SG_ALERT,
478 "Error drawing more current than available!");
483 // for each "battery" supplier, propagate the electrical
485 for ( i = 0; i < suppliers.size(); ++i ) {
486 FGElectricalSupplier *node = (FGElectricalSupplier *)suppliers[i];
487 if ( node->get_model() == FGElectricalSupplier::FG_BATTERY ) {
489 // cout << "Starting propagation: " << suppliers[i]->get_name()
491 load = propagate( suppliers[i], dt,
492 node->get_output_volts(),
493 node->get_output_amps(),
495 // cout << "battery load = " << load << endl;
497 if ( node->apply_load( load, dt ) < 0.0 ) {
498 SG_LOG(SG_ALL, SG_ALERT,
499 "Error drawing more current than available!");
505 = fgGetFloat("/systems/electrical/suppliers/alternator") / 60.0;
507 // impliment an extremely simplistic voltage model (assumes
508 // certain naming conventions in electrical system config)
509 // FIXME: we probably want to be able to feed power from all
510 // engines if they are running and the master-alt is switched on
512 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
515 if ( fgGetBool("/controls/engines/engine[0]/master-alt") ) {
516 if ( fgGetFloat("/engines/engine[0]/rpm") > 800 ) {
517 float alt_contrib = 28.0;
518 if ( alt_contrib > volts ) {
521 } else if ( fgGetFloat("/engines/engine[0]/rpm") > 200 ) {
522 float alt_contrib = 20.0;
523 if ( alt_contrib > volts ) {
528 _volts_out->setFloatValue( volts );
530 // impliment an extremely simplistic amps model (assumes certain
531 // naming conventions in the electrical system config) ... FIXME:
532 // make this more generic
534 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
535 if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
536 fgGetFloat("/engines/engine[0]/rpm") > 800 )
538 amps += 40.0 * alt_norm;
540 amps -= 15.0; // normal load
541 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
544 if ( fgGetBool("/controls/switches/nav-lights") ) {
551 _amps_out->setFloatValue( amps );
555 bool FGElectricalSystem::build () {
556 SGPropertyNode *node;
559 int count = config_props->nChildren();
560 for ( i = 0; i < count; ++i ) {
561 node = config_props->getChild(i);
562 string name = node->getName();
563 // cout << name << endl;
564 if ( name == "supplier" ) {
565 FGElectricalSupplier *s =
566 new FGElectricalSupplier( node );
567 suppliers.push_back( s );
568 } else if ( name == "bus" ) {
570 new FGElectricalBus( node );
571 buses.push_back( b );
572 } else if ( name == "output" ) {
573 FGElectricalOutput *o =
574 new FGElectricalOutput( node );
575 outputs.push_back( o );
576 } else if ( name == "connector" ) {
577 FGElectricalConnector *c =
578 new FGElectricalConnector( node, this );
579 connectors.push_back( c );
581 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
591 // propagate the electrical current through the network, returns the
592 // total current drawn by the children of this node.
593 float FGElectricalSystem::propagate( FGElectricalComponent *node, double dt,
594 float input_volts, float input_amps,
598 float total_load = 0.0;
600 // determine the current to carry forward
602 if ( !fgGetBool("/systems/electrical/serviceable") ) {
604 } else if ( node->get_kind() == FGElectricalComponent::FG_SUPPLIER ) {
605 // cout << s << "is a supplier (" << node->get_name() << ")" << endl;
606 FGElectricalSupplier *supplier = (FGElectricalSupplier *)node;
607 if ( supplier->get_model() == FGElectricalSupplier::FG_BATTERY ) {
608 // cout << s << " (and is a battery)" << endl;
609 float battery_volts = supplier->get_output_volts();
610 if ( battery_volts < (input_volts - 0.1) ) {
611 // special handling of a battery charge condition
612 // cout << s << " (and is being charged) in v = "
613 // << input_volts << " current v = " << battery_volts
615 supplier->apply_load( -supplier->get_charge_amps(), dt );
616 return supplier->get_charge_amps();
620 } else if ( node->get_kind() == FGElectricalComponent::FG_BUS ) {
621 // cout << s << "is a bus (" << node->get_name() << ")" << endl;
623 } else if ( node->get_kind() == FGElectricalComponent::FG_OUTPUT ) {
624 // cout << s << "is an output (" << node->get_name() << ")" << endl;
627 // draw current if we have voltage
628 total_load = node->get_load_amps();
630 } else if ( node->get_kind() == FGElectricalComponent::FG_CONNECTOR ) {
631 // cout << s << "is a connector (" << node->get_name() << ")" << endl;
632 if ( ((FGElectricalConnector *)node)->get_state() ) {
637 // cout << s << " input_volts = " << volts << endl;
639 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
644 // if this node has found a stronger power source, update the
645 // value and propagate to all children
646 if ( volts > node->get_volts() ) {
647 node->set_volts( volts );
648 for ( i = 0; i < node->get_num_outputs(); ++i ) {
649 FGElectricalComponent *child = node->get_output(i);
650 // send current equal to load
651 total_load += propagate( child, dt,
652 volts, child->get_load_amps(),
656 // if not an output node, register the downstream current draw
657 // (sum of all children) with this node. If volts are zero,
658 // current draw should be zero.
659 if ( node->get_kind() != FGElectricalComponent::FG_OUTPUT ) {
660 node->set_load_amps( total_load );
663 node->set_available_amps( input_amps - total_load );
665 // publish values to specified properties
666 for ( i = 0; i < node->get_num_props(); ++i ) {
667 fgSetFloat( node->get_prop(i).c_str(), node->get_volts() );
671 cout << s << node->get_name() << " -> (volts) " << node->get_volts()
673 cout << s << node->get_name() << " -> (load amps) " << total_load
675 cout << s << node->get_name() << " -> (input amps) " << input_amps
677 cout << s << node->get_name() << " -> (extra amps) "
678 << node->get_available_amps() << endl;
683 // cout << s << "no further propagation" << endl;
689 // search for the named component and return a pointer to it, NULL otherwise
690 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
695 for ( i = 0; i < suppliers.size(); ++i ) {
696 s = suppliers[i]->get_name();
697 // cout << " " << s << endl;
704 for ( i = 0; i < buses.size(); ++i ) {
705 s = buses[i]->get_name();
706 // cout << " " << s << endl;
712 // then search outputs
713 for ( i = 0; i < outputs.size(); ++i ) {
714 s = outputs[i]->get_name();
715 // cout << " " << s << endl;