Move voltage control parameters to DcBus class

The sink and source voltage boundaries for droop control and battery
charge voltage management are now part of the DcBus class.
Also added source and sink current margins, which will be used by the
DC/DC for control targets instead of port current limits, which were
difficult to synchronize before.
This commit is contained in:
Martin Jaeger
2020-02-07 12:03:25 +01:00
parent a7d6dbc3b5
commit 43f90eea7c
16 changed files with 421 additions and 390 deletions
+29 -20
View File
@@ -263,7 +263,7 @@ void Charger::discharge_control(BatConf *bat_conf)
if (port->neg_current_limit < 0) {
// discharging currently allowed. see if that's still valid:
if (port->bus->voltage < num_batteries *
(bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res))
(bat_conf->voltage_load_disconnect - port->current * port->bus->droop_res))
{
// low state of charge
port->neg_current_limit = 0;
@@ -298,7 +298,7 @@ void Charger::discharge_control(BatConf *bat_conf)
else {
// discharging currently not allowed. should we allow it?
if (port->bus->voltage >= num_batteries *
(bat_conf->voltage_load_reconnect - port->current * port->neg_droop_res)
(bat_conf->voltage_load_reconnect - port->current * port->bus->droop_res)
&& bat_temperature < bat_conf->discharge_temp_max - 1
&& bat_temperature > bat_conf->discharge_temp_min + 1)
{
@@ -338,11 +338,12 @@ void Charger::charge_control(BatConf *bat_conf)
switch (state) {
case CHG_STATE_IDLE: {
if (port->bus->voltage < bat_conf->voltage_recharge * num_batteries
&& port->bus->voltage > bat_conf->voltage_absolute_min * num_batteries // ToDo set error flag otherwise
&& (uptime() - time_state_changed) > bat_conf->time_limit_recharge
&& bat_temperature < bat_conf->charge_temp_max - 1
&& bat_temperature > bat_conf->charge_temp_min + 1)
{
port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
port->pos_current_limit = bat_conf->charge_current_max;
full = false;
@@ -355,11 +356,11 @@ void Charger::charge_control(BatConf *bat_conf)
}
case CHG_STATE_BULK: {
// continuously adjust voltage setting for temperature compensation
port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->bus->voltage > num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
(port->bus->sink_voltage_bound - port->current * port->bus->droop_res))
{
enter_state(CHG_STATE_TOPPING);
}
@@ -367,11 +368,11 @@ void Charger::charge_control(BatConf *bat_conf)
}
case CHG_STATE_TOPPING: {
// continuously adjust voltage setting for temperature compensation
port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->topping_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->bus->voltage >= num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
(port->bus->sink_voltage_bound - port->current * port->bus->droop_res))
{
time_voltage_limit_reached = uptime();
}
@@ -393,12 +394,12 @@ void Charger::charge_control(BatConf *bat_conf)
num_deep_discharges - deep_dis_last_equalization
>= bat_conf->equalization_trigger_deep_cycles))
{
port->sink_voltage_max = bat_conf->equalization_voltage;
port->bus->sink_voltage_bound = bat_conf->equalization_voltage;
port->pos_current_limit = bat_conf->equalization_current_limit;
enter_state(CHG_STATE_EQUALIZATION);
}
else if (bat_conf->trickle_enabled) {
port->sink_voltage_max = num_batteries * (bat_conf->trickle_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
enter_state(CHG_STATE_TRICKLE);
}
@@ -411,11 +412,11 @@ void Charger::charge_control(BatConf *bat_conf)
}
case CHG_STATE_TRICKLE: {
// continuously adjust voltage setting for temperature compensation
port->sink_voltage_max = num_batteries * (bat_conf->trickle_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->bus->voltage >= num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
(port->bus->sink_voltage_bound - port->current * port->bus->droop_res))
{
time_voltage_limit_reached = uptime();
}
@@ -432,7 +433,7 @@ void Charger::charge_control(BatConf *bat_conf)
}
case CHG_STATE_EQUALIZATION: {
// continuously adjust voltage setting for temperature compensation
port->sink_voltage_max = num_batteries * (bat_conf->equalization_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->equalization_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
// current or time limit for equalization reached
@@ -445,7 +446,7 @@ void Charger::charge_control(BatConf *bat_conf)
discharged_Ah = 0; // reset coulomb counter again
if (bat_conf->trickle_enabled) {
port->sink_voltage_max = num_batteries * (bat_conf->trickle_voltage +
port->bus->sink_voltage_bound = num_batteries * (bat_conf->trickle_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
enter_state(CHG_STATE_TRICKLE);
}
@@ -459,21 +460,29 @@ void Charger::charge_control(BatConf *bat_conf)
}
}
void battery_init_dc_bus(PowerPort *port, BatConf *bat, unsigned int num_batteries)
void battery_init_terminal(PowerPort *port, BatConf *bat, unsigned int num_batteries)
{
unsigned int n = (num_batteries == 2 ? 2 : 1); // only 1 or 2 allowed
port->src_voltage_start = bat->voltage_load_reconnect * n;
port->src_voltage_stop = bat->voltage_load_disconnect * n;
port->neg_current_limit = -bat->discharge_current_max;
// negative sign for compensation of actual resistance
port->neg_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n;
port->bus->droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n;
port->sink_voltage_max = bat->topping_voltage * n;
port->sink_voltage_min = bat->voltage_absolute_min * n;
port->bus->sink_voltage_bound = bat->topping_voltage * n;
port->pos_current_limit = bat->charge_current_max;
port->bus->src_voltage_bound = bat->voltage_load_disconnect * n;
// negative sign for compensation of actual resistance
port->pos_droop_res = -bat->wire_resistance * n;
port->bus->droop_res = -bat->wire_resistance * n;
}
void battery_init_load(LoadOutput *load, BatConf *bat, unsigned int num_batteries)
{
unsigned int n = (num_batteries == 2 ? 2 : 1); // only 1 or 2 allowed
load->reconnect_voltage = bat->voltage_load_reconnect * n;
load->disconnect_voltage = bat->voltage_load_disconnect * n;
load->overvoltage = bat->voltage_absolute_max * n;
}
+9 -2
View File
@@ -18,6 +18,7 @@
#include <time.h>
#include "power_port.h"
#include "load.h"
/** Battery cell types
*/
@@ -352,10 +353,16 @@ bool battery_conf_check(BatConf *bat);
*/
void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *charger = NULL);
/** Initialize dc bus for battery connection
/** Initialize terminal and dc bus for battery connection
*
* @param num_batteries definies the number of series connected batteries, e.g. 2 for 24V system
*/
void battery_init_dc_bus(PowerPort *port, BatConf *bat, unsigned int num_batteries);
void battery_init_terminal(PowerPort *port, BatConf *bat, unsigned int num_batteries);
/** Initialize terminal and dc bus for battery connection
*
* @param num_batteries definies the number of series connected batteries, e.g. 2 for 24V system
*/
void battery_init_load(LoadOutput *port, BatConf *bat, unsigned int num_batteries);
#endif /* BAT_CHARGER_H */
+23 -9
View File
@@ -30,6 +30,18 @@ uint32_t device_id = CONFIG_DEVICE_ID;
extern uint32_t timestamp;
#if CONFIG_LV_TERMINAL_BATTERY
#define bat_bus lv_bus
#elif CONFIG_HV_TERMINAL_BATTERY
#define bat_bus hv_bus
#endif
#if CONFIG_HV_TERMINAL_SOLAR
#define solar_bus hv_bus
#elif CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
#define solar_bus lv_bus
#endif
/** Data Objects
*
* IDs from 0x00 to 0x17 consume only 1 byte, so they are reserved for output data
@@ -173,7 +185,7 @@ const data_object_t data_objects[] = {
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#if CONFIG_HAS_DCDC_CONVERTER
TS_DATA_OBJ_BOOL(0x62, "DcdcEn", &dcdc.enabled,
TS_DATA_OBJ_BOOL(0x62, "DcdcEn", &dcdc.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
@@ -183,13 +195,10 @@ const data_object_t data_objects[] = {
#endif
#if CONFIG_HV_TERMINAL_NANOGRID
TS_DATA_OBJ_FLOAT(0x64, "GridSink_V", &grid_terminal.sink_voltage_max, 2,
TS_DATA_OBJ_FLOAT(0x64, "GridSink_V", &hv_bus.sink_voltage_bound, 2,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
TS_DATA_OBJ_FLOAT(0x65, "GridSrcStart_V", &grid_terminal.src_voltage_start, 2,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
TS_DATA_OBJ_FLOAT(0x66, "GridSrcStop_V", &grid_terminal.src_voltage_stop, 2,
TS_DATA_OBJ_FLOAT(0x66, "GridSrc_V", &hv_bus.src_voltage_bound, 2,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
@@ -207,11 +216,11 @@ const data_object_t data_objects[] = {
TS_OUTPUT, TS_READ_ALL),
// battery related data objects
TS_DATA_OBJ_FLOAT(0x70, "Bat_V", &bat_terminal.bus->voltage, 2,
TS_DATA_OBJ_FLOAT(0x70, "Bat_V", &bat_bus.voltage, 2,
TS_OUTPUT, TS_READ_ALL),
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
TS_DATA_OBJ_FLOAT(0x71, "Solar_V", &solar_terminal.bus->voltage, 2,
TS_DATA_OBJ_FLOAT(0x71, "Solar_V", &solar_bus.voltage, 2,
TS_OUTPUT, TS_READ_ALL),
#endif
@@ -248,7 +257,7 @@ const data_object_t data_objects[] = {
TS_OUTPUT, TS_READ_ALL),
#endif
TS_DATA_OBJ_FLOAT(0x7B, "BatTarget_V", &bat_terminal.sink_voltage_max, 2,
TS_DATA_OBJ_FLOAT(0x7B, "BatTarget_V", &bat_bus.sink_voltage_bound, 2,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_FLOAT(0x7C, "BatTarget_A", &bat_terminal.pos_current_limit, 2,
@@ -265,6 +274,11 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_FLOAT(0x7F, "Load_W", &load_terminal.power, 2,
TS_OUTPUT, TS_READ_ALL),
#if CONFIG_HV_TERMINAL_NANOGRID
TS_DATA_OBJ_FLOAT(0x80, "Grid_V", &hv_bus.voltage, 2,
TS_OUTPUT, TS_READ_ALL),
#endif
TS_DATA_OBJ_UINT32(0x90, "ErrorFlags", &dev_stat.error_flags,
TS_OUTPUT, TS_READ_ALL),
+27 -23
View File
@@ -30,13 +30,15 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode)
hvs = hv_side;
lvs = lv_side;
mode = op_mode;
enabled = true;
enable = true;
state = DCDC_STATE_OFF;
lvs->neg_current_limit = -DCDC_CURRENT_MAX;
lvs->pos_current_limit = DCDC_CURRENT_MAX;
ls_current_max = DCDC_CURRENT_MAX;
hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX;
ls_voltage_max = LOW_SIDE_VOLTAGE_MAX;
ls_voltage_min = 9.0;
output_power_min = 1; // switch off iff power < 1 W
output_power_min = 1; // switch off if power < 1 W
restart_interval = 60;
off_timestamp = -10000; // start immediately
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
@@ -72,17 +74,11 @@ int Dcdc::duty_cycle_delta()
int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power
print_test("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, "
"v_chg_target: %.2f, i_chg_max: %.2f, PWM: %.1f, chg_state: %d\n",
out->power, power_prev, in->bus->voltage, out->bus->voltage, in->current, out->current,
out->sink_voltage_max, out->pos_current_limit, half_bridge_get_duty_cycle() * 100.0,
state);
if ((uptime() - power_good_timestamp > 10 || out->power < -1.0) && mode != MODE_NANOGRID) {
// swith off after 10s low power or negative power (if not in nanogrid mode)
pwr_inc_goal = 0;
}
else if (out->bus->voltage > (out->sink_voltage_max - out->pos_droop_res * out->current)) {
else if (out->bus->voltage > out->bus->droop_voltage(out->bus->sink_voltage_bound)) {
// output voltage target reached
state = DCDC_STATE_CV;
pwr_inc_goal = -1; // decrease output power
@@ -94,14 +90,14 @@ int Dcdc::duty_cycle_delta()
}
else if (fabs(lvs->current) > ls_current_max // current above hardware maximum
|| temp_mosfets > 80 // temperature limits exceeded
|| (in->bus->voltage < (in->src_voltage_stop - in->neg_droop_res * in->current)
|| (in->bus->voltage < in->bus->droop_voltage(in->bus->src_voltage_bound)
&& out->current > 0.1) // input voltage below limit
|| in->current < in->neg_current_limit) // input current (negative signs) limit exceeded
{
state = DCDC_STATE_DERATING;
pwr_inc_goal = -1; // decrease output power
}
else if (out->power < output_power_min && out->bus->voltage < out->src_voltage_start) {
else if (out->power < output_power_min && out->bus->voltage < out->bus->src_voltage_bound) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwr_inc_goal = 1; // increase output power
}
@@ -114,13 +110,22 @@ int Dcdc::duty_cycle_delta()
pwr_inc_goal = pwm_delta;
}
print_test("P: %.2fW (prev %.2fW), in: %.2fV %.2fA (margin %.2fA limit %.2fA), "
"out: %.2fV (target %.2fV) %.2fA (margin %.2fA limit %.2fA), "
"PWM: %.1f, chg_state: %d, pwr_inc: %d, buck/boost: %d\n",
out->power, power_prev, in->bus->voltage, in->current, in->bus->src_current_margin,
in->neg_current_limit,
out->bus->voltage, out->bus->sink_voltage_bound, out->current, out->bus->sink_current_margin,
out->pos_current_limit,
half_bridge_get_duty_cycle() * 100.0, state, pwr_inc_goal, pwr_inc_pwm_direction);
power_prev = out->power;
return pwr_inc_goal * pwr_inc_pwm_direction;
}
int Dcdc::check_start_conditions()
{
if (enabled == false ||
if (enable == false ||
hvs->bus->voltage > hs_voltage_max || // also critical for buck mode because of ringing
lvs->bus->voltage > ls_voltage_max ||
lvs->bus->voltage < ls_voltage_min ||
@@ -130,20 +135,19 @@ int Dcdc::check_start_conditions()
return 0; // no energy transfer allowed
}
if (lvs->pos_current_limit > 0 &&
lvs->bus->voltage < lvs->sink_voltage_max &&
lvs->bus->voltage > lvs->sink_voltage_min &&
hvs->neg_current_limit < 0 &&
hvs->bus->voltage > hvs->src_voltage_start)
if (lvs->bus->sink_current_margin > 0 &&
lvs->bus->voltage < lvs->bus->sink_voltage_bound &&
hvs->bus->src_current_margin < 0 &&
hvs->bus->voltage > hvs->bus->src_voltage_bound &&
hvs->bus->voltage * 0.85 > lvs->bus->voltage)
{
return 1; // start in buck mode allowed
}
if (hvs->pos_current_limit > 0 &&
hvs->bus->voltage < hvs->sink_voltage_max &&
hvs->bus->voltage > hvs->sink_voltage_min &&
lvs->neg_current_limit < 0 &&
lvs->bus->voltage > lvs->src_voltage_start)
if (hvs->bus->sink_current_margin > 0 &&
hvs->bus->voltage < hvs->bus->sink_voltage_bound &&
lvs->bus->src_current_margin < 0 &&
lvs->bus->voltage > lvs->bus->src_voltage_bound)
{
return -1; // start in boost mode allowed
}
@@ -158,7 +162,7 @@ void Dcdc::control()
if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) {
stop_reason = "emergency (voltage limits exceeded)";
}
else if (enabled == false) {
else if (enable == false) {
stop_reason = "disabled";
}
else {
+1 -1
View File
@@ -93,7 +93,7 @@ public:
void fuse_destruction();
DcdcOperationMode mode; ///< DC/DC mode (buck, boost or nanogrid)
bool enabled; ///< Can be used to disable the DC/DC power stage
bool enable; ///< Can be used to disable the DC/DC power stage
uint16_t state; ///< Control state (off / MPPT / CC / CV)
// actual measurements
+11 -9
View File
@@ -52,7 +52,7 @@ void LoadOutput::usb_state_machine()
switch (usb_state) {
case LOAD_STATE_DISABLED:
if (usb_enable == true) {
if (port->pos_current_limit > 0) {
if (port->bus->src_current_margin < 0) {
usb_set(true);
usb_state = LOAD_STATE_ON;
}
@@ -132,24 +132,25 @@ void LoadOutput::control()
oc_timestamp = uptime();
}
if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) {
if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE) ||
port->bus->voltage < disconnect_voltage)
{
dev_stat.set_error(ERR_LOAD_LOW_SOC);
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)
static int debounce_counter = 0;
if (port->bus->voltage > port->sink_voltage_max ||
if (port->bus->voltage > port->bus->sink_voltage_bound ||
port->bus->voltage > LOW_SIDE_VOLTAGE_MAX)
{
debounce_counter++;
if (debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag
ov_debounce_counter++;
if (ov_debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag
dev_stat.set_error(ERR_LOAD_OVERVOLTAGE);
}
}
else {
debounce_counter = 0;
ov_debounce_counter = 0;
}
if (dev_stat.has_error(ERR_LOAD_ANY)) {
@@ -165,6 +166,7 @@ void LoadOutput::control()
if (dev_stat.has_error(ERR_LOAD_LOW_SOC) &&
!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE) &&
port->bus->voltage > reconnect_voltage &&
uptime() - lvd_timestamp > lvd_recovery_delay)
{
dev_stat.clear_error(ERR_LOAD_LOW_SOC);
@@ -178,7 +180,7 @@ void LoadOutput::control()
}
if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) &&
port->bus->voltage < (port->sink_voltage_max - ov_hysteresis) &&
port->bus->voltage < (port->bus->sink_voltage_bound - ov_hysteresis) &&
port->bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
{
dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE);
@@ -190,7 +192,7 @@ void LoadOutput::control()
}
// finally switch on if all errors were resolved
if (enable == true && !dev_stat.has_error(ERR_LOAD_ANY) && port->pos_current_limit > 0) {
if (enable == true && !dev_stat.has_error(ERR_LOAD_ANY) && port->bus->src_current_margin < 0) {
switch_set(true);
}
}
+8 -2
View File
@@ -83,9 +83,9 @@ public:
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
///< measurement of (external) load output terminal
bool enable; ///< Target on state set via communication port (overruled if
bool enable = false; ///< Target on state set via communication port (overruled if
///< battery is empty or any errors occured)
bool usb_enable; ///< Target on state for USB output
bool usb_enable = false; ///< Target on state for USB output
bool pgood = false; ///< Power good flag that is true if load switch is on
bool usb_pgood = false; ///< Power good flag for USB output
@@ -94,6 +94,9 @@ public:
int32_t oc_recovery_delay; ///< Seconds before we attempt to re-enable the load
///< after an overcurrent event
float disconnect_voltage = 0; ///< Low voltage disconnect (LVD) setpoint
float reconnect_voltage = 0; ///< Low voltage reconnect (LVD) setpoint
time_t lvd_timestamp; ///< Time when last low voltage disconnect happened
int32_t lvd_recovery_delay; ///< Seconds before we re-enable the load after a low voltage
///< disconnect
@@ -101,12 +104,15 @@ public:
float junction_temperature; ///< calculated using thermal model based on current and ambient
///< temperature measurement (unit: °C)
float overvoltage = 0;
float ov_hysteresis; ///< Hysteresis to switch back on after an overvoltage event
private:
int determine_load_state();
void short_circuit_comp_init();
int ov_debounce_counter = 0;
#ifdef __ZEPHYR__
void get_bindings();
+8 -15
View File
@@ -32,12 +32,12 @@
#include "bl_support.h" // Bootloader support from the application side
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
#if CONFIG_HAS_DCDC_CONVERTER
DcBus hv_bus;
PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID);
#elif CONFIG_HV_TERMINAL_BATTERY
@@ -123,8 +123,8 @@ int main()
#endif
charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system
battery_init_dc_bus(&bat_terminal, &bat_conf, charger.num_batteries);
load_terminal.init_load(bat_conf.voltage_absolute_max * charger.num_batteries);
battery_init_terminal(&bat_terminal, &bat_conf, charger.num_batteries);
battery_init_load(&load, &bat_conf, charger.num_batteries);
wait(2); // safety feature: be able to re-flash before starting
control_timer_start(CONTROL_FREQUENCY);
@@ -147,14 +147,6 @@ int main()
charger.discharge_control(&bat_conf);
charger.charge_control(&bat_conf);
#if CONFIG_HAS_DCDC_CONVERTER
bat_terminal.pass_voltage_targets(&dcdc_lv_port);
#endif
#if CONFIG_HAS_PWM_SWITCH
bat_terminal.pass_voltage_targets(&pwm_switch);
#endif
eeprom_update();
leds_update_1s();
@@ -189,14 +181,15 @@ void system_control()
// alerts should trigger only for transients, so update based on actual voltage
daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8);
bat_terminal.update_bus_current_margins();
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_switch, &bat_terminal, &load_terminal);
pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
#if CONFIG_HAS_DCDC_CONVERTER
ports_update_current_limits(&dcdc_lv_port, &bat_terminal, &load_terminal);
hv_terminal.update_bus_current_margins();
dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
#endif
+9 -8
View File
@@ -29,12 +29,12 @@
#include "data_objects.h" // for access to internal data via ThingSet
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
#if CONFIG_HAS_DCDC_CONVERTER
DcBus hv_bus;
PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID);
#elif CONFIG_HV_TERMINAL_BATTERY
@@ -115,8 +115,8 @@ void main(void)
#endif
charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system
battery_init_dc_bus(&bat_terminal, &bat_conf, charger.num_batteries);
load_terminal.init_load(bat_conf.voltage_absolute_max * charger.num_batteries);
battery_init_terminal(&bat_terminal, &bat_conf, charger.num_batteries);
battery_init_load(&load, &bat_conf, charger.num_batteries);
k_sleep(2000); // safety feature: be able to re-flash before starting
@@ -143,15 +143,16 @@ void control_thread()
// alerts should trigger only for transients, so update based on actual voltage
daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8);
lv_terminal.update_bus_current_margins();
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_switch, &bat_terminal, &load_terminal);
//pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
#if CONFIG_HAS_DCDC_CONVERTER
ports_update_current_limits(&dcdc_lv_port, &bat_terminal, &load_terminal);
//dcdc.control(); // control of DC/DC including MPPT algorithm
hv_terminal.update_bus_current_margins();
dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
#endif
+8 -37
View File
@@ -10,37 +10,19 @@ void PowerPort::init_solar()
{
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
// also initialize the connected bus
src_voltage_start = 16.0;
src_voltage_stop = 14.0;
}
void PowerPort::init_nanogrid()
{
neg_current_limit = -5.0;
neg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
pos_current_limit = 5.0;
pos_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
neg_current_limit = -5.0;
bus->droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
// also initialize the connected bus
src_voltage_start = 30.0; // starting buck mode above this point
src_voltage_stop = 30.0; // stopping buck mode below this point
sink_voltage_max = 28.0; // starting idle mode above this point
sink_voltage_min = 10.0;
bus->src_voltage_bound = 30.0; // starting buck mode above this point
bus->sink_voltage_bound = 28.0; // boost mode until this voltage is reached
}
void PowerPort::init_load(float max_load_voltage)
{
sink_voltage_max = max_load_voltage; // above this voltage, load is switched off
sink_voltage_min = 0;
// other settings are not relevant for load output
}
// must be called exactly once per second, otherwise energy calculation gets wrong
void PowerPort::energy_balance()
{
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
@@ -52,24 +34,13 @@ void PowerPort::energy_balance()
}
}
void PowerPort::pass_voltage_targets(PowerPort *port)
{
// droop resistance added only to limits where current actually flows
port->sink_voltage_min = sink_voltage_min;
port->sink_voltage_max = sink_voltage_max + pos_droop_res * current;
port->src_voltage_start = src_voltage_start + pos_droop_res * current;
port->src_voltage_stop = src_voltage_stop + pos_droop_res * current;
}
void ports_update_current_limits(PowerPort *internal, PowerPort *p_bat, PowerPort *p_load)
void PowerPort::update_bus_current_margins()
{
// charging direction of battery
internal->pos_current_limit = p_bat->pos_current_limit + p_load->current;
bus->sink_current_margin = pos_current_limit - current;
// discharging direction of battery
internal->neg_current_limit = p_bat->neg_current_limit;
bus->src_current_margin = neg_current_limit - current;
// load output current = max. negative (discharging) battery current.
// this statement enables or disables the load output if discharging is not allowed
p_load->pos_current_limit = -p_bat->neg_current_limit;
//printf("pos: %.3f neg: %.3f current: %.3f\n", pos_current_limit, neg_current_limit, current);
}
+125 -61
View File
@@ -14,44 +14,95 @@
#include <stdbool.h>
class PowerPort; // forward-declaration
/**
* These values may be calculated based on external data, if multiple devices are connected to the
* bus.
* DC bus class
*
* Stores measurement data and settings necessary for voltage control
*/
class DcBus
{
public:
float voltage; ///< Measured bus voltage
/**
* Measured bus voltage
*/
float voltage;
float voltage_max; ///< Maximum voltage of a DC bus, equivalent to battery
///< charging target voltage if hard-wired to a battery
/**
* Upper voltage boundary where this bus may be used to sink current.
*
* This value is the voltage at zero current. Values for other currents are calculated
* using the droop resistance.
*/
float sink_voltage_bound;
float voltage_min; ///< Absolute minimum for both chg and discharging direction
/**
* Lower voltage boundary where this bus may be used to source current.
*
* This value is the voltage at zero current. Values for other currents are calculated
* using the droop resistance.
*/
float src_voltage_bound;
/**
* Droop resistance to adjust voltage bounds depending on actual current
*
* control voltage = nominal voltage - droop_res * current
*/
float droop_res;
/**
* Pointer to current measurement that is used to determine the droop. This is typically the
* battery or nanogrid terminal.
*/
float *ref_current;
/**
* Available additional current towards the DC bus until limits of the connected ports are
* reached
*/
float sink_current_margin;
/**
* Available additional current from the DC bus until limits of the connected ports are reached
* (has a negative sign)
*/
float src_current_margin;
inline float droop_voltage(float v0)
{
if (ref_current != nullptr) {
return v0 - droop_res * (*ref_current);
}
else {
return v0;
}
}
};
// -----------------
// | | >> positive current/power direction
// | o---->----
// | | + |
// | considered | | | external device: battery / solar panel / load / DC grid
// | system or | | |
// | sub-circuit | | | (the port should be named after the external device)
// | | - |
// | o---------
// | |
// -----------------
/** Power Port class
/**
* Power Port class
*
* Stores actual measurements and voltage / current limits for external terminals or internal ports
* Stores current measurements and limits for external terminals or internal ports
*
* The signs follow the passive sign convention. Current or power from the considered system or
* circuit towards an external device connected to the port has a positive sign. For all terminals,
* the entire charge controller is considered as the system boundary and acts as a source or a sink.
* For internal sub-circuits, e.g. the DC/DC converter circuit defines the sub-system boundaries.
*
* -----------------
* | | >> positive current/power direction
* | o---->----
* | | + |
* | considered | | | external device: battery / solar panel / load / DC grid
* | system or | | |
* | sub-circuit | | | (the port should be named after the external device)
* | | - |
* | o---------
* | |
* -----------------
*
* Examples:
* - Charging a connected battery has a positive sign, as current flows from the charge controller
* into the battery, i.e. the battery acts as a sink.
@@ -64,67 +115,80 @@ public:
class PowerPort
{
public:
DcBus *bus; ///< Pointer to DC bus containing voltage measurement
/**
* Each power port is connected to a DC bus, containing relevant voltage information
*/
DcBus *bus;
float sink_voltage_max; ///< Maximum voltage if connected device acts as a sink
///< (equivalent to battery charging target voltage)
float sink_voltage_min; ///< Minimum voltage to allow positive current (necessary
///< to prevent charging of deep-discharged Li-ion batteries)
/**
* Measured current through this port (positive sign = current into the external device)
*/
float current;
float src_voltage_start; ///< Minimum voltage to allow source current from the device
///< (equal to load reconnect voltage)
float src_voltage_stop; ///< Absolute minimum = load disconnect for batteries
/**
* Product of port current and bus voltage
*/
float power;
float current = 0; ///< Measured current through this port (positive sign =
///< sinking current into the named external device)
float power = 0; ///< Product of port current and bus voltage
/**
* Maximum positive current (valid values >= 0.0)
*/
float pos_current_limit;
float pos_current_limit; ///< Maximum positive current (valid values >= 0.0)
float pos_droop_res; ///< control voltage = nominal voltage - droop_res * current
/**
* Maximum negative current (valid values <= 0.0)
*/
float neg_current_limit;
float neg_current_limit; ///< Maximum negative current (valid values <= 0.0)
float neg_droop_res; ///< control voltage = nominal voltage - droop_res * current
/**
* Cumulated energy in positive current direction since last counter reset (Wh)
*/
float pos_energy_Wh;
float pos_energy_Wh; ///< Cumulated sunk energy since last counter reset (Wh)
float neg_energy_Wh; ///< Cumulated sourced energy since last counter reset (Wh)
/**
* Cumulated energy in negative current direction since last counter reset (Wh)
*/
float neg_energy_Wh;
PowerPort(DcBus *dc_bus) :
bus(dc_bus)
{}
/** Initialize power port for solar panel connection
/**
* Constructor assigning the port to a DC bus
*
* @param max_abs_current Maximum input current allowed by PCB (as a positive value)
* @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
/**
* Initialize power port for nanogrid connection
*/
void init_nanogrid();
/** Initialize power port for load output connection
*/
void init_load(float max_load_voltage);
/** Energy balance calculation for power port
/**
* Energy balance calculation for power port
*
* Must be called exactly once per second, otherwise energy calculation gets wrong.
*/
void energy_balance();
/** Passes own voltage target settings to another port
/**
* Sets current limits for control of the bus voltage
*
* @param port Port where the voltage settings will be adjusted
* 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 pass_voltage_targets(PowerPort *port);
void update_bus_current_margins();
};
/** Sets current limits of DC/DC or PWM switch according to load and battery status
*
* @param internal DC/DC or PWM switch internal port that can be controlled
* @param p_bat Battery terminal port whose current limits have to be respected
* @param p_load Load output port with current determined by external consumers
*/
void ports_update_current_limits(PowerPort *internal, PowerPort *p_bat, PowerPort *p_load);
#endif /* POWER_PORT_H */
+4 -5
View File
@@ -179,8 +179,8 @@ void PwmSwitch::control()
off_timestamp = uptime();
print_info("PWM charger stop.\n");
}
else if (bus->voltage > (sink_voltage_max -
pos_droop_res * current) // voltage above target
else if (bus->voltage > (bus->sink_voltage_bound -
bus->droop_res * current) // voltage above target
|| current < neg_current_limit // port current limit exceeded
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
{
@@ -215,9 +215,8 @@ void PwmSwitch::control()
}
}
else {
if (pos_current_limit > 0 // charging allowed
&& bus->voltage < bus->voltage_max
&& bus->voltage > bus->voltage_min
if (bus->sink_current_margin > 0 // charging allowed
&& bus->voltage < bus->sink_voltage_bound
&& ext_voltage > bus->voltage + offset_voltage_start
&& uptime() > (off_timestamp + restart_interval)
&& enable == true)
+1 -2
View File
@@ -14,8 +14,7 @@
static void init_structs()
{
battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100);
//charger_init(&charger);
battery_init_dc_bus(&bat_terminal, &bat_conf, 1);
battery_init_terminal(&bat_terminal, &bat_conf, 1);
charger.state = CHG_STATE_IDLE;
charger.bat_temperature = 25;
bat_terminal.bus->voltage = 14.0;
+17 -29
View File
@@ -18,20 +18,22 @@ static void init_structs_buck()
{
hv_terminal.init_solar();
hv_terminal.bus->voltage = 20;
hv_terminal.src_voltage_start = 18;
hv_terminal.bus->src_voltage_bound = 18;
hv_terminal.current = 0;
hv_terminal.update_bus_current_margins();
battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100);
battery_init_dc_bus(&dcdc_lv_port, &bat_conf, 1);
battery_init_terminal(&dcdc_lv_port, &bat_conf, 1);
dcdc_lv_port.bus->voltage = 14;
dcdc_lv_port.current = 0;
dcdc_lv_port.update_bus_current_margins();
dcdc.mode = MODE_MPPT_BUCK;
dcdc.temp_mosfets = 25;
dcdc.off_timestamp = 0;
dcdc.power_prev = 0;
dcdc.pwm_delta = 1;
dcdc.enabled = true;
dcdc.enable = true;
}
static void start_buck()
@@ -50,22 +52,24 @@ static void init_structs_boost()
hv_terminal.init_solar();
dcdc_lv_port.bus->voltage = 20;
dcdc_lv_port.src_voltage_start = 18;
dcdc_lv_port.bus->src_voltage_bound = 18;
dcdc_lv_port.current = 0;
dcdc_lv_port.power = 0;
dcdc_lv_port.update_bus_current_margins();
battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9);
battery_init_dc_bus(&hv_terminal, &bat_conf, 1);
battery_init_terminal(&hv_terminal, &bat_conf, 1);
hv_terminal.bus->voltage = 3.7 * 10;
hv_terminal.current = 0;
hv_terminal.power = 0;
hv_terminal.update_bus_current_margins();
dcdc.mode = MODE_MPPT_BOOST;
dcdc.temp_mosfets = 25;
dcdc.off_timestamp = 0;
dcdc.power_prev = 0;
dcdc.pwm_delta = 1;
dcdc.enabled = true;
dcdc.enable = true;
}
static void start_boost()
@@ -102,7 +106,7 @@ void no_start_before_restart_delay()
void no_start_if_dcdc_disabled()
{
init_structs_buck();
dcdc.enabled = false;
dcdc.enable = false;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
@@ -115,13 +119,6 @@ void no_start_if_dcdc_lv_voltage_low()
// buck
void no_buck_start_if_bat_voltage_low()
{
init_structs_buck();
dcdc_lv_port.bus->voltage = bat_conf.voltage_absolute_min - 0.1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_buck_start_if_bat_voltage_high()
{
init_structs_buck();
@@ -132,7 +129,7 @@ void no_buck_start_if_bat_voltage_high()
void no_buck_start_if_bat_chg_not_allowed()
{
init_structs_buck();
dcdc_lv_port.pos_current_limit = 0;
dcdc_lv_port.bus->sink_current_margin = 0;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
@@ -152,13 +149,6 @@ void no_buck_start_if_solar_voltage_low()
// boost
void no_boost_start_if_bat_voltage_low()
{
init_structs_boost();
hv_terminal.bus->voltage = bat_conf.voltage_absolute_min - 0.1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_boost_start_if_bat_voltage_high()
{
init_structs_boost();
@@ -169,7 +159,7 @@ void no_boost_start_if_bat_voltage_high()
void no_boost_start_if_bat_chg_not_allowed()
{
init_structs_boost();
hv_terminal.pos_current_limit = 0;
hv_terminal.bus->sink_current_margin = 0;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
@@ -202,7 +192,7 @@ void buck_derating_output_voltage_too_high()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
dcdc_lv_port.bus->voltage = dcdc_lv_port.sink_voltage_max + 0.1;
dcdc_lv_port.bus->voltage = dcdc_lv_port.bus->sink_voltage_bound + 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage
@@ -224,7 +214,7 @@ void buck_derating_input_voltage_too_low()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.bus->voltage = hv_terminal.src_voltage_stop - 0.1;
hv_terminal.bus->voltage = hv_terminal.bus->src_voltage_bound - 0.1;
dcdc_lv_port.current = 0.2;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
@@ -303,7 +293,7 @@ void boost_derating_output_voltage_too_high()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.bus->voltage = hv_terminal.sink_voltage_max + 0.5;
hv_terminal.bus->voltage = hv_terminal.bus->sink_voltage_bound + 0.5;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power
@@ -323,7 +313,7 @@ void boost_derating_input_voltage_too_low()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
dcdc_lv_port.bus->voltage = dcdc_lv_port.src_voltage_stop - 0.1;
dcdc_lv_port.bus->voltage = dcdc_lv_port.bus->src_voltage_bound - 0.1;
hv_terminal.current = 0.2;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
@@ -400,7 +390,6 @@ void dcdc_tests()
// 2. Check startup for MPPT buck converter scenario
RUN_TEST(no_buck_start_if_bat_voltage_low);
RUN_TEST(no_buck_start_if_bat_voltage_high);
RUN_TEST(no_buck_start_if_bat_chg_not_allowed);
RUN_TEST(no_buck_start_if_solar_voltage_high);
@@ -408,7 +397,6 @@ void dcdc_tests()
// 3. Check startup for MPPT boost converter scenario
RUN_TEST(no_boost_start_if_bat_voltage_low);
RUN_TEST(no_boost_start_if_bat_voltage_high);
RUN_TEST(no_boost_start_if_bat_chg_not_allowed);
RUN_TEST(no_boost_start_if_solar_voltage_high);
+141 -136
View File
@@ -16,9 +16,14 @@
void load_init(LoadOutput *l)
{
l->port->init_load(14.6);
l->overvoltage = 14.6;
l->port->current = 0;
l->port->pos_current_limit = 10;
l->port->bus->voltage = 14;
l->port->bus->sink_voltage_bound = 14.4;
l->port->bus->src_voltage_bound = 12;
l->port->bus->sink_current_margin = 10;
l->port->bus->src_current_margin = -10;
l->junction_temperature = 25;
}
@@ -26,276 +31,276 @@ void control_off_to_pgood_if_everything_fine()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
LoadOutput load_out(&port);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load.control();
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
}
void control_pgood_to_off_overvoltage()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_ON;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
port.bus->voltage = port.sink_voltage_max + 0.6;
port.bus->voltage = port.bus->sink_voltage_bound + 0.6;
// increase debounce counter to 1 before limit
for (int i = 0; i < CONTROL_FREQUENCY; i++) {
load.control();
load_out.control();
}
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
load.control(); // once more
TEST_ASSERT_EQUAL(false, load.pgood);
load_out.control(); // once more
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state); // deprecated
}
void control_pgood_to_off_overcurrent()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
LoadOutput load_out(&port);
load_init(&load_out);
port.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately
load.state = LOAD_STATE_ON;
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
load.control();
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
// almost 2x current = 4x heat generation: Should definitely trigger after waiting one time constant
int trigger_steps = MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY;
for (int i = 0; i <= trigger_steps; i++) {
load.control();
load_out.control();
}
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state); // deprecated
}
void control_pgood_to_off_voltage_dip()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_ON;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
load.control();
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
load.stop(ERR_LOAD_VOLTAGE_DIP);
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
load_out.stop(ERR_LOAD_VOLTAGE_DIP);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state); // deprecated
}
void control_pgood_to_off_int_temp()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_ON;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
load.control();
TEST_ASSERT_EQUAL(true, load.pgood);
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
dev_stat.set_error(ERR_INT_OVERTEMP);
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
}
void control_pgood_to_off_bat_temp()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
LoadOutput load_out(&port);
load_init(&load_out);
// overtemp
load.state = LOAD_STATE_ON;
load.usb_state = LOAD_STATE_ON;
load_out.state = LOAD_STATE_ON;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
dev_stat.set_error(ERR_BAT_DIS_OVERTEMP);
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(false, load.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); // deprecated
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.usb_state); // deprecated
// undertemp
load.state = LOAD_STATE_ON;
load.usb_state = LOAD_STATE_ON;
load_out.state = LOAD_STATE_ON;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP);
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(false, load.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); // deprecated
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.usb_state); // deprecated
}
void control_pgood_to_off_low_soc()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
LoadOutput load_out(&port);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load.state = LOAD_STATE_ON;
load.control();
TEST_ASSERT_EQUAL(true, load.pgood);
load_out.state = LOAD_STATE_ON;
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC | ERR_BAT_UNDERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.state); // deprecated
}
void control_pgood_to_off_if_enable_false()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
LoadOutput load_out(&port);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load.state = LOAD_STATE_ON;
load.control();
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(true, load.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
load_out.state = LOAD_STATE_ON;
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(true, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
load.enable = false;
load.control();
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
load_out.enable = false;
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
load.usb_enable = false;
load.control();
TEST_ASSERT_EQUAL(false, load.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.usb_state);
load_out.usb_enable = false;
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.usb_state);
}
void control_off_low_soc_to_on_after_delay()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_OFF_LOW_SOC;
load.usb_state = LOAD_STATE_OFF_LOW_SOC;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_LOW_SOC;
load_out.usb_state = LOAD_STATE_OFF_LOW_SOC;
dev_stat.error_flags = ERR_LOAD_LOW_SOC;
load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay + 1;
load.control();
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay + 1;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.usb_state); // deprecated
load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay - 1;
load.control();
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay - 1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(true, load.pgood);
TEST_ASSERT_EQUAL(true, load.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(true, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
}
void control_off_overcurrent_to_on_after_delay()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_OFF_OVERCURRENT;
load.usb_state = LOAD_STATE_ON;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_OVERCURRENT;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.error_flags = ERR_LOAD_OVERCURRENT;
load.oc_timestamp = time(NULL) - load.oc_recovery_delay + 1;
load.control();
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay + 1;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overcurrent
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overcurrent
load.oc_timestamp = time(NULL) - load.oc_recovery_delay - 1;
load.control();
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay - 1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
}
void control_off_overvoltage_to_on_at_lower_voltage()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_OFF_OVERVOLTAGE;
load.usb_state = LOAD_STATE_ON;
port.bus->voltage = port.sink_voltage_max + 0.1;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_OVERVOLTAGE;
load_out.usb_state = LOAD_STATE_ON;
port.bus->voltage = port.bus->sink_voltage_bound + 0.1;
dev_stat.error_flags = ERR_LOAD_OVERVOLTAGE;
load.control();
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overvoltage
port.bus->voltage = port.sink_voltage_max - 0.1; // test hysteresis
load.control();
port.bus->voltage = port.bus->sink_voltage_bound - 0.1; // test hysteresis
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state);
port.bus->voltage = port.sink_voltage_max - load.ov_hysteresis - 0.1;
load.control();
port.bus->voltage = port.bus->sink_voltage_bound - load_out.ov_hysteresis - 0.1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
}
void control_off_short_circuit_flag_reset()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load(&port);
load_init(&load);
load.state = LOAD_STATE_OFF_SHORT_CIRCUIT;
load.usb_state = LOAD_STATE_ON;
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_SHORT_CIRCUIT;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.error_flags = ERR_LOAD_SHORT_CIRCUIT;
load.control();
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_SHORT_CIRCUIT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overvoltage
load.enable = false; // this is like a manual reset
load.control();
load_out.enable = false; // this is like a manual reset
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
}
void load_tests()
-31
View File
@@ -91,35 +91,6 @@ void load_output_energy_calculation_valid()
TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load_terminal.pos_energy_Wh));
}
void pass_voltage_targets_to_adjacent_bus()
{
// without any droop
bat_terminal.current = 0;
bat_terminal.pass_voltage_targets(&dcdc_lv_port);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max, dcdc_lv_port.sink_voltage_max);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start, dcdc_lv_port.src_voltage_start);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop, dcdc_lv_port.src_voltage_stop);
// now with droop for positive current
bat_terminal.current = 11;
bat_terminal.pos_droop_res = 0.1;
bat_terminal.pass_voltage_targets(&dcdc_lv_port);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max + 11*0.1, dcdc_lv_port.sink_voltage_max);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start + 11*0.1, dcdc_lv_port.src_voltage_start);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop + 11*0.1, dcdc_lv_port.src_voltage_stop);
// now with droop for negative current
bat_terminal.current = -11;
bat_terminal.pos_droop_res = 0.1;
bat_terminal.pass_voltage_targets(&dcdc_lv_port);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min);
TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max - 11*0.1, dcdc_lv_port.sink_voltage_max);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start - 11*0.1, dcdc_lv_port.src_voltage_start);
TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop - 11*0.1, dcdc_lv_port.src_voltage_stop);
}
void power_port_tests()
{
energy_calculation_init();
@@ -132,7 +103,5 @@ void power_port_tests()
RUN_TEST(solar_input_energy_calculation_valid);
RUN_TEST(load_output_energy_calculation_valid);
RUN_TEST(pass_voltage_targets_to_adjacent_bus);
UNITY_END();
}