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/misc/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 ( (string)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 ( (string)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 ( (string)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");
242 SGPath config( globals->get_fg_root() );
243 config.append( path_n->getStringValue() );
245 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
248 readProperties( config.str(), config_props );
253 SG_LOG( SG_ALL, SG_ALERT,
254 "Detected an internal inconsistancy in the electrical");
255 SG_LOG( SG_ALL, SG_ALERT,
256 " system specification file. See earlier errors for" );
257 SG_LOG( SG_ALL, SG_ALERT,
261 } catch (const sg_exception& exc) {
262 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
267 SG_LOG( SG_ALL, SG_ALERT,
268 "No electrical model specified for this model!");
274 void FGElectricalSystem::bind () {
278 void FGElectricalSystem::unbind () {
282 void FGElectricalSystem::update (double dt) {
287 // cout << "Updating electrical system" << endl;
291 // zero everything out before we start
292 for ( i = 0; i < suppliers.size(); ++i ) {
293 suppliers[i]->set_value( 0.0 );
295 for ( i = 0; i < buses.size(); ++i ) {
296 buses[i]->set_value( 0.0 );
298 for ( i = 0; i < outputs.size(); ++i ) {
299 outputs[i]->set_value( 0.0 );
301 for ( i = 0; i < connectors.size(); ++i ) {
302 connectors[i]->set_value( 0.0 );
305 // for each supplier, propagate the electrical current
306 for ( i = 0; i < suppliers.size(); ++i ) {
307 // cout << " Updating: " << suppliers[i]->get_name() << endl;
308 propagate( suppliers[i], 0.0, " " );
314 bool FGElectricalSystem::build () {
315 SGPropertyNode *node;
318 int count = config_props->nChildren();
319 for ( i = 0; i < count; ++i ) {
320 node = config_props->getChild(i);
321 string name = node->getName();
322 // cout << name << endl;
323 if ( name == "supplier" ) {
324 FGElectricalSupplier *s =
325 new FGElectricalSupplier( node );
326 suppliers.push_back( s );
327 } else if ( name == "bus" ) {
329 new FGElectricalBus( node );
330 buses.push_back( b );
331 } else if ( name == "output" ) {
332 FGElectricalOutput *o =
333 new FGElectricalOutput( node );
334 outputs.push_back( o );
335 } else if ( name == "connector" ) {
336 FGElectricalConnector *c =
337 new FGElectricalConnector( node, this );
338 connectors.push_back( c );
340 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
350 // propagate the electrical current through the network
351 void FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
355 // determine the current to carry forward
356 double current = 0.0;
357 if ( !fgGetBool("/systems/electrical/serviceable") ) {
359 } else if ( node->get_kind() == FG_SUPPLIER ) {
360 // cout << s << " is a supplier" << endl;
361 current = ((FGElectricalSupplier *)node)->get_output();
362 } else if ( node->get_kind() == FG_BUS ) {
363 // cout << s << " is a bus" << endl;
365 } else if ( node->get_kind() == FG_OUTPUT ) {
366 // cout << s << " is an output" << endl;
368 } else if ( node->get_kind() == FG_CONNECTOR ) {
369 // cout << s << " is a connector" << endl;
370 if ( ((FGElectricalConnector *)node)->get_state() ) {
375 // cout << s << " val = " << current << endl;
377 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
380 if ( current > node->get_value() ) {
381 node->set_value( current );
386 // publish values to specified properties
387 for ( i = 0; i < node->get_num_props(); ++i ) {
388 fgSetDouble( node->get_prop(i).c_str(), node->get_value() );
390 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
392 // propagate to all children
393 for ( i = 0; i < node->get_num_outputs(); ++i ) {
394 propagate( node->get_output(i), current, s );
399 // search for the named component and return a pointer to it, NULL otherwise
400 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
405 for ( i = 0; i < suppliers.size(); ++i ) {
406 s = suppliers[i]->get_name();
407 // cout << " " << s << endl;
414 for ( i = 0; i < buses.size(); ++i ) {
415 s = buses[i]->get_name();
416 // cout << " " << s << endl;
422 // then search outputs
423 for ( i = 0; i < outputs.size(); ++i ) {
424 s = outputs[i]->get_name();
425 // cout << " " << s << endl;