PowerPort added to store current independent from DC bus type

The DC bus type caused issues because current limits could not be defined individually for each
connection to a junction, as described in issue #50.

With this commit, also some structs were converted to C++ classes to improve readibility of the
code and make sure that pointers (e.g. to PowerPort objects) are always assigned during
initialization.
This commit is contained in:
Martin Jaeger
2019-10-20 14:52:35 +02:00
parent d7ed3cdd13
commit 0399c4e877
25 changed files with 860 additions and 823 deletions
+97 -101
View File
@@ -1,46 +1,41 @@
#include "tests.h"
#include "dcdc.h"
#include "dc_bus.h"
#include "log.h"
#include "half_bridge.h"
#include <time.h>
#include <stdio.h>
#include <math.h>
extern Dcdc dcdc;
extern DcBus hv_terminal;
extern DcBus lv_bus_int;
extern BatConf bat_conf;
#include "main.h"
static void init_structs_buck()
{
dc_bus_init_solar(&hv_terminal, 10);
hv_terminal.voltage = 20;
hv_terminal.dis_voltage_start = 18;
hv_terminal.current = 0;
dcdc_port_hv.init_solar(10);
dcdc_port_hv.bus->voltage = 20;
dcdc_port_hv.bus->dis_voltage_start = 18;
dcdc_port_hv.current = 0;
battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100);
battery_init_dc_bus(&lv_bus_int, &bat_conf, 1);
lv_bus_int.voltage = 14;
lv_bus_int.current = 0;
battery_init_dc_bus(&lv_bus, &dcdc_port_lv, &bat_conf, 1);
dcdc_port_lv.bus->voltage = 14;
dcdc_port_lv.current = 0;
dcdc.mode = MODE_MPPT_BUCK;
dcdc.temp_mosfets = 25;
dcdc.off_timestamp = 0;
dcdc.power_prev = 0;
dcdc.pwm_delta = 1;
dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BUCK);
dcdc.enabled = true;
}
static void start_buck()
{
init_structs_buck();
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
dcdc_control(&dcdc);
dcdc_control(&dcdc);
dcdc_control(&dcdc); // call multiple times because of startup delay
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state);
}
@@ -48,68 +43,69 @@ static void init_structs_boost()
{
half_bridge_stop();
dc_bus_init_solar(&lv_bus_int, 10);
lv_bus_int.voltage = 20;
lv_bus_int.dis_voltage_start = 18;
lv_bus_int.current = 0;
lv_bus_int.power = 0;
dcdc_port_hv.init_solar(10);
dcdc_port_lv.bus->voltage = 20;
dcdc_port_lv.bus->dis_voltage_start = 18;
dcdc_port_lv.current = 0;
dcdc_port_lv.power = 0;
battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9);
battery_init_dc_bus(&hv_terminal, &bat_conf, 1);
hv_terminal.voltage = 3.7 * 10;
hv_terminal.current = 0;
hv_terminal.power = 0;
battery_init_dc_bus(&hv_bus, &dcdc_port_hv, &bat_conf, 1);
dcdc_port_hv.bus->voltage = 3.7 * 10;
dcdc_port_hv.current = 0;
dcdc_port_hv.power = 0;
dcdc.mode = MODE_MPPT_BOOST;
dcdc.temp_mosfets = 25;
dcdc.off_timestamp = 0;
dcdc.power_prev = 0;
dcdc.pwm_delta = -1;
dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BOOST);
dcdc.pwm_delta = 1;
dcdc.enabled = true;
}
static void start_boost()
{
init_structs_boost();
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
dcdc_control(&dcdc);
dcdc_control(&dcdc);
dcdc_control(&dcdc); // call multiple times because of startup delay
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state);
}
void start_valid_mppt_buck()
{
init_structs_buck();
TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc));
TEST_ASSERT_EQUAL(1, dcdc.check_start_conditions());
}
void start_valid_mppt_boost()
{
init_structs_boost();
TEST_ASSERT_EQUAL(-1, dcdc_check_start_conditions(&dcdc));
TEST_ASSERT_EQUAL(-1, dcdc.check_start_conditions());
}
void no_start_before_restart_delay()
{
init_structs_buck();
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval;
TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc));
TEST_ASSERT_EQUAL(1, dcdc.check_start_conditions());
}
void no_start_if_dcdc_disabled()
{
init_structs_buck();
dcdc.enabled = false;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_start_if_dcdc_lv_voltage_low()
{
init_structs_buck();
lv_bus_int.voltage = dcdc.ls_voltage_min - 0.5;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.bus->voltage = dcdc.ls_voltage_min - 0.5;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
// buck
@@ -117,36 +113,36 @@ void no_start_if_dcdc_lv_voltage_low()
void no_buck_start_if_bat_voltage_low()
{
init_structs_buck();
lv_bus_int.voltage = bat_conf.voltage_absolute_min - 0.1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.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();
lv_bus_int.voltage = bat_conf.topping_voltage + 0.1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.bus->voltage = bat_conf.topping_voltage + 0.1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_buck_start_if_bat_chg_not_allowed()
{
init_structs_buck();
lv_bus_int.chg_current_limit = 0;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.chg_current_limit = 0;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_buck_start_if_solar_voltage_high()
{
init_structs_buck();
hv_terminal.voltage = dcdc.hs_voltage_max + 1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_buck_start_if_solar_voltage_low()
{
init_structs_buck();
hv_terminal.voltage = 17;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_hv.bus->voltage = 17;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
// boost
@@ -154,36 +150,36 @@ void no_buck_start_if_solar_voltage_low()
void no_boost_start_if_bat_voltage_low()
{
init_structs_boost();
hv_terminal.voltage = bat_conf.voltage_absolute_min - 0.1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_hv.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();
hv_terminal.voltage = bat_conf.topping_voltage + 0.1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_hv.bus->voltage = bat_conf.topping_voltage + 0.1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_boost_start_if_bat_chg_not_allowed()
{
init_structs_boost();
hv_terminal.chg_current_limit = 0;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_hv.chg_current_limit = 0;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_boost_start_if_solar_voltage_high()
{
init_structs_boost();
lv_bus_int.voltage = dcdc.ls_voltage_max + 1;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 1;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
void no_boost_start_if_solar_voltage_low()
{
init_structs_boost();
lv_bus_int.voltage = 17;
TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc));
dcdc_port_lv.bus->voltage = 17;
TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions());
}
// buck operation
@@ -192,7 +188,7 @@ void buck_increasing_power()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
dcdc_control(&dcdc);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before);
}
@@ -201,8 +197,8 @@ void buck_derating_output_voltage_too_high()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
lv_bus_int.voltage = lv_bus_int.chg_voltage_target + 0.1;
dcdc_control(&dcdc);
dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->chg_voltage_target + 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage
TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state);
@@ -212,8 +208,8 @@ void buck_derating_output_current_too_high()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
lv_bus_int.current = lv_bus_int.chg_current_limit + 0.1;
dcdc_control(&dcdc);
dcdc_port_lv.current = dcdc_port_lv.chg_current_limit + 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state);
@@ -223,23 +219,23 @@ void buck_derating_input_voltage_too_low()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.voltage = hv_terminal.dis_voltage_stop - 0.1;
lv_bus_int.current = 0.2;
dcdc_control(&dcdc);
dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->dis_voltage_stop - 0.1;
dcdc_port_lv.current = 0.2;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state);
TEST_ASSERT_EQUAL(DCDC_STATE_DERATING, dcdc.state);
}
void buck_derating_input_current_too_high()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.current = hv_terminal.dis_current_limit - 0.1;
dcdc_control(&dcdc);
dcdc_port_hv.current = dcdc_port_hv.dis_current_limit - 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state);
TEST_ASSERT_EQUAL(DCDC_STATE_DERATING, dcdc.state);
}
void buck_derating_temperature_limits_exceeded()
@@ -247,7 +243,7 @@ void buck_derating_temperature_limits_exceeded()
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
dcdc.temp_mosfets = 81;
dcdc_control(&dcdc);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before);
}
@@ -257,7 +253,7 @@ void buck_stop_input_power_too_low()
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc_control(&dcdc);
dcdc.control();
TEST_ASSERT(half_bridge_enabled() == false);
}
@@ -265,8 +261,8 @@ void buck_stop_high_voltage_emergency()
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle();
lv_bus_int.voltage = dcdc.ls_voltage_max + 0.1;
dcdc_control(&dcdc);
dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 0.1;
dcdc.control();
TEST_ASSERT(half_bridge_enabled() == false);
}
@@ -274,16 +270,16 @@ void buck_correct_mppt_operation()
{
start_buck();
lv_bus_int.power = 5;
dcdc_control(&dcdc);
dcdc_port_lv.power = 5;
dcdc.control();
float pwm1 = half_bridge_get_duty_cycle();
lv_bus_int.power = 7;
dcdc_control(&dcdc);
dcdc_port_lv.power = 7;
dcdc.control();
float pwm2 = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm2 > pwm1);
lv_bus_int.power = 6; // decrease power to make the direction turn around
dcdc_control(&dcdc);
dcdc_port_lv.power = 6; // decrease power to make the direction turn around
dcdc.control();
float pwm3 = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm3 < pwm2);
}
@@ -295,7 +291,7 @@ void boost_increasing_power()
// make sure that without changing anything, dcdc_control goes in direction of increasing power
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
dcdc_control(&dcdc);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after < pwm_before);
}
@@ -304,8 +300,8 @@ void boost_derating_output_voltage_too_high()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.voltage = hv_terminal.chg_voltage_target + 0.5;
dcdc_control(&dcdc);
dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->chg_voltage_target + 0.5;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power
}
@@ -314,8 +310,8 @@ void boost_derating_output_current_too_high()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
hv_terminal.current = hv_terminal.chg_current_limit + 0.1;
dcdc_control(&dcdc);
dcdc_port_hv.current = dcdc_port_hv.chg_current_limit + 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before);
}
@@ -324,9 +320,9 @@ void boost_derating_input_voltage_too_low()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
lv_bus_int.voltage = lv_bus_int.dis_voltage_stop - 0.1;
hv_terminal.current = 0.2;
dcdc_control(&dcdc);
dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->dis_voltage_stop - 0.1;
dcdc_port_hv.current = 0.2;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before);
}
@@ -335,8 +331,8 @@ void boost_derating_input_current_too_high()
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
lv_bus_int.current = lv_bus_int.dis_current_limit - 0.1;
dcdc_control(&dcdc);
dcdc_port_lv.current = dcdc_port_lv.dis_current_limit - 0.1;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before);
}
@@ -346,7 +342,7 @@ void boost_derating_temperature_limits_exceeded()
start_boost();
float pwm_before = half_bridge_get_duty_cycle();
dcdc.temp_mosfets = 81;
dcdc_control(&dcdc);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm_after > pwm_before);
}
@@ -355,15 +351,15 @@ void boost_stop_input_power_too_low()
{
start_boost();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc_control(&dcdc);
dcdc.control();
TEST_ASSERT(half_bridge_enabled() == false);
}
void boost_stop_high_voltage_emergency()
{
start_boost();
hv_terminal.voltage = dcdc.hs_voltage_max + 0.1;
dcdc_control(&dcdc);
dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 0.1;
dcdc.control();
TEST_ASSERT(half_bridge_enabled() == false);
}
@@ -371,16 +367,16 @@ void boost_correct_mppt_operation()
{
start_boost();
hv_terminal.power = 5;
dcdc_control(&dcdc);
dcdc_port_hv.power = 5;
dcdc.control();
float pwm1 = half_bridge_get_duty_cycle();
hv_terminal.power = 7;
dcdc_control(&dcdc);
dcdc_port_hv.power = 7;
dcdc.control();
float pwm2 = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm2 < pwm1);
hv_terminal.power = 6; // decrease power to make the direction turn around
dcdc_control(&dcdc);
dcdc_port_hv.power = 6; // decrease power to make the direction turn around
dcdc.control();
float pwm3 = half_bridge_get_duty_cycle();
TEST_ASSERT(pwm3 > pwm2);
}