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");
61 for ( i = 0; i < node->nChildren(); ++i ) {
62 SGPropertyNode *child = node->getChild(i);
63 // cout << " scanning: " << child->getName() << endl;
64 if ( (string)child->getName() == "prop" ) {
65 string prop = child->getStringValue();
66 // cout << " Adding prop = " << prop << endl;
68 fgSetDouble( prop.c_str(), amps );
72 _rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
76 double FGElectricalSupplier::get_output() {
77 if ( model == FG_BATTERY ) {
78 // cout << "battery amps = " << amps << endl;
80 } else if ( model == FG_ALTERNATOR ) {
81 // scale alternator output for rpms < 600. For rpms >= 600
82 // give full output. This is just a WAG, and probably not how
83 // it really works but I'm keeping things "simple" to start.
84 double rpm = _rpm_node->getDoubleValue();
85 double factor = rpm / 600.0;
89 // cout << "alternator amps = " << amps * factor << endl;
91 } else if ( model == FG_EXTERNAL ) {
92 // cout << "external amps = " << 0.0 << endl;
95 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
102 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
105 name = node->getStringValue("name");
107 for ( i = 0; i < node->nChildren(); ++i ) {
108 SGPropertyNode *child = node->getChild(i);
109 if ( (string)child->getName() == "prop" ) {
110 string prop = child->getStringValue();
117 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
120 name = node->getStringValue("name");
122 for ( i = 0; i < node->nChildren(); ++i ) {
123 SGPropertyNode *child = node->getChild(i);
124 if ( (string)child->getName() == "prop" ) {
125 string prop = child->getStringValue();
132 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
133 FGElectricalSystem *es ) {
137 for ( i = 0; i < node->nChildren(); ++i ) {
138 SGPropertyNode *child = node->getChild(i);
139 string cname = child->getName();
140 string cval = child->getStringValue();
141 // cout << " " << cname << " = " << cval << endl;
142 if ( cname == "input" ) {
143 FGElectricalComponent *s = es->find( child->getStringValue() );
146 if ( s->get_kind() == FG_SUPPLIER ) {
147 s->add_output( this );
148 } else if ( s->get_kind() == FG_BUS ) {
149 s->add_output( this );
151 SG_LOG( SG_ALL, SG_ALERT,
152 "Attempt to connect to something that can't provide an output: "
153 << child->getStringValue() );
156 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
157 << child->getStringValue() );
159 } else if ( cname == "output" ) {
160 FGElectricalComponent *s = es->find( child->getStringValue() );
163 if ( s->get_kind() == FG_BUS ) {
164 s->add_input( this );
165 } else if ( s->get_kind() == FG_OUTPUT ) {
166 s->add_input( this );
168 SG_LOG( SG_ALL, SG_ALERT,
169 "Attempt to connect to something that can't provide an input: "
170 << child->getStringValue() );
173 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: "
174 << child->getStringValue() );
176 } else if ( cname == "switch" ) {
177 // set default value of switch to true
178 // cout << "Switch = " << child->getStringValue() << endl;
179 fgSetBool( child->getStringValue(), true );
180 add_switch( fgGetNode( child->getStringValue(), true ) );
186 // return true if all switches are true, false otherwise. A connector
187 // could have multiple switches, but they all need to be true(closed)
188 // for current to get through.
189 bool FGElectricalConnector::get_state() {
191 for ( i = 0; i < switches.size(); ++i ) {
192 if ( ! switches[i]->getBoolValue() ) {
201 FGElectricalSystem::FGElectricalSystem () :
207 FGElectricalSystem::~FGElectricalSystem () {
211 void FGElectricalSystem::init () {
212 config_props = new SGPropertyNode;
214 SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
217 SGPath config( globals->get_fg_root() );
218 config.append( path_n->getStringValue() );
220 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
223 readProperties( config.str(), config_props );
228 SG_LOG( SG_ALL, SG_ALERT,
229 "Detected an internal inconsistancy in the electrical");
230 SG_LOG( SG_ALL, SG_ALERT,
231 " system specification file. See earlier errors for" );
232 SG_LOG( SG_ALL, SG_ALERT,
236 } catch (const sg_exception& exc) {
237 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
242 SG_LOG( SG_ALL, SG_ALERT,
243 "No electrical model specified for this model!");
249 void FGElectricalSystem::bind () {
253 void FGElectricalSystem::unbind () {
257 void FGElectricalSystem::update (double dt) {
262 // cout << "Updating electrical system" << endl;
266 // zero everything out before we start
267 for ( i = 0; i < suppliers.size(); ++i ) {
268 suppliers[i]->set_value( 0.0 );
270 for ( i = 0; i < buses.size(); ++i ) {
271 buses[i]->set_value( 0.0 );
273 for ( i = 0; i < outputs.size(); ++i ) {
274 outputs[i]->set_value( 0.0 );
276 for ( i = 0; i < connectors.size(); ++i ) {
277 connectors[i]->set_value( 0.0 );
280 // for each supplier, propogate the electrical current
281 for ( i = 0; i < suppliers.size(); ++i ) {
282 // cout << " Updating: " << suppliers[i]->get_name() << endl;
283 propogate( suppliers[i], 0.0, " " );
289 bool FGElectricalSystem::build () {
290 SGPropertyNode *node;
293 int count = config_props->nChildren();
294 for ( i = 0; i < count; ++i ) {
295 node = config_props->getChild(i);
296 string name = node->getName();
297 // cout << name << endl;
298 if ( name == "supplier" ) {
299 FGElectricalSupplier *s =
300 new FGElectricalSupplier( node );
301 suppliers.push_back( s );
302 } else if ( name == "bus" ) {
304 new FGElectricalBus( node );
305 buses.push_back( b );
306 } else if ( name == "output" ) {
307 FGElectricalOutput *o =
308 new FGElectricalOutput( node );
309 outputs.push_back( o );
310 } else if ( name == "connector" ) {
311 FGElectricalConnector *c =
312 new FGElectricalConnector( node, this );
313 connectors.push_back( c );
315 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
325 // propogate the electrical current through the network
326 void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
330 // determine the current to carry forward
331 double current = 0.0;
332 if ( !fgGetBool("/systems/electrical/serviceable") ) {
334 } else if ( node->get_kind() == FG_SUPPLIER ) {
335 // cout << s << " is a supplier" << endl;
336 current = ((FGElectricalSupplier *)node)->get_output();
337 } else if ( node->get_kind() == FG_BUS ) {
338 // cout << s << " is a bus" << endl;
340 } else if ( node->get_kind() == FG_OUTPUT ) {
341 // cout << s << " is an output" << endl;
343 } else if ( node->get_kind() == FG_CONNECTOR ) {
344 // cout << s << " is a connector" << endl;
345 if ( ((FGElectricalConnector *)node)->get_state() ) {
350 // cout << s << " val = " << current << endl;
352 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
355 if ( current > node->get_value() ) {
356 node->set_value( current );
361 // publish values to specified properties
362 for ( i = 0; i < node->get_num_props(); ++i ) {
363 fgSetDouble( node->get_prop(i).c_str(), node->get_value() );
365 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
367 // propogate to all children
368 for ( i = 0; i < node->get_num_outputs(); ++i ) {
369 propogate( node->get_output(i), current, s );
374 // search for the named component and return a pointer to it, NULL otherwise
375 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
380 for ( i = 0; i < suppliers.size(); ++i ) {
381 s = suppliers[i]->get_name();
382 // cout << " " << s << endl;
389 for ( i = 0; i < buses.size(); ++i ) {
390 s = buses[i]->get_name();
391 // cout << " " << s << endl;
397 // then search outputs
398 for ( i = 0; i < outputs.size(); ++i ) {
399 s = outputs[i]->get_name();
400 // cout << " " << s << endl;