]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.cxx
Currently, when the sim pauses, all IO is also halted. To me it generally
[flightgear.git] / src / Systems / electrical.cxx
1 // electrical.cxx - a flexible, generic electrical system model.
2 //
3 // Written by Curtis Olson, started September 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/misc/sg_path.hxx>
26
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
29
30 #include "electrical.hxx"
31
32
33 FGElectricalComponent::FGElectricalComponent() :
34     kind(-1),
35     name(""),
36     volts(0.0),
37     load_amps(0.0)
38 {
39 }
40
41
42 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
43     kind = FG_SUPPLIER;
44
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" ) {
50         model = FG_BATTERY;
51     } else if ( _model == "alternator" ) {
52         model = FG_ALTERNATOR;
53     } else if ( _model == "external" ) {
54         model = FG_EXTERNAL;
55     } else {
56         model = FG_UNKNOWN;
57     }
58     volts = node->getDoubleValue("volts");
59     amps = node->getDoubleValue("amps");
60     rpm_src = node->getStringValue("rpm-source");
61
62     int i;
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;
69             add_prop( prop );
70             fgSetDouble( prop.c_str(), amps );
71         }
72     }
73
74     _rpm_node = fgGetNode( rpm_src.c_str(), true);
75 }  
76
77
78 double FGElectricalSupplier::get_output() {
79     if ( model == FG_BATTERY ) {
80         // cout << "battery amps = " << amps << endl;
81         return amps;
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;
88         if ( factor > 1.0 ) {
89             factor = 1.0;
90         }
91         // cout << "alternator amps = " << amps * factor << endl;
92         return amps * factor;
93     } else if ( model == FG_EXTERNAL ) {
94         // cout << "external amps = " << 0.0 << endl;
95         return 0.0;
96     } else {
97         SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
98     }
99
100     return 0.0;
101 }
102
103
104 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
105     kind = FG_BUS;
106
107     name = node->getStringValue("name");
108     int i;
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();
113             add_prop( prop );
114         }
115     }
116 }  
117
118
119 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
120     kind = FG_OUTPUT;
121     output_amps = 0.1;          // arbitrary default value
122
123     name = node->getStringValue("name");
124     SGPropertyNode *draw = node->getNode("rated-draw");
125     if ( draw != NULL ) {
126         output_amps = draw->getDoubleValue();
127     }
128     // cout << "rated draw = " << output_amps << endl;
129
130     int i;
131     for ( i = 0; i < node->nChildren(); ++i ) {
132         SGPropertyNode *child = node->getChild(i);
133         if ( !strcmp(child->getName(), "prop") ) {
134             string prop = child->getStringValue();
135             add_prop( prop );
136         }
137     }
138 }  
139
140
141 FGElectricalSwitch::FGElectricalSwitch( SGPropertyNode *node ) :
142     switch_node( NULL ),
143     rating_amps( 0.0f ),
144     circuit_breaker( false )
145 {
146     bool initial_state = true;
147     int i;
148     for ( i = 0; i < node->nChildren(); ++i ) {
149         SGPropertyNode *child = node->getChild(i);
150         string cname = child->getName();
151         string cval = child->getStringValue();
152         if ( cname == "prop" ) {
153             switch_node = fgGetNode( cval.c_str(), true );
154             // cout << "switch node = " << cval << endl;
155         } else if ( cname == "initial-state" ) {
156             if ( cval == "off" || cval == "false" ) {
157                 initial_state = false;
158             }
159             // cout << "initial state = " << initial_state << endl;
160         } else if ( cname == "rating-amps" ) {
161             rating_amps = atof( cval.c_str() );
162             circuit_breaker = true;
163             // cout << "initial state = " << initial_state << endl;
164         }            
165     }
166
167     switch_node->setBoolValue( initial_state );
168     // cout << "  value = " << switch_node->getBoolValue() << endl;
169 }
170
171
172 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
173                                                FGElectricalSystem *es ) {
174     kind = FG_CONNECTOR;
175     name = "connector";
176     int i;
177     for ( i = 0; i < node->nChildren(); ++i ) {
178         SGPropertyNode *child = node->getChild(i);
179         string cname = child->getName();
180         string cval = child->getStringValue();
181         // cout << "  " << cname << " = " << cval << endl;
182         if ( cname == "input" ) {
183             FGElectricalComponent *s = es->find( child->getStringValue() );
184             if ( s != NULL ) {
185                 add_input( s );
186                 if ( s->get_kind() == FG_SUPPLIER ) {
187                     s->add_output( this );
188                 } else if ( s->get_kind() == FG_BUS ) {
189                     s->add_output( this );
190                 } else {
191                     SG_LOG( SG_ALL, SG_ALERT,
192                             "Attempt to connect to something that can't provide an output: " 
193                             << child->getStringValue() );
194                 }
195             } else {
196                 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: " 
197                         << child->getStringValue() );
198             }
199         } else if ( cname == "output" ) {
200             FGElectricalComponent *s = es->find( child->getStringValue() );
201             if ( s != NULL ) {
202                 add_output( s );
203                 if ( s->get_kind() == FG_BUS ) {
204                     s->add_input( this );
205                 } else if ( s->get_kind() == FG_OUTPUT ) {
206                     s->add_input( this );
207                 } else {
208                     SG_LOG( SG_ALL, SG_ALERT,
209                             "Attempt to connect to something that can't provide an input: " 
210                             << child->getStringValue() );
211                 }
212             } else {
213                 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: " 
214                         << child->getStringValue() );
215             }
216         } else if ( cname == "switch" ) {
217              // cout << "Switch = " << child->getStringValue() << endl;
218             FGElectricalSwitch s( child );
219             add_switch( s );
220         }
221     }
222 }  
223
224
225 // set all switches to the specified state
226 void FGElectricalConnector::set_switches( bool state ) {
227     // cout << "setting switch state to " << state << endl;
228     for ( unsigned int i = 0; i < switches.size(); ++i ) {
229         switches[i].set_state( state );
230     }
231 }
232
233
234 // return true if all switches are true, false otherwise.  A connector
235 // could have multiple switches, but they all need to be true(closed)
236 // for current to get through.
237 bool FGElectricalConnector::get_state() {
238     unsigned int i;
239     for ( i = 0; i < switches.size(); ++i ) {
240         if ( ! switches[i].get_state() ) {
241             return false;
242         }
243     }
244
245     return true;
246 }
247
248
249 FGElectricalSystem::FGElectricalSystem () :
250     enabled(false)
251 {
252 }
253
254
255 FGElectricalSystem::~FGElectricalSystem () {
256 }
257
258
259 void FGElectricalSystem::init () {
260     config_props = new SGPropertyNode;
261
262     SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
263     _volts_out = fgGetNode( "/systems/electrical/volts", true );
264     _amps_out = fgGetNode( "/systems/electrical/amps", true );
265
266     if (path_n) {
267         SGPath config( globals->get_fg_root() );
268         config.append( path_n->getStringValue() );
269
270         SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
271                 << config.str() );
272         try {
273             readProperties( config.str(), config_props );
274
275             if ( build() ) {
276                 enabled = true;
277             } else {
278                 SG_LOG( SG_ALL, SG_ALERT,
279                         "Detected an internal inconsistancy in the electrical");
280                 SG_LOG( SG_ALL, SG_ALERT,
281                         " system specification file.  See earlier errors for" );
282                 SG_LOG( SG_ALL, SG_ALERT,
283                         " details.");
284                 exit(-1);
285             }        
286         } catch (const sg_exception& exc) {
287             SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
288                     << config.str() );
289         }
290
291     } else
292         SG_LOG( SG_ALL, SG_ALERT,
293                 "No electrical model specified for this model!");
294
295     delete config_props;
296 }
297
298
299 void FGElectricalSystem::bind () {
300 }
301
302
303 void FGElectricalSystem::unbind () {
304 }
305
306
307 void FGElectricalSystem::update (double dt) {
308     if ( !enabled ) {
309         _amps_out->setDoubleValue(0);
310         return;
311     }
312
313     // cout << "Updating electrical system" << endl;
314
315     unsigned int i;
316
317     // zero everything out before we start
318     for ( i = 0; i < suppliers.size(); ++i ) {
319         suppliers[i]->set_volts( 0.0 );
320         suppliers[i]->set_load_amps( 0.0 );
321     }
322     for ( i = 0; i < buses.size(); ++i ) {
323         buses[i]->set_volts( 0.0 );
324         buses[i]->set_load_amps( 0.0 );
325     }
326     for ( i = 0; i < outputs.size(); ++i ) {
327         outputs[i]->set_volts( 0.0 );
328         outputs[i]->set_load_amps( 0.0 );
329     }
330     for ( i = 0; i < connectors.size(); ++i ) {
331         connectors[i]->set_volts( 0.0 );
332         connectors[i]->set_load_amps( 0.0 );
333     }
334
335     // for each supplier, propagate the electrical current
336     for ( i = 0; i < suppliers.size(); ++i ) {
337         // cout << " Updating: " << suppliers[i]->get_name() << endl;
338         propagate( suppliers[i], 0.0, " " );
339     }
340
341     double alt_norm
342         = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
343
344     // impliment an extremely simplistic voltage model (assumes
345     // certain naming conventions in electrical system config)
346     double volts = 0.0;
347     if ( fgGetBool("/controls/switches/master-bat") ) {
348         volts = 24.0;
349     }
350     if ( fgGetBool("/controls/switches/master-alt") &&
351          fgGetDouble("/engines/engine[0]/rpm") > 800 )
352     {
353         double alt_contrib = 28.0;
354         if ( alt_contrib > volts ) {
355             volts = alt_contrib;
356         }
357     }
358     _volts_out->setDoubleValue( volts );
359
360     // impliment an extremely simplistic amps model (assumes certain
361     // naming conventions in the electrical system config) ... FIXME:
362     // make this more generic
363     double amps = 0.0;
364     if ( fgGetBool("/controls/switches/master-bat") ) {
365         if ( fgGetBool("/controls/switches/master-alt") &&
366              fgGetDouble("/engines/engine[0]/rpm") > 800 )
367         {
368             amps += 40.0 * alt_norm;
369         }
370         amps -= 15.0;            // normal load
371         if ( fgGetBool("/controls/switches/flashing-beacon") ) {
372             amps -= 7.5;
373         }
374         if ( fgGetBool("/controls/switches/nav-lights") ) {
375             amps -= 7.5;
376         }
377         if ( amps > 7.0 ) {
378             amps = 7.0;
379         }
380     }
381     _amps_out->setDoubleValue( amps );
382 }
383
384
385 bool FGElectricalSystem::build () {
386     SGPropertyNode *node;
387     int i;
388
389     int count = config_props->nChildren();
390     for ( i = 0; i < count; ++i ) {
391         node = config_props->getChild(i);
392         string name = node->getName();
393         // cout << name << endl;
394         if ( name == "supplier" ) {
395             FGElectricalSupplier *s =
396                 new FGElectricalSupplier( node );
397             suppliers.push_back( s );
398         } else if ( name == "bus" ) {
399             FGElectricalBus *b =
400                 new FGElectricalBus( node );
401             buses.push_back( b );
402         } else if ( name == "output" ) {
403             FGElectricalOutput *o =
404                 new FGElectricalOutput( node );
405             outputs.push_back( o );
406         } else if ( name == "connector" ) {
407             FGElectricalConnector *c =
408                 new FGElectricalConnector( node, this );
409             connectors.push_back( c );
410         } else {
411             SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: " 
412                     << name );
413             return false;
414         }
415     }
416
417     return true;
418 }
419
420
421 // propagate the electrical current through the network, returns the
422 // total current drawn by the children of this node.
423 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
424                                     string s ) {
425     s += " ";
426     
427     float current_amps = 0.0;
428
429     // determine the current to carry forward
430     double volts = 0.0;
431     if ( !fgGetBool("/systems/electrical/serviceable") ) {
432         volts = 0;
433     } else if ( node->get_kind() == FG_SUPPLIER ) {
434         // cout << s << " is a supplier" << endl;
435         volts = ((FGElectricalSupplier *)node)->get_output();
436     } else if ( node->get_kind() == FG_BUS ) {
437         // cout << s << " is a bus" << endl;
438         volts = val;
439     } else if ( node->get_kind() == FG_OUTPUT ) {
440         // cout << s << " is an output" << endl;
441         volts = val;
442         if ( volts > 1.0 ) {
443             // draw current if we have voltage
444             current_amps = ((FGElectricalOutput *)node)->get_output_amps();
445         }
446     } else if ( node->get_kind() == FG_CONNECTOR ) {
447         // cout << s << " is a connector" << endl;
448         if ( ((FGElectricalConnector *)node)->get_state() ) {
449             volts = val;
450         } else {
451             volts = 0.0;
452         }
453         // cout << s << "  val = " << volts << endl;
454     } else {
455         SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
456     }
457
458     if ( volts > node->get_volts() ) {
459         node->set_volts( volts );
460     }
461
462     int i;
463
464     // publish values to specified properties
465     for ( i = 0; i < node->get_num_props(); ++i ) {
466         fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
467     }
468     // cout << s << node->get_name() << " -> " << node->get_value() << endl;
469
470     // propagate to all children
471     for ( i = 0; i < node->get_num_outputs(); ++i ) {
472         current_amps += propagate( node->get_output(i), volts, s );
473     }
474
475     // if not an output node, register the downstream current draw
476     // with this node.  If volts are zero, current draw should be zero.
477     if ( node->get_kind() != FG_OUTPUT ) {
478         node->set_load_amps( current_amps );
479     }
480     // cout << s << node->get_name() << " -> " << current_amps << endl;
481
482     return current_amps;
483 }
484
485
486 // search for the named component and return a pointer to it, NULL otherwise
487 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
488     unsigned int i;
489     string s;
490
491     // search suppliers
492     for ( i = 0; i < suppliers.size(); ++i ) {
493         s = suppliers[i]->get_name();
494         // cout <<  "    " << s << endl;
495         if ( s == name ) {
496             return suppliers[i];
497         }
498     }
499
500     // then search buses
501     for ( i = 0; i < buses.size(); ++i ) {
502         s = buses[i]->get_name();
503         // cout <<  "    " << s << endl;
504         if ( s == name ) {
505             return buses[i];
506         }
507     }
508
509     // then search outputs
510     for ( i = 0; i < outputs.size(); ++i ) {
511         s = outputs[i]->get_name();
512         // cout <<  "    " << s << endl;
513         if ( s == name ) {
514             return outputs[i];
515         }
516     }
517
518     // nothing found
519     return NULL;
520 }