FlightGear next
electrical.hxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: electrical.hxx
3 * SPDX-FileComment: a flexible, generic electrical system model
4 * SPDX-FileCopyrightText: Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#pragma once
9
10#include <string>
11#include <vector>
12
13#include <simgear/props/props.hxx>
14#include <simgear/structure/subsystem_mgr.hxx>
15
16
17// Forward declaration
19
20
21// Base class for other electrical components
23{
24public:
32
33protected:
34 using comp_list = std::vector<FGElectricalComponent*>;
35
36 int kind;
37 std::string name;
38 float volts;
39 float load_amps; // sum of current draw (load) due to
40 // this node and all it's children
41 float available_amps; // available current (after the load
42 // is subtracted)
43
46
47 simgear::PropertyList props;
48
49public:
51 virtual ~FGElectricalComponent() = default;
52
53 inline const std::string& get_name() { return name; }
54
55 inline int get_kind() const { return kind; }
56
57 inline float get_volts() const { return volts; }
58 inline void set_volts(float val) { volts = val; }
59
60 inline float get_load_amps() const { return load_amps; }
61 inline void set_load_amps(float val) { load_amps = val; }
62
63 inline float get_available_amps() const { return available_amps; }
64 inline void set_available_amps(float val) { available_amps = val; }
65
66 inline int get_num_inputs() const { return outputs.size(); }
68 {
69 return inputs[i];
70 }
72 {
73 inputs.push_back(c);
74 }
75
76 inline int get_num_outputs() const { return outputs.size(); }
78 {
79 return outputs[i];
80 }
82 {
83 outputs.push_back(c);
84 }
85
86 void add_prop(const std::string& s);
87
88 void publishVoltageToProps() const;
89};
90
91
92// Electrical supplier
94{
95public:
102
103private:
104 SGPropertyNode_ptr _rpm_node;
105
106 FGSupplierType model; // store supplier type
107 float ideal_volts; // ideal volts
108
109 // alternator fields
110 std::string rpm_src; // property name of alternator power source
111 float rpm_threshold; // minimal rpm to generate full power
112
113 // alt & ext supplier fields
114 float ideal_amps; // total amps produced (above rpm threshold).
115
116 // battery fields
117 float amp_hours; // fully charged battery capacity
118 float percent_remaining; // percent of charge remaining
119 float charge_amps; // maximum charge load battery can draw
120
121public:
122 FGElectricalSupplier(SGPropertyNode* node);
124
125 inline FGSupplierType get_model() const { return model; }
126 float apply_load(float amps, float dt);
127 float get_output_volts();
128 float get_output_amps();
129 float get_charge_amps() const { return charge_amps; }
130};
131
132
133// Electrical bus (can take multiple inputs and provide multiple
134// outputs)
136{
137public:
138 FGElectricalBus(SGPropertyNode* node);
140};
141
142
143// A lot like an FGElectricalBus, but here for convenience and future
144// flexibility
146{
147public:
148 FGElectricalOutput(SGPropertyNode* node);
150};
151
152
153// Model an electrical switch. If the rating_amps > 0 then this
154// becomes a circuit breaker type switch that can trip
156{
157private:
158 SGPropertyNode_ptr switch_node;
159 float rating_amps;
160 bool circuit_breaker;
161
162public:
163 FGElectricalSwitch(SGPropertyNode* node);
164
166
167 inline bool get_state() const { return switch_node->getBoolValue(); }
168 void set_state(bool val) { switch_node->setBoolValue(val); }
169};
170
171
172// Connects multiple sources to multiple destinations with optional
173// switches/fuses/circuit breakers inline
175{
176 comp_list inputs;
177 comp_list outputs;
178 typedef std::vector<FGElectricalSwitch> switch_list;
179 switch_list switches;
180
181public:
182 FGElectricalConnector(SGPropertyNode* node, FGElectricalSystem* es);
184
186 {
187 switches.push_back(s);
188 }
189
190 // set all switches to the specified state
191 void set_switches(bool state);
192
193 bool get_state();
194};
195
196
201
202class FGElectricalSystem : public SGSubsystem
203{
204public:
205 FGElectricalSystem(SGPropertyNode* node);
206 virtual ~FGElectricalSystem();
207
208 // Subsystem API.
209 void bind() override;
210 void init() override;
211 void shutdown() override;
212 void unbind() override;
213 void update(double dt) override;
214
215 // Subsystem identification.
216 static const char* staticSubsystemClassId() { return "electrical"; }
217
218 bool build(SGPropertyNode* config_props);
219 float propagate(FGElectricalComponent* node, double dt,
220 float input_volts, float input_amps,
221 std::string s = "");
222 FGElectricalComponent* find(const std::string& name);
223
224protected:
225 typedef std::vector<FGElectricalComponent*> comp_list;
226
227private:
228 void deleteComponents(comp_list& comps);
229
230 std::string name;
231 int num;
232 std::string path;
233
234 bool enabled;
235
236 comp_list suppliers;
237 comp_list buses;
238 comp_list outputs;
239 comp_list connectors;
240
241 SGPropertyNode_ptr _volts_out;
242 SGPropertyNode_ptr _amps_out;
243 SGPropertyNode_ptr _serviceable_node;
244 bool _serviceable = true;
245};
#define i(x)
FGElectricalBus(SGPropertyNode *node)
void add_input(FGElectricalComponent *c)
float get_available_amps() const
void set_volts(float val)
const std::string & get_name()
simgear::PropertyList props
void add_prop(const std::string &s)
std::vector< FGElectricalComponent * > comp_list
float get_volts() const
int get_num_outputs() const
int get_num_inputs() const
void set_load_amps(float val)
FGElectricalComponent * get_input(const int i)
virtual ~FGElectricalComponent()=default
float get_load_amps() const
void set_available_amps(float val)
FGElectricalComponent * get_output(const int i)
void publishVoltageToProps() const
void add_output(FGElectricalComponent *c)
FGElectricalConnector(SGPropertyNode *node, FGElectricalSystem *es)
void set_switches(bool state)
void add_switch(FGElectricalSwitch s)
FGElectricalOutput(SGPropertyNode *node)
FGElectricalSupplier(SGPropertyNode *node)
float get_charge_amps() const
float apply_load(float amps, float dt)
FGSupplierType get_model() const
FGElectricalSwitch(SGPropertyNode *node)
void set_state(bool val)
bool get_state() const
Model an electrical system.
void bind() override
void shutdown() override
bool build(SGPropertyNode *config_props)
float propagate(FGElectricalComponent *node, double dt, float input_volts, float input_amps, std::string s="")
void update(double dt) override
FGElectricalSystem(SGPropertyNode *node)
FGElectricalComponent * find(const std::string &name)
static const char * staticSubsystemClassId()
virtual ~FGElectricalSystem()
void unbind() override
std::vector< FGElectricalComponent * > comp_list
void init() override
const char * name