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 ) {
123 name = node->getStringValue("name");
125 for ( i = 0; i < node->nChildren(); ++i ) {
126 SGPropertyNode *child = node->getChild(i);
127 if ( !strcmp(child->getName(), "prop") ) {
128 string prop = child->getStringValue();
135 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
136 FGElectricalSystem *es ) {
140 for ( i = 0; i < node->nChildren(); ++i ) {
141 SGPropertyNode *child = node->getChild(i);
142 string cname = child->getName();
143 string cval = child->getStringValue();
144 // cout << " " << cname << " = " << cval << endl;
145 if ( cname == "input" ) {
146 FGElectricalComponent *s = es->find( child->getStringValue() );
149 if ( s->get_kind() == FG_SUPPLIER ) {
150 s->add_output( this );
151 } else if ( s->get_kind() == FG_BUS ) {
152 s->add_output( this );
154 SG_LOG( SG_ALL, SG_ALERT,
155 "Attempt to connect to something that can't provide an output: "
156 << child->getStringValue() );
159 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
160 << child->getStringValue() );
162 } else if ( cname == "output" ) {
163 FGElectricalComponent *s = es->find( child->getStringValue() );
166 if ( s->get_kind() == FG_BUS ) {
167 s->add_input( this );
168 } else if ( s->get_kind() == FG_OUTPUT ) {
169 s->add_input( this );
171 SG_LOG( SG_ALL, SG_ALERT,
172 "Attempt to connect to something that can't provide an input: "
173 << child->getStringValue() );
176 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
177 << child->getStringValue() );
179 } else if ( cname == "switch" ) {
180 // set default value of switch to true
181 // cout << "Switch = " << child->getStringValue() << endl;
182 fgSetBool( child->getStringValue(), true );
183 FGElectricalSwitch s( fgGetNode(child->getStringValue(), true),
190 // do a 2nd pass to pick up starting switch value if specified
191 for ( i = 0; i < node->nChildren(); ++i ) {
192 SGPropertyNode *child = node->getChild(i);
193 string cname = child->getName();
194 string cval = child->getStringValue();
195 // cout << " " << cname << " = " << cval << endl;
196 if ( cname == "initial-state" ) {
197 if ( cval == "off" ) {
198 set_switches( false );
200 set_switches( true );
207 // set all switches to the specified state
208 void FGElectricalConnector::set_switches( bool state ) {
209 // cout << "setting switch state to " << state << endl;
210 for ( unsigned int i = 0; i < switches.size(); ++i ) {
211 switches[i].set_state( state );
216 // return true if all switches are true, false otherwise. A connector
217 // could have multiple switches, but they all need to be true(closed)
218 // for current to get through.
219 bool FGElectricalConnector::get_state() {
221 for ( i = 0; i < switches.size(); ++i ) {
222 if ( ! switches[i].get_state() ) {
231 FGElectricalSystem::FGElectricalSystem () :
237 FGElectricalSystem::~FGElectricalSystem () {
241 void FGElectricalSystem::init () {
242 config_props = new SGPropertyNode;
244 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
245 _volts_out = fgGetNode( "/systems/electrical/volts", true );
246 _amps_out = fgGetNode( "/systems/electrical/amps", true );
249 SGPath config( globals->get_fg_root() );
250 config.append( path_n->getStringValue() );
252 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
255 readProperties( config.str(), config_props );
260 SG_LOG( SG_ALL, SG_ALERT,
261 "Detected an internal inconsistancy in the electrical");
262 SG_LOG( SG_ALL, SG_ALERT,
263 " system specification file. See earlier errors for" );
264 SG_LOG( SG_ALL, SG_ALERT,
268 } catch (const sg_exception& exc) {
269 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
274 SG_LOG( SG_ALL, SG_ALERT,
275 "No electrical model specified for this model!");
281 void FGElectricalSystem::bind () {
285 void FGElectricalSystem::unbind () {
289 void FGElectricalSystem::update (double dt) {
291 _amps_out->setDoubleValue(0);
295 // cout << "Updating electrical system" << endl;
299 // zero everything out before we start
300 for ( i = 0; i < suppliers.size(); ++i ) {
301 suppliers[i]->set_volts( 0.0 );
302 suppliers[i]->set_load_amps( 0.0 );
304 for ( i = 0; i < buses.size(); ++i ) {
305 buses[i]->set_volts( 0.0 );
306 buses[i]->set_load_amps( 0.0 );
308 for ( i = 0; i < outputs.size(); ++i ) {
309 outputs[i]->set_volts( 0.0 );
310 outputs[i]->set_load_amps( 0.0 );
312 for ( i = 0; i < connectors.size(); ++i ) {
313 connectors[i]->set_volts( 0.0 );
314 connectors[i]->set_load_amps( 0.0 );
317 // for each supplier, propagate the electrical current
318 for ( i = 0; i < suppliers.size(); ++i ) {
319 // cout << " Updating: " << suppliers[i]->get_name() << endl;
320 propagate( suppliers[i], 0.0, " " );
324 = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
326 // impliment an extremely simplistic voltage model (assumes
327 // certain naming conventions in electrical system config)
329 if ( fgGetBool("/controls/switches/master-bat") ) {
332 if ( fgGetBool("/controls/switches/master-alt") &&
333 fgGetDouble("/engines/engine[0]/rpm") > 800 )
335 double alt_contrib = 28.0;
336 if ( alt_contrib > volts ) {
340 _volts_out->setDoubleValue( volts );
342 // impliment an extremely simplistic amps model (assumes certain
343 // naming conventions in the electrical system config) ... FIXME:
344 // make this more generic
346 if ( fgGetBool("/controls/switches/master-bat") ) {
347 if ( fgGetBool("/controls/switches/master-alt") &&
348 fgGetDouble("/engines/engine[0]/rpm") > 800 )
350 amps += 40.0 * alt_norm;
352 amps -= 15.0; // normal load
353 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
356 if ( fgGetBool("/controls/switches/nav-lights") ) {
363 _amps_out->setDoubleValue( amps );
367 bool FGElectricalSystem::build () {
368 SGPropertyNode *node;
371 int count = config_props->nChildren();
372 for ( i = 0; i < count; ++i ) {
373 node = config_props->getChild(i);
374 string name = node->getName();
375 // cout << name << endl;
376 if ( name == "supplier" ) {
377 FGElectricalSupplier *s =
378 new FGElectricalSupplier( node );
379 suppliers.push_back( s );
380 } else if ( name == "bus" ) {
382 new FGElectricalBus( node );
383 buses.push_back( b );
384 } else if ( name == "output" ) {
385 FGElectricalOutput *o =
386 new FGElectricalOutput( node );
387 outputs.push_back( o );
388 } else if ( name == "connector" ) {
389 FGElectricalConnector *c =
390 new FGElectricalConnector( node, this );
391 connectors.push_back( c );
393 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
403 // propagate the electrical current through the network, returns the
404 // total current drawn by the children of this node.
405 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
409 float current_amps = 0.0;
411 // determine the current to carry forward
413 if ( !fgGetBool("/systems/electrical/serviceable") ) {
415 } else if ( node->get_kind() == FG_SUPPLIER ) {
416 // cout << s << " is a supplier" << endl;
417 volts = ((FGElectricalSupplier *)node)->get_output();
418 } else if ( node->get_kind() == FG_BUS ) {
419 // cout << s << " is a bus" << endl;
421 } else if ( node->get_kind() == FG_OUTPUT ) {
422 // cout << s << " is an output" << endl;
425 // draw current if we have voltage
426 current_amps = ((FGElectricalOutput *)node)->get_output_amps();
428 } else if ( node->get_kind() == FG_CONNECTOR ) {
429 // cout << s << " is a connector" << endl;
430 if ( ((FGElectricalConnector *)node)->get_state() ) {
435 // cout << s << " val = " << volts << endl;
437 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
440 if ( volts > node->get_volts() ) {
441 node->set_volts( volts );
446 // publish values to specified properties
447 for ( i = 0; i < node->get_num_props(); ++i ) {
448 fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
450 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
452 // propagate to all children
453 for ( i = 0; i < node->get_num_outputs(); ++i ) {
454 current_amps += propagate( node->get_output(i), volts, s );
457 // if not an output node, register the downstream current draw
458 // with this node. If volts are zero, current draw should be zero.
459 if ( node->get_kind() != FG_OUTPUT ) {
460 node->set_load_amps( current_amps );
462 // cout << s << node->get_name() << " -> " << current_amps << endl;
468 // search for the named component and return a pointer to it, NULL otherwise
469 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
474 for ( i = 0; i < suppliers.size(); ++i ) {
475 s = suppliers[i]->get_name();
476 // cout << " " << s << endl;
483 for ( i = 0; i < buses.size(); ++i ) {
484 s = buses[i]->get_name();
485 // cout << " " << s << endl;
491 // then search outputs
492 for ( i = 0; i < outputs.size(); ++i ) {
493 s = outputs[i]->get_name();
494 // cout << " " << s << endl;