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() :
42 FGElectricalSupplier::FGElectricalSupplier ( string _name, string _prop,
44 double _volts, double _amps )
50 // cout << "_model = " << _model << endl;
51 if ( _model == "battery" ) {
53 } else if ( _model == "alternator" ) {
54 model = FG_ALTERNATOR;
55 } else if ( _model == "external" ) {
63 fgSetDouble( prop.c_str(), amps );
65 _rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
69 double FGElectricalSupplier::get_output() {
70 if ( model == FG_BATTERY ) {
71 // cout << "battery amps = " << amps << endl;
73 } else if ( model == FG_ALTERNATOR ) {
74 // scale alternator output for rpms < 600. For rpms >= 600
75 // give full output. This is just a WAG, and probably not how
76 // it really works but I'm keeping things "simple" to start.
77 double rpm = _rpm_node->getDoubleValue();
78 double factor = rpm / 600.0;
82 // cout << "alternator amps = " << amps * factor << endl;
84 } else if ( model == FG_EXTERNAL ) {
85 // cout << "external amps = " << 0.0 << endl;
88 SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
95 FGElectricalBus::FGElectricalBus ( string _name, string _prop )
104 FGElectricalOutput::FGElectricalOutput ( string _name, string _prop )
113 FGElectricalConnector::FGElectricalConnector ()
120 // return true if all switches are true, false otherwise. A connector
121 // could have multiple switches, but they all need to be true(closed)
122 // for current to get through.
123 bool FGElectricalConnector::get_state() {
125 for ( i = 0; i < switches.size(); ++i ) {
126 if ( ! switches[i]->getBoolValue() ) {
135 FGElectricalSystem::FGElectricalSystem () :
141 FGElectricalSystem::~FGElectricalSystem () {
145 void FGElectricalSystem::init () {
146 config_props = new SGPropertyNode;
148 SGPath config( globals->get_fg_root() );
149 config.append( fgGetString("/sim/systems/electrical/path") );
151 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
154 readProperties( config.str(), config_props );
159 SG_LOG( SG_ALL, SG_ALERT,
160 "Detected an internal inconsistancy in the electrical" );
161 SG_LOG( SG_ALL, SG_ALERT,
162 " system specification file. See earlier errors for" );
163 SG_LOG( SG_ALL, SG_ALERT,
167 } catch (const sg_exception& exc) {
168 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
176 void FGElectricalSystem::bind () {
180 void FGElectricalSystem::unbind () {
184 void FGElectricalSystem::update (double dt) {
189 // cout << "Updating electrical system" << endl;
193 // zero everything out before we start
194 for ( i = 0; i < suppliers.size(); ++i ) {
195 suppliers[i]->set_value( 0.0 );
197 for ( i = 0; i < buses.size(); ++i ) {
198 buses[i]->set_value( 0.0 );
200 for ( i = 0; i < outputs.size(); ++i ) {
201 outputs[i]->set_value( 0.0 );
203 for ( i = 0; i < connectors.size(); ++i ) {
204 connectors[i]->set_value( 0.0 );
207 // for each supplier, propogate the electrical current
208 for ( i = 0; i < suppliers.size(); ++i ) {
209 // cout << " Updating: " << suppliers[i]->get_name() << endl;
210 propogate( suppliers[i], 0.0, " " );
216 bool FGElectricalSystem::build () {
217 SGPropertyNode *node;
220 int count = config_props->nChildren();
221 for ( i = 0; i < count; ++i ) {
222 node = config_props->getChild(i);
223 string name = node->getName();
224 // cout << name << endl;
225 if ( name == "supplier" ) {
226 FGElectricalSupplier *s =
227 new FGElectricalSupplier( node->getStringValue("name"),
228 node->getStringValue("prop"),
229 node->getStringValue("kind"),
230 node->getDoubleValue("volts"),
231 node->getDoubleValue("amps") );
232 suppliers.push_back( s );
233 } else if ( name == "bus" ) {
235 new FGElectricalBus( node->getStringValue("name"),
236 node->getStringValue("prop") );
237 buses.push_back( b );
238 } else if ( name == "output" ) {
239 FGElectricalOutput *o =
240 new FGElectricalOutput( node->getStringValue("name"),
241 node->getStringValue("prop") );
242 outputs.push_back( o );
243 } else if ( name == "connector" ) {
244 FGElectricalConnector *c =
245 new FGElectricalConnector();
246 connectors.push_back( c );
247 SGPropertyNode *child;
248 int ccount = node->nChildren();
249 for ( j = 0; j < ccount; ++j ) {
250 child = node->getChild(j);
251 string cname = child->getName();
252 string cval = child->getStringValue();
253 // cout << " " << cname << " = " << cval << endl;
254 if ( cname == "input" ) {
255 FGElectricalComponent *s = find( child->getStringValue() );
258 if ( s->get_kind() == FG_SUPPLIER ) {
260 } else if ( s->get_kind() == FG_BUS ) {
263 SG_LOG( SG_ALL, SG_ALERT,
264 "Attempt to connect to something that can't provide an output: "
265 << child->getStringValue() );
269 SG_LOG( SG_ALL, SG_ALERT,
270 "Can't find named source: "
271 << child->getStringValue() );
274 } else if ( cname == "output" ) {
275 FGElectricalComponent *s = find( child->getStringValue() );
278 if ( s->get_kind() == FG_BUS ) {
280 } else if ( s->get_kind() == FG_OUTPUT ) {
283 SG_LOG( SG_ALL, SG_ALERT,
284 "Attempt to connect to something that can't provide an input: "
285 << child->getStringValue() );
289 SG_LOG( SG_ALL, SG_ALERT,
290 "Can't find named source: "
291 << child->getStringValue() );
294 } else if ( cname == "switch" ) {
295 // set default value of switch to true
296 // cout << "Switch = " << child->getStringValue() << endl;
297 fgSetBool( child->getStringValue(), true );
298 c->add_switch( fgGetNode( child->getStringValue(), true ) );
302 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
312 // propogate the electrical current through the network
313 void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
317 // determine the current to carry forward
318 double current = 0.0;
319 if ( node->get_kind() == FG_SUPPLIER ) {
320 // cout << s << " is a supplier" << endl;
321 current = ((FGElectricalSupplier *)node)->get_output();
322 } else if ( node->get_kind() == FG_BUS ) {
323 // cout << s << " is a bus" << endl;
325 } else if ( node->get_kind() == FG_OUTPUT ) {
326 // cout << s << " is an output" << endl;
328 } else if ( node->get_kind() == FG_CONNECTOR ) {
329 // cout << s << " is a connector" << endl;
330 if ( ((FGElectricalConnector *)node)->get_state() ) {
335 // cout << s << " val = " << current << endl;
337 SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
340 if ( current > node->get_value() ) {
341 node->set_value( current );
344 if ( ! node->get_prop().empty() ) {
345 fgSetDouble( node->get_prop().c_str(), node->get_value() );
347 // cout << s << node->get_name() << " -> " << node->get_value() << endl;
349 // propogate to all children
351 for ( i = 0; i < node->get_num_outputs(); ++i ) {
352 propogate( node->get_output(i), current, s );
357 // search for the named component and return a pointer to it, NULL otherwise
358 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
363 for ( i = 0; i < suppliers.size(); ++i ) {
364 s = suppliers[i]->get_name();
365 // cout << " " << s << endl;
372 for ( i = 0; i < buses.size(); ++i ) {
373 s = buses[i]->get_name();
374 // cout << " " << s << endl;
380 // then search outputs
381 for ( i = 0; i < outputs.size(); ++i ) {
382 s = outputs[i]->get_name();
383 // cout << " " << s << endl;