| 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 |
| 18 | class FGElectricalSystem; |
| 19 | |
| 20 | |
| 21 | // Base class for other electrical components |
| 22 | class FGElectricalComponent |
| 23 | { |
| 24 | public: |
| 25 | enum FGElectricalComponentType { |
| 26 | FG_UNKNOWN, |
| 27 | FG_SUPPLIER, |
| 28 | FG_BUS, |
| 29 | FG_OUTPUT, |
| 30 | FG_CONNECTOR |
| 31 | }; |
| 32 | |
| 33 | protected: |
| 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 | |
| 44 | comp_list inputs; |
| 45 | comp_list outputs; |
| 46 | |
| 47 | simgear::PropertyList props; |
| 48 | |
| 49 | public: |
| 50 | FGElectricalComponent(); |
| 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(); } |
| 67 | inline FGElectricalComponent* get_input(const int i) |
| 68 | { |
| 69 | return inputs[i]; |
| 70 | } |
| 71 | inline void add_input(FGElectricalComponent* c) |
| 72 | { |
| 73 | inputs.push_back(c); |
| 74 | } |
| 75 | |
| 76 | inline int get_num_outputs() const { return outputs.size(); } |
| 77 | inline FGElectricalComponent* get_output(const int i) |
| 78 | { |
| 79 | return outputs[i]; |
| 80 | } |
| 81 | inline void add_output(FGElectricalComponent* c) |
| 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 |
| 93 | class FGElectricalSupplier : public FGElectricalComponent |
| 94 | { |
| 95 | public: |
| 96 | enum FGSupplierType { |
| 97 | FG_BATTERY, |
| 98 | FG_ALTERNATOR, |
| 99 | FG_EXTERNAL, |
| 100 | FG_UNKNOWN |
| 101 | }; |
| 102 | |
| 103 | private: |
| 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 | |
| 121 | public: |
| 122 | FGElectricalSupplier(SGPropertyNode* node); |
| 123 | ~FGElectricalSupplier() {} |
| 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) |
| 135 | class FGElectricalBus : public FGElectricalComponent |
| 136 | { |
| 137 | public: |
| 138 | FGElectricalBus(SGPropertyNode* node); |
| 139 | ~FGElectricalBus() {} |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | // A lot like an FGElectricalBus, but here for convenience and future |
| 144 | // flexibility |
| 145 | class FGElectricalOutput : public FGElectricalComponent |
| 146 | { |
| 147 | public: |
| 148 | FGElectricalOutput(SGPropertyNode* node); |
| 149 | ~FGElectricalOutput() {} |
| 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 |
| 155 | class FGElectricalSwitch |
| 156 | { |
| 157 | private: |
| 158 | SGPropertyNode_ptr switch_node; |
| 159 | float rating_amps; |
| 160 | bool circuit_breaker; |
| 161 | |
| 162 | public: |
| 163 | FGElectricalSwitch(SGPropertyNode* node); |
| 164 | |
| 165 | ~FGElectricalSwitch(){}; |
| 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 |
| 174 | class FGElectricalConnector : public FGElectricalComponent |
| 175 | { |
| 176 | comp_list inputs; |
| 177 | comp_list outputs; |
| 178 | typedef std::vector<FGElectricalSwitch> switch_list; |
| 179 | switch_list switches; |
| 180 | |
| 181 | public: |
| 182 | FGElectricalConnector(SGPropertyNode* node, FGElectricalSystem* es); |
| 183 | ~FGElectricalConnector() {} |
| 184 | |
| 185 | void add_switch(FGElectricalSwitch s) |
| 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 | |
| 197 | /** |
| 198 | * Model an electrical system. This is a fairly simplistic system |
| 199 | * |
| 200 | */ |
| 201 | |
| 202 | class FGElectricalSystem : public SGSubsystem |
| 203 | { |
| 204 | public: |
| 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 | |
| 224 | protected: |
| 225 | typedef std::vector<FGElectricalComponent*> comp_list; |
| 226 | |
| 227 | private: |
| 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 | }; |