libelec
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
|
#include <stdarg.h>
#include <stdbool.h>
#include <acfutils/conf.h>
#include <acfutils/geom.h>
#include <acfutils/sysmacros.h>
Go to the source code of this file.
Data Structures | |
struct | elec_batt_info_t |
struct | elec_gen_info_t |
struct | elec_tru_info_t |
struct | elec_xfrmr_info_t |
struct | elec_load_info_t |
struct | elec_bus_info_t |
struct | elec_cb_info_t |
struct | elec_diode_info_t |
struct | elec_label_box_info_t |
struct | elec_comp_info_t |
Macros | |
#define | STATIC_ARRAY_LEN_ARG(len) |
#define | CONST_ARRAY_LEN_ARG(len) len |
Typedefs | |
typedef double(* | elec_get_temp_cb_t) (elec_comp_t *comp, void *userinfo) |
typedef double(* | elec_get_rpm_cb_t) (elec_comp_t *comp, void *userinfo) |
typedef double(* | elec_get_load_cb_t) (elec_comp_t *comp, void *userinfo) |
typedef void(* | elec_user_cb_t) (elec_sys_t *sys, bool pre, void *userinfo) |
Enumerations | |
enum | { ELEC_MAX_SRCS = 64 } |
enum | elec_comp_type_t { ELEC_BATT , ELEC_GEN , ELEC_TRU , ELEC_INV , ELEC_XFRMR , ELEC_LOAD , ELEC_BUS , ELEC_CB , ELEC_SHUNT , ELEC_TIE , ELEC_DIODE , ELEC_LABEL_BOX } |
enum | gui_load_type_t { GUI_LOAD_GENERIC , GUI_LOAD_MOTOR } |
Functions | |
elec_sys_t * | libelec_new (const char *filename) |
void | libelec_destroy (elec_sys_t *sys) |
const elec_comp_info_t * | libelec_get_comp_infos (const elec_sys_t *sys, size_t *num_infos) |
bool | libelec_sys_start (elec_sys_t *sys) |
void | libelec_sys_stop (elec_sys_t *sys) |
bool | libelec_sys_is_started (const elec_sys_t *sys) |
bool | libelec_sys_can_start (const elec_sys_t *sys) |
void | libelec_sys_set_time_factor (elec_sys_t *sys, double time_factor) |
double | libelec_sys_get_time_factor (const elec_sys_t *sys) |
void | libelec_serialize (elec_sys_t *sys, conf_t *ser, const char *prefix) |
bool | libelec_deserialize (elec_sys_t *sys, const conf_t *ser, const char *prefix) |
void | libelec_add_user_cb (elec_sys_t *sys, bool pre, elec_user_cb_t cb, void *userinfo) |
void | libelec_remove_user_cb (elec_sys_t *sys, bool pre, elec_user_cb_t cb, void *userinfo) |
elec_comp_t * | libelec_comp_find (elec_sys_t *sys, const char *name) |
void | libelec_walk_comps (const elec_sys_t *sys, void(*cb)(elec_comp_t *, void *), void *userinfo) |
const elec_comp_info_t * | libelec_comp2info (const elec_comp_t *comp) |
bool | libelec_comp_is_AC (const elec_comp_t *comp) |
elec_comp_type_t | libelec_comp_get_type (const elec_comp_t *comp) |
const char * | libelec_comp_get_name (const elec_comp_t *comp) |
const char * | libelec_comp_get_location (const elec_comp_t *comp) |
bool | libelec_comp_get_autogen (const elec_comp_t *comp) |
size_t | libelec_comp_get_num_conns (const elec_comp_t *comp) |
elec_comp_t * | libelec_comp_get_conn (const elec_comp_t *comp, size_t i) |
double | libelec_comp_get_in_volts (const elec_comp_t *comp) |
double | libelec_comp_get_out_volts (const elec_comp_t *comp) |
double | libelec_comp_get_in_amps (const elec_comp_t *comp) |
double | libelec_comp_get_out_amps (const elec_comp_t *comp) |
double | libelec_comp_get_in_pwr (const elec_comp_t *comp) |
double | libelec_comp_get_out_pwr (const elec_comp_t *comp) |
double | libelec_comp_get_in_freq (const elec_comp_t *comp) |
double | libelec_comp_get_out_freq (const elec_comp_t *comp) |
double | libelec_comp_get_incap_volts (const elec_comp_t *comp) |
bool | libelec_comp_is_powered (const elec_comp_t *comp) |
double | libelec_comp_get_eff (const elec_comp_t *gen) |
unsigned | libelec_comp_get_srcs (const elec_comp_t *comp, elec_comp_t *srcs[ELEC_MAX_SRCS]) |
void | libelec_comp_set_failed (elec_comp_t *comp, bool failed) |
bool | libelec_comp_get_failed (const elec_comp_t *comp) |
void | libelec_comp_set_shorted (elec_comp_t *comp, bool shorted) |
bool | libelec_comp_get_shorted (const elec_comp_t *comp) |
double | libelec_gen_set_random_volts (elec_comp_t *comp, double stddev) |
double | libelec_gen_set_random_freq (elec_comp_t *comp, double stddev) |
void | libelec_comp_set_userinfo (elec_comp_t *comp, void *userinfo) |
void * | libelec_comp_get_userinfo (const elec_comp_t *comp) |
void | libelec_batt_set_temp_cb (elec_comp_t *batt, elec_get_temp_cb_t cb) |
elec_get_temp_cb_t | libelec_batt_get_temp_cb (const elec_comp_t *batt) |
void | libelec_gen_set_rpm_cb (elec_comp_t *gen, elec_get_rpm_cb_t cb) |
elec_get_rpm_cb_t | libelec_gen_get_rpm_cb (const elec_comp_t *gen) |
void | libelec_load_set_load_cb (elec_comp_t *load, elec_get_load_cb_t cb) |
elec_get_load_cb_t | libelec_load_get_load_cb (elec_comp_t *load) |
void | libelec_cb_set (elec_comp_t *comp, bool set) |
bool | libelec_cb_get (const elec_comp_t *comp) |
double | libelec_cb_get_temp (const elec_comp_t *comp) |
void | libelec_tie_set_list (elec_comp_t *comp, size_t list_len, elec_comp_t *const bus_list[]) |
void | libelec_tie_set (elec_comp_t *comp,...) SENTINEL_ATTR |
void | libelec_tie_set_v (elec_comp_t *comp, va_list ap) |
void | libelec_tie_set_all (elec_comp_t *comp, bool tied) |
bool | libelec_tie_get_all (elec_comp_t *comp) |
size_t | libelec_tie_get_list (elec_comp_t *comp, size_t cap, elec_comp_t *bus_list[]) |
size_t | libelec_tie_get_num_buses (const elec_comp_t *comp) |
bool | libelec_tie_get (elec_comp_t *tie, bool_t exhaustive,...) SENTINEL_ATTR |
bool | libelec_tie_get_v (elec_comp_t *tie, bool exhaustive, va_list ap) |
void | libelec_gen_set_rpm (elec_comp_t *gen, double rpm) |
double | libelec_gen_get_rpm (const elec_comp_t *gen) |
double | libelec_batt_get_chg_rel (const elec_comp_t *batt) |
void | libelec_batt_set_chg_rel (elec_comp_t *batt, double chg_rel) |
double | libelec_batt_get_temp (const elec_comp_t *batt) |
void | libelec_batt_set_temp (elec_comp_t *batt, double T) |
bool | libelec_chgr_get_working (const elec_comp_t *chgr) |
double | libelec_phys_get_batt_voltage (double U_nominal, double chg_rel, double I_rel) |
This is the master libelec include file. You should include this file to interface with libelec.
Definition in file libelec.h.
typedef double(* elec_get_load_cb_t) (elec_comp_t *comp, void *userinfo) |
This is the callback type used by electrical loads for cases where the caller has installed a custom load demand callback using libelec_load_set_load_cb().
comp | The component for which the load demand is being queried. |
userinfo | Custom userinfo pointer, which was previously set up on the component using libelec_comp_set_userinfo(). |
STAB TRUE
in the definition file), this callback must return the power demand of the load in Watts.STAB FALSE
in the definition file, the default), this callback must return the current demand of the load in Amps. typedef double(* elec_get_rpm_cb_t) (elec_comp_t *comp, void *userinfo) |
This is the callback type used by generators to determine the speed of the generator's input shaft, which is then used for determining the generator's output voltage and frequency behavior. This callback is installed for generators using libelec_gen_set_rpm_cb().
comp | The component for which the generator rpm is being queried. |
userinfo | Custom userinfo pointer, which was previously set up on the component using libelec_comp_set_userinfo(). |
EXC_RPM
, MIN_RPM
and MAX_RPM
stanzas when defining the generator in the config file. libelec doesn't enforce any specific type of units for generator, so you can use whatever units are most convenient. typedef double(* elec_get_temp_cb_t) (elec_comp_t *comp, void *userinfo) |
typedef void(* elec_user_cb_t) (elec_sys_t *sys, bool pre, void *userinfo) |
Custom physics callback, which you can install using libelec_add_user_cb(), or remove using libelec_remove_user_cb(). This will be called from the physics calculation thread, allowing you perform precise accounting of all physics state as it's being calculated.
enum elec_comp_type_t |
Identifies the type of electrical component. Every component in a libelec network must be one of these enumerations.
Enumerator | |
---|---|
ELEC_BATT | A battery is a component which can hold a certain amount of energy. This energy can then either be discharged (when a load is connected to the battery), or recharged (when a generator or energy source of a higher voltage than the battery's output voltage is attached). The rate of recharge is controlled by the battery's internal resistance, while the rate of discharge is largely driven by the demands of the downstream loads. To simulate the effects of internal battery resistance to discharge, a battery will depress its output voltage as it approaches its power flow limits. |
ELEC_GEN | A generator is a component which converts mechanical input energy into electrical output energy. You MUST supply a callback using libelec_gen_set_rpm_cb(), which informs libelec how fast the input shaft of the generator is turning. |
ELEC_TRU | A Transformer-Rectifier Unit is a component which performs two tasks:
libelec's TRUs are simple devices, whose output voltage always scales in proportion to their input voltage. Thus, they do not provide a stabilized output voltage, but instead depend on the ability of the AC-side generator to supply a stable voltage. A TRU doesn't permit reverse current flow from its output side to the input. |
ELEC_INV | An inverter is conceptually the opposite of a TRU. It changes DC power into AC power, and optionally also changes the input voltage to a different output voltage.
|
ELEC_XFRMR | A transformer changes the AC voltage on its input into a different AC voltage on its output. The voltage ratio is fixed based on the ratio of the number of turns between the transformer's primary and secondary windings. |
ELEC_LOAD | A load is a consumer of electrical energy. This can be anything you need it to be. libelec tells you if the load is powered (and how much energy it is consuming), and you then implement your custom systems behaviors based on that energy usage. Loads can either have internal power supplies, or be simple unregulated energy consumers. You can customize all of this behavior to suit your needs. |
ELEC_BUS | Buses are the interconnecting "tissue" of the libelec network. Every component on the network must connect to one or more buses, and the buses themselves serve to distribute energy flows throughout the network. libelec buses are idealized power distribution mechanisms with no internal resistance. |
ELEC_CB | Circuit breakers are devices which allow the passage of electrical current when closed. If open, current is not allowed to flow. A breaker can become open by either by manual pilot action, or automatically if the current flowing through the breaker exceeds the breaker's maximum rating. As such, circuit breakers are safety devices which prevent excessive current draw to protect the electrical network from damage due to faults or short-circuits. libelec assumes CBs as being specifically thermal circuit breakers, which are breakers where a small amount of energy is used to heat up an internal filament. This amount of energy loss is carefully callibrated such that when current flow through the breaker exceeds its rating, the filament deforms sufficiently to trigger an internal control circuit, which then opens the breaker. As such, thermal breakers have a certain amount of "inertia" before responding. If the current exceedance is very brief and/or not very high, the filament's thermal inertia might cause the breaker not to "trip." Conversely, when the breaker trips, the filament might remain very hot and must cool off - the operator must wait a few seconds before attempting to close the breaker again, to account for this cooling period. |
ELEC_SHUNT | Shunts are current measuring devices inserted in-line with a circuit. In reality, shunts are very small, carefully callibrated resistors, which cause a small amount of voltage drop across their leads. This amount of voltage drop is then measured using a voltmeter and converted into a current measurement mathematically. Since a properly sized shunt is meant to only cause a very small voltage drop in the circuit, libelec shunts are idealized devices which cause no voltage drop at all. You can also use these idealized shunts to represent other current measuring devices, such as AC current clamps (which cause no voltage drop in the circuit). |
ELEC_TIE | Ties are devices which connect two or more buses together. You can think of ties as a generalization of contactors and relays. Each of the endpoints of a tie has two possible states: tied or not tied. When tied, the endpoint allows current to pass (in any direction) to all other endpoints which are currently tied. Conceptually, the tie behaves as a single electrical node, with links to the outside world which can be dynamically connected and disconnected: 4-pole tie fully open B & C tied, A & D open
(no current can flow) (current can flow between B & C)
A o o C A o o B
/
+ +
/
B o o D C o o D
A & B tied, C & D open All endpoints tied
(current can flow between A & B) (current can flow between all)
A o o B A o o B
\ / \ /
+ +
/ \
C o o D C o o D
|
ELEC_DIODE | A diode is a component which prevents current flow opposite to the diode's forward sense. libelec's diode are DC-only devices and cannot be used on AC circuits. Use a component of type ELEC_TRU to implement an AC-to-DC rectifier. |
ELEC_LABEL_BOX | This is a pseudo component, only used placing text labels on the graphical network drawing. It plays no role in the actual electrical simulation. |
void libelec_add_user_cb | ( | elec_sys_t * | sys, |
bool | pre, | ||
elec_user_cb_t | cb, | ||
void * | userinfo | ||
) |
Adds a custom user callback to the library. This will be called from the physics calculation thread, allowing you perform precise accounting of all physics state as it's being calculated.
pre | If set to true, the callback will be called just before the physics integration is run. If set to false, will be called immediately after the physics integration. |
cb | The callback to be called. |
userinfo | A custom userinfo pointer parameter, which will be passed to the callback function every time it is called. |
double libelec_batt_get_chg_rel | ( | const elec_comp_t * | batt | ) |
batt
. This MUST be a component of type ELEC_BATT. double libelec_batt_get_temp | ( | const elec_comp_t * | batt | ) |
batt
in Kelvin. This MUST be a component of type ELEC_BATT. elec_get_temp_cb_t libelec_batt_get_temp_cb | ( | const elec_comp_t * | batt | ) |
NULL
if no callback was set. void libelec_batt_set_chg_rel | ( | elec_comp_t * | batt, |
double | chg_rel | ||
) |
Sets the relative state-of-charge of a battery. Due to the temperature-dependent behavior of batteries, libelec uses relative state of charge, rather than absolute energy content.
void libelec_batt_set_temp | ( | elec_comp_t * | batt, |
double | T | ||
) |
Sets the temperature of a battery. This affects the battery's capability to output current and total energy content, due to temperature significantly affecting how the chemistry inside of the battery behaves.
T | The new temperature of the battery in Kelvin. Must NOT be negative. |
void libelec_batt_set_temp_cb | ( | elec_comp_t * | batt, |
elec_get_temp_cb_t | cb | ||
) |
Sets the battery temperature callback. libelec doesn't itself implement battery heat flow. Rather, it relies on external code to tell it what temperature the battery is. libelec then employs that value to determine the battery's electrical properties (such as voltage, max current, capacity, etc.)
batt | The battery for which to configure the callback. Must be a component of type ELEC_BATT. |
cb | The callback to set. You may pass NULL here to remove the callback. You may also use libelec_batt_set_temp() to set the battery's temperature directly. |
bool libelec_cb_get | ( | const elec_comp_t * | comp | ) |
double libelec_cb_get_temp | ( | const elec_comp_t * | comp | ) |
void libelec_cb_set | ( | elec_comp_t * | comp, |
bool | set | ||
) |
bool libelec_chgr_get_working | ( | const elec_comp_t * | chgr | ) |
const elec_comp_info_t* libelec_comp2info | ( | const elec_comp_t * | comp | ) |
elec_comp_t* libelec_comp_find | ( | elec_sys_t * | sys, |
const char * | name | ||
) |
Locates an electrical component on the network by name.
name | The name of the component to find. |
elec_comp_t* libelec_comp_get_conn | ( | const elec_comp_t * | comp, |
size_t | i | ||
) |
i
. i | The index of the connection. This MUST be less than the return value of libelec_comp_get_num_conns(). |
double libelec_comp_get_eff | ( | const elec_comp_t * | comp | ) |
bool libelec_comp_get_failed | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_in_amps | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_in_freq | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_in_pwr | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_in_volts | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_incap_volts | ( | const elec_comp_t * | comp | ) |
comp | The component for which to return the input capacitance voltage. This MUST be a component of type ELEC_LOAD. |
size_t libelec_comp_get_num_conns | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_out_amps | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_out_freq | ( | const elec_comp_t * | comp | ) |
double libelec_comp_get_out_pwr | ( | const elec_comp_t * | comp | ) |
MIN_VOLTS
value defined, as that communicates intent better than simply checking the output power).double libelec_comp_get_out_volts | ( | const elec_comp_t * | comp | ) |
bool libelec_comp_get_shorted | ( | const elec_comp_t * | comp | ) |
void* libelec_comp_get_userinfo | ( | const elec_comp_t * | comp | ) |
NULL
. bool libelec_comp_is_AC | ( | const elec_comp_t * | comp | ) |
bool libelec_comp_is_powered | ( | const elec_comp_t * | comp | ) |
MIN_VOLTS
stanza will be considered unpowered if their power supply output voltage drops below the MIN_VOLTS
requirement. void libelec_comp_set_failed | ( | elec_comp_t * | comp, |
bool | failed | ||
) |
Sets a component's failed status. The behavior of a failed component depends on its type:
ELEC_BATT
: provides no output voltage and cannot be charged.ELEC_DIODE
, ELEC_BUS
, ELEC_CB
, ELEC_SHUNT
and ELEC_TIE
: doesn't allow any current to pass through the component.ELEC_TRU
: generates no electrical load on its AC input and provides no voltage on its DC output.ELEC_INV
: generates no electrical load on its DC input and provides no voltage on its AC output.ELEC_XFRMR
: generates no electrical load on its input and provides no voltage on its output.ELEC_GEN
: generates no voltage on its output and requires no mechanical input power.ELEC_LOAD
: draws no electrical current and is permanently unpowered (libelec_comp_is_powered() always returns false). void libelec_comp_set_shorted | ( | elec_comp_t * | comp, |
bool | shorted | ||
) |
Sets a component's short-circuit failure status. Short circuits are simulated by creating a random massive increase in a leakage current entering the component. This energy is not directed to any other connected components, or accounted for in a load's output power, but is rather "lost" to ground. The amount of leakage current is randomized and depends on the component's normal input current flow. It will typically oscillate somewhere between 30-40x the normal current. This should guarantee that any breaker protecting this device will trip in fairly short order.
void libelec_comp_set_userinfo | ( | elec_comp_t * | comp, |
void * | userinfo | ||
) |
Sets the userinfo pointer of a component. This userinfo pointer is passed to all callbacks (such as those set up using libelec_batt_set_temp_cb() or ibelec_gen_set_rpm_cb()) in the second argument of the callback. You can use this information to pass along any extra information to the callbacks that you may need.
bool libelec_deserialize | ( | elec_sys_t * | sys, |
const conf_t * | ser, | ||
const char * | prefix | ||
) |
Deserializes a serialized network state previously saved using libelec_serialize(). Before attempting deserialization, the library compares the saved state to the currently running one. If there were any alterations done to the electrical definition file, no attempt is made to try to reload the serialized state and this function aborts with an error.
ser | A libacfutils conf_t object, which was reloaded from persistent storage after having been previously populated with libelec_serialize(). |
prefix | The same conf key prefix as was passed to libelec_serialize(). |
void libelec_destroy | ( | elec_sys_t * | sys | ) |
Destroys a libelec network, which was previously loaded using libelec_new(). You MUST stop the network using libelec_sys_stop() before attempting to destroy it.
double libelec_gen_get_rpm | ( | const elec_comp_t * | gen | ) |
gen
. This MUST be a component of type ELEC_GEN. elec_get_rpm_cb_t libelec_gen_get_rpm_cb | ( | const elec_comp_t * | gen | ) |
NULL
if no callback was set. double libelec_gen_set_random_freq | ( | elec_comp_t * | comp, |
double | stddev | ||
) |
Same as libelec_gen_set_random_volts(), but operates on an AC generator's frequency stability.
double libelec_gen_set_random_volts | ( | elec_comp_t * | comp, |
double | stddev | ||
) |
Sets up a random under- or over-voltage failure condition on a generator.
comp | The generator for which to set the failure up. This MUST be a component of type ELEC_GEN. |
stddev | Standard deviation of the error. The library will use a Gaussian random number generator to pick a value which is at least 0.5x stddev and at most 1.5x stddev away from the nominal voltage of the generator. The standard deviation MUST be less than 66% of the nominal voltage (to guarantee that the random voltage value doesn't go to zero or negative). For example, a good value for a 115V generator would be something like 30V. This would generate a random voltage deviation of 15-45V from the nominal (randomly selected to either be an over-voltage or under-voltage condition). To disable the failure, simply set stddev to zero. |
stddev
was zero, returns the nominal voltage of the generator. void libelec_gen_set_rpm | ( | elec_comp_t * | gen, |
double | rpm | ||
) |
Sets the generator's RPM value.
gen | The generator for which to set the rpm. This MUST be a component of type ELEC_GEN. |
rpm | The RPM to set on the generator. |
rpm
are arbitrary. You can use whatever units you prefer. You simply need to be consistent with the EXC_RPM
, MIN_RPM
and MAX_RPM
stanzas when defining the generator in the config file. void libelec_gen_set_rpm_cb | ( | elec_comp_t * | gen, |
elec_get_rpm_cb_t | cb | ||
) |
Sets the generator rpm callback. libelec uses this callback to determine the rotational speed of the generator's input shaft, which it then uses to calculate the output voltage and frequency.
batt | The generator for which to configure the callback. Must be a component of type ELEC_GEN. |
cb | The callback to set. You may pass NULL here to remove the calllback. You can use libelec_gen_set_rpm() to set the rpm directly. |
const elec_comp_info_t* libelec_get_comp_infos | ( | const elec_sys_t * | sys, |
size_t * | num_infos | ||
) |
num_infos | Mandatory return parameter, which will be filled with the number of elements in the returned array. |
elec_get_load_cb_t libelec_load_get_load_cb | ( | elec_comp_t * | load | ) |
NULL
if no callback was set. void libelec_load_set_load_cb | ( | elec_comp_t * | load, |
elec_get_load_cb_t | cb | ||
) |
Sets the load demand callback for an electrical load. libelec can use this callback to determine the electrical load demand of the component.
batt | The load for which to configure the callback. Must be a component of type ELEC_LOAD. |
cb | The callback to set. You may pass NULL here to remove the callback. A load which does not have a custom load callback configured can still fall back to its standard load demand, as set using the STD_LOAD configuration stanza. |
elec_sys_t* libelec_new | ( | const char * | filename | ) |
Allocates and initializes a new electrical system. You must supply a file, which holds the definition of the electrical network layout. See Electrical Network File Format for more information.
After initialization, the electrical network is in a stopped state. This means, the simulation isn't yet running, allowing you to configure the network by setting up custom callbacks, or manipulating the parsed info structures in the components. Use libelec_comp_find() to locate components by name and libelec_comp2info() to extract the parsed info structures for the respective component and modify the info structure as necessary.
Once you are satisfied with the configuration of the network, you can start the simulation up by running libelec_sys_start(). This spawns a background thread, running the physics simulation of the network. You should only call this when the host simulator has finished loading your airplane (i.e. on the first frame of the simulator actually running).
On unload, first stop the network using libelec_sys_stop(). After stopping the network, you could potentially modify the info structs and restart the network again. A stopped network can be freed using libelec_destroy().
filename | Full file path and name to the electrical network definition file. This must conform to the syntax described in Configuration File Format. |
double libelec_phys_get_batt_voltage | ( | double | U_nominal, |
double | chg_rel, | ||
double | I_rel | ||
) |
Utility function which allows you to determine the battery voltage of a custom battery, mirroring the same algorithm which libelec uses to calculate battery voltage.
U_nominal | Nominal battery voltage when at full state of charge and standard temperature (typically around 20°C). |
chg_rel | Relative state of charge from 0.0 to 1.0. |
I_rel | Relative current draw out of the battery, compared to its nominal design current draw rating. Higher current draws cause the battery voltage to depress more, due to the effects of internal resistance. |
void libelec_remove_user_cb | ( | elec_sys_t * | sys, |
bool | pre, | ||
elec_user_cb_t | cb, | ||
void * | userinfo | ||
) |
Removes a custom user callback, which was previously registered using libelec_add_user_cb(). You must pass the exact same combination of pre
, cb
and userinfo
as was previously used during the registration. The callback MUST exist, otherwise an assertion failure is triggered.
void libelec_serialize | ( | elec_sys_t * | sys, |
conf_t * | ser, | ||
const char * | prefix | ||
) |
Serializes the run-time state of the network. You can use this to implement aircraft state persistence. When saving your aircraft state, call this function to save all electrical network state. Use libelec_deserialize() to restore a previously serialized state.
This function is safe to call after the network has been started (in fact, that is its intended purpose - you don't have to stop the network to perform state serializations). Since this function can block while trying to interlock with the network's physics worker thread, it is recommended not to call this from the simulator's main thread (to prevent micro-stutter), but instead from a dedicated state saving background thread.
ser | A libacfutils conf_t object, which will be filled with the serialized network state. This is the object which you should write to persistence storage to later reload. |
prefix | A name prefix which will be prepended to the configuration keys that will be placed into the ser conf_t object. This is to prevent namespace clashing with any other data you might be using that conf_t object for. A common name prefix would be something like "libelec" . |
bool libelec_sys_can_start | ( | const elec_sys_t * | sys | ) |
Performs a validation check on the electrical network to see if it is ready for start.
double libelec_sys_get_time_factor | ( | const elec_sys_t * | sys | ) |
bool libelec_sys_is_started | ( | const elec_sys_t * | sys | ) |
void libelec_sys_set_time_factor | ( | elec_sys_t * | sys, |
double | time_factor | ||
) |
Sets the simulation rate of libelec. This can be used to adapt libelec's physics to the host simulator's simulation rate, in case the simulator is running faster or slower than real time, or is paused.
XPLANE
macro ddefined, this function does nothing, as libelec automatically adapts to X-Plane's simulation rate.time_factor | The non-negative factor by which time is accelerated. Normal real-time operation is 1.0. If the simulator is running twice as fast as real time, time_factor is 2.0. If the simulator is running half as fast as real time, time_factor is 0.5. To tell libelec that the simulator is paused, set time_factor to 0. |
time_factor=10
will consume 50% of one CPU core. libelec cannot run faster than a single CPU core on the machine, imposing a hard cap on the maximum time_factor
that can be effectively followed. If the CPU cannot keep up, libelec will appear to be lagging behind the desired simulation rate. bool libelec_sys_start | ( | elec_sys_t * | sys | ) |
Starts a stopped libelec network. If the network was already started, this function does nothing.
Initially, after loading with libelec_new(), a libelec instance is stopped and not performing any physics simulation. You should configure the returned network as you see fit prior to starting - any modifications to the component info structure are not allowed after the network has been started, as that can lead to race conditions and unpredictable behavior.
Since there is usually a significant delay between your aircraft loading into the simulator and the simulation actually starting up, you should delay starting the electrical network until the first simulator frame.
void libelec_sys_stop | ( | elec_sys_t * | sys | ) |
Stops the electrical network after it has been started using libelec_sys_start(). If the network was already stopped, this function does nothing.
bool libelec_tie_get | ( | elec_comp_t * | tie, |
bool_t | exhaustive, | ||
... | |||
) |
Given a variadic argument list of buses, determines if the buses are currently tied.
tie | The tie to examine. This must be a component of type ELEC_TIE. |
exhaustive | See description of return value below. |
... | List elec_comp_t pointers to the buses which are to be examined. The argument list MUST be terminated by a NULL pointer (sentinel). |
exhaustive
is set to true, this function returns false
if the argument list doesn't mention all of the buses currently tied (meaning, the argument list must be an exhaustive list of buses which are currently tied). bool libelec_tie_get_all | ( | elec_comp_t * | comp | ) |
comp | The tie to examine. This must be of type ELEC_TIE. |
size_t libelec_tie_get_list | ( | elec_comp_t * | comp, |
size_t | cap, | ||
elec_comp_t * | bus_list[] | ||
) |
Retrieves the exact list of tied buses for a ELEC_TIE component.
comp | The tie for which to retrieve the list. This MUST be a component of ELEC_TIE. |
cap | Capacity of bus_list . This MUST be greater than or equal to the return value of libelec_tie_get_num_buses(). |
bus_list | A return argument, which will be filled with the list of buses currently tied. Any buses NOT mentioned in this list are currently NOT tied. This array MUST be able to hold at least libelec_tie_get_num_buses() elements. |
bus_list
. size_t libelec_tie_get_num_buses | ( | const elec_comp_t * | comp | ) |
comp
. This MUST be a component of ELEC_TIE. The returned number DOESN'T signify that that number of buses are currently tied together, only that the tie is connected to this number of buses and CAN be tied together. Use libelec_tie_get_list() to determine the exact buses which are currently tied together. bool libelec_tie_get_v | ( | elec_comp_t * | tie, |
bool | exclusive, | ||
va_list | ap | ||
) |
Same as libelec_tie_get(), but takes a va_list
argument for the bus list, to allow for nesting inside of other variadic functions.
void libelec_tie_set | ( | elec_comp_t * | comp, |
... | |||
) |
A convenience variadic version of libelec_tie_set_list().
comp | The tie to reconfigure. This must be of type ELEC_TIE. |
... | A variadic list of elec_comp_t pointers to buses, which should be tied together. This list MUST be terminated by a single NULL pointer (sentinel). Any buses which the tie is connected to but are not mentioned on this list become untied. |
void libelec_tie_set_all | ( | elec_comp_t * | comp, |
bool | tied | ||
) |
Convenience function, which allows either tying all connections of an ELEC_TIE component, or untying all of them. This is equivalent to calling libelec_tie_set_list() with a list of all buses to which the tie is connected.
comp | The tie to reconfigure. This must be of type ELEC_TIE. |
tied | Flag indicating whether all endpoints of the tie should be tied, or untied. |
void libelec_tie_set_list | ( | elec_comp_t * | comp, |
size_t | list_len, | ||
elec_comp_t *const | bus_list[] | ||
) |
Reconfigures a tie's current state to tie together the bus connections matching a list of elec_comp_t pointers.
comp | The tie to reconfigure. This must be of type ELEC_TIE. |
list_len | Number of elements in bus_list . |
bus_list | List of elec_comp_t pointers, which should be tied together. The tie MUST be connected to these buses. Any buses which the tie is connected to but are not mentioned on this list become untied. |
void libelec_tie_set_v | ( | elec_comp_t * | comp, |
va_list | ap | ||
) |
Same as libelec_tie_set(), but takes a va_list
argument, allowing for nesting within other variadic functions.
comp | The tie to reconfigure. This must be of type ELEC_TIE. |
void libelec_walk_comps | ( | const elec_sys_t * | sys, |
void(*)(elec_comp_t *, void *) | cb, | ||
void * | userinfo | ||
) |
Walker function, which will go through all components on the network. This is mostly used for debugging.
cb | A callback, which you must supply and will be called in turn with every electrical component on the network in its first argument. |
userinfo | An optional custom pointer parameter. This will be passed in the second argument to the cb callback you supply above. |