1 // electrical.hxx - a flexible, generic electrical system model.
3 // Written by Curtis Olson, started September 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #ifndef _SYSTEMS_ELECTRICAL_HXX
25 #define _SYSTEMS_ELECTRICAL_HXX 1
28 # error This library requires C++
41 #include <simgear/props/props.hxx>
42 #include <simgear/structure/subsystem_mgr.hxx>
45 // Forward declaration
46 class FGElectricalSystem;
49 // Base class for other electrical components
50 class FGElectricalComponent {
54 enum FGElectricalComponentType {
64 typedef vector<FGElectricalComponent *> comp_list;
65 typedef vector<string> string_list;
70 float load_amps; // sum of current draw (load) due to
71 // this node and all it's children
72 float available_amps; // available current (after the load
81 FGElectricalComponent();
82 virtual ~FGElectricalComponent() {}
84 inline const string& get_name() { return name; }
86 inline int get_kind() const { return kind; }
88 inline float get_volts() const { return volts; }
89 inline void set_volts( float val ) { volts = val; }
91 inline float get_load_amps() const { return load_amps; }
92 inline void set_load_amps( float val ) { load_amps = val; }
94 inline float get_available_amps() const { return available_amps; }
95 inline void set_available_amps( float val ) { available_amps = val; }
97 inline int get_num_inputs() const { return outputs.size(); }
98 inline FGElectricalComponent *get_input( const int i ) {
101 inline void add_input( FGElectricalComponent *c ) {
102 inputs.push_back( c );
105 inline int get_num_outputs() const { return outputs.size(); }
106 inline FGElectricalComponent *get_output( const int i ) {
109 inline void add_output( FGElectricalComponent *c ) {
110 outputs.push_back( c );
113 inline int get_num_props() const { return props.size(); }
114 inline const string& get_prop( const int i ) {
117 inline void add_prop( const string &s ) {
118 props.push_back( s );
124 // Electrical supplier
125 class FGElectricalSupplier : public FGElectricalComponent {
129 enum FGSupplierType {
138 SGPropertyNode_ptr _rpm_node;
140 FGSupplierType model; // store supplier type
141 float ideal_volts; // ideal volts
144 string rpm_src; // property name of alternator power source
145 float rpm_threshold; // minimal rpm to generate full power
147 // alt & ext supplier fields
148 float ideal_amps; // total amps produced (above rpm threshold).
151 float amp_hours; // fully charged battery capacity
152 float percent_remaining; // percent of charge remaining
153 float charge_amps; // maximum charge load battery can draw
157 FGElectricalSupplier ( SGPropertyNode *node );
158 ~FGElectricalSupplier () {}
160 inline FGSupplierType get_model() const { return model; }
161 float apply_load( float amps, float dt );
162 float get_output_volts();
163 float get_output_amps();
164 float get_charge_amps() const { return charge_amps; }
168 // Electrical bus (can take multiple inputs and provide multiple
170 class FGElectricalBus : public FGElectricalComponent {
174 FGElectricalBus ( SGPropertyNode *node );
175 ~FGElectricalBus () {}
179 // A lot like an FGElectricalBus, but here for convenience and future
181 class FGElectricalOutput : public FGElectricalComponent {
185 FGElectricalOutput ( SGPropertyNode *node );
186 ~FGElectricalOutput () {}
190 // Model an electrical switch. If the rating_amps > 0 then this
191 // becomes a circuit breaker type switch that can trip
192 class FGElectricalSwitch {
196 SGPropertyNode_ptr switch_node;
198 bool circuit_breaker;
202 FGElectricalSwitch( SGPropertyNode *node );
204 ~FGElectricalSwitch() { };
206 inline bool get_state() const { return switch_node->getBoolValue(); }
207 void set_state( bool val ) { switch_node->setBoolValue( val ); }
211 // Connects multiple sources to multiple destinations with optional
212 // switches/fuses/circuit breakers inline
213 class FGElectricalConnector : public FGElectricalComponent {
217 typedef vector< FGElectricalSwitch> switch_list;
218 switch_list switches;
222 FGElectricalConnector ( SGPropertyNode *node, FGElectricalSystem *es );
223 ~FGElectricalConnector () {}
225 void add_switch( FGElectricalSwitch s ) {
226 switches.push_back( s );
229 // set all switches to the specified state
230 void set_switches( bool state );
237 * Model an electrical system. This is a fairly simplistic system
241 class FGElectricalSystem : public SGSubsystem
246 FGElectricalSystem ( SGPropertyNode *node );
247 virtual ~FGElectricalSystem ();
249 virtual void init ();
250 virtual void bind ();
251 virtual void unbind ();
252 virtual void update (double dt);
255 float propagate( FGElectricalComponent *node, double dt,
256 float input_volts, float input_amps,
258 FGElectricalComponent *find ( const string &name );
262 typedef vector<FGElectricalComponent *> comp_list;
269 SGPropertyNode *config_props;
276 comp_list connectors;
278 SGPropertyNode_ptr _volts_out;
279 SGPropertyNode_ptr _amps_out;
283 #endif // _SYSTEMS_ELECTRICAL_HXX