DC/DC startup check reworked and unit-tests added

This commit is contained in:
Martin Jaeger
2019-10-15 13:54:05 +02:00
parent 11e3a3e102
commit cb206b6864
5 changed files with 272 additions and 40 deletions
+58 -33
View File
@@ -39,6 +39,7 @@ void dcdc_init(Dcdc *dcdc)
dcdc->ls_current_min = 0.05; // A if lower, charger is switched off
dcdc->hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX; // V
dcdc->ls_voltage_max = LOW_SIDE_VOLTAGE_MAX; // V
dcdc->ls_voltage_min = 9.0;
//dcdc->offset_voltage_start = 4.0; // V charging switched on if Vsolar > Vbat + offset
//dcdc->offset_voltage_stop = 1.0; // V charging switched off if Vsolar < Vbat + offset
dcdc->restart_interval = 60; // s --> when should we retry to start charging after low solar power cut-off?
@@ -49,7 +50,7 @@ void dcdc_init(Dcdc *dcdc)
// returns if output power should be increased (1), decreased (-1) or switched off (0)
/**
* @param dcdc_dir 1 => current flows from ls to hs through the dcdc, -1 opposite
*/
*/
int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir)
{
float dcdc_power_new = dcdc->ls_voltage * dcdc->ls_current * dcdc_dir;
@@ -72,16 +73,16 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir)
if (
(out->chg_allowed == false && out->dis_allowed == false) // we are not supposed to transfer any energy into the outputs
|| low_dcdc_power_count > 100 // the transfered energy drops below a threshold for more than 10s, we make a small averaging here
|| in->dis_allowed == false // we are not allowed to take energy from the input
|| in->dis_allowed == false // we are not allowed to take energy from the input
// || (in->voltage < in->dis_voltage_stop && out->current < 0.1) // input voltage is too low and we have no output consumption (battery or grid)
)
)
{
#if 0
#if 0
printf("P: %.2f, P_prev: %.2f, i_v: %.2f, o_v: %.2f, i_cur: %.2f, o_cur: %.2f, o_chg_cur_max: %.2f, PWM: %.1f, o_chg_en: %d o_dis_en: %d, i_dis_en: %d, i_v_dis_volt_stop: %.2f, dc_ls_cur: %.2f\n",
dcdc_power_new, dcdc->power, in->voltage, out->voltage, in->current, out->current,
out->chg_current_max, half_bridge_get_duty_cycle() * 100.0, out->chg_allowed, out->dis_allowed, in->dis_allowed, in->dis_voltage_stop, dcdc->ls_current);
#endif
low_dcdc_power_count = 0; // reset low dcdc current "timer"
#endif
low_dcdc_power_count = 0; // reset low dcdc current "timer"
retval = 0;
}
else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current) // output voltage above target
@@ -119,35 +120,55 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir)
return retval;
}
bool _dcdc_check_start_conditions(Dcdc *dcdc, DcBus *out, DcBus *in)
int dcdc_check_start_conditions(Dcdc *dcdc, DcBus *hv, DcBus *lv)
{
return dcdc->enabled == true
&& out->chg_allowed == true
&& out->voltage < out->chg_voltage_target
&& out->voltage > out->chg_voltage_min
&& in->dis_allowed == true
&& in->voltage > in->dis_voltage_start
//&& dcdc->hs_voltage - dcdc->ls_voltage > dcdc->offset_voltage_start
&& time(NULL) > (dcdc->off_timestamp + dcdc->restart_interval);
if (dcdc->enabled == false ||
hv->voltage > dcdc->hs_voltage_max || // also critical for buck mode because of ringing
lv->voltage > dcdc->ls_voltage_max ||
lv->voltage < dcdc->ls_voltage_min ||
time(NULL) < (dcdc->off_timestamp + dcdc->restart_interval))
{
return 0; // no energy transfer allowed
}
if (lv->chg_current_max > 0 &&
lv->voltage < lv->chg_voltage_target &&
lv->voltage > lv->chg_voltage_min &&
hv->dis_current_max < 0 &&
hv->voltage > hv->dis_voltage_start)
{
return 1; // start in buck mode allowed
}
if (hv->chg_current_max > 0 &&
hv->voltage < hv->chg_voltage_target &&
hv->voltage > hv->chg_voltage_min &&
lv->dis_current_max < 0 &&
lv->voltage > lv->dis_voltage_start)
{
return -1; // start in boost mode allowed
}
return 0;
}
void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls)
void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv)
{
if (half_bridge_enabled()) {
int step;
if (dcdc->mode == MODE_MPPT_BUCK || dcdc->ls_current > 0.1 ) {
if (dcdc->mode == MODE_MPPT_BUCK || dcdc->ls_current > 0.1 ) {
// always in buck mode or when boost or nano grid are receiving energy from high side enegry producer
// ( grid [nanogrid] or battery [boost] )
// for low side energy consumers (battery and/or load).
//printf("-");
step = _dcdc_output_control(dcdc, ls, hs, 1);
step = _dcdc_output_control(dcdc, lv, hv, 1);
half_bridge_duty_cycle_step(step);
}
else {
//printf("+");
step = _dcdc_output_control(dcdc, hs, ls, -1);
step = _dcdc_output_control(dcdc, hv, lv, -1);
half_bridge_duty_cycle_step(-step);
}
@@ -157,7 +178,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls)
dcdc->off_timestamp = time(NULL);
printf("DC/DC stop.\n");
}
else if (ls->voltage > dcdc->ls_voltage_max || hs->voltage > dcdc->hs_voltage_max) {
else if (lv->voltage > dcdc->ls_voltage_max || hv->voltage > dcdc->hs_voltage_max) {
half_bridge_stop();
dcdc->state = DCDC_STATE_OFF;
dcdc->off_timestamp = time(NULL);
@@ -187,21 +208,23 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls)
static int startup_delay_counter = 0;
static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; // wait at least 100 ms for voltages to settle
if (_dcdc_check_start_conditions(dcdc, ls, hs) && ls->voltage < dcdc->ls_voltage_max) {
int startup_mode = dcdc_check_start_conditions(dcdc, hv, lv);
if (startup_mode == 1) {
if (startup_delay_counter >= num_wait_calls) {
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents and stress on MOSFETs
half_bridge_start(ls->voltage / (hs->voltage - 1));
printf("DC/DC buck mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100);
half_bridge_start(lv->voltage / (hv->voltage - 1));
printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_delay_counter++;
}
}
else if (_dcdc_check_start_conditions(dcdc, hs, ls) && hs->voltage < dcdc->hs_voltage_max) {
else if (startup_mode == -1) {
if (startup_delay_counter >= num_wait_calls) {
// will automatically start with max. duty (0.97) if connected to a nanogrid not yet started up (zero voltage)
half_bridge_start(ls->voltage / (hs->voltage + 1));
printf("DC/DC boost mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100);
half_bridge_start(lv->voltage / (hv->voltage + 1));
printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_delay_counter++;
@@ -213,7 +236,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls)
}
}
void dcdc_test(Dcdc *dcdc, DcBus *hs, DcBus *ls)
void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv)
{
if (half_bridge_enabled()) {
if (half_bridge_get_duty_cycle() > 0.5) {
@@ -223,21 +246,23 @@ void dcdc_test(Dcdc *dcdc, DcBus *hs, DcBus *ls)
else {
static int startup_delay_counter = 0;
static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; // wait at least 100 ms for voltages to settle
if (_dcdc_check_start_conditions(dcdc, ls, hs) && ls->voltage < dcdc->ls_voltage_max) {
int startup_mode = dcdc_check_start_conditions(dcdc, hv, lv);
if (startup_mode == 1) {
if (startup_delay_counter >= num_wait_calls) {
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents and stress on MOSFETs
half_bridge_start(ls->voltage / (hs->voltage - 1));
printf("DC/DC buck mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100);
half_bridge_start(lv->voltage / (hv->voltage - 1));
printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_delay_counter++;
}
}
else if (_dcdc_check_start_conditions(dcdc, hs, ls) && hs->voltage < dcdc->hs_voltage_max) {
else if (startup_mode == -1) {
if (startup_delay_counter >= num_wait_calls) {
// will automatically start with max. duty (0.97) if connected to a nanogrid not yet started up (zero voltage)
half_bridge_start(ls->voltage / (hs->voltage + 1));
printf("DC/DC boost mode start (HS: %.2fV, LS: %.2fV, PWM: %.1f).\n", hs->voltage, ls->voltage, half_bridge_get_duty_cycle() * 100);
half_bridge_start(lv->voltage / (hv->voltage + 1));
printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_delay_counter++;