]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.cxx
Check point commit:
[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 FGElectricalSupplier::FGElectricalSupplier ( string _name, string _model,
34                                              double _volts, double _amps )
35 {
36     kind = FG_SUPPLIER;
37
38     name = _name;
39     if ( _model == "battery" ) {
40         model = FG_BATTERY;
41     } else if ( _model == "alternator" ) {
42         model = FG_ALTERNATOR;
43     } else if ( _model == "external" ) {
44         model = FG_EXTERNAL;
45     } else {
46         model = FG_UNKNOWN;
47     }
48     volts = _volts;
49     amps = _amps;
50 }  
51
52
53 FGElectricalBus::FGElectricalBus ( string _name )
54 {
55     kind = FG_BUS;
56
57     name = _name;
58 }  
59
60
61 FGElectricalOutput::FGElectricalOutput ( string _name )
62 {
63     kind = FG_OUTPUT;
64
65     name = _name;
66 }  
67
68
69 FGElectricalConnector::FGElectricalConnector ()
70 {
71     kind = FG_CONNECTOR;
72 }  
73
74
75 FGElectricalSystem::FGElectricalSystem () :
76     enabled(false)
77 {
78 }
79
80
81 FGElectricalSystem::~FGElectricalSystem () {
82 }
83
84
85 void FGElectricalSystem::init () {
86     config_props = new SGPropertyNode;
87
88     SGPath config( globals->get_fg_root() );
89     config.append( fgGetString("/systems/electrical/path") );
90
91     SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
92             << config.str() );
93     try {
94         readProperties( config.str(), config_props );
95
96         if ( build() ) {
97             enabled = true;
98         } else {
99             SG_LOG( SG_ALL, SG_ALERT,
100                     "Detected an internal inconsistancy in the electrical" );
101             SG_LOG( SG_ALL, SG_ALERT,
102                     " system specification file.  See earlier errors for" );
103             SG_LOG( SG_ALL, SG_ALERT,
104                     " details.");
105             exit(-1);
106         }        
107     } catch (const sg_exception& exc) {
108         SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
109                 << config.str() );
110     }
111
112     delete config_props;
113 }
114
115
116 void FGElectricalSystem::bind () {
117 }
118
119
120 void FGElectricalSystem::unbind () {
121 }
122
123
124 void FGElectricalSystem::update (double dt) {
125 }
126
127
128 bool FGElectricalSystem::build () {
129     SGPropertyNode *node;
130     int i, j;
131
132     int count = config_props->nChildren();
133     for ( i = 0; i < count; ++i ) {
134         node = config_props->getChild(i);
135         string name = node->getName();
136         // cout << name << endl;
137         if ( name == "supplier" ) {
138             FGElectricalSupplier *s =
139                 new FGElectricalSupplier( node->getStringValue("name"),
140                                           node->getStringValue("kind"),
141                                           node->getDoubleValue("volts"),
142                                           node->getDoubleValue("amps") );
143             suppliers.push_back( s );
144         } else if ( name == "bus" ) {
145             FGElectricalBus *b =
146                 new FGElectricalBus( node->getStringValue("name") );
147             buses.push_back( b );
148         } else if ( name == "output" ) {
149             FGElectricalOutput *o =
150                 new FGElectricalOutput( node->getStringValue("name") );
151             outputs.push_back( o );
152         } else if ( name == "connector" ) {
153             FGElectricalConnector *c =
154                 new FGElectricalConnector();
155             connectors.push_back( c );
156             SGPropertyNode *child;
157             int ccount = node->nChildren();
158             for ( j = 0; j < ccount; ++j ) {
159                 child = node->getChild(j);
160                 string cname = child->getName();
161                 string cval = child->getStringValue();
162                 // cout << "  " << cname << " = " << cval << endl;
163                 if ( cname == "input" ) {
164                     FGElectricalComponent *s = find( child->getStringValue() );
165                     if ( s != NULL ) {
166                         c->add_input( s );
167                         if ( s->get_kind() == FG_SUPPLIER ) {
168                             ((FGElectricalSupplier *)s)->add_output( c );
169                         } else if ( s->get_kind() == FG_BUS ) {
170                             ((FGElectricalBus *)s)->add_output( c );
171                         } else {
172                             SG_LOG( SG_ALL, SG_ALERT,
173                                     "Attempt to connect to something that can't provide an output: " 
174                                     << child->getStringValue() );
175                             return false;
176                         }
177                     } else {
178                         SG_LOG( SG_ALL, SG_ALERT,
179                                 "Can't find named source: " 
180                                 << child->getStringValue() );
181                         return false;
182                     }
183                 } else if ( cname == "output" ) {
184                     FGElectricalComponent *s = find( child->getStringValue() );
185                     if ( s != NULL ) {
186                         c->add_output( s );
187                         if ( s->get_kind() == FG_BUS ) {
188                             ((FGElectricalBus *)s)->add_input( c );
189                         } else if ( s->get_kind() == FG_OUTPUT ) {
190                             ((FGElectricalOutput *)s)->add_input( c );
191                         } else {
192                             SG_LOG( SG_ALL, SG_ALERT,
193                                     "Attempt to connect to something that can't provide an input: " 
194                                     << child->getStringValue() );
195                             return false;
196                         }
197                     } else {
198                         SG_LOG( SG_ALL, SG_ALERT,
199                                 "Can't find named source: " 
200                                 << child->getStringValue() );
201                         return false;
202                     }
203                 } else if ( cname == "switch" ) {
204                     c->add_switch( child->getStringValue() );
205                 }
206             }
207         } else {
208             SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: " 
209                     << name );
210             return false;
211         }
212     }
213
214     return true;
215 }
216
217
218 // search for the named component and return a pointer to it, NULL otherwise
219 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
220     unsigned int i;
221     string s;
222
223     // search suppliers
224     for ( i = 0; i < suppliers.size(); ++i ) {
225         s = ((FGElectricalSupplier *)suppliers[i])->get_name();
226         // cout <<  "    " << s << endl;
227         if ( s == name ) {
228             return suppliers[i];
229         }
230     }
231
232     // then search buses
233     for ( i = 0; i < buses.size(); ++i ) {
234         s = ((FGElectricalBus *)buses[i])->get_name();
235         // cout <<  "    " << s << endl;
236         if ( s == name ) {
237             return buses[i];
238         }
239     }
240
241     // then search outputs
242     for ( i = 0; i < outputs.size(); ++i ) {
243         s = ((FGElectricalOutput *)outputs[i])->get_name();
244         // cout <<  "    " << s << endl;
245         if ( s == name ) {
246             return outputs[i];
247         }
248     }
249
250     // nothing found
251     return NULL;
252 }