]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[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_INFO, "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_WARN,
293                 "No electrical model specified for this model!");
294     }
295
296     delete config_props;
297 }
298
299
300 void FGElectricalSystem::bind () {
301 }
302
303
304 void FGElectricalSystem::unbind () {
305 }
306
307
308 void FGElectricalSystem::update (double dt) {
309     if ( !enabled ) {
310         _amps_out->setDoubleValue(0);
311         return;
312     }
313
314     // cout << "Updating electrical system" << endl;
315
316     unsigned int i;
317
318     // zero everything out before we start
319     for ( i = 0; i < suppliers.size(); ++i ) {
320         suppliers[i]->set_volts( 0.0 );
321         suppliers[i]->set_load_amps( 0.0 );
322     }
323     for ( i = 0; i < buses.size(); ++i ) {
324         buses[i]->set_volts( 0.0 );
325         buses[i]->set_load_amps( 0.0 );
326     }
327     for ( i = 0; i < outputs.size(); ++i ) {
328         outputs[i]->set_volts( 0.0 );
329         outputs[i]->set_load_amps( 0.0 );
330     }
331     for ( i = 0; i < connectors.size(); ++i ) {
332         connectors[i]->set_volts( 0.0 );
333         connectors[i]->set_load_amps( 0.0 );
334     }
335
336     // for each supplier, propagate the electrical current
337     for ( i = 0; i < suppliers.size(); ++i ) {
338         // cout << " Updating: " << suppliers[i]->get_name() << endl;
339         propagate( suppliers[i], 0.0, " " );
340     }
341
342     double alt_norm
343         = fgGetDouble("/systems/electrical/suppliers/alternator") / 60.0;
344
345     // impliment an extremely simplistic voltage model (assumes
346     // certain naming conventions in electrical system config)
347     double volts = 0.0;
348     if ( fgGetBool("/controls/switches/master-bat") ) {
349         volts = 24.0;
350     }
351     if ( fgGetBool("/controls/switches/master-alt") &&
352          fgGetDouble("/engines/engine[0]/rpm") > 800 )
353     {
354         double alt_contrib = 28.0;
355         if ( alt_contrib > volts ) {
356             volts = alt_contrib;
357         }
358     }
359     _volts_out->setDoubleValue( volts );
360
361     // impliment an extremely simplistic amps model (assumes certain
362     // naming conventions in the electrical system config) ... FIXME:
363     // make this more generic
364     double amps = 0.0;
365     if ( fgGetBool("/controls/switches/master-bat") ) {
366         if ( fgGetBool("/controls/switches/master-alt") &&
367              fgGetDouble("/engines/engine[0]/rpm") > 800 )
368         {
369             amps += 40.0 * alt_norm;
370         }
371         amps -= 15.0;            // normal load
372         if ( fgGetBool("/controls/switches/flashing-beacon") ) {
373             amps -= 7.5;
374         }
375         if ( fgGetBool("/controls/switches/nav-lights") ) {
376             amps -= 7.5;
377         }
378         if ( amps > 7.0 ) {
379             amps = 7.0;
380         }
381     }
382     _amps_out->setDoubleValue( amps );
383 }
384
385
386 bool FGElectricalSystem::build () {
387     SGPropertyNode *node;
388     int i;
389
390     int count = config_props->nChildren();
391     for ( i = 0; i < count; ++i ) {
392         node = config_props->getChild(i);
393         string name = node->getName();
394         // cout << name << endl;
395         if ( name == "supplier" ) {
396             FGElectricalSupplier *s =
397                 new FGElectricalSupplier( node );
398             suppliers.push_back( s );
399         } else if ( name == "bus" ) {
400             FGElectricalBus *b =
401                 new FGElectricalBus( node );
402             buses.push_back( b );
403         } else if ( name == "output" ) {
404             FGElectricalOutput *o =
405                 new FGElectricalOutput( node );
406             outputs.push_back( o );
407         } else if ( name == "connector" ) {
408             FGElectricalConnector *c =
409                 new FGElectricalConnector( node, this );
410             connectors.push_back( c );
411         } else {
412             SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: " 
413                     << name );
414             return false;
415         }
416     }
417
418     return true;
419 }
420
421
422 // propagate the electrical current through the network, returns the
423 // total current drawn by the children of this node.
424 float FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
425                                     string s ) {
426     s += " ";
427     
428     float current_amps = 0.0;
429
430     // determine the current to carry forward
431     double volts = 0.0;
432     if ( !fgGetBool("/systems/electrical/serviceable") ) {
433         volts = 0;
434     } else if ( node->get_kind() == FG_SUPPLIER ) {
435         // cout << s << " is a supplier" << endl;
436         volts = ((FGElectricalSupplier *)node)->get_output();
437     } else if ( node->get_kind() == FG_BUS ) {
438         // cout << s << " is a bus" << endl;
439         volts = val;
440     } else if ( node->get_kind() == FG_OUTPUT ) {
441         // cout << s << " is an output" << endl;
442         volts = val;
443         if ( volts > 1.0 ) {
444             // draw current if we have voltage
445             current_amps = ((FGElectricalOutput *)node)->get_output_amps();
446         }
447     } else if ( node->get_kind() == FG_CONNECTOR ) {
448         // cout << s << " is a connector" << endl;
449         if ( ((FGElectricalConnector *)node)->get_state() ) {
450             volts = val;
451         } else {
452             volts = 0.0;
453         }
454         // cout << s << "  val = " << volts << endl;
455     } else {
456         SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
457     }
458
459     if ( volts > node->get_volts() ) {
460         node->set_volts( volts );
461     }
462
463     int i;
464
465     // publish values to specified properties
466     for ( i = 0; i < node->get_num_props(); ++i ) {
467         fgSetDouble( node->get_prop(i).c_str(), node->get_volts() );
468     }
469     // cout << s << node->get_name() << " -> " << node->get_value() << endl;
470
471     // propagate to all children
472     for ( i = 0; i < node->get_num_outputs(); ++i ) {
473         current_amps += propagate( node->get_output(i), volts, s );
474     }
475
476     // if not an output node, register the downstream current draw
477     // with this node.  If volts are zero, current draw should be zero.
478     if ( node->get_kind() != FG_OUTPUT ) {
479         node->set_load_amps( current_amps );
480     }
481     // cout << s << node->get_name() << " -> " << current_amps << endl;
482
483     return current_amps;
484 }
485
486
487 // search for the named component and return a pointer to it, NULL otherwise
488 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
489     unsigned int i;
490     string s;
491
492     // search suppliers
493     for ( i = 0; i < suppliers.size(); ++i ) {
494         s = suppliers[i]->get_name();
495         // cout <<  "    " << s << endl;
496         if ( s == name ) {
497             return suppliers[i];
498         }
499     }
500
501     // then search buses
502     for ( i = 0; i < buses.size(); ++i ) {
503         s = buses[i]->get_name();
504         // cout <<  "    " << s << endl;
505         if ( s == name ) {
506             return buses[i];
507         }
508     }
509
510     // then search outputs
511     for ( i = 0; i < outputs.size(); ++i ) {
512         s = outputs[i]->get_name();
513         // cout <<  "    " << s << endl;
514         if ( s == name ) {
515             return outputs[i];
516         }
517     }
518
519     // nothing found
520     return NULL;
521 }