]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
3b20d298c5a34791eee37f560a60542368792d04
[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/misc/props.hxx>
42 #include <Main/fgfs.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     double value;
66
67     comp_list inputs;
68     comp_list outputs;
69     string_list props;
70
71 public:
72
73     FGElectricalComponent();
74     virtual ~FGElectricalComponent() {}
75
76     inline string get_name() { return name; }
77
78     inline int get_kind() const { return kind; }
79     inline double get_value() const { return value; }
80     inline void set_value( double val ) { value = val; }
81
82     inline int get_num_inputs() const { return outputs.size(); }
83     inline FGElectricalComponent *get_input( const int i ) {
84         return inputs[i];
85     }
86     inline void add_input( FGElectricalComponent *c ) {
87         inputs.push_back( c );
88     }
89
90     inline int get_num_outputs() const { return outputs.size(); }
91     inline FGElectricalComponent *get_output( const int i ) {
92         return outputs[i];
93     }
94     inline void add_output( FGElectricalComponent *c ) {
95         outputs.push_back( c );
96     }
97
98     inline int get_num_props() const { return props.size(); }
99     inline string get_prop( const int i ) {
100         return props[i];
101     }
102     inline void add_prop( const string &s ) {
103         props.push_back( s );
104     }
105
106 };
107
108
109 // Electrical supplier
110 class FGElectricalSupplier : public FGElectricalComponent {
111
112     SGPropertyNode_ptr _rpm_node;
113
114     enum FGSupplierType {
115         FG_BATTERY = 0,
116         FG_ALTERNATOR = 1,
117         FG_EXTERNAL = 2
118     };
119
120     int model;
121     double volts;
122     double amps;
123
124 public:
125
126     FGElectricalSupplier ( SGPropertyNode *node );
127     ~FGElectricalSupplier () {}
128
129     double get_output();
130 };
131
132
133 // Electrical bus (can take multiple inputs and provide multiple
134 // outputs)
135 class FGElectricalBus : public FGElectricalComponent {
136
137 public:
138
139     FGElectricalBus ( SGPropertyNode *node );
140     ~FGElectricalBus () {}
141 };
142
143
144 // A lot like an FGElectricalBus, but here for convenience and future
145 // flexibility
146 class FGElectricalOutput : public FGElectricalComponent {
147
148 public:
149
150     FGElectricalOutput ( SGPropertyNode *node );
151     ~FGElectricalOutput () {}
152 };
153
154
155 // Connects multiple sources to multiple destinations with optional
156 // switches/fuses/circuit breakers inline
157 class FGElectricalConnector : public FGElectricalComponent {
158
159     comp_list inputs;
160     comp_list outputs;
161     typedef vector<SGPropertyNode *> switch_list;
162     switch_list switches;
163
164 public:
165
166     FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
167     ~FGElectricalConnector () {}
168
169     void add_switch( SGPropertyNode *node ) {
170         switches.push_back( node );
171     }
172
173     // set all switches to the specified state
174     void set_switches( bool state );
175
176     bool get_state();
177 };
178
179
180 /**
181  * Model an electrical system.  This is a simple system with the
182  * alternator hardwired to engine[0]/rpm
183  *
184  * Input properties:
185  *
186  * /engines/engine[0]/rpm
187  *
188  * Output properties:
189  *
190  * 
191  */
192
193 class FGElectricalSystem : public FGSubsystem
194 {
195
196 public:
197
198     FGElectricalSystem ();
199     virtual ~FGElectricalSystem ();
200
201     virtual void init ();
202     virtual void bind ();
203     virtual void unbind ();
204     virtual void update (double dt);
205
206     bool build ();
207     void propagate( FGElectricalComponent *node, double val, string s = "" );
208     FGElectricalComponent *find ( const string &name );
209
210 protected:
211
212     typedef vector<FGElectricalComponent *> comp_list;
213
214 private:
215
216     SGPropertyNode *config_props;
217
218     bool enabled;
219
220     comp_list suppliers;
221     comp_list buses;
222     comp_list outputs;
223     comp_list connectors;
224 };
225
226
227 #endif // _SYSTEMS_ELECTRICAL_HXX