]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[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 );
182
183     ~FGElectricalSwitch() { };
184
185     inline bool get_state() const { return switch_node->getBoolValue(); }
186     void set_state( bool val ) { switch_node->setBoolValue( val ); }
187 };
188
189
190 // Connects multiple sources to multiple destinations with optional
191 // switches/fuses/circuit breakers inline
192 class FGElectricalConnector : public FGElectricalComponent {
193
194     comp_list inputs;
195     comp_list outputs;
196     typedef vector< FGElectricalSwitch> switch_list;
197     switch_list switches;
198
199 public:
200
201     FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
202     ~FGElectricalConnector () {}
203
204     void add_switch( FGElectricalSwitch s ) {
205         switches.push_back( s );
206     }
207
208     // set all switches to the specified state
209     void set_switches( bool state );
210
211     bool get_state();
212 };
213
214
215 /**
216  * Model an electrical system.  This is a fairly simplistic system
217  * 
218  */
219
220 class FGElectricalSystem : public SGSubsystem
221 {
222
223 public:
224
225     FGElectricalSystem ();
226     virtual ~FGElectricalSystem ();
227
228     virtual void init ();
229     virtual void bind ();
230     virtual void unbind ();
231     virtual void update (double dt);
232
233     bool build ();
234     float propagate( FGElectricalComponent *node, double val, string s = "" );
235     FGElectricalComponent *find ( const string &name );
236
237 protected:
238
239     typedef vector<FGElectricalComponent *> comp_list;
240
241 private:
242
243     SGPropertyNode *config_props;
244
245     bool enabled;
246
247     comp_list suppliers;
248     comp_list buses;
249     comp_list outputs;
250     comp_list connectors;
251
252     SGPropertyNode *_volts_out;
253     SGPropertyNode *_amps_out;
254 };
255
256
257 #endif // _SYSTEMS_ELECTRICAL_HXX