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!");
300 void FGElectricalSystem::bind () {
304 void FGElectricalSystem::unbind () {
308 void FGElectricalSystem::update (double dt) {
310 _amps_out->setDoubleValue(0);
314 // cout << "Updating electrical system" << endl;
318 // zero everything out before we start
319 for ( i = 0; i < suppliers.size(); ++i ) {
320 suppliers[i]->set_volts( 0.0 );
321 suppliers[i]->set_load_amps( 0.0 );
323 for ( i = 0; i < buses.size(); ++i ) {
324 buses[i]->set_volts( 0.0 );
325 buses[i]->set_load_amps( 0.0 );
327 for ( i = 0; i < outputs.size(); ++i ) {
328 outputs[i]->set_volts( 0.0 );
329 outputs[i]->set_load_amps( 0.0 );
331 for ( i = 0; i < connectors.size(); ++i ) {
332 connectors[i]->set_volts( 0.0 );
333 connectors[i]->set_load_amps( 0.0 );
336 // for each supplier, propagate the electrical current
337 for ( i = 0; i < suppliers.size(); ++i ) {
338 // cout << " Updating: " << suppliers[i]->get_name() << endl;
339 propagate( suppliers[i], 0.0, " " );
343 = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
345 // impliment an extremely simplistic voltage model (assumes
346 // certain naming conventions in electrical system config)
347 // FIXME: we probably want to be able to feed power from all
348 // engines if they are running and the master-alt is switched on
350 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
353 if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
354 fgGetDouble("/engines/engine[0]/rpm") > 800 )
356 double alt_contrib = 28.0;
357 if ( alt_contrib > volts ) {
361 _volts_out->setDoubleValue( volts );
363 // impliment an extremely simplistic amps model (assumes certain
364 // naming conventions in the electrical system config) ... FIXME:
365 // make this more generic
367 if ( fgGetBool("/controls/engines/engine[0]/master-bat") ) {
368 if ( fgGetBool("/controls/engines/engine[0]/master-alt") &&
369 fgGetDouble("/engines/engine[0]/rpm") > 800 )
371 amps += 40.0 * alt_norm;
373 amps -= 15.0; // normal load
374 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
377 if ( fgGetBool("/controls/switches/nav-lights") ) {
384 _amps_out->setDoubleValue( amps );
388 bool FGElectricalSystem::build () {
389 SGPropertyNode *node;
392 int count = config_props->nChildren();
393 for ( i = 0; i < count; ++i ) {
394 node = config_props->getChild(i);
395 string name = node->getName();
396 // cout << name << endl;
397 if ( name == "supplier" ) {
398 FGElectricalSupplier *s =
399 new FGElectricalSupplier( node );
400 suppliers.push_back( s );
401 } else if ( name == "bus" ) {
403 new FGElectricalBus( node );
404 buses.push_back( b );
405 } else if ( name == "output" ) {
406 FGElectricalOutput *o =
407 new FGElectricalOutput( node );
408 outputs.push_back( o );
409 } else if ( name == "connector" ) {
410 FGElectricalConnector *c =
411 new FGElectricalConnector( node, this );
412 connectors.push_back( c );
414 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
424 // propagate the electrical current through the network, returns the
425 // total current drawn by the children of this node.
426 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
430 float current_amps = 0.0;
432 // determine the current to carry forward
434 if ( !fgGetBool("/systems/electrical/serviceable") ) {
436 } else if ( node->get_kind() == FG_SUPPLIER ) {
437 // cout << s << " is a supplier" << endl;
438 volts = ((FGElectricalSupplier *)node)->get_output();
439 } else if ( node->get_kind() == FG_BUS ) {
440 // cout << s << " is a bus" << endl;
442 } else if ( node->get_kind() == FG_OUTPUT ) {
443 // cout << s << " is an output" << endl;
446 // draw current if we have voltage
447 current_amps = ((FGElectricalOutput *)node)->get_output_amps();
449 } else if ( node->get_kind() == FG_CONNECTOR ) {
450 // cout << s << " is a connector" << endl;
451 if ( ((FGElectricalConnector *)node)->get_state() ) {
456 // cout << s << " val = " << volts << endl;
458 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
461 if ( volts > node->get_volts() ) {
462 node->set_volts( volts );
467 // publish values to specified properties
468 for ( i = 0; i < node->get_num_props(); ++i ) {
469 fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
471 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
473 // propagate to all children
474 for ( i = 0; i < node->get_num_outputs(); ++i ) {
475 current_amps += propagate( node->get_output(i), volts, s );
478 // if not an output node, register the downstream current draw
479 // with this node. If volts are zero, current draw should be zero.
480 if ( node->get_kind() != FG_OUTPUT ) {
481 node->set_load_amps( current_amps );
483 // cout << s << node->get_name() << " -> " << current_amps << endl;
489 // search for the named component and return a pointer to it, NULL otherwise
490 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
495 for ( i = 0; i < suppliers.size(); ++i ) {
496 s = suppliers[i]->get_name();
497 // cout << " " << s << endl;
504 for ( i = 0; i < buses.size(); ++i ) {
505 s = buses[i]->get_name();
506 // cout << " " << s << endl;
512 // then search outputs
513 for ( i = 0; i < outputs.size(); ++i ) {
514 s = outputs[i]->get_name();
515 // cout << " " << s << endl;