]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
16a86ba8e5c6650eb19a1413449a08a898f6b3a1
[flightgear.git] / src / Systems / electrical.hxx
1 // electrical.hxx - 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 #ifndef _SYSTEMS_ELECTRICAL_HXX
25 #define _SYSTEMS_ELECTRICAL_HXX 1
26
27 #ifndef __cplusplus
28 # error This library requires C++
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34
35 #include STL_STRING
36 #include <vector>
37
38 SG_USING_STD(string);
39 SG_USING_STD(vector);
40
41 #include <simgear/props/props.hxx>
42 #include <simgear/structure/subsystem_mgr.hxx>
43
44
45 // Forward declaration
46 class FGElectricalSystem;
47
48
49 #define FG_UNKNOWN  -1
50 #define FG_SUPPLIER  0
51 #define FG_BUS       1
52 #define FG_OUTPUT    2
53 #define FG_CONNECTOR 3
54
55 // Base class for other electrical components
56 class FGElectricalComponent {
57
58 protected:
59
60     typedef vector<FGElectricalComponent *> comp_list;
61     typedef vector<string> string_list;
62
63     int kind;
64     string name;
65     float volts;
66     float load_amps;
67
68     comp_list inputs;
69     comp_list outputs;
70     string_list props;
71
72 public:
73
74     FGElectricalComponent();
75     virtual ~FGElectricalComponent() {}
76
77     inline string get_name() { return name; }
78
79     inline int get_kind() const { return kind; }
80
81     inline float get_volts() const { return volts; }
82     inline void set_volts( float val ) { volts = val; }
83
84     inline float get_load_amps() const { return load_amps; }
85     inline void set_load_amps( float val ) { load_amps = val; }
86
87     inline int get_num_inputs() const { return outputs.size(); }
88     inline FGElectricalComponent *get_input( const int i ) {
89         return inputs[i];
90     }
91     inline void add_input( FGElectricalComponent *c ) {
92         inputs.push_back( c );
93     }
94
95     inline int get_num_outputs() const { return outputs.size(); }
96     inline FGElectricalComponent *get_output( const int i ) {
97         return outputs[i];
98     }
99     inline void add_output( FGElectricalComponent *c ) {
100         outputs.push_back( c );
101     }
102
103     inline int get_num_props() const { return props.size(); }
104     inline string get_prop( const int i ) {
105         return props[i];
106     }
107     inline void add_prop( const string &s ) {
108         props.push_back( s );
109     }
110
111 };
112
113
114 // Electrical supplier
115 class FGElectricalSupplier : public FGElectricalComponent {
116
117     SGPropertyNode_ptr _rpm_node;
118
119     enum FGSupplierType {
120         FG_BATTERY = 0,
121         FG_ALTERNATOR = 1,
122         FG_EXTERNAL = 2
123     };
124
125     string rpm_src;
126     int model;
127     double volts;
128     double amps;
129
130 public:
131
132     FGElectricalSupplier ( SGPropertyNode *node );
133     ~FGElectricalSupplier () {}
134
135     double get_output();
136 };
137
138
139 // Electrical bus (can take multiple inputs and provide multiple
140 // outputs)
141 class FGElectricalBus : public FGElectricalComponent {
142
143 public:
144
145     FGElectricalBus ( SGPropertyNode *node );
146     ~FGElectricalBus () {}
147 };
148
149
150 // A lot like an FGElectricalBus, but here for convenience and future
151 // flexibility
152 class FGElectricalOutput : public FGElectricalComponent {
153
154 private:
155
156     // number of amps drawn by this output
157     float output_amps;
158
159 public:
160
161     FGElectricalOutput ( SGPropertyNode *node );
162     ~FGElectricalOutput () {}
163
164     inline float get_output_amps() const { return output_amps; }
165     inline void set_output_amps( float val ) { output_amps = val; }
166 };
167
168
169 // Model an electrical switch.  If the rating_amps > 0 then this
170 // becomes a circuit breaker type switch that can trip
171 class FGElectricalSwitch {
172
173 private:
174
175     SGPropertyNode *switch_node;
176     float rating_amps;
177     bool circuit_breaker;
178
179 public:
180
181     FGElectricalSwitch( SGPropertyNode *node, float rate, bool cb ) {
182         switch_node = node;
183         rating_amps = rate;
184         circuit_breaker = cb;
185     }
186
187     ~FGElectricalSwitch() { };
188
189     inline bool get_state() const { return switch_node->getBoolValue(); }
190     void set_state( bool val ) { switch_node->setBoolValue( val ); }
191 };
192
193
194 // Connects multiple sources to multiple destinations with optional
195 // switches/fuses/circuit breakers inline
196 class FGElectricalConnector : public FGElectricalComponent {
197
198     comp_list inputs;
199     comp_list outputs;
200     typedef vector< FGElectricalSwitch> switch_list;
201     switch_list switches;
202
203 public:
204
205     FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
206     ~FGElectricalConnector () {}
207
208     void add_switch( FGElectricalSwitch s ) {
209         switches.push_back( s );
210     }
211
212     // set all switches to the specified state
213     void set_switches( bool state );
214
215     bool get_state();
216 };
217
218
219 /**
220  * Model an electrical system.  This is a fairly simplistic system
221  * 
222  */
223
224 class FGElectricalSystem : public SGSubsystem
225 {
226
227 public:
228
229     FGElectricalSystem ();
230     virtual ~FGElectricalSystem ();
231
232     virtual void init ();
233     virtual void bind ();
234     virtual void unbind ();
235     virtual void update (double dt);
236
237     bool build ();
238     float propagate( FGElectricalComponent *node, double val, string s = "" );
239     FGElectricalComponent *find ( const string &name );
240
241 protected:
242
243     typedef vector<FGElectricalComponent *> comp_list;
244
245 private:
246
247     SGPropertyNode *config_props;
248
249     bool enabled;
250
251     comp_list suppliers;
252     comp_list buses;
253     comp_list outputs;
254     comp_list connectors;
255
256     SGPropertyNode *_volts_out;
257     SGPropertyNode *_amps_out;
258 };
259
260
261 #endif // _SYSTEMS_ELECTRICAL_HXX