app: convert power port to C

DcBus and PowerPort become struct dc_bus and struct power_port with
prefixed functions instead of member functions.

The default member initializers are replaced by dc_bus_init() and
power_port_init(). The only non-zero default value is the series
multiplier of the bus, which is set to 1 by dc_bus_init() and in the
static initializers of the global buses.

The optional voltage parameter of the control voltage functions has to
be passed explicitly now, 0 still selects the voltage intercept of the
bus.

LoadOutput and PwmSwitch still inherit from the C struct until they are
converted, and initialize it with power_port_init().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 15:12:51 +01:00
co-authored by Claude Opus 5.5
parent 297fd9e336
commit 3cc24411b5
18 changed files with 297 additions and 229 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ target_sources(app PRIVATE
leds.cpp
load.cpp
main.cpp
power_port.cpp
power_port.c
pwm_switch.cpp
setup.cpp
)
+10 -8
View File
@@ -435,7 +435,9 @@ void Charger::discharge_control(BatConf *bat_conf)
// This limit should normally never be reached, as the load output settings should be
// higher. The flag can be used to trigger actions of last resort, e.g. deep-sleep
// of the charge controller itself.
if (port->bus->voltage < port->bus->src_control_voltage(bat_conf->absolute_min_voltage)) {
if (port->bus->voltage
< dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage))
{
port->neg_current_limit = 0;
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
}
@@ -453,7 +455,7 @@ void Charger::discharge_control(BatConf *bat_conf)
// discharging currently not allowed. should we allow it?
if (port->bus->voltage
>= port->bus->src_control_voltage(bat_conf->absolute_min_voltage + 0.1F))
>= dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage + 0.1F))
{
dev_stat.clear_error(ERR_BAT_UNDERVOLTAGE);
}
@@ -506,9 +508,9 @@ void Charger::charge_control(BatConf *bat_conf)
if ((time_state_changed == CHARGER_TIME_NEVER
|| ((uptime() - time_state_changed) > bat_conf->time_limit_recharge
&& port->bus->voltage
< port->bus->sink_control_voltage(bat_conf->recharge_voltage)))
< dc_bus_sink_control_voltage(port->bus, bat_conf->recharge_voltage)))
&& port->bus->voltage
> port->bus->sink_control_voltage(bat_conf->absolute_min_voltage)
> dc_bus_sink_control_voltage(port->bus, bat_conf->absolute_min_voltage)
&& bat_temperature < bat_conf->charge_temp_max - 1
&& bat_temperature > bat_conf->charge_temp_min + 1)
{
@@ -531,7 +533,7 @@ void Charger::charge_control(BatConf *bat_conf)
bat_conf->topping_voltage
+ bat_conf->temperature_compensation * (bat_temperature - 25);
if (port->bus->voltage > port->bus->sink_control_voltage()) {
if (port->bus->voltage > dc_bus_sink_control_voltage(port->bus, 0)) {
target_voltage_timer = 0;
enter_state(CHG_STATE_TOPPING);
}
@@ -546,7 +548,7 @@ void Charger::charge_control(BatConf *bat_conf)
// power sharing: multiple devices in parallel supply the same current
target_current_control = port->current_filtered;
if (port->bus->voltage_filtered >= port->bus->sink_control_voltage() - 0.05F) {
if (port->bus->voltage_filtered >= dc_bus_sink_control_voltage(port->bus, 0) - 0.05F) {
// battery is full if topping target voltage is still reached (i.e. sufficient
// solar power available) and time limit or cut-off current reached
if (port->current_filtered < bat_conf->topping_cutoff_current
@@ -597,13 +599,13 @@ void Charger::charge_control(BatConf *bat_conf)
target_current_control = port->current_filtered;
if (port->bus->voltage >= port->bus->sink_control_voltage()) {
if (port->bus->voltage >= dc_bus_sink_control_voltage(port->bus, 0)) {
time_target_voltage_reached = uptime();
}
if ((uptime() - time_target_voltage_reached > bat_conf->float_recharge_time)
&& port->bus->voltage_filtered
< port->bus->sink_control_voltage(bat_conf->recharge_voltage))
< dc_bus_sink_control_voltage(port->bus, bat_conf->recharge_voltage))
{
// the battery was discharged: float voltage could not be reached anymore
port->pos_current_limit = bat_conf->charge_current_max;
+2 -2
View File
@@ -341,9 +341,9 @@ enum ChargerState
class Charger
{
public:
Charger(PowerPort *pwr_port) : port(pwr_port){};
Charger(struct power_port *pwr_port) : port(pwr_port) {};
PowerPort *port;
struct power_port *port;
/**
* Current charger state (see enum ChargerState)
+15 -13
View File
@@ -41,7 +41,8 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
extern DeviceStatus dev_stat;
Dcdc::Dcdc(const struct device *hb, DcBus *high, DcBus *low, DcdcOperationMode op_mode)
Dcdc::Dcdc(const struct device *hb, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode op_mode)
{
half_bridge = hb;
hvb = high;
@@ -78,7 +79,7 @@ void Dcdc::perturb_observe_buck()
// switch off after 10s low power or negative power (if not in nanogrid mode)
pwm_direction = 0;
}
else if (lvb->voltage > lvb->sink_control_voltage()) {
else if (lvb->voltage > dc_bus_sink_control_voltage(lvb, 0)) {
state = DCDC_CONTROL_CV_LS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
@@ -90,7 +91,7 @@ void Dcdc::perturb_observe_buck()
state = DCDC_CONTROL_CC_HS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (hvb->voltage < hvb->src_control_voltage() && power > output_power_min) {
else if (hvb->voltage < dc_bus_src_control_voltage(hvb, 0) && power > output_power_min) {
// input voltage below limit
state = DCDC_CONTROL_CV_HS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
@@ -99,7 +100,7 @@ void Dcdc::perturb_observe_buck()
state = DCDC_CONTROL_DERATING;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (power < output_power_min && lvb->voltage < lvb->src_control_voltage()) {
else if (power < output_power_min && lvb->voltage < dc_bus_src_control_voltage(lvb, 0)) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwm_direction = BUCK_DUTY_POWER_INCREASE;
}
@@ -123,7 +124,7 @@ void Dcdc::perturb_observe_boost()
// switch off after 10s low power or negative power (if not in nanogrid mode)
pwm_direction = 0;
}
else if (hvb->voltage > hvb->sink_control_voltage()) {
else if (hvb->voltage > dc_bus_sink_control_voltage(hvb, 0)) {
state = DCDC_CONTROL_CV_HS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
@@ -135,7 +136,7 @@ void Dcdc::perturb_observe_boost()
state = DCDC_CONTROL_CC_LS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (lvb->voltage < lvb->src_control_voltage() && -power > output_power_min) {
else if (lvb->voltage < dc_bus_src_control_voltage(lvb, 0) && -power > output_power_min) {
// input voltage below limit
state = DCDC_CONTROL_CV_LS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
@@ -144,7 +145,7 @@ void Dcdc::perturb_observe_boost()
state = DCDC_CONTROL_DERATING;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (-power < output_power_min && hvb->voltage < hvb->src_control_voltage()) {
else if (-power < output_power_min && hvb->voltage < dc_bus_src_control_voltage(hvb, 0)) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwm_direction = BOOST_DUTY_POWER_INCREASE;
}
@@ -169,15 +170,15 @@ __weak DcdcOperationMode Dcdc::check_start_conditions()
return DCDC_MODE_OFF;
}
if (lvb->sink_current_margin > 0 && lvb->voltage < lvb->sink_control_voltage()
&& hvb->src_current_margin < 0 && hvb->voltage > hvb->src_control_voltage()
if (lvb->sink_current_margin > 0 && lvb->voltage < dc_bus_sink_control_voltage(lvb, 0)
&& hvb->src_current_margin < 0 && hvb->voltage > dc_bus_src_control_voltage(hvb, 0)
&& hvb->voltage * 0.85f > lvb->voltage)
{
return DCDC_MODE_BUCK;
}
if (hvb->sink_current_margin > 0 && hvb->voltage < hvb->sink_control_voltage()
&& lvb->src_current_margin < 0 && lvb->voltage > lvb->src_control_voltage())
if (hvb->sink_current_margin > 0 && hvb->voltage < dc_bus_sink_control_voltage(hvb, 0)
&& lvb->src_current_margin < 0 && lvb->voltage > dc_bus_src_control_voltage(lvb, 0))
{
return DCDC_MODE_BOOST;
}
@@ -189,7 +190,7 @@ bool Dcdc::check_hs_mosfet_short()
{
static uint32_t first_time_detected;
if (half_bridge_is_enabled(half_bridge) == false && inductor_current > 0.5F
&& lvb->voltage_filtered > lvb->sink_control_voltage())
&& lvb->voltage_filtered > dc_bus_sink_control_voltage(lvb, 0))
{
// if there is current even though the DC/DC is switched off, the
// high-side MOSFET must be broken --> set flag and let main() decide
@@ -413,7 +414,8 @@ void Dcdc::output_hvs_disable()
#else
Dcdc::Dcdc(const struct device *hb, DcBus *high, DcBus *low, DcdcOperationMode op_mode)
Dcdc::Dcdc(const struct device *hb, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode op_mode)
{}
#endif /* BOARD_HAS_DCDC */
+4 -3
View File
@@ -101,7 +101,8 @@ public:
* @param low Low voltage bus (e.g. battery output for MPPT buck)
* @param mode Operation mode (buck, boost or nanogrid)
*/
Dcdc(const struct device *half_bridge, DcBus *high, DcBus *low, DcdcOperationMode mode);
Dcdc(const struct device *half_bridge, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode mode);
/**
* Check for valid start conditions of the DC/DC converter
@@ -147,8 +148,8 @@ public:
uint16_t state; ///< Control state (off / MPPT / CC / CV)
// actual measurements
DcBus *hvb; ///< Pointer to DC bus at high voltage side
DcBus *lvb; ///< Pointer to DC bus at low voltage (inductor) side
struct dc_bus *hvb; ///< Pointer to DC bus at high voltage side
struct dc_bus *lvb; ///< Pointer to DC bus at low voltage (inductor) side
float inductor_current; ///< Inductor current
float power; ///< Low-side power
float temp_mosfets; ///< MOSFET temperature measurement (if existing)
+10 -6
View File
@@ -30,9 +30,11 @@ LOG_MODULE_REGISTER(load, CONFIG_LOAD_LOG_LEVEL);
extern DeviceStatus dev_stat;
LoadOutput::LoadOutput(DcBus *dc_bus, const struct device *sw, int led_pos)
: PowerPort(dc_bus), power_switch(sw), led(led_pos)
LoadOutput::LoadOutput(struct dc_bus *dc_bus, const struct device *sw, int led_pos)
: power_switch(sw), led(led_pos)
{
power_port_init(this, dc_bus, false);
state = LOAD_STATE_OFF;
if (!device_is_ready(power_switch)) {
@@ -82,14 +84,16 @@ void LoadOutput::control()
flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
}
if (bus->voltage < bus->src_control_voltage(disconnect_voltage)) {
if (bus->voltage < dc_bus_src_control_voltage(bus, disconnect_voltage)) {
flags_set(&error_flags, ERR_LOAD_SHEDDING);
lvd_timestamp = uptime();
}
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
// off the solar input instead of the load output)
if (bus->voltage > bus->series_voltage(overvoltage) || bus->voltage > PCB_LS_VOLTAGE_MAX) {
if (bus->voltage > dc_bus_series_voltage(bus, overvoltage)
|| bus->voltage > PCB_LS_VOLTAGE_MAX)
{
ov_debounce_counter++;
if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) {
// waited 1s before setting the flag
@@ -113,7 +117,7 @@ void LoadOutput::control()
// load is off: check if errors are resolved and if load can be switched on
if (flags_check(&error_flags, ERR_LOAD_SHEDDING)
&& bus->voltage > bus->src_control_voltage(reconnect_voltage)
&& bus->voltage > dc_bus_src_control_voltage(bus, reconnect_voltage)
&& uptime() - lvd_timestamp > lvd_recovery_delay)
{
flags_clear(&error_flags, ERR_LOAD_SHEDDING);
@@ -126,7 +130,7 @@ void LoadOutput::control()
}
if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE)
&& bus->voltage < (bus->series_voltage(overvoltage) - ov_hysteresis)
&& bus->voltage < (dc_bus_series_voltage(bus, overvoltage) - ov_hysteresis)
&& bus->voltage < (PCB_LS_VOLTAGE_MAX - ov_hysteresis))
{
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
+2 -2
View File
@@ -92,7 +92,7 @@ enum LoadErrorFlag
*
* Stores status of load output incl. 5V USB output (if existing on PCB)
*/
class LoadOutput : public PowerPort
class LoadOutput : public power_port
{
public:
/**
@@ -102,7 +102,7 @@ public:
* @param power_switch Power switch device of the output
* @param led LED indicating the switch state or -1 if not existing
*/
LoadOutput(DcBus *dc_bus, const struct device *power_switch, int led = -1);
LoadOutput(struct dc_bus *dc_bus, const struct device *power_switch, int led = -1);
/** Main load control function, should be called by control timer
*
+8 -8
View File
@@ -43,11 +43,11 @@ int main(void)
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
solar_terminal.init_solar();
power_port_init_solar(&solar_terminal);
#endif
#if CONFIG_HV_TERMINAL_NANOGRID
grid_terminal.init_nanogrid();
power_port_init_nanogrid(&grid_terminal);
#endif
// make sure loaded configuration is valid
@@ -88,21 +88,21 @@ int main(void)
// energy + soc calculation must be called exactly once per second
#if BOARD_HAS_DCDC
if (dcdc.state != DCDC_CONTROL_OFF) {
hv_terminal.energy_balance();
power_port_energy_balance(&hv_terminal);
}
#endif
#if BOARD_HAS_PWM_PORT
if (pwm_switch.active() == 1) {
pwm_switch.energy_balance();
power_port_energy_balance(&pwm_switch);
}
#endif
lv_terminal.energy_balance();
power_port_energy_balance(&lv_terminal);
#if BOARD_HAS_LOAD_OUTPUT
if (load.state == 1) {
load.energy_balance();
power_port_energy_balance(&load);
}
#endif
@@ -146,7 +146,7 @@ void control_thread()
// alerts should trigger only for transients, so update based on actual voltage
daq_set_lv_limits(lv_terminal.bus->voltage * 1.2F, lv_terminal.bus->voltage * 0.8F);
lv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&lv_terminal);
#if BOARD_HAS_PWM_PORT
pwm_switch.control();
@@ -154,7 +154,7 @@ void control_thread()
#endif
#if BOARD_HAS_DCDC
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control(); // control of DC/DC including MPPT algorithm
charging |= half_bridge_is_enabled(dcdc.half_bridge);
#endif
+2 -2
View File
@@ -69,9 +69,9 @@ void oled_update()
char buf[30];
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
PowerPort &in_terminal = solar_terminal;
struct power_port &in_terminal = solar_terminal;
#elif CONFIG_HV_TERMINAL_NANOGRID
PowerPort &in_terminal = grid_terminal;
struct power_port &in_terminal = grid_terminal;
#endif
#if BOARD_HAS_PWM_PORT
+35 -19
View File
@@ -6,44 +6,60 @@
#include "power_port.h"
void PowerPort::init_solar()
#include <string.h>
void dc_bus_init(struct dc_bus *bus)
{
neg_current_limit = -50; // derating based on max. DC/DC or PWM switch current only
pos_current_limit = 0; // no current towards solar panel allowed
memset(bus, 0, sizeof(*bus));
bus->series_multiplier = 1;
}
void PowerPort::init_nanogrid()
void power_port_init(struct power_port *port, struct dc_bus *bus, bool assign_ref_current)
{
pos_current_limit = 5.0;
neg_current_limit = -5.0;
memset(port, 0, sizeof(*port));
port->bus = bus;
if (assign_ref_current) {
bus->ref_current = &port->current;
}
}
void power_port_init_solar(struct power_port *port)
{
port->neg_current_limit = -50; // derating based on max. DC/DC or PWM switch current only
port->pos_current_limit = 0; // no current towards solar panel allowed
}
void power_port_init_nanogrid(struct power_port *port)
{
port->pos_current_limit = 5.0F;
port->neg_current_limit = -5.0F;
// 1 Ohm means 1V change of target voltage per amp, same droop for both current directions.
bus->sink_droop_res = 0.1;
bus->src_droop_res = 0.1;
port->bus->sink_droop_res = 0.1F;
port->bus->src_droop_res = 0.1F;
// also initialize the connected bus
bus->src_voltage_intercept = 30.0; // starting buck mode above this point
bus->sink_voltage_intercept = 28.0; // boost mode until this voltage is reached
port->bus->src_voltage_intercept = 30.0F; // starting buck mode above this point
port->bus->sink_voltage_intercept = 28.0F; // boost mode until this voltage is reached
}
void PowerPort::energy_balance()
void power_port_energy_balance(struct power_port *port)
{
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
if (current >= 0.0F) {
pos_energy_Wh += bus->voltage * current / 3600.0F;
if (port->current >= 0.0F) {
port->pos_energy_Wh += port->bus->voltage * port->current / 3600.0F;
}
else {
neg_energy_Wh -= bus->voltage * current / 3600.0F;
port->neg_energy_Wh -= port->bus->voltage * port->current / 3600.0F;
}
}
void PowerPort::update_bus_current_margins() const
void power_port_update_bus_current_margins(const struct power_port *port)
{
// charging direction of battery
bus->sink_current_margin = pos_current_limit - current;
port->bus->sink_current_margin = port->pos_current_limit - port->current;
// discharging direction of battery
bus->src_current_margin = neg_current_limit - current;
// printf("pos: %.3f neg: %.3f current: %.3f\n", pos_current_limit, neg_current_limit, current);
port->bus->src_current_margin = port->neg_current_limit - port->current;
}
+132 -109
View File
@@ -16,20 +16,21 @@
#include <stddef.h>
#include <stdint.h>
class PowerPort; // forward-declaration
#ifdef __cplusplus
extern "C" {
#endif
/**
* DC bus class
*
* Stores measurement data and settings necessary for voltage control
*/
class DcBus
struct dc_bus
{
public:
/**
* Measured bus voltage
*/
float voltage = 0;
float voltage;
/**
* Filtered bus voltage
@@ -37,7 +38,7 @@ public:
* Suitable filter time constants depend on type of DC bus. This filtering is applied in
* addition to the raw ADC value filtering.
*/
float voltage_filtered = 0;
float voltage_filtered;
/**
* Multiplier for series connection of batteries
@@ -46,7 +47,7 @@ public:
*
* This factor must be applied to all voltage setpoints
*/
int16_t series_multiplier = 1;
int16_t series_multiplier;
/**
* Upper voltage boundary where this bus may be used to sink current.
@@ -54,7 +55,7 @@ public:
* This value is the voltage at zero current. Values for other currents are calculated
* using the droop resistance.
*/
float sink_voltage_intercept = 0;
float sink_voltage_intercept;
/**
* Lower voltage boundary where this bus may be used to source current.
@@ -62,33 +63,33 @@ public:
* This value is the voltage at zero current. Values for other currents are calculated
* using the droop resistance.
*/
float src_voltage_intercept = 0;
float src_voltage_intercept;
/**
* Droop resistance to adjust voltage bounds for current in sourcing direction
*
* control voltage = nominal voltage - droop_res * current
*/
float src_droop_res = 0;
float src_droop_res;
/**
* Droop resistance to adjust voltage bounds for current in sinking direction
*
* control voltage = nominal voltage - droop_res * current
*/
float sink_droop_res = 0;
float sink_droop_res;
/**
* Pointer to current measurement that is used to determine the droop. This is typically the
* battery or nanogrid terminal.
*/
float *ref_current = NULL;
float *ref_current;
/**
* Available additional current towards the DC bus until limits of the connected ports are
* reached
*/
float sink_current_margin = 0;
float sink_current_margin;
/**
* Available additional current from the DC bus until limits of the connected ports are reached
@@ -98,57 +99,11 @@ public:
* we still get power via solar, current up to the amount of solar input can be diverted into
* the load (until the margin gets 0 and the battery would be discharged).
*/
float src_current_margin = 0;
/**
* Calculate current-compensated src control voltage, considering droop and series multiplier
*
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
* left empty, the src_voltage_intercept is considered.
*/
inline float src_control_voltage(float voltage_zero_current = 0)
{
float v0 = (voltage_zero_current == 0) ? src_voltage_intercept : voltage_zero_current;
if (ref_current != nullptr) {
return (v0 - src_droop_res * (*ref_current)) * series_multiplier;
}
else {
return v0 * series_multiplier;
}
}
/**
* Calculate current-compensated sink control voltage, considering droop and series multiplier
*
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
* left empty, the sink_voltage_intercept is considered.
*/
inline float sink_control_voltage(float voltage_zero_current = 0)
{
float v0 = (voltage_zero_current == 0) ? sink_voltage_intercept : voltage_zero_current;
if (ref_current != nullptr) {
return (v0 - sink_droop_res * (*ref_current)) * series_multiplier;
}
else {
return v0 * series_multiplier;
}
}
/**
* Calculate voltage for series connected batteries based on setpoint for single battery
*
* @param single_voltage Voltage for single battery
*
* @returns Total voltage
*/
inline float series_voltage(float single_voltage)
{
return single_voltage * series_multiplier;
}
float src_current_margin;
};
/**
* Power Port class
* Power port
*
* Stores current measurements and limits for external terminals or internal ports
*
@@ -178,18 +133,17 @@ public:
* a negative current at the high voltage side. The system boundary is the DC/DC sub-circuit,
* which sources current from the high side port and sinks it through the low side port.
*/
class PowerPort
struct power_port
{
public:
/**
* Each power port is connected to a DC bus, containing relevant voltage information
*/
DcBus *bus = NULL;
struct dc_bus *bus;
/**
* Measured current through this port (positive sign = current into the external device)
*/
float current = 0;
float current;
/**
* Filtered port current
@@ -197,71 +151,140 @@ public:
* Suitable filter time constants depend on type of port. This filtering is applied in
* addition to the raw ADC value filtering.
*/
float current_filtered = 0;
float current_filtered;
/**
* Product of port current and bus voltage
*/
float power = 0;
float power;
/**
* Maximum positive current (valid values >= 0.0)
*/
float pos_current_limit = 0;
float pos_current_limit;
/**
* Maximum negative current (valid values <= 0.0)
*/
float neg_current_limit = 0;
float neg_current_limit;
/**
* Cumulated energy in positive current direction since last counter reset (Wh)
*/
float pos_energy_Wh = 0;
float pos_energy_Wh;
/**
* Cumulated energy in negative current direction since last counter reset (Wh)
*/
float neg_energy_Wh = 0;
/**
* Constructor assigning the port to a DC bus
*
* @param dc_bus The DC bus this port is assigned to
* @param assign_ref_current defines if the bus ref_current should point to the current
* of this port (must be true for at least one port)
*/
PowerPort(DcBus *dc_bus, bool assign_ref_current = false) : bus(dc_bus)
{
if (assign_ref_current) {
bus->ref_current = &current;
}
}
/**
* Initialize power port for solar panel connection
*/
void init_solar();
/**
* Initialize power port for nanogrid connection
*/
void init_nanogrid();
/**
* Energy balance calculation for power port
*
* Must be called exactly once per second, otherwise energy calculation gets wrong.
*/
void energy_balance();
/**
* Sets current limits for control of the bus voltage
*
* This function has to be called using the port defining the bus control targets, i.e.
* the battery, the solar panel or the DC grid.
*/
void update_bus_current_margins() const;
float neg_energy_Wh;
};
/**
* Initialize a DC bus
*
* All values are set to zero, except for the series multiplier, which is set to 1.
*
* @param bus DC bus
*/
void dc_bus_init(struct dc_bus *bus);
/**
* Calculate current-compensated src control voltage, considering droop and series multiplier
*
* @param bus DC bus
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
* 0, the src_voltage_intercept is considered.
*/
static inline float dc_bus_src_control_voltage(const struct dc_bus *bus, float voltage_zero_current)
{
float v0 = (voltage_zero_current == 0) ? bus->src_voltage_intercept : voltage_zero_current;
if (bus->ref_current != NULL) {
return (v0 - bus->src_droop_res * (*bus->ref_current)) * bus->series_multiplier;
}
else {
return v0 * bus->series_multiplier;
}
}
/**
* Calculate current-compensated sink control voltage, considering droop and series multiplier
*
* @param bus DC bus
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
* 0, the sink_voltage_intercept is considered.
*/
static inline float dc_bus_sink_control_voltage(const struct dc_bus *bus,
float voltage_zero_current)
{
float v0 = (voltage_zero_current == 0) ? bus->sink_voltage_intercept : voltage_zero_current;
if (bus->ref_current != NULL) {
return (v0 - bus->sink_droop_res * (*bus->ref_current)) * bus->series_multiplier;
}
else {
return v0 * bus->series_multiplier;
}
}
/**
* Calculate voltage for series connected batteries based on setpoint for single battery
*
* @param bus DC bus
* @param single_voltage Voltage for single battery
*
* @returns Total voltage
*/
static inline float dc_bus_series_voltage(const struct dc_bus *bus, float single_voltage)
{
return single_voltage * bus->series_multiplier;
}
/**
* Initialize a power port and assign it to a DC bus
*
* All values are set to zero.
*
* @param port Power port
* @param bus The DC bus this port is assigned to
* @param assign_ref_current defines if the bus ref_current should point to the current
* of this port (must be true for at least one port)
*/
void power_port_init(struct power_port *port, struct dc_bus *bus, bool assign_ref_current);
/**
* Initialize power port for solar panel connection
*
* @param port Power port
*/
void power_port_init_solar(struct power_port *port);
/**
* Initialize power port for nanogrid connection
*
* @param port Power port
*/
void power_port_init_nanogrid(struct power_port *port);
/**
* Energy balance calculation for power port
*
* Must be called exactly once per second, otherwise energy calculation gets wrong.
*
* @param port Power port
*/
void power_port_energy_balance(struct power_port *port);
/**
* Sets current limits for control of the bus voltage
*
* This function has to be called using the port defining the bus control targets, i.e.
* the battery, the solar panel or the DC grid.
*
* @param port Power port
*/
void power_port_update_bus_current_margins(const struct power_port *port);
#ifdef __cplusplus
}
#endif
#endif /* POWER_PORT_H */
+9 -7
View File
@@ -36,8 +36,10 @@ bool PwmSwitch::signal_high()
return power_switch_is_conducting(power_switch);
}
PwmSwitch::PwmSwitch(DcBus *dc_bus, const struct device *sw) : PowerPort(dc_bus), power_switch(sw)
PwmSwitch::PwmSwitch(struct dc_bus *dc_bus, const struct device *sw) : power_switch(sw)
{
power_port_init(this, dc_bus, false);
if (!device_is_ready(power_switch)) {
LOG_ERR("PWM switch not ready");
enable = false;
@@ -92,15 +94,15 @@ void PwmSwitch::control()
off_timestamp = uptime();
LOG_INF("PWM charger stop, current = %d mA", (int)(current * 1000.0F));
}
else if (bus->voltage > bus->sink_control_voltage() + 0.3F) {
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) + 0.3F) {
power_switch_off(power_switch);
off_timestamp = uptime();
dev_stat.set_error(ERR_PWM_SWITCH_OVERVOLTAGE);
LOG_INF("PWM charger stop, overvoltage.");
}
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|| current < neg_current_limit // port current limit exceeded
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) // bus voltage above target
|| current < neg_current_limit // port current limit exceeded
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
{
// decrease power, as limits were reached
@@ -137,14 +139,14 @@ void PwmSwitch::control()
}
if (dev_stat.has_error(ERR_PWM_SWITCH_OVERVOLTAGE)
&& bus->voltage < bus->sink_control_voltage() - 0.5F)
&& bus->voltage < dc_bus_sink_control_voltage(bus, 0) - 0.5F)
{
dev_stat.clear_error(ERR_PWM_SWITCH_OVERVOLTAGE);
}
}
else {
if (bus->sink_current_margin > 0 && // charging allowed
bus->voltage < bus->sink_control_voltage()
bus->voltage < dc_bus_sink_control_voltage(bus, 0)
&& ext_voltage > bus->voltage + offset_voltage_start
&& uptime() > (off_timestamp + restart_interval) && enable == true)
{
+2 -2
View File
@@ -29,10 +29,10 @@
*
* Contains all data belonging to the PWM switching sub-component.
*/
class PwmSwitch : public PowerPort
class PwmSwitch : public power_port
{
public:
PwmSwitch(DcBus *dc_bus, const struct device *power_switch);
PwmSwitch(struct dc_bus *dc_bus, const struct device *power_switch);
/**
* Main control function for the PWM switching algorithm
+14 -10
View File
@@ -24,12 +24,16 @@
#include "load.h" // load and USB output management
#include "pwm_switch.h" // PWM charge controller
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
// low voltage terminal (battery for typical MPPT)
extern struct power_port lv_terminal;
struct dc_bus lv_bus = { .series_multiplier = 1, .ref_current = &lv_terminal.current };
struct power_port lv_terminal = { .bus = &lv_bus };
#if BOARD_HAS_DCDC
DcBus hv_bus;
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
// high voltage terminal (solar for typical MPPT)
extern struct power_port hv_terminal;
struct dc_bus hv_bus = { .series_multiplier = 1, .ref_current = &hv_terminal.current };
struct power_port hv_terminal = { .bus = &hv_bus };
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_AUTO);
#elif CONFIG_HV_TERMINAL_BATTERY
@@ -56,21 +60,21 @@ LoadOutput usb_pwr(&lv_bus, DEVICE_DT_GET(USB_SWITCH_NODE));
#endif
#if CONFIG_HV_TERMINAL_SOLAR
PowerPort &solar_terminal = hv_terminal;
struct power_port &solar_terminal = hv_terminal;
#elif CONFIG_LV_TERMINAL_SOLAR
PowerPort &solar_terminal = lv_terminal;
struct power_port &solar_terminal = lv_terminal;
#elif CONFIG_PWM_TERMINAL_SOLAR
PowerPort &solar_terminal = pwm_switch;
struct power_port &solar_terminal = pwm_switch;
#endif
#if CONFIG_HV_TERMINAL_NANOGRID
PowerPort &grid_terminal = hv_terminal;
struct power_port &grid_terminal = hv_terminal;
#endif
#if CONFIG_LV_TERMINAL_BATTERY
PowerPort &bat_terminal = lv_terminal;
struct power_port &bat_terminal = lv_terminal;
#elif CONFIG_HV_TERMINAL_BATTERY
PowerPort &bat_terminal = hv_terminal;
struct power_port &bat_terminal = hv_terminal;
#endif
Charger charger(&bat_terminal);
+7 -7
View File
@@ -16,12 +16,12 @@
#include "pwm_switch.h"
#include "thingset.h"
extern DcBus lv_bus;
extern PowerPort lv_terminal;
extern struct dc_bus lv_bus;
extern struct power_port lv_terminal;
#if BOARD_HAS_DCDC
extern DcBus hv_bus;
extern PowerPort hv_terminal;
extern struct dc_bus hv_bus;
extern struct power_port hv_terminal;
extern Dcdc dcdc;
#endif
@@ -29,9 +29,9 @@ extern Dcdc dcdc;
extern PwmSwitch pwm_switch;
#endif
extern PowerPort &bat_terminal;
extern PowerPort &solar_terminal;
extern PowerPort &grid_terminal;
extern struct power_port &bat_terminal;
extern struct power_port &solar_terminal;
extern struct power_port &grid_terminal;
extern DeviceStatus dev_stat;
extern Charger charger;
+10 -10
View File
@@ -19,12 +19,12 @@ static void init_structs_buck(int num_batteries = 1)
half_bridge_stop(dcdc.half_bridge);
dev_stat.error_flags = 0;
hv_terminal.init_solar();
power_port_init_solar(&hv_terminal);
hv_terminal.bus->voltage = 20 * num_batteries;
hv_terminal.bus->src_voltage_intercept = 18 * num_batteries;
hv_terminal.bus->series_multiplier = 1;
hv_terminal.current = 0;
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100);
charger.port = &lv_terminal;
@@ -32,7 +32,7 @@ static void init_structs_buck(int num_batteries = 1)
lv_terminal.bus->voltage = 14 * num_batteries;
lv_terminal.bus->series_multiplier = num_batteries;
lv_terminal.current = 0;
lv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&lv_terminal);
dcdc.mode = DCDC_MODE_BUCK;
dcdc.temp_mosfets = 25;
@@ -60,13 +60,13 @@ static void init_structs_boost(int num_batteries = 1)
{
half_bridge_stop(dcdc.half_bridge);
hv_terminal.init_solar();
power_port_init_solar(&hv_terminal);
lv_terminal.bus->voltage = 20;
lv_terminal.bus->src_voltage_intercept = 18;
lv_terminal.bus->series_multiplier = 1;
dcdc.inductor_current = 0;
dcdc.power = 0;
lv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&lv_terminal);
int num_cells = (num_batteries == 1) ? 10 : 5;
battery_conf_init(&bat_conf, BAT_TYPE_NMC, num_cells, 9);
@@ -76,7 +76,7 @@ static void init_structs_boost(int num_batteries = 1)
hv_terminal.bus->series_multiplier = num_batteries;
hv_terminal.current = 0;
hv_terminal.power = 0;
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
dcdc.mode = DCDC_MODE_BOOST;
dcdc.temp_mosfets = 25;
@@ -231,7 +231,7 @@ ZTEST(dcdc, test_buck_derating_output_current_too_high)
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
lv_terminal.current = lv_terminal.pos_current_limit + 0.1f;
lv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&lv_terminal);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
@@ -266,7 +266,7 @@ ZTEST(dcdc, test_buck_derating_input_current_too_high)
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.current = hv_terminal.neg_current_limit - 0.1f;
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
@@ -344,7 +344,7 @@ ZTEST(dcdc, test_boost_derating_output_current_too_high)
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.current = hv_terminal.pos_current_limit + 0.1f;
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before);
@@ -356,7 +356,7 @@ ZTEST(dcdc, test_boost_derating_input_voltage_too_low)
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
lv_terminal.bus->voltage = lv_terminal.bus->src_voltage_intercept - 0.1f;
dcdc.power = -1.2f;
hv_terminal.update_bus_current_margins();
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_equal(dcdc.state, DCDC_CONTROL_CV_LS);
+28 -14
View File
@@ -45,7 +45,8 @@ static void load_init(LoadOutput *l, bool on = false, int num_batteries = 1)
ZTEST(load, test_control_off_to_on_if_everything_fine)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
zassert_false(power_switch_is_on(load_switch));
@@ -59,7 +60,8 @@ ZTEST(load, test_control_off_to_on_if_everything_fine)
ZTEST(load, test_control_off_to_on_if_everything_fine_dual_battery)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, false, 2);
@@ -71,7 +73,8 @@ ZTEST(load, test_control_off_to_on_if_everything_fine_dual_battery)
ZTEST(load, test_control_on_to_off_shedding)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -84,7 +87,8 @@ ZTEST(load, test_control_on_to_off_shedding)
ZTEST(load, test_control_on_to_off_overvoltage)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -105,7 +109,8 @@ ZTEST(load, test_control_on_to_off_overvoltage)
ZTEST(load, test_control_on_to_off_overvoltage_dual_battery)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true, 2);
@@ -126,7 +131,8 @@ ZTEST(load, test_control_on_to_off_overvoltage_dual_battery)
ZTEST(load, test_control_on_to_off_overcurrent)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -147,7 +153,8 @@ ZTEST(load, test_control_on_to_off_overcurrent)
ZTEST(load, test_control_on_to_off_voltage_dip)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -159,7 +166,8 @@ ZTEST(load, test_control_on_to_off_voltage_dip)
ZTEST(load, test_control_on_to_off_bus_limit)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -171,7 +179,8 @@ ZTEST(load, test_control_on_to_off_bus_limit)
ZTEST(load, test_control_on_to_off_if_enable_false)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
@@ -183,7 +192,8 @@ ZTEST(load, test_control_on_to_off_if_enable_false)
ZTEST(load, test_control_off_shedding_to_on_after_delay)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_SHEDDING;
@@ -201,7 +211,8 @@ ZTEST(load, test_control_off_shedding_to_on_after_delay)
ZTEST(load, test_control_off_overcurrent_to_on_after_delay)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_OVERCURRENT;
@@ -219,7 +230,8 @@ ZTEST(load, test_control_off_overcurrent_to_on_after_delay)
ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
bus.voltage = load_out.overvoltage + 0.1f;
@@ -242,7 +254,8 @@ ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage)
ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage_dual_battery)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, false, 2);
bus.voltage = (load_out.overvoltage + 0.1f) * bus.series_multiplier;
@@ -265,7 +278,8 @@ ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage_dual_battery)
ZTEST(load, test_control_off_short_circuit_flag_reset)
{
DcBus bus = {};
struct dc_bus bus;
dc_bus_init(&bus);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_SHORT_CIRCUIT;
+6 -6
View File
@@ -47,9 +47,9 @@ void energy_calculation_init()
daq_update();
for (int i = 0; i < 60 * 60 * sun_hours; i++) {
hv_terminal.energy_balance();
lv_terminal.energy_balance();
load.energy_balance();
power_port_energy_balance(&hv_terminal);
power_port_energy_balance(&lv_terminal);
power_port_energy_balance(&load);
}
// disable DC/DC = solar charging
@@ -59,9 +59,9 @@ void energy_calculation_init()
daq_update();
for (int i = 0; i < 60 * 60 * night_hours; i++) {
hv_terminal.energy_balance();
lv_terminal.energy_balance();
load.energy_balance();
power_port_energy_balance(&hv_terminal);
power_port_energy_balance(&lv_terminal);
power_port_energy_balance(&load);
}
}