1 // electrical.cxx - a flexible, generic electrical system model.
3 // Written by Curtis Olson, started September 2002.
5 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/misc/sg_path.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
30 #include "electrical.hxx"
33 FGElectricalComponent::FGElectricalComponent() :
42 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
45 // cout << "Creating a supplier" << endl;
46 name = node->getStringValue("name");
47 string _model = node->getStringValue("kind");
48 // cout << "_model = " << _model << endl;
49 if ( _model == "battery" ) {
51 } else if ( _model == "alternator" ) {
52 model = FG_ALTERNATOR;
53 } else if ( _model == "external" ) {
58 volts = node->getDoubleValue("volts");
59 amps = node->getDoubleValue("amps");
60 rpm_src = node->getStringValue("rpm-source");
63 for ( i = 0; i < node->nChildren(); ++i ) {
64 SGPropertyNode *child = node->getChild(i);
65 // cout << " scanning: " << child->getName() << endl;
66 if ( !strcmp(child->getName(), "prop") ) {
67 string prop = child->getStringValue();
68 // cout << " Adding prop = " << prop << endl;
70 fgSetDouble( prop.c_str(), amps );
74 _rpm_node = fgGetNode( rpm_src.c_str(), true);
78 double FGElectricalSupplier::get_output() {
79 if ( model == FG_BATTERY ) {
80 // cout << "battery amps = " << amps << endl;
82 } else if ( model == FG_ALTERNATOR ) {
83 // scale alternator output for rpms < 600. For rpms >= 600
84 // give full output. This is just a WAG, and probably not how
85 // it really works but I'm keeping things "simple" to start.
86 double rpm = _rpm_node->getDoubleValue();
87 double factor = rpm / 600.0;
91 // cout << "alternator amps = " << amps * factor << endl;
93 } else if ( model == FG_EXTERNAL ) {
94 // cout << "external amps = " << 0.0 << endl;
97 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
104 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
107 name = node->getStringValue("name");
109 for ( i = 0; i < node->nChildren(); ++i ) {
110 SGPropertyNode *child = node->getChild(i);
111 if ( !strcmp(child->getName(), "prop") ) {
112 string prop = child->getStringValue();
119 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
121 output_amps = 0.1; // arbitrary default value
123 name = node->getStringValue("name");
124 SGPropertyNode *draw = node->getNode("rated-draw");
125 if ( draw != NULL ) {
126 output_amps = draw->getDoubleValue();
128 // cout << "rated draw = " << output_amps << endl;
131 for ( i = 0; i < node->nChildren(); ++i ) {
132 SGPropertyNode *child = node->getChild(i);
133 if ( !strcmp(child->getName(), "prop") ) {
134 string prop = child->getStringValue();
141 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
144 circuit_breaker( false )
146 bool initial_state = true;
148 for ( i = 0; i < node->nChildren(); ++i ) {
149 SGPropertyNode *child = node->getChild(i);
150 string cname = child->getName();
151 string cval = child->getStringValue();
152 if ( cname == "prop" ) {
153 switch_node = fgGetNode( cval.c_str(), true );
154 // cout << "switch node = " << cval << endl;
155 } else if ( cname == "initial-state" ) {
156 if ( cval == "off" || cval == "false" ) {
157 initial_state = false;
159 // cout << "initial state = " << initial_state << endl;
160 } else if ( cname == "rating-amps" ) {
161 rating_amps = atof( cval.c_str() );
162 circuit_breaker = true;
163 // cout << "initial state = " << initial_state << endl;
167 switch_node->setBoolValue( initial_state );
168 // cout << " value = " << switch_node->getBoolValue() << endl;
172 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
173 FGElectricalSystem *es ) {
177 for ( i = 0; i < node->nChildren(); ++i ) {
178 SGPropertyNode *child = node->getChild(i);
179 string cname = child->getName();
180 string cval = child->getStringValue();
181 // cout << " " << cname << " = " << cval << endl;
182 if ( cname == "input" ) {
183 FGElectricalComponent *s = es->find( child->getStringValue() );
186 if ( s->get_kind() == FG_SUPPLIER ) {
187 s->add_output( this );
188 } else if ( s->get_kind() == FG_BUS ) {
189 s->add_output( this );
191 SG_LOG( SG_ALL, SG_ALERT,
192 "Attempt to connect to something that can't provide an output: "
193 << child->getStringValue() );
196 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
197 << child->getStringValue() );
199 } else if ( cname == "output" ) {
200 FGElectricalComponent *s = es->find( child->getStringValue() );
203 if ( s->get_kind() == FG_BUS ) {
204 s->add_input( this );
205 } else if ( s->get_kind() == FG_OUTPUT ) {
206 s->add_input( this );
208 SG_LOG( SG_ALL, SG_ALERT,
209 "Attempt to connect to something that can't provide an input: "
210 << child->getStringValue() );
213 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
214 << child->getStringValue() );
216 } else if ( cname == "switch" ) {
217 // cout << "Switch = " << child->getStringValue() << endl;
218 FGElectricalSwitch s( child );
225 // set all switches to the specified state
226 void FGElectricalConnector::set_switches( bool state ) {
227 // cout << "setting switch state to " << state << endl;
228 for ( unsigned int i = 0; i < switches.size(); ++i ) {
229 switches[i].set_state( state );
234 // return true if all switches are true, false otherwise. A connector
235 // could have multiple switches, but they all need to be true(closed)
236 // for current to get through.
237 bool FGElectricalConnector::get_state() {
239 for ( i = 0; i < switches.size(); ++i ) {
240 if ( ! switches[i].get_state() ) {
249 FGElectricalSystem::FGElectricalSystem () :
255 FGElectricalSystem::~FGElectricalSystem () {
259 void FGElectricalSystem::init () {
260 config_props = new SGPropertyNode;
262 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
263 _volts_out = fgGetNode( "/systems/electrical/volts", true );
264 _amps_out = fgGetNode( "/systems/electrical/amps", true );
267 SGPath config( globals->get_fg_root() );
268 config.append( path_n->getStringValue() );
270 SG_LOG( SG_ALL, SG_INFO, "Reading electrical system model from "
273 readProperties( config.str(), config_props );
278 SG_LOG( SG_ALL, SG_ALERT,
279 "Detected an internal inconsistancy in the electrical");
280 SG_LOG( SG_ALL, SG_ALERT,
281 " system specification file. See earlier errors for" );
282 SG_LOG( SG_ALL, SG_ALERT,
286 } catch (const sg_exception& exc) {
287 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
292 SG_LOG( SG_ALL, SG_WARN,
293 "No electrical model specified for this model!");
299 void FGElectricalSystem::bind () {
303 void FGElectricalSystem::unbind () {
307 void FGElectricalSystem::update (double dt) {
309 _amps_out->setDoubleValue(0);
313 // cout << "Updating electrical system" << endl;
317 // zero everything out before we start
318 for ( i = 0; i < suppliers.size(); ++i ) {
319 suppliers[i]->set_volts( 0.0 );
320 suppliers[i]->set_load_amps( 0.0 );
322 for ( i = 0; i < buses.size(); ++i ) {
323 buses[i]->set_volts( 0.0 );
324 buses[i]->set_load_amps( 0.0 );
326 for ( i = 0; i < outputs.size(); ++i ) {
327 outputs[i]->set_volts( 0.0 );
328 outputs[i]->set_load_amps( 0.0 );
330 for ( i = 0; i < connectors.size(); ++i ) {
331 connectors[i]->set_volts( 0.0 );
332 connectors[i]->set_load_amps( 0.0 );
335 // for each supplier, propagate the electrical current
336 for ( i = 0; i < suppliers.size(); ++i ) {
337 // cout << " Updating: " << suppliers[i]->get_name() << endl;
338 propagate( suppliers[i], 0.0, " " );
342 = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
344 // impliment an extremely simplistic voltage model (assumes
345 // certain naming conventions in electrical system config)
347 if ( fgGetBool("/controls/switches/master-bat") ) {
350 if ( fgGetBool("/controls/switches/master-alt") &&
351 fgGetDouble("/engines/engine[0]/rpm") > 800 )
353 double alt_contrib = 28.0;
354 if ( alt_contrib > volts ) {
358 _volts_out->setDoubleValue( volts );
360 // impliment an extremely simplistic amps model (assumes certain
361 // naming conventions in the electrical system config) ... FIXME:
362 // make this more generic
364 if ( fgGetBool("/controls/switches/master-bat") ) {
365 if ( fgGetBool("/controls/switches/master-alt") &&
366 fgGetDouble("/engines/engine[0]/rpm") > 800 )
368 amps += 40.0 * alt_norm;
370 amps -= 15.0; // normal load
371 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
374 if ( fgGetBool("/controls/switches/nav-lights") ) {
381 _amps_out->setDoubleValue( amps );
385 bool FGElectricalSystem::build () {
386 SGPropertyNode *node;
389 int count = config_props->nChildren();
390 for ( i = 0; i < count; ++i ) {
391 node = config_props->getChild(i);
392 string name = node->getName();
393 // cout << name << endl;
394 if ( name == "supplier" ) {
395 FGElectricalSupplier *s =
396 new FGElectricalSupplier( node );
397 suppliers.push_back( s );
398 } else if ( name == "bus" ) {
400 new FGElectricalBus( node );
401 buses.push_back( b );
402 } else if ( name == "output" ) {
403 FGElectricalOutput *o =
404 new FGElectricalOutput( node );
405 outputs.push_back( o );
406 } else if ( name == "connector" ) {
407 FGElectricalConnector *c =
408 new FGElectricalConnector( node, this );
409 connectors.push_back( c );
411 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
421 // propagate the electrical current through the network, returns the
422 // total current drawn by the children of this node.
423 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
427 float current_amps = 0.0;
429 // determine the current to carry forward
431 if ( !fgGetBool("/systems/electrical/serviceable") ) {
433 } else if ( node->get_kind() == FG_SUPPLIER ) {
434 // cout << s << " is a supplier" << endl;
435 volts = ((FGElectricalSupplier *)node)->get_output();
436 } else if ( node->get_kind() == FG_BUS ) {
437 // cout << s << " is a bus" << endl;
439 } else if ( node->get_kind() == FG_OUTPUT ) {
440 // cout << s << " is an output" << endl;
443 // draw current if we have voltage
444 current_amps = ((FGElectricalOutput *)node)->get_output_amps();
446 } else if ( node->get_kind() == FG_CONNECTOR ) {
447 // cout << s << " is a connector" << endl;
448 if ( ((FGElectricalConnector *)node)->get_state() ) {
453 // cout << s << " val = " << volts << endl;
455 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
458 if ( volts > node->get_volts() ) {
459 node->set_volts( volts );
464 // publish values to specified properties
465 for ( i = 0; i < node->get_num_props(); ++i ) {
466 fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
468 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
470 // propagate to all children
471 for ( i = 0; i < node->get_num_outputs(); ++i ) {
472 current_amps += propagate( node->get_output(i), volts, s );
475 // if not an output node, register the downstream current draw
476 // with this node. If volts are zero, current draw should be zero.
477 if ( node->get_kind() != FG_OUTPUT ) {
478 node->set_load_amps( current_amps );
480 // cout << s << node->get_name() << " -> " << current_amps << endl;
486 // search for the named component and return a pointer to it, NULL otherwise
487 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
492 for ( i = 0; i < suppliers.size(); ++i ) {
493 s = suppliers[i]->get_name();
494 // cout << " " << s << endl;
501 for ( i = 0; i < buses.size(); ++i ) {
502 s = buses[i]->get_name();
503 // cout << " " << s << endl;
509 // then search outputs
510 for ( i = 0; i < outputs.size(); ++i ) {
511 s = outputs[i]->get_name();
512 // cout << " " << s << endl;