]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
f36f92af9159d49f3e62cd1b9ec0f1a46ffc3dd2
[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     string rpm_src;
121     int model;
122     double volts;
123     double amps;
124
125 public:
126
127     FGElectricalSupplier ( SGPropertyNode *node );
128     ~FGElectricalSupplier () {}
129
130     double get_output();
131 };
132
133
134 // Electrical bus (can take multiple inputs and provide multiple
135 // outputs)
136 class FGElectricalBus : public FGElectricalComponent {
137
138 public:
139
140     FGElectricalBus ( SGPropertyNode *node );
141     ~FGElectricalBus () {}
142 };
143
144
145 // A lot like an FGElectricalBus, but here for convenience and future
146 // flexibility
147 class FGElectricalOutput : public FGElectricalComponent {
148
149 public:
150
151     FGElectricalOutput ( SGPropertyNode *node );
152     ~FGElectricalOutput () {}
153 };
154
155
156 // Connects multiple sources to multiple destinations with optional
157 // switches/fuses/circuit breakers inline
158 class FGElectricalConnector : public FGElectricalComponent {
159
160     comp_list inputs;
161     comp_list outputs;
162     typedef vector<SGPropertyNode *> switch_list;
163     switch_list switches;
164
165 public:
166
167     FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
168     ~FGElectricalConnector () {}
169
170     void add_switch( SGPropertyNode *node ) {
171         switches.push_back( node );
172     }
173
174     // set all switches to the specified state
175     void set_switches( bool state );
176
177     bool get_state();
178 };
179
180
181 /**
182  * Model an electrical system.  This is a fairly simplistic system
183  * 
184  */
185
186 class FGElectricalSystem : public FGSubsystem
187 {
188
189 public:
190
191     FGElectricalSystem ();
192     virtual ~FGElectricalSystem ();
193
194     virtual void init ();
195     virtual void bind ();
196     virtual void unbind ();
197     virtual void update (double dt);
198
199     bool build ();
200     void propagate( FGElectricalComponent *node, double val, string s = "" );
201     FGElectricalComponent *find ( const string &name );
202
203 protected:
204
205     typedef vector<FGElectricalComponent *> comp_list;
206
207 private:
208
209     SGPropertyNode *config_props;
210
211     bool enabled;
212
213     comp_list suppliers;
214     comp_list buses;
215     comp_list outputs;
216     comp_list connectors;
217 };
218
219
220 #endif // _SYSTEMS_ELECTRICAL_HXX