]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.cxx
Moved some of the low level scene graph construction code over to simgear.
[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/misc/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     value(0.0)
37 {
38 }
39
40
41 FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
42     kind = FG_SUPPLIER;
43
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" ) {
49         model = FG_BATTERY;
50     } else if ( _model == "alternator" ) {
51         model = FG_ALTERNATOR;
52     } else if ( _model == "external" ) {
53         model = FG_EXTERNAL;
54     } else {
55         model = FG_UNKNOWN;
56     }
57     volts = node->getDoubleValue("volts");
58     amps = node->getDoubleValue("amps");
59     rpm_src = node->getStringValue("rpm-source");
60
61     int i;
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;
68             add_prop( prop );
69             fgSetDouble( prop.c_str(), amps );
70         }
71     }
72
73     _rpm_node = fgGetNode( rpm_src.c_str(), true);
74 }  
75
76
77 double FGElectricalSupplier::get_output() {
78     if ( model == FG_BATTERY ) {
79         // cout << "battery amps = " << amps << endl;
80         return amps;
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;
87         if ( factor > 1.0 ) {
88             factor = 1.0;
89         }
90         // cout << "alternator amps = " << amps * factor << endl;
91         return amps * factor;
92     } else if ( model == FG_EXTERNAL ) {
93         // cout << "external amps = " << 0.0 << endl;
94         return 0.0;
95     } else {
96         SG_LOG( SG_ALL, SG_ALERT, "unknown supplier type" );
97     }
98
99     return 0.0;
100 }
101
102
103 FGElectricalBus::FGElectricalBus ( SGPropertyNode *node ) {
104     kind = FG_BUS;
105
106     name = node->getStringValue("name");
107     int i;
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();
112             add_prop( prop );
113         }
114     }
115 }  
116
117
118 FGElectricalOutput::FGElectricalOutput ( SGPropertyNode *node ) {
119     kind = FG_OUTPUT;
120
121     name = node->getStringValue("name");
122     int i;
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();
127             add_prop( prop );
128         }
129     }
130 }  
131
132
133 FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
134                                                FGElectricalSystem *es ) {
135     kind = FG_CONNECTOR;
136     name = "connector";
137     int i;
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() );
145             if ( s != NULL ) {
146                 add_input( s );
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 );
151                 } else {
152                     SG_LOG( SG_ALL, SG_ALERT,
153                             "Attempt to connect to something that can't provide an output: " 
154                             << child->getStringValue() );
155                 }
156             } else {
157                 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: " 
158                         << child->getStringValue() );
159             }
160         } else if ( cname == "output" ) {
161             FGElectricalComponent *s = es->find( child->getStringValue() );
162             if ( s != NULL ) {
163                 add_output( s );
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 );
168                 } else {
169                     SG_LOG( SG_ALL, SG_ALERT,
170                             "Attempt to connect to something that can't provide an input: " 
171                             << child->getStringValue() );
172                 }
173             } else {
174                 SG_LOG( SG_ALL, SG_ALERT, "Can't find named source: " 
175                         << child->getStringValue() );
176             }
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 ) );
182         }
183     }
184
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 );
194             } else {
195                 set_switches( true );
196             }
197         }
198     }
199 }  
200
201
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 );
207     }
208 }
209
210
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() {
215     unsigned int i;
216     for ( i = 0; i < switches.size(); ++i ) {
217         if ( ! switches[i]->getBoolValue() ) {
218             return false;
219         }
220     }
221
222     return true;
223 }
224
225
226 FGElectricalSystem::FGElectricalSystem () :
227     enabled(false)
228 {
229 }
230
231
232 FGElectricalSystem::~FGElectricalSystem () {
233 }
234
235
236 void FGElectricalSystem::init () {
237     config_props = new SGPropertyNode;
238
239     SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
240
241     if (path_n) {
242         SGPath config( globals->get_fg_root() );
243         config.append( path_n->getStringValue() );
244
245         SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
246                 << config.str() );
247         try {
248             readProperties( config.str(), config_props );
249
250             if ( build() ) {
251                 enabled = true;
252             } else {
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,
258                         " details.");
259                 exit(-1);
260             }        
261         } catch (const sg_exception& exc) {
262             SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
263                     << config.str() );
264         }
265
266     } else
267         SG_LOG( SG_ALL, SG_ALERT,
268                 "No electrical model specified for this model!");
269
270     delete config_props;
271 }
272
273
274 void FGElectricalSystem::bind () {
275 }
276
277
278 void FGElectricalSystem::unbind () {
279 }
280
281
282 void FGElectricalSystem::update (double dt) {
283     if ( !enabled ) {
284         return;
285     }
286
287     // cout << "Updating electrical system" << endl;
288
289     unsigned int i;
290
291     // zero everything out before we start
292     for ( i = 0; i < suppliers.size(); ++i ) {
293         suppliers[i]->set_value( 0.0 );
294     }
295     for ( i = 0; i < buses.size(); ++i ) {
296         buses[i]->set_value( 0.0 );
297     }
298     for ( i = 0; i < outputs.size(); ++i ) {
299         outputs[i]->set_value( 0.0 );
300     }
301     for ( i = 0; i < connectors.size(); ++i ) {
302         connectors[i]->set_value( 0.0 );
303     }
304
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, " " );
309     }
310
311 }
312
313
314 bool FGElectricalSystem::build () {
315     SGPropertyNode *node;
316     int i;
317
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" ) {
328             FGElectricalBus *b =
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 );
339         } else {
340             SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: " 
341                     << name );
342             return false;
343         }
344     }
345
346     return true;
347 }
348
349
350 // propagate the electrical current through the network
351 void FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
352                                     string s ) {
353     s += " ";
354
355     // determine the current to carry forward
356     double current = 0.0;
357     if ( !fgGetBool("/systems/electrical/serviceable") ) {
358         current = 0;
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;
364         current = val;
365     } else if ( node->get_kind() == FG_OUTPUT ) {
366         // cout << s << " is an output" << endl;
367         current = val;
368     } else if ( node->get_kind() == FG_CONNECTOR ) {
369         // cout << s << " is a connector" << endl;
370         if ( ((FGElectricalConnector *)node)->get_state() ) {
371             current = val;
372         } else {
373             current = 0.0;
374         }
375         // cout << s << "  val = " << current << endl;
376     } else {
377         SG_LOG( SG_ALL, SG_ALERT, "unkown node type" );
378     }
379
380     if ( current > node->get_value() ) {
381         node->set_value( current );
382     }
383
384     int i;
385
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() );
389     }
390     // cout << s << node->get_name() << " -> " << node->get_value() << endl;
391
392     // propagate to all children
393     for ( i = 0; i < node->get_num_outputs(); ++i ) {
394         propagate( node->get_output(i), current, s );
395     }
396 }
397
398
399 // search for the named component and return a pointer to it, NULL otherwise
400 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
401     unsigned int i;
402     string s;
403
404     // search suppliers
405     for ( i = 0; i < suppliers.size(); ++i ) {
406         s = suppliers[i]->get_name();
407         // cout <<  "    " << s << endl;
408         if ( s == name ) {
409             return suppliers[i];
410         }
411     }
412
413     // then search buses
414     for ( i = 0; i < buses.size(); ++i ) {
415         s = buses[i]->get_name();
416         // cout <<  "    " << s << endl;
417         if ( s == name ) {
418             return buses[i];
419         }
420     }
421
422     // then search outputs
423     for ( i = 0; i < outputs.size(); ++i ) {
424         s = outputs[i]->get_name();
425         // cout <<  "    " << s << endl;
426         if ( s == name ) {
427             return outputs[i];
428         }
429     }
430
431     // nothing found
432     return NULL;
433 }