DC bus integrated into PowerPort class
Reason: Previous idea with storing the voltage only once and not in every port doesn't work well for droop settings, e.g. the DC/DC output port doesn't know the actual battery current, which should be used to calculate the droop caued by wire resistance.
This commit is contained in:
+15
-14
@@ -94,20 +94,23 @@ void update_measurements()
|
||||
// rely on LDO accuracy
|
||||
//int vcc = 3300;
|
||||
|
||||
// calculate LS voltage first, as it is needed for HS voltage calculation for PWM charge controller
|
||||
lv_bus.voltage =
|
||||
// calculate lower voltage first, as it is needed for PWM terminal voltage calculation
|
||||
lv_terminal.voltage =
|
||||
(float)(((adc_filtered[ADC_POS_V_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_V_BAT / 1000.0;
|
||||
load_terminal.voltage = lv_terminal.voltage;
|
||||
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
hv_bus.voltage =
|
||||
hv_terminal.voltage =
|
||||
(float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_V_SOLAR / 1000.0;
|
||||
dcdc_lv_port.voltage = lv_terminal.voltage;
|
||||
#endif
|
||||
#if FEATURE_PWM_SWITCH
|
||||
pwm_bus.voltage = lv_bus.voltage - vcc * ADC_OFFSET_V_SOLAR / 1000.0 -
|
||||
pwm_terminal.voltage = lv_terminal.voltage - vcc * ADC_OFFSET_V_SOLAR / 1000.0 -
|
||||
(float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_V_SOLAR / 1000.0;
|
||||
pwm_port_int.voltage = lv_terminal.voltage;
|
||||
#endif
|
||||
|
||||
load_terminal.current =
|
||||
@@ -121,22 +124,20 @@ void update_measurements()
|
||||
ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset);
|
||||
pwm_port_int.current = pwm_terminal.current;
|
||||
lv_terminal.current = -pwm_port_int.current - load_terminal.current;
|
||||
pwm_terminal.power = pwm_bus.voltage * pwm_terminal.current;
|
||||
pwm_terminal.power = pwm_terminal.voltage * pwm_terminal.current;
|
||||
#endif
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
dcdc_port_lv.current =
|
||||
dcdc_lv_port.current =
|
||||
(float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_I_DCDC / 1000.0 + solar_current_offset;
|
||||
lv_terminal.current = dcdc_port_lv.current - load_terminal.current;
|
||||
hv_terminal.current = -dcdc_port_lv.current * lv_bus.voltage / hv_bus.voltage;
|
||||
dcdc_port_hv.current = hv_terminal.current;
|
||||
lv_terminal.current = dcdc_lv_port.current - load_terminal.current;
|
||||
hv_terminal.current = -dcdc_lv_port.current * lv_terminal.voltage / hv_terminal.voltage;
|
||||
|
||||
dcdc_port_lv.power = lv_bus.voltage * dcdc_port_lv.current;
|
||||
dcdc_port_hv.power = hv_bus.voltage * dcdc_port_hv.current;
|
||||
hv_terminal.power = hv_bus.voltage * hv_terminal.current;
|
||||
dcdc_lv_port.power = dcdc_lv_port.voltage * dcdc_lv_port.current;
|
||||
hv_terminal.power = hv_terminal.voltage * hv_terminal.current;
|
||||
#endif
|
||||
lv_terminal.power = lv_bus.voltage * lv_terminal.current;
|
||||
load_terminal.power = lv_bus.voltage * load_terminal.current;
|
||||
lv_terminal.power = lv_terminal.voltage * lv_terminal.current;
|
||||
load_terminal.power = load_terminal.voltage * load_terminal.current;
|
||||
|
||||
/** \todo Improved (faster) temperature calculation:
|
||||
https://www.embeddedrelated.com/showarticle/91.php
|
||||
|
||||
+24
-24
@@ -217,8 +217,8 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char
|
||||
|
||||
void Charger::detect_num_batteries(BatConf *bat)
|
||||
{
|
||||
if (bus->voltage > bat->voltage_absolute_min * 2 &&
|
||||
bus->voltage < bat->voltage_absolute_max * 2) {
|
||||
if (port->voltage > bat->voltage_absolute_min * 2 &&
|
||||
port->voltage < bat->voltage_absolute_max * 2) {
|
||||
num_batteries = 2;
|
||||
printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2);
|
||||
}
|
||||
@@ -232,7 +232,7 @@ void Charger::update_soc(BatConf *bat_conf)
|
||||
static int soc_filtered = 0; // SOC / 100 for better filtering
|
||||
|
||||
if (fabs(port->current) < 0.2) {
|
||||
int soc_new = (int)((bus->voltage - bat_conf->ocv_empty) /
|
||||
int soc_new = (int)((port->voltage - bat_conf->ocv_empty) /
|
||||
(bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0);
|
||||
|
||||
if (soc_new > 500 && soc_filtered == 0) {
|
||||
@@ -267,7 +267,7 @@ void Charger::discharge_control(BatConf *bat_conf)
|
||||
{
|
||||
// load output state is defined by battery bus dis_allowed flag
|
||||
if (port->dis_current_limit < 0 // discharging allowed
|
||||
&& bus->voltage < num_batteries *
|
||||
&& port->voltage < num_batteries *
|
||||
(bat_conf->voltage_load_disconnect - port->current * port->dis_droop_res))
|
||||
{
|
||||
// low state of charge
|
||||
@@ -298,7 +298,7 @@ void Charger::discharge_control(BatConf *bat_conf)
|
||||
port->dis_current_limit = 0;
|
||||
}
|
||||
|
||||
if (bus->voltage >= num_batteries *
|
||||
if (port->voltage >= num_batteries *
|
||||
(bat_conf->voltage_load_reconnect - port->current * port->dis_droop_res)
|
||||
&& bat_temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->discharge_temp_min + 1)
|
||||
@@ -322,12 +322,12 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
// state machine
|
||||
switch (state) {
|
||||
case CHG_STATE_IDLE: {
|
||||
if (bus->voltage < bat_conf->voltage_recharge * num_batteries
|
||||
if (port->voltage < bat_conf->voltage_recharge * num_batteries
|
||||
&& (time(NULL) - time_state_changed) > bat_conf->time_limit_recharge
|
||||
&& bat_temperature < bat_conf->charge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->charge_temp_min + 1)
|
||||
{
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
port->chg_current_limit = bat_conf->charge_current_max;
|
||||
full = false;
|
||||
@@ -337,11 +337,11 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
}
|
||||
case CHG_STATE_BULK: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
|
||||
if (bus->voltage > num_batteries *
|
||||
(bus->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
if (port->voltage > num_batteries *
|
||||
(port->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
{
|
||||
enter_state(CHG_STATE_TOPPING);
|
||||
}
|
||||
@@ -349,11 +349,11 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
}
|
||||
case CHG_STATE_TOPPING: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->topping_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
|
||||
if (bus->voltage >= num_batteries *
|
||||
(bus->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
if (port->voltage >= num_batteries *
|
||||
(port->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
{
|
||||
time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
@@ -375,12 +375,12 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
num_deep_discharges - deep_dis_last_equalization
|
||||
>= bat_conf->equalization_trigger_deep_cycles))
|
||||
{
|
||||
bus->chg_voltage_target = bat_conf->equalization_voltage;
|
||||
port->chg_voltage_target = bat_conf->equalization_voltage;
|
||||
port->chg_current_limit = bat_conf->equalization_current_limit;
|
||||
enter_state(CHG_STATE_EQUALIZATION);
|
||||
}
|
||||
else if (bat_conf->trickle_enabled) {
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
enter_state(CHG_STATE_TRICKLE);
|
||||
}
|
||||
@@ -393,11 +393,11 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
}
|
||||
case CHG_STATE_TRICKLE: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
|
||||
if (bus->voltage >= num_batteries *
|
||||
(bus->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
if (port->voltage >= num_batteries *
|
||||
(port->chg_voltage_target - port->current * port->chg_droop_res))
|
||||
{
|
||||
time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
@@ -414,7 +414,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
}
|
||||
case CHG_STATE_EQUALIZATION: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->equalization_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->equalization_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
|
||||
// current or time limit for equalization reached
|
||||
@@ -427,7 +427,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
discharged_Ah = 0; // reset coulomb counter again
|
||||
|
||||
if (bat_conf->trickle_enabled) {
|
||||
bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
port->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
enter_state(CHG_STATE_TRICKLE);
|
||||
}
|
||||
@@ -445,15 +445,15 @@ void battery_init_dc_bus(PowerPort *port, BatConf *bat, unsigned int num_batteri
|
||||
{
|
||||
unsigned int n = (num_batteries == 2 ? 2 : 1); // only 1 or 2 allowed
|
||||
|
||||
port->bus->dis_voltage_start = bat->voltage_load_reconnect * n;
|
||||
port->bus->dis_voltage_stop = bat->voltage_load_disconnect * n;
|
||||
port->dis_voltage_start = bat->voltage_load_reconnect * n;
|
||||
port->dis_voltage_stop = bat->voltage_load_disconnect * n;
|
||||
port->dis_current_limit = -bat->discharge_current_max;
|
||||
|
||||
// negative sign for compensation of actual resistance
|
||||
port->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n;
|
||||
|
||||
port->bus->chg_voltage_target = bat->topping_voltage * n;
|
||||
port->bus->chg_voltage_min = bat->voltage_absolute_min * n;
|
||||
port->chg_voltage_target = bat->topping_voltage * n;
|
||||
port->chg_voltage_min = bat->voltage_absolute_min * n;
|
||||
port->chg_current_limit = bat->charge_current_max;
|
||||
|
||||
// negative sign for compensation of actual resistance
|
||||
|
||||
+1
-2
@@ -236,10 +236,9 @@ class Charger
|
||||
{
|
||||
public:
|
||||
Charger(PowerPort *pwr_port):
|
||||
port(pwr_port), bus(pwr_port->bus) {};
|
||||
port(pwr_port) {};
|
||||
|
||||
PowerPort *port;
|
||||
DcBus *bus;
|
||||
|
||||
unsigned int state; ///< Current charger state (see enum ChargerState)
|
||||
|
||||
|
||||
@@ -26,10 +26,7 @@
|
||||
#define DCDC_MODE_INIT MODE_MPPT_BUCK
|
||||
//#define DCDC_MODE_INIT MODE_NANOGRID
|
||||
|
||||
#define SOLAR_BUS hv_bus
|
||||
#define SOLAR_TERMINAL hv_terminal
|
||||
|
||||
#define BATTERY_BUS lv_bus
|
||||
#define BATTERY_TERMINAL lv_terminal
|
||||
|
||||
// basic battery configuration
|
||||
|
||||
@@ -123,12 +123,8 @@ const data_object_t data_objects[] = {
|
||||
{0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t
|
||||
|
||||
// battery related data objects
|
||||
{0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(BATTERY_BUS.voltage), "Bat_V"}, // TODO: reference to bat_terminal
|
||||
#if FEATURE_PWM_SWITCH
|
||||
{0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(pwm_bus.voltage), "Solar_V"},
|
||||
#else
|
||||
{0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(SOLAR_BUS.voltage), "Solar_V"},
|
||||
#endif
|
||||
{0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.voltage), "Bat_V"},
|
||||
{0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.voltage), "Solar_V"},
|
||||
{0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.current), "Bat_A"},
|
||||
{0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.current), "Load_A"},
|
||||
{0x74, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(charger.bat_temperature), "Bat_degC"},
|
||||
@@ -142,7 +138,7 @@ const data_object_t data_objects[] = {
|
||||
{0x79, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"},
|
||||
#endif
|
||||
{0x7A, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.current), "Solar_A"},
|
||||
{0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(BATTERY_BUS.chg_voltage_target), "BatTarget_V"}, // TODO: reference to bat_terminal
|
||||
{0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.chg_voltage_target), "BatTarget_V"},
|
||||
{0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.chg_current_limit), "BatTarget_A"},
|
||||
{0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.power), "Bat_W"},
|
||||
{0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.power), "Solar_W"},
|
||||
|
||||
+23
-23
@@ -83,8 +83,8 @@ int Dcdc::duty_cycle_delta()
|
||||
#if 0
|
||||
printf("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->bus->chg_voltage_target, out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0,
|
||||
out->power, power_prev, in->voltage, out->voltage, in->current, out->current,
|
||||
out->chg_voltage_target, out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0,
|
||||
state);
|
||||
#endif
|
||||
|
||||
@@ -93,7 +93,7 @@ int Dcdc::duty_cycle_delta()
|
||||
{
|
||||
pwr_inc_goal = 0; // switch off
|
||||
}
|
||||
else if (out->bus->voltage > (out->bus->chg_voltage_target - out->chg_droop_res * out->current))
|
||||
else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current))
|
||||
{
|
||||
// output voltage target reached
|
||||
state = DCDC_STATE_CV;
|
||||
@@ -107,14 +107,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->bus->dis_voltage_start - in->dis_droop_res * in->current)
|
||||
|| (in->voltage < (in->dis_voltage_start - in->dis_droop_res * in->current)
|
||||
&& out->current > 0.1) // input voltage below limit
|
||||
|| in->current < in->dis_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->bus->dis_voltage_start)
|
||||
else if (out->power < output_power_min && out->voltage < out->dis_voltage_start)
|
||||
{
|
||||
// no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
pwr_inc_goal = 1; // increase output power
|
||||
@@ -135,28 +135,28 @@ int Dcdc::duty_cycle_delta()
|
||||
int Dcdc::check_start_conditions()
|
||||
{
|
||||
if (enabled == 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 ||
|
||||
hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing
|
||||
lvs->voltage > ls_voltage_max ||
|
||||
lvs->voltage < ls_voltage_min ||
|
||||
time(NULL) < (off_timestamp + restart_interval))
|
||||
{
|
||||
return 0; // no energy transfer allowed
|
||||
}
|
||||
|
||||
if (lvs->chg_current_limit > 0 &&
|
||||
lvs->bus->voltage < lvs->bus->chg_voltage_target &&
|
||||
lvs->bus->voltage > lvs->bus->chg_voltage_min &&
|
||||
lvs->voltage < lvs->chg_voltage_target &&
|
||||
lvs->voltage > lvs->chg_voltage_min &&
|
||||
hvs->dis_current_limit < 0 &&
|
||||
hvs->bus->voltage > hvs->bus->dis_voltage_start)
|
||||
hvs->voltage > hvs->dis_voltage_start)
|
||||
{
|
||||
return 1; // start in buck mode allowed
|
||||
}
|
||||
|
||||
if (hvs->chg_current_limit > 0 &&
|
||||
hvs->bus->voltage < hvs->bus->chg_voltage_target &&
|
||||
hvs->bus->voltage > hvs->bus->chg_voltage_min &&
|
||||
hvs->voltage < hvs->chg_voltage_target &&
|
||||
hvs->voltage > hvs->chg_voltage_min &&
|
||||
lvs->dis_current_limit < 0 &&
|
||||
lvs->bus->voltage > lvs->bus->dis_voltage_start)
|
||||
lvs->voltage > lvs->dis_voltage_start)
|
||||
{
|
||||
return -1; // start in boost mode allowed
|
||||
}
|
||||
@@ -176,7 +176,7 @@ void Dcdc::control()
|
||||
off_timestamp = time(NULL);
|
||||
printf("DC/DC stop (low power).\n");
|
||||
}
|
||||
else if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) {
|
||||
else if (lvs->voltage > ls_voltage_max || hvs->voltage > hs_voltage_max) {
|
||||
half_bridge_stop();
|
||||
state = DCDC_STATE_OFF;
|
||||
off_timestamp = time(NULL);
|
||||
@@ -216,9 +216,9 @@ void Dcdc::control()
|
||||
power_good_timestamp = time(NULL);
|
||||
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents
|
||||
// and stress on MOSFETs
|
||||
half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1));
|
||||
half_bridge_start(lvs->voltage / (hvs->voltage - 1));
|
||||
printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
}
|
||||
else {
|
||||
startup_delay_counter++;
|
||||
@@ -229,9 +229,9 @@ void Dcdc::control()
|
||||
power_good_timestamp = time(NULL);
|
||||
// will automatically start with max. duty (0.97) if connected to a nanogrid not
|
||||
// yet started up (zero voltage)
|
||||
half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1));
|
||||
half_bridge_start(lvs->voltage / (hvs->voltage + 1));
|
||||
printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
}
|
||||
else {
|
||||
startup_delay_counter++;
|
||||
@@ -261,9 +261,9 @@ void Dcdc::test()
|
||||
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(lvs->bus->voltage / (hvs->bus->voltage - 1));
|
||||
half_bridge_start(lvs->voltage / (hvs->voltage - 1));
|
||||
printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
}
|
||||
else {
|
||||
startup_delay_counter++;
|
||||
@@ -273,9 +273,9 @@ void Dcdc::test()
|
||||
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(lvs->bus->voltage / (hvs->bus->voltage + 1));
|
||||
half_bridge_start(lvs->voltage / (hvs->voltage + 1));
|
||||
printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
}
|
||||
else {
|
||||
startup_delay_counter++;
|
||||
|
||||
+3
-4
@@ -192,7 +192,6 @@ void LoadOutput::usb_set(bool enabled) {;}
|
||||
LoadOutput::LoadOutput(PowerPort *pwr_port)
|
||||
{
|
||||
port = pwr_port;
|
||||
bus = pwr_port->bus;
|
||||
current_max = LOAD_CURRENT_MAX;
|
||||
enabled_target = true;
|
||||
usb_enabled_target = true;
|
||||
@@ -296,7 +295,7 @@ void LoadOutput::state_machine()
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_OFF_OVERVOLTAGE:
|
||||
if (bus->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis?
|
||||
if (port->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis?
|
||||
switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
}
|
||||
break;
|
||||
@@ -335,8 +334,8 @@ void LoadOutput::control(float load_max_voltage)
|
||||
}
|
||||
|
||||
static int debounce_counter = 0;
|
||||
if (bus->voltage > load_max_voltage ||
|
||||
bus->voltage > LOW_SIDE_VOLTAGE_MAX)
|
||||
if (port->voltage > load_max_voltage ||
|
||||
port->voltage > LOW_SIDE_VOLTAGE_MAX)
|
||||
{
|
||||
debounce_counter++;
|
||||
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
|
||||
|
||||
@@ -81,8 +81,6 @@ public:
|
||||
uint16_t switch_state; ///< Current state of load output switch
|
||||
uint16_t usb_state; ///< Current state of USB output
|
||||
|
||||
DcBus *bus; ///< Pointer to internal DC bus where the load output
|
||||
///< current is coming from
|
||||
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
|
||||
///< measurement of (external) load output terminal
|
||||
|
||||
|
||||
+5
-5
@@ -39,7 +39,7 @@ void log_update_energy(LogData *log_data)
|
||||
static uint32_t bat_chg_total_Wh_prev = log_data->bat_chg_total_Wh;
|
||||
static uint32_t bat_dis_total_Wh_prev = log_data->bat_dis_total_Wh;
|
||||
|
||||
if (solar_terminal.bus->voltage < bat_terminal.bus->voltage) {
|
||||
if (solar_terminal.voltage < bat_terminal.voltage) {
|
||||
seconds_zero_solar += 1;
|
||||
}
|
||||
else {
|
||||
@@ -72,12 +72,12 @@ void log_update_energy(LogData *log_data)
|
||||
|
||||
void log_update_min_max_values(LogData *log_data)
|
||||
{
|
||||
if (bat_terminal.bus->voltage > log_data->battery_voltage_max) {
|
||||
log_data->battery_voltage_max = bat_terminal.bus->voltage;
|
||||
if (bat_terminal.voltage > log_data->battery_voltage_max) {
|
||||
log_data->battery_voltage_max = bat_terminal.voltage;
|
||||
}
|
||||
|
||||
if (solar_terminal.bus->voltage > log_data->solar_voltage_max) {
|
||||
log_data->solar_voltage_max = solar_terminal.bus->voltage;
|
||||
if (solar_terminal.voltage > log_data->solar_voltage_max) {
|
||||
log_data->solar_voltage_max = solar_terminal.voltage;
|
||||
}
|
||||
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
|
||||
+8
-12
@@ -42,21 +42,17 @@
|
||||
#include "thingset_serial.h" // UART or USB serial communication
|
||||
#include "thingset_can.h" // CAN bus communication
|
||||
|
||||
DcBus lv_bus;
|
||||
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
|
||||
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
|
||||
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
DcBus hv_bus;
|
||||
PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT)
|
||||
PowerPort dcdc_port_hv(&hv_bus); // internal high voltage side port of DC/DC converter
|
||||
PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter
|
||||
Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT);
|
||||
PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT)
|
||||
PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter
|
||||
Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT);
|
||||
#endif
|
||||
|
||||
#if FEATURE_PWM_SWITCH
|
||||
DcBus pwm_bus;
|
||||
PowerPort pwm_terminal(&pwm_bus); // external terminal of PWM switch port (normally solar)
|
||||
PowerPort pwm_port_int(&lv_bus); // internal side of PWM switch
|
||||
PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar)
|
||||
PowerPort pwm_port_int; // internal side of PWM switch
|
||||
PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int);
|
||||
PowerPort &solar_terminal = pwm_terminal;
|
||||
#else
|
||||
@@ -64,7 +60,7 @@ PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h
|
||||
#endif
|
||||
|
||||
#if FEATURE_LOAD_OUTPUT
|
||||
PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus)
|
||||
PowerPort load_terminal; // load terminal (also connected to lv_bus)
|
||||
LoadOutput load(&load_terminal);
|
||||
#endif
|
||||
|
||||
@@ -152,7 +148,7 @@ int main()
|
||||
charger.charge_control(&bat_conf);
|
||||
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
update_dcdc_current_targets(&dcdc_port_lv, &bat_terminal, &load_terminal);
|
||||
update_dcdc_current_targets(&dcdc_lv_port, &bat_terminal, &load_terminal);
|
||||
#endif
|
||||
|
||||
load.state_machine();
|
||||
|
||||
+1
-5
@@ -26,20 +26,16 @@
|
||||
#include "load.h"
|
||||
#include "pcb.h"
|
||||
|
||||
extern DcBus lv_bus;
|
||||
extern PowerPort lv_terminal;
|
||||
extern PowerPort load_terminal;
|
||||
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
extern DcBus hv_bus;
|
||||
extern PowerPort hv_terminal;
|
||||
extern PowerPort dcdc_port_hv;
|
||||
extern PowerPort dcdc_port_lv;
|
||||
extern PowerPort dcdc_lv_port;
|
||||
extern Dcdc dcdc;
|
||||
#endif
|
||||
|
||||
#if FEATURE_PWM_SWITCH
|
||||
extern DcBus pwm_bus;
|
||||
extern PowerPort pwm_terminal;
|
||||
extern PowerPort pwm_port_int;
|
||||
extern PwmSwitch pwm_switch;
|
||||
|
||||
+8
-9
@@ -22,8 +22,8 @@ void PowerPort::init_solar()
|
||||
chg_current_limit = 0; // no current towards solar panel allowed
|
||||
|
||||
// also initialize the connected bus
|
||||
bus->dis_voltage_start = 16.0;
|
||||
bus->dis_voltage_stop = 14.0;
|
||||
dis_voltage_start = 16.0;
|
||||
dis_voltage_stop = 14.0;
|
||||
}
|
||||
|
||||
void PowerPort::init_nanogrid()
|
||||
@@ -35,11 +35,11 @@ void PowerPort::init_nanogrid()
|
||||
chg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
|
||||
|
||||
// also initialize the connected bus
|
||||
bus->dis_voltage_start = 30.0; // starting buck mode above this point
|
||||
bus->dis_voltage_stop = 20.0; // stopping buck mode below this point
|
||||
dis_voltage_start = 30.0; // starting buck mode above this point
|
||||
dis_voltage_stop = 20.0; // stopping buck mode below this point
|
||||
|
||||
bus->chg_voltage_target = 28.0; // starting idle mode above this point
|
||||
bus->chg_voltage_min = 10.0;
|
||||
chg_voltage_target = 28.0; // starting idle mode above this point
|
||||
chg_voltage_min = 10.0;
|
||||
}
|
||||
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
@@ -47,10 +47,9 @@ void PowerPort::energy_balance()
|
||||
{
|
||||
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
|
||||
if (current >= 0) {
|
||||
chg_energy_Wh += bus->voltage * current / 3600.0;
|
||||
chg_energy_Wh += voltage * current / 3600.0;
|
||||
}
|
||||
else {
|
||||
dis_energy_Wh -= bus->voltage * current / 3600.0;
|
||||
dis_energy_Wh -= voltage * current / 3600.0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-24
@@ -19,19 +19,16 @@
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @brief Definition of charge controller terminals and internal DC buses
|
||||
* @brief Definition of charge controller terminals and internal power port / connections
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/** DC bus type
|
||||
/** Power Port class
|
||||
*
|
||||
* Stores the voltage measurement and voltage limits for a DC bus inside the charge controller PCB
|
||||
*
|
||||
* A DC bus is considered a connection of two or more electrical circuits in one point, such that
|
||||
* they have the same electric potential.
|
||||
* Stores actual measurements and voltage / current limits for external terminals or internal ports
|
||||
*/
|
||||
class DcBus
|
||||
class PowerPort
|
||||
{
|
||||
public:
|
||||
float voltage; ///< Measured bus voltage
|
||||
@@ -43,22 +40,6 @@ public:
|
||||
float dis_voltage_start; ///< Minimum voltage to allow discharge current from the bus
|
||||
///< (equal to load reconnect voltage)
|
||||
float dis_voltage_stop; ///< Absolute minimum = load disconnect for batteries
|
||||
};
|
||||
|
||||
|
||||
/** Power Port type
|
||||
*
|
||||
* Stores the current and power measurements and limits for one connection to a DC bus. One power
|
||||
* port is assigned to exactly ony DC bus, but one DC bus can have multiple ports.
|
||||
*/
|
||||
class PowerPort
|
||||
{
|
||||
public:
|
||||
PowerPort(DcBus *dc_bus):
|
||||
bus(dc_bus) {};
|
||||
|
||||
DcBus *bus; ///< Pointer to the DC bus this port is connected to (also
|
||||
///< storing its voltage measurement)
|
||||
|
||||
float current; ///< Measured current through this port (positive sign =
|
||||
///< increasing voltage/charge of the named device)
|
||||
@@ -92,5 +73,4 @@ public:
|
||||
void energy_balance();
|
||||
};
|
||||
|
||||
|
||||
#endif /* POWER_PORT_H */
|
||||
|
||||
+4
-4
@@ -152,7 +152,7 @@ void PwmSwitch::control()
|
||||
off_timestamp = time(NULL);
|
||||
printf("PWM charger stop.\n");
|
||||
}
|
||||
else if (port_int->bus->voltage > (port_int->bus->chg_voltage_target - port_int->chg_droop_res * port_int->current) // output voltage above target
|
||||
else if (port_int->voltage > (port_int->chg_voltage_target - port_int->chg_droop_res * port_int->current) // output voltage above target
|
||||
|| port_int->current > port_int->chg_current_limit // output current limit exceeded
|
||||
|| terminal->current < terminal->dis_current_limit) // input current (negative signs) above limit
|
||||
{
|
||||
@@ -184,10 +184,10 @@ void PwmSwitch::control()
|
||||
}
|
||||
else {
|
||||
if (port_int->chg_current_limit > 0 // charging allowed
|
||||
&& port_int->bus->voltage < port_int->bus->chg_voltage_target
|
||||
&& port_int->bus->voltage > port_int->bus->chg_voltage_min
|
||||
&& port_int->voltage < port_int->chg_voltage_target
|
||||
&& port_int->voltage > port_int->chg_voltage_min
|
||||
&& terminal->dis_current_limit < 0 // dischraging allowed
|
||||
&& terminal->bus->voltage > port_int->bus->voltage + offset_voltage_start
|
||||
&& terminal->voltage > port_int->voltage + offset_voltage_start
|
||||
&& time(NULL) > (off_timestamp + restart_interval)
|
||||
&& enabled == true)
|
||||
{
|
||||
|
||||
@@ -100,18 +100,18 @@ void uext_process_1s()
|
||||
oled.printf("n/a");
|
||||
}
|
||||
#ifndef CHARGER_TYPE_PWM
|
||||
if (solar_terminal.bus->voltage > bat_terminal.bus->voltage)
|
||||
if (solar_terminal.voltage > bat_terminal.voltage)
|
||||
#endif
|
||||
{
|
||||
oled.setTextCursor(0, 26);
|
||||
oled.printf("%4.1fV", solar_terminal.bus->voltage);
|
||||
oled.printf("%4.1fV", solar_terminal.voltage);
|
||||
}
|
||||
|
||||
// battery data
|
||||
oled.setTextCursor(42, 18);
|
||||
oled.printf("%5.1fW", (abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros
|
||||
oled.setTextCursor(42, 26);
|
||||
oled.printf("%5.1fV", bat_terminal.bus->voltage);
|
||||
oled.printf("%5.1fV", bat_terminal.voltage);
|
||||
|
||||
// load data
|
||||
oled.setTextCursor(90, 18);
|
||||
|
||||
Reference in New Issue
Block a user