]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
Moved random ground cover object management code (userdata.[ch]xx) over
[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
43 #include <Main/fgfs.hxx>
44
45
46 // Forward declaration
47 class FGElectricalSystem;
48
49
50 #define FG_UNKNOWN  -1
51 #define FG_SUPPLIER  0
52 #define FG_BUS       1
53 #define FG_OUTPUT    2
54 #define FG_CONNECTOR 3
55
56 // Base class for other electrical components
57 class FGElectricalComponent {
58
59 protected:
60
61     typedef vector<FGElectricalComponent *> comp_list;
62     typedef vector<string> string_list;
63
64     int kind;
65     string name;
66     double value;
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     inline double get_value() const { return value; }
81     inline void set_value( double val ) { value = val; }
82
83     inline int get_num_inputs() const { return outputs.size(); }
84     inline FGElectricalComponent *get_input( const int i ) {
85         return inputs[i];
86     }
87     inline void add_input( FGElectricalComponent *c ) {
88         inputs.push_back( c );
89     }
90
91     inline int get_num_outputs() const { return outputs.size(); }
92     inline FGElectricalComponent *get_output( const int i ) {
93         return outputs[i];
94     }
95     inline void add_output( FGElectricalComponent *c ) {
96         outputs.push_back( c );
97     }
98
99     inline int get_num_props() const { return props.size(); }
100     inline string get_prop( const int i ) {
101         return props[i];
102     }
103     inline void add_prop( const string &s ) {
104         props.push_back( s );
105     }
106
107 };
108
109
110 // Electrical supplier
111 class FGElectricalSupplier : public FGElectricalComponent {
112
113     SGPropertyNode_ptr _rpm_node;
114
115     enum FGSupplierType {
116         FG_BATTERY = 0,
117         FG_ALTERNATOR = 1,
118         FG_EXTERNAL = 2
119     };
120
121     string rpm_src;
122     int model;
123     double volts;
124     double amps;
125
126 public:
127
128     FGElectricalSupplier ( SGPropertyNode *node );
129     ~FGElectricalSupplier () {}
130
131     double get_output();
132 };
133
134
135 // Electrical bus (can take multiple inputs and provide multiple
136 // outputs)
137 class FGElectricalBus : public FGElectricalComponent {
138
139 public:
140
141     FGElectricalBus ( SGPropertyNode *node );
142     ~FGElectricalBus () {}
143 };
144
145
146 // A lot like an FGElectricalBus, but here for convenience and future
147 // flexibility
148 class FGElectricalOutput : public FGElectricalComponent {
149
150 public:
151
152     FGElectricalOutput ( SGPropertyNode *node );
153     ~FGElectricalOutput () {}
154 };
155
156
157 // Connects multiple sources to multiple destinations with optional
158 // switches/fuses/circuit breakers inline
159 class FGElectricalConnector : public FGElectricalComponent {
160
161     comp_list inputs;
162     comp_list outputs;
163     typedef vector<SGPropertyNode *> switch_list;
164     switch_list switches;
165
166 public:
167
168     FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
169     ~FGElectricalConnector () {}
170
171     void add_switch( SGPropertyNode *node ) {
172         switches.push_back( node );
173     }
174
175     // set all switches to the specified state
176     void set_switches( bool state );
177
178     bool get_state();
179 };
180
181
182 /**
183  * Model an electrical system.  This is a fairly simplistic system
184  * 
185  */
186
187 class FGElectricalSystem : public FGSubsystem
188 {
189
190 public:
191
192     FGElectricalSystem ();
193     virtual ~FGElectricalSystem ();
194
195     virtual void init ();
196     virtual void bind ();
197     virtual void unbind ();
198     virtual void update (double dt);
199
200     bool build ();
201     void propagate( FGElectricalComponent *node, double val, string s = "" );
202     FGElectricalComponent *find ( const string &name );
203
204 protected:
205
206     typedef vector<FGElectricalComponent *> comp_list;
207
208 private:
209
210     SGPropertyNode *config_props;
211
212     bool enabled;
213
214     comp_list suppliers;
215     comp_list buses;
216     comp_list outputs;
217     comp_list connectors;
218
219     SGPropertyNode *_volts_out;
220     SGPropertyNode *_amps_out;
221 };
222
223
224 #endif // _SYSTEMS_ELECTRICAL_HXX