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() :
41 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
44 // cout << "Creating a supplier" << endl;
45 name = node->getStringValue("name");
46 string _model = node->getStringValue("kind");
47 // cout << "_model = " << _model << endl;
48 if ( _model == "battery" ) {
50 } else if ( _model == "alternator" ) {
51 model = FG_ALTERNATOR;
52 } else if ( _model == "external" ) {
57 volts = node->getDoubleValue("volts");
58 amps = node->getDoubleValue("amps");
59 rpm_src = node->getStringValue("rpm-source");
62 for ( i = 0; i < node->nChildren(); ++i ) {
63 SGPropertyNode *child = node->getChild(i);
64 // cout << " scanning: " << child->getName() << endl;
65 if ( !strcmp(child->getName(), "prop") ) {
66 string prop = child->getStringValue();
67 // cout << " Adding prop = " << prop << endl;
69 fgSetDouble( prop.c_str(), amps );
73 _rpm_node = fgGetNode( rpm_src.c_str(), true);
77 double FGElectricalSupplier::get_output() {
78 if ( model == FG_BATTERY ) {
79 // cout << "battery amps = " << amps << endl;
81 } else if ( model == FG_ALTERNATOR ) {
82 // scale alternator output for rpms < 600. For rpms >= 600
83 // give full output. This is just a WAG, and probably not how
84 // it really works but I'm keeping things "simple" to start.
85 double rpm = _rpm_node->getDoubleValue();
86 double factor = rpm / 600.0;
90 // cout << "alternator amps = " << amps * factor << endl;
92 } else if ( model == FG_EXTERNAL ) {
93 // cout << "external amps = " << 0.0 << endl;
96 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
103 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
106 name = node->getStringValue("name");
108 for ( i = 0; i < node->nChildren(); ++i ) {
109 SGPropertyNode *child = node->getChild(i);
110 if ( !strcmp(child->getName(), "prop") ) {
111 string prop = child->getStringValue();
118 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
121 name = node->getStringValue("name");
123 for ( i = 0; i < node->nChildren(); ++i ) {
124 SGPropertyNode *child = node->getChild(i);
125 if ( !strcmp(child->getName(), "prop") ) {
126 string prop = child->getStringValue();
133 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
134 FGElectricalSystem *es ) {
138 for ( i = 0; i < node->nChildren(); ++i ) {
139 SGPropertyNode *child = node->getChild(i);
140 string cname = child->getName();
141 string cval = child->getStringValue();
142 // cout << " " << cname << " = " << cval << endl;
143 if ( cname == "input" ) {
144 FGElectricalComponent *s = es->find( child->getStringValue() );
147 if ( s->get_kind() == FG_SUPPLIER ) {
148 s->add_output( this );
149 } else if ( s->get_kind() == FG_BUS ) {
150 s->add_output( this );
152 SG_LOG( SG_ALL, SG_ALERT,
153 "Attempt to connect to something that can't provide an output: "
154 << child->getStringValue() );
157 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
158 << child->getStringValue() );
160 } else if ( cname == "output" ) {
161 FGElectricalComponent *s = es->find( child->getStringValue() );
164 if ( s->get_kind() == FG_BUS ) {
165 s->add_input( this );
166 } else if ( s->get_kind() == FG_OUTPUT ) {
167 s->add_input( this );
169 SG_LOG( SG_ALL, SG_ALERT,
170 "Attempt to connect to something that can't provide an input: "
171 << child->getStringValue() );
174 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
175 << child->getStringValue() );
177 } else if ( cname == "switch" ) {
178 // set default value of switch to true
179 // cout << "Switch = " << child->getStringValue() << endl;
180 fgSetBool( child->getStringValue(), true );
181 add_switch( fgGetNode( child->getStringValue(), true ) );
185 // do a 2nd pass to pick up starting switch value if specified
186 for ( i = 0; i < node->nChildren(); ++i ) {
187 SGPropertyNode *child = node->getChild(i);
188 string cname = child->getName();
189 string cval = child->getStringValue();
190 // cout << " " << cname << " = " << cval << endl;
191 if ( cname == "initial-state" ) {
192 if ( cval == "off" ) {
193 set_switches( false );
195 set_switches( true );
202 // set all switches to the specified state
203 void FGElectricalConnector::set_switches( bool state ) {
204 // cout << "setting switch state to " << state << endl;
205 for ( unsigned int i = 0; i < switches.size(); ++i ) {
206 switches[i]->setBoolValue( state );
211 // return true if all switches are true, false otherwise. A connector
212 // could have multiple switches, but they all need to be true(closed)
213 // for current to get through.
214 bool FGElectricalConnector::get_state() {
216 for ( i = 0; i < switches.size(); ++i ) {
217 if ( ! switches[i]->getBoolValue() ) {
226 FGElectricalSystem::FGElectricalSystem () :
232 FGElectricalSystem::~FGElectricalSystem () {
236 void FGElectricalSystem::init () {
237 config_props = new SGPropertyNode;
239 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
240 _volts_out = fgGetNode( "/systems/electrical/volts", true );
241 _amps_out = fgGetNode( "/systems/electrical/amps", true );
244 SGPath config( globals->get_fg_root() );
245 config.append( path_n->getStringValue() );
247 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
250 readProperties( config.str(), config_props );
255 SG_LOG( SG_ALL, SG_ALERT,
256 "Detected an internal inconsistancy in the electrical");
257 SG_LOG( SG_ALL, SG_ALERT,
258 " system specification file. See earlier errors for" );
259 SG_LOG( SG_ALL, SG_ALERT,
263 } catch (const sg_exception& exc) {
264 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
269 SG_LOG( SG_ALL, SG_ALERT,
270 "No electrical model specified for this model!");
276 void FGElectricalSystem::bind () {
280 void FGElectricalSystem::unbind () {
284 void FGElectricalSystem::update (double dt) {
286 _amps_out->setDoubleValue(0);
290 // cout << "Updating electrical system" << endl;
294 // zero everything out before we start
295 for ( i = 0; i < suppliers.size(); ++i ) {
296 suppliers[i]->set_value( 0.0 );
298 for ( i = 0; i < buses.size(); ++i ) {
299 buses[i]->set_value( 0.0 );
301 for ( i = 0; i < outputs.size(); ++i ) {
302 outputs[i]->set_value( 0.0 );
304 for ( i = 0; i < connectors.size(); ++i ) {
305 connectors[i]->set_value( 0.0 );
308 // for each supplier, propagate the electrical current
309 for ( i = 0; i < suppliers.size(); ++i ) {
310 // cout << " Updating: " << suppliers[i]->get_name() << endl;
311 propagate( suppliers[i], 0.0, " " );
315 = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
317 // impliment an extremely simplistic voltage model (assumes
318 // certain naming conventions in electrical system config)
320 if ( fgGetBool("/controls/switches/master-bat") ) {
323 if ( fgGetBool("/controls/switches/master-alt") &&
324 fgGetDouble("/engines/engine[0]/rpm") > 800 )
326 double alt_contrib = 28.0;
327 if ( alt_contrib > volts ) {
331 _volts_out->setDoubleValue( volts );
333 // impliment an extremely simplistic amps model (assumes certain
334 // naming conventions in the electrical system config) ... FIXME:
335 // make this more generic
337 if ( fgGetBool("/controls/switches/master-bat") ) {
338 if ( fgGetBool("/controls/switches/master-alt") &&
339 fgGetDouble("/engines/engine[0]/rpm") > 800 )
341 amps += 40.0 * alt_norm;
343 amps -= 15.0; // normal load
344 if ( fgGetBool("/controls/switches/flashing-beacon") ) {
347 if ( fgGetBool("/controls/switches/nav-lights") ) {
354 _amps_out->setDoubleValue( amps );
358 bool FGElectricalSystem::build () {
359 SGPropertyNode *node;
362 int count = config_props->nChildren();
363 for ( i = 0; i < count; ++i ) {
364 node = config_props->getChild(i);
365 string name = node->getName();
366 // cout << name << endl;
367 if ( name == "supplier" ) {
368 FGElectricalSupplier *s =
369 new FGElectricalSupplier( node );
370 suppliers.push_back( s );
371 } else if ( name == "bus" ) {
373 new FGElectricalBus( node );
374 buses.push_back( b );
375 } else if ( name == "output" ) {
376 FGElectricalOutput *o =
377 new FGElectricalOutput( node );
378 outputs.push_back( o );
379 } else if ( name == "connector" ) {
380 FGElectricalConnector *c =
381 new FGElectricalConnector( node, this );
382 connectors.push_back( c );
384 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
394 // propagate the electrical current through the network
395 void FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
399 // determine the current to carry forward
400 double current = 0.0;
401 if ( !fgGetBool("/systems/electrical/serviceable") ) {
403 } else if ( node->get_kind() == FG_SUPPLIER ) {
404 // cout << s << " is a supplier" << endl;
405 current = ((FGElectricalSupplier *)node)->get_output();
406 } else if ( node->get_kind() == FG_BUS ) {
407 // cout << s << " is a bus" << endl;
409 } else if ( node->get_kind() == FG_OUTPUT ) {
410 // cout << s << " is an output" << endl;
412 } else if ( node->get_kind() == FG_CONNECTOR ) {
413 // cout << s << " is a connector" << endl;
414 if ( ((FGElectricalConnector *)node)->get_state() ) {
419 // cout << s << " val = " << current << endl;
421 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
424 if ( current > node->get_value() ) {
425 node->set_value( current );
430 // publish values to specified properties
431 for ( i = 0; i < node->get_num_props(); ++i ) {
432 fgSetDouble( node->get_prop(i).c_str(), node->get_value() );
434 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
436 // propagate to all children
437 for ( i = 0; i < node->get_num_outputs(); ++i ) {
438 propagate( node->get_output(i), current, s );
443 // search for the named component and return a pointer to it, NULL otherwise
444 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
449 for ( i = 0; i < suppliers.size(); ++i ) {
450 s = suppliers[i]->get_name();
451 // cout << " " << s << endl;
458 for ( i = 0; i < buses.size(); ++i ) {
459 s = buses[i]->get_name();
460 // cout << " " << s << endl;
466 // then search outputs
467 for ( i = 0; i < outputs.size(); ++i ) {
468 s = outputs[i]->get_name();
469 // cout << " " << s << endl;