]> git.mxchange.org Git - flightgear.git/blob - src/Systems/electrical.hxx
1e94f2fb754fdea0ac40f07a3abdb2a7c4d9c5f9
[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 #define FG_UNKNOWN  -1
46 #define FG_SUPPLIER  0
47 #define FG_BUS       1
48 #define FG_OUTPUT    2
49 #define FG_CONNECTOR 3
50
51 // Base class for other electrical components
52 class FGElectricalComponent {
53
54 protected:
55
56     typedef vector<FGElectricalComponent *> comp_list;
57     typedef vector<string> string_list;
58
59     int kind;
60     string name;
61     string prop;
62     double value;
63
64     comp_list inputs;
65     comp_list outputs;
66
67 public:
68
69     FGElectricalComponent();
70     virtual ~FGElectricalComponent() {}
71
72     inline string get_name() { return name; }
73     inline string get_prop() { return prop; }
74
75     inline int get_kind() const { return kind; }
76     inline double get_value() const { return value; }
77     inline void set_value( double val ) { value = val; }
78
79     inline int get_num_outputs() const { return outputs.size(); }
80     inline FGElectricalComponent *get_output( const int i ) {
81         return outputs[i];
82     }
83     inline void add_output( FGElectricalComponent *c ) {
84         outputs.push_back( c );
85     }
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
96
97 // Electrical supplier
98 class FGElectricalSupplier : public FGElectricalComponent {
99
100     SGPropertyNode_ptr _rpm_node;
101
102     enum FGSupplierType {
103         FG_BATTERY = 0,
104         FG_ALTERNATOR = 1,
105         FG_EXTERNAL = 2
106     };
107
108     int model;
109     double volts;
110     double amps;
111
112 public:
113
114     FGElectricalSupplier ( string _name, string _prop, string _model,
115                            double _volts, double _amps );
116     ~FGElectricalSupplier () {}
117
118     double get_output();
119 };
120
121
122 // Electrical bus (can take multiple inputs and provide multiple
123 // outputs)
124 class FGElectricalBus : public FGElectricalComponent {
125
126 public:
127
128     FGElectricalBus ( string _name, string _prop );
129     ~FGElectricalBus () {}
130 };
131
132
133 // A lot like an FGElectricalBus, but here for convenience and future
134 // flexibility
135 class FGElectricalOutput : public FGElectricalComponent {
136
137 public:
138
139     FGElectricalOutput ( string _name, string _prop );
140     ~FGElectricalOutput () {}
141 };
142
143
144 // Connects multiple sources to multiple destinations with optional
145 // switches/fuses/circuit breakers inline
146 class FGElectricalConnector : public FGElectricalComponent {
147
148     comp_list inputs;
149     comp_list outputs;
150     typedef vector<SGPropertyNode *> switch_list;
151     switch_list switches;
152
153 public:
154
155     FGElectricalConnector ();
156     ~FGElectricalConnector () {}
157
158     void add_switch( SGPropertyNode *node ) {
159         switches.push_back( node );
160     }
161
162     bool get_state();
163 };
164
165
166 /**
167  * Model an electrical system.  This is a simple system with the
168  * alternator hardwired to engine[0]/rpm
169  *
170  * Input properties:
171  *
172  * /engines/engine[0]/rpm
173  *
174  * Output properties:
175  *
176  * 
177  */
178
179 class FGElectricalSystem : public FGSubsystem
180 {
181
182 public:
183
184     FGElectricalSystem ();
185     virtual ~FGElectricalSystem ();
186
187     virtual void init ();
188     virtual void bind ();
189     virtual void unbind ();
190     virtual void update (double dt);
191
192     bool build ();
193     void propogate( FGElectricalComponent *node, double val, string s = "" );
194     FGElectricalComponent *find ( const string &name );
195
196 protected:
197
198     typedef vector<FGElectricalComponent *> comp_list;
199
200 private:
201
202     SGPropertyNode *config_props;
203
204     bool enabled;
205
206     comp_list suppliers;
207     comp_list buses;
208     comp_list outputs;
209     comp_list connectors;
210 };
211
212
213 #endif // _SYSTEMS_ELECTRICAL_HXX