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 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
138 circuit_breaker( false )
140 bool initial_state = true;
142 for ( i = 0; i < node->nChildren(); ++i ) {
143 SGPropertyNode *child = node->getChild(i);
144 string cname = child->getName();
145 string cval = child->getStringValue();
146 if ( cname == "prop" ) {
147 switch_node = fgGetNode( cval.c_str(), true );
148 // cout << "switch node = " << cval << endl;
149 } else if ( cname == "initial-state" ) {
150 if ( cval == "off" || cval == "false" ) {
151 initial_state = false;
153 // cout << "initial state = " << initial_state << endl;
157 switch_node->setBoolValue( initial_state );
158 // cout << " value = " << switch_node->getBoolValue() << endl;
162 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
163 FGElectricalSystem *es ) {
167 for ( i = 0; i < node->nChildren(); ++i ) {
168 SGPropertyNode *child = node->getChild(i);
169 string cname = child->getName();
170 string cval = child->getStringValue();
171 // cout << " " << cname << " = " << cval << endl;
172 if ( cname == "input" ) {
173 FGElectricalComponent *s = es->find( child->getStringValue() );
176 if ( s->get_kind() == FG_SUPPLIER ) {
177 s->add_output( this );
178 } else if ( s->get_kind() == FG_BUS ) {
179 s->add_output( this );
181 SG_LOG( SG_ALL, SG_ALERT,
182 "Attempt to connect to something that can't provide an output: "
183 << child->getStringValue() );
186 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
187 << child->getStringValue() );
189 } else if ( cname == "output" ) {
190 FGElectricalComponent *s = es->find( child->getStringValue() );
193 if ( s->get_kind() == FG_BUS ) {
194 s->add_input( this );
195 } else if ( s->get_kind() == FG_OUTPUT ) {
196 s->add_input( this );
198 SG_LOG( SG_ALL, SG_ALERT,
199 "Attempt to connect to something that can't provide an input: "
200 << child->getStringValue() );
203 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
204 << child->getStringValue() );
206 } else if ( cname == "switch" ) {
207 // cout << "Switch = " << child->getStringValue() << endl;
208 FGElectricalSwitch s( child );
215 // set all switches to the specified state
216 void FGElectricalConnector::set_switches( bool state ) {
217 // cout << "setting switch state to " << state << endl;
218 for ( unsigned int i = 0; i < switches.size(); ++i ) {
219 switches[i].set_state( state );
224 // return true if all switches are true, false otherwise. A connector
225 // could have multiple switches, but they all need to be true(closed)
226 // for current to get through.
227 bool FGElectricalConnector::get_state() {
229 for ( i = 0; i < switches.size(); ++i ) {
230 if ( ! switches[i].get_state() ) {
239 FGElectricalSystem::FGElectricalSystem () :
245 FGElectricalSystem::~FGElectricalSystem () {
249 void FGElectricalSystem::init () {
250 config_props = new SGPropertyNode;
252 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
253 _volts_out = fgGetNode( "/systems/electrical/volts", true );
254 _amps_out = fgGetNode( "/systems/electrical/amps", true );
257 SGPath config( globals->get_fg_root() );
258 config.append( path_n->getStringValue() );
260 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
263 readProperties( config.str(), config_props );
268 SG_LOG( SG_ALL, SG_ALERT,
269 "Detected an internal inconsistancy in the electrical");
270 SG_LOG( SG_ALL, SG_ALERT,
271 " system specification file. See earlier errors for" );
272 SG_LOG( SG_ALL, SG_ALERT,
276 } catch (const sg_exception& exc) {
277 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
282 SG_LOG( SG_ALL, SG_ALERT,
283 "No electrical model specified for this model!");
289 void FGElectricalSystem::bind () {
293 void FGElectricalSystem::unbind () {
297 void FGElectricalSystem::update (double dt) {
299 _amps_out->setDoubleValue(0);
303 // cout << "Updating electrical system" << endl;
307 // zero everything out before we start
308 for ( i = 0; i < suppliers.size(); ++i ) {
309 suppliers[i]->set_volts( 0.0 );
310 suppliers[i]->set_load_amps( 0.0 );
312 for ( i = 0; i < buses.size(); ++i ) {
313 buses[i]->set_volts( 0.0 );
314 buses[i]->set_load_amps( 0.0 );
316 for ( i = 0; i < outputs.size(); ++i ) {
317 outputs[i]->set_volts( 0.0 );
318 outputs[i]->set_load_amps( 0.0 );
320 for ( i = 0; i < connectors.size(); ++i ) {
321 connectors[i]->set_volts( 0.0 );
322 connectors[i]->set_load_amps( 0.0 );
325 // for each supplier, propagate the electrical current
326 for ( i = 0; i < suppliers.size(); ++i ) {
327 // cout << " Updating: " << suppliers[i]->get_name() << endl;
328 propagate( suppliers[i], 0.0, " " );
332 = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
334 // impliment an extremely simplistic voltage model (assumes
335 // certain naming conventions in electrical system config)
337 if ( fgGetBool("/controls/switches/master-bat") ) {
340 if ( fgGetBool("/controls/switches/master-alt") &&
341 fgGetDouble("/engines/engine[0]/rpm") > 800 )
343 double alt_contrib = 28.0;
344 if ( alt_contrib > volts ) {
348 _volts_out->setDoubleValue( volts );
350 // impliment an extremely simplistic amps model (assumes certain
351 // naming conventions in the electrical system config) ... FIXME:
352 // make this more generic
354 if ( fgGetBool("/controls/switches/master-bat") ) {
355 if ( fgGetBool("/controls/switches/master-alt") &&
356 fgGetDouble("/engines/engine[0]/rpm") > 800 )
358 amps += 40.0 * alt_norm;
360 amps -= 15.0; // normal load
361 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
364 if ( fgGetBool("/controls/switches/nav-lights") ) {
371 _amps_out->setDoubleValue( amps );
375 bool FGElectricalSystem::build () {
376 SGPropertyNode *node;
379 int count = config_props->nChildren();
380 for ( i = 0; i < count; ++i ) {
381 node = config_props->getChild(i);
382 string name = node->getName();
383 // cout << name << endl;
384 if ( name == "supplier" ) {
385 FGElectricalSupplier *s =
386 new FGElectricalSupplier( node );
387 suppliers.push_back( s );
388 } else if ( name == "bus" ) {
390 new FGElectricalBus( node );
391 buses.push_back( b );
392 } else if ( name == "output" ) {
393 FGElectricalOutput *o =
394 new FGElectricalOutput( node );
395 outputs.push_back( o );
396 } else if ( name == "connector" ) {
397 FGElectricalConnector *c =
398 new FGElectricalConnector( node, this );
399 connectors.push_back( c );
401 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
411 // propagate the electrical current through the network, returns the
412 // total current drawn by the children of this node.
413 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
417 float current_amps = 0.0;
419 // determine the current to carry forward
421 if ( !fgGetBool("/systems/electrical/serviceable") ) {
423 } else if ( node->get_kind() == FG_SUPPLIER ) {
424 // cout << s << " is a supplier" << endl;
425 volts = ((FGElectricalSupplier *)node)->get_output();
426 } else if ( node->get_kind() == FG_BUS ) {
427 // cout << s << " is a bus" << endl;
429 } else if ( node->get_kind() == FG_OUTPUT ) {
430 // cout << s << " is an output" << endl;
433 // draw current if we have voltage
434 current_amps = ((FGElectricalOutput *)node)->get_output_amps();
436 } else if ( node->get_kind() == FG_CONNECTOR ) {
437 // cout << s << " is a connector" << endl;
438 if ( ((FGElectricalConnector *)node)->get_state() ) {
443 // cout << s << " val = " << volts << endl;
445 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
448 if ( volts > node->get_volts() ) {
449 node->set_volts( volts );
454 // publish values to specified properties
455 for ( i = 0; i < node->get_num_props(); ++i ) {
456 fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
458 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
460 // propagate to all children
461 for ( i = 0; i < node->get_num_outputs(); ++i ) {
462 current_amps += propagate( node->get_output(i), volts, s );
465 // if not an output node, register the downstream current draw
466 // with this node. If volts are zero, current draw should be zero.
467 if ( node->get_kind() != FG_OUTPUT ) {
468 node->set_load_amps( current_amps );
470 // cout << s << node->get_name() << " -> " << current_amps << endl;
476 // search for the named component and return a pointer to it, NULL otherwise
477 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
482 for ( i = 0; i < suppliers.size(); ++i ) {
483 s = suppliers[i]->get_name();
484 // cout << " " << s << endl;
491 for ( i = 0; i < buses.size(); ++i ) {
492 s = buses[i]->get_name();
493 // cout << " " << s << endl;
499 // then search outputs
500 for ( i = 0; i < outputs.size(); ++i ) {
501 s = outputs[i]->get_name();
502 // cout << " " << s << endl;