Voltage replaced by DcBus ptr in PowerPort class

Preparation to separate current and voltage measurements and control
parameters into DcBus and PowerPort classes. This will help to
manage current from different sources (grid, solar) going towards
one bus and control the battery voltage. It will also help to align
multiple charge controllers in parallel, communicating via CAN bus.
This commit is contained in:
Martin Jaeger
2020-02-07 11:55:51 +01:00
parent 86bfce8a0c
commit f0e99cd80f
21 changed files with 214 additions and 180 deletions
+10 -10
View File
@@ -211,8 +211,8 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char
void Charger::detect_num_batteries(BatConf *bat)
{
if (port->voltage > bat->voltage_absolute_min * 2 &&
port->voltage < bat->voltage_absolute_max * 2) {
if (port->bus->voltage > bat->voltage_absolute_min * 2 &&
port->bus->voltage < bat->voltage_absolute_max * 2) {
num_batteries = 2;
printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2);
}
@@ -226,7 +226,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)((port->voltage - bat_conf->ocv_empty) /
int soc_new = (int)((port->bus->voltage - bat_conf->ocv_empty) /
(bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0);
if (soc_new > 500 && soc_filtered == 0) {
@@ -262,7 +262,7 @@ void Charger::discharge_control(BatConf *bat_conf)
// load output state is defined by battery negative current limit
if (port->neg_current_limit < 0) {
// discharging currently allowed. see if that's still valid:
if (port->voltage < num_batteries *
if (port->bus->voltage < num_batteries *
(bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res))
{
// low state of charge
@@ -297,7 +297,7 @@ void Charger::discharge_control(BatConf *bat_conf)
}
else {
// discharging currently not allowed. should we allow it?
if (port->voltage >= num_batteries *
if (port->bus->voltage >= num_batteries *
(bat_conf->voltage_load_reconnect - port->current * port->neg_droop_res)
&& bat_temperature < bat_conf->discharge_temp_max - 1
&& bat_temperature > bat_conf->discharge_temp_min + 1)
@@ -329,7 +329,7 @@ void Charger::charge_control(BatConf *bat_conf)
}
if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) &&
port->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries)
port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries)
{
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
}
@@ -337,7 +337,7 @@ void Charger::charge_control(BatConf *bat_conf)
// state machine
switch (state) {
case CHG_STATE_IDLE: {
if (port->voltage < bat_conf->voltage_recharge * num_batteries
if (port->bus->voltage < bat_conf->voltage_recharge * num_batteries
&& (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)
@@ -358,7 +358,7 @@ void Charger::charge_control(BatConf *bat_conf)
port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->voltage > num_batteries *
if (port->bus->voltage > num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
{
enter_state(CHG_STATE_TOPPING);
@@ -370,7 +370,7 @@ void Charger::charge_control(BatConf *bat_conf)
port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->voltage >= num_batteries *
if (port->bus->voltage >= num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
{
time_voltage_limit_reached = uptime();
@@ -414,7 +414,7 @@ void Charger::charge_control(BatConf *bat_conf)
port->sink_voltage_max = num_batteries * (bat_conf->trickle_voltage +
bat_conf->temperature_compensation * (bat_temperature - 25));
if (port->voltage >= num_batteries *
if (port->bus->voltage >= num_batteries *
(port->sink_voltage_max - port->current * port->pos_droop_res))
{
time_voltage_limit_reached = uptime();
+17 -17
View File
@@ -154,17 +154,15 @@ void daq_update()
//int vcc = 3300;
// calculate lower voltage first, as it is needed for PWM terminal voltage calculation
lv_terminal.voltage = adc_scaled(ADC_POS_V_BAT, vcc, ADC_GAIN_V_BAT);
load_terminal.voltage = lv_terminal.voltage;
lv_bus.voltage = adc_scaled(ADC_POS_V_BAT, vcc, ADC_GAIN_V_BAT);
#if CONFIG_HAS_DCDC_CONVERTER
hv_terminal.voltage = adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR);
dcdc_lv_port.voltage = lv_terminal.voltage;
hv_bus.voltage = adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR);
#endif
#if CONFIG_HAS_PWM_SWITCH
pwm_terminal.voltage = lv_terminal.voltage - vcc * (ADC_OFFSET_V_SOLAR / 1000.0) -
pwm_switch.ext_voltage = lv_bus.voltage - vcc * (ADC_OFFSET_V_SOLAR / 1000.0) -
adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR);
pwm_port_int.voltage = lv_terminal.voltage;
#endif
load_terminal.current =
@@ -172,25 +170,27 @@ void daq_update()
#if CONFIG_HAS_PWM_SWITCH
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation
pwm_port_int.current = pwm_switch.get_duty_cycle() * (
pwm_terminal.current = -pwm_switch.get_duty_cycle() * (
adc_scaled(ADC_POS_I_SOLAR, vcc, ADC_GAIN_I_SOLAR) + solar_current_offset);
pwm_terminal.current = -pwm_port_int.current;
lv_terminal.current = pwm_port_int.current - load_terminal.current;
pwm_port_int.power = pwm_port_int.voltage * pwm_port_int.current;
pwm_terminal.power = pwm_terminal.voltage * pwm_terminal.current;
lv_terminal.current = -pwm_terminal.current - load_terminal.current;
pwm_terminal.power = pwm_terminal.bus->voltage * pwm_terminal.current;
#endif
#if CONFIG_HAS_DCDC_CONVERTER
dcdc_lv_port.current =
adc_scaled(ADC_POS_I_DCDC, vcc, ADC_GAIN_I_DCDC) + solar_current_offset;
lv_terminal.current = dcdc_lv_port.current - load_terminal.current;
hv_terminal.current = -dcdc_lv_port.current * lv_terminal.voltage / hv_terminal.voltage;
dcdc_lv_port.power = dcdc_lv_port.voltage * dcdc_lv_port.current;
hv_terminal.power = hv_terminal.voltage * hv_terminal.current;
lv_terminal.current = dcdc_lv_port.current - load_terminal.current;
hv_terminal.current = -dcdc_lv_port.current * lv_terminal.bus->voltage / hv_terminal.bus->voltage;
dcdc_lv_port.power = dcdc_lv_port.bus->voltage * dcdc_lv_port.current;
hv_terminal.power = hv_terminal.bus->voltage * hv_terminal.current;
#endif
lv_terminal.power = lv_terminal.voltage * lv_terminal.current;
load_terminal.power = load_terminal.voltage * load_terminal.current;
lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current;
load_terminal.power = load_terminal.bus->voltage * load_terminal.current;
#ifdef PIN_ADC_TEMP_BAT
// battery temperature calculation
+2 -2
View File
@@ -207,11 +207,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.voltage, 2,
TS_DATA_OBJ_FLOAT(0x70, "Bat_V", &bat_terminal.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.voltage, 2,
TS_DATA_OBJ_FLOAT(0x71, "Solar_V", &solar_terminal.bus->voltage, 2,
TS_OUTPUT, TS_READ_ALL),
#endif
+19 -19
View File
@@ -74,7 +74,7 @@ int Dcdc::duty_cycle_delta()
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->voltage, out->voltage, in->current, out->current,
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);
@@ -82,7 +82,7 @@ int Dcdc::duty_cycle_delta()
// swith off after 10s low power or negative power (if not in nanogrid mode)
pwr_inc_goal = 0;
}
else if (out->voltage > (out->sink_voltage_max - out->pos_droop_res * out->current)) {
else if (out->bus->voltage > (out->sink_voltage_max - out->pos_droop_res * out->current)) {
// output voltage target reached
state = DCDC_STATE_CV;
pwr_inc_goal = -1; // decrease output power
@@ -94,14 +94,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->voltage < (in->src_voltage_stop - in->neg_droop_res * in->current)
|| (in->bus->voltage < (in->src_voltage_stop - in->neg_droop_res * in->current)
&& 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->voltage < out->src_voltage_start) {
else if (out->power < output_power_min && out->bus->voltage < out->src_voltage_start) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwr_inc_goal = 1; // increase output power
}
@@ -121,9 +121,9 @@ int Dcdc::duty_cycle_delta()
int Dcdc::check_start_conditions()
{
if (enabled == false ||
hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing
lvs->voltage > ls_voltage_max ||
lvs->voltage < ls_voltage_min ||
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 ||
dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) ||
(int)uptime() < (off_timestamp + (int)restart_interval))
{
@@ -131,19 +131,19 @@ int Dcdc::check_start_conditions()
}
if (lvs->pos_current_limit > 0 &&
lvs->voltage < lvs->sink_voltage_max &&
lvs->voltage > lvs->sink_voltage_min &&
lvs->bus->voltage < lvs->sink_voltage_max &&
lvs->bus->voltage > lvs->sink_voltage_min &&
hvs->neg_current_limit < 0 &&
hvs->voltage > hvs->src_voltage_start)
hvs->bus->voltage > hvs->src_voltage_start)
{
return 1; // start in buck mode allowed
}
if (hvs->pos_current_limit > 0 &&
hvs->voltage < hvs->sink_voltage_max &&
hvs->voltage > hvs->sink_voltage_min &&
hvs->bus->voltage < hvs->sink_voltage_max &&
hvs->bus->voltage > hvs->sink_voltage_min &&
lvs->neg_current_limit < 0 &&
lvs->voltage > lvs->src_voltage_start)
lvs->bus->voltage > lvs->src_voltage_start)
{
return -1; // start in boost mode allowed
}
@@ -155,7 +155,7 @@ void Dcdc::control()
{
if (half_bridge_enabled()) {
const char *stop_reason = NULL;
if (lvs->voltage > ls_voltage_max || hvs->voltage > hs_voltage_max) {
if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) {
stop_reason = "emergency (voltage limits exceeded)";
}
else if (enabled == false) {
@@ -212,17 +212,17 @@ void Dcdc::control()
mode_name = "buck";
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush
// currents and stress on MOSFETs
half_bridge_start(lvs->voltage / (hvs->voltage - 1));
half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1));
}
else {
mode_name = "boost";
// Will automatically start with max. duty (0.97) if connected to a
// nanogrid not yet started up (zero voltage)
half_bridge_start(lvs->voltage / (hvs->voltage + 1));
half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1));
}
power_good_timestamp = uptime();
print_info("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
startup_delay_counter = 0; // ensure we will have a delay before next start
}
}
@@ -246,9 +246,9 @@ void Dcdc::test()
if (startup_delay_counter > num_wait_calls) {
if (check_start_conditions() != 0) {
half_bridge_start(lvs->voltage / hvs->voltage);
half_bridge_start(lvs->bus->voltage / hvs->bus->voltage);
print_info("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100);
}
}
else {
+5 -5
View File
@@ -39,7 +39,7 @@ void DeviceStatus::update_energy()
}
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
if (solar_terminal.voltage < bat_terminal.voltage) {
if (solar_terminal.bus->voltage < bat_terminal.bus->voltage) {
seconds_zero_solar += 1;
}
else {
@@ -75,13 +75,13 @@ void DeviceStatus::update_energy()
void DeviceStatus::update_min_max_values()
{
if (bat_terminal.voltage > battery_voltage_max) {
battery_voltage_max = bat_terminal.voltage;
if (bat_terminal.bus->voltage > battery_voltage_max) {
battery_voltage_max = bat_terminal.bus->voltage;
}
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
if (solar_terminal.voltage > solar_voltage_max) {
solar_voltage_max = solar_terminal.voltage;
if (solar_terminal.bus->voltage > solar_voltage_max) {
solar_voltage_max = solar_terminal.bus->voltage;
}
#endif
+3 -3
View File
@@ -150,11 +150,11 @@ void ExtOled::process_1s()
oled.writeString(buf, len);
}
#ifndef CHARGER_TYPE_PWM
if (in_terminal.voltage > bat_terminal.voltage)
if (in_terminal.bus->voltage > bat_terminal.bus->voltage)
#endif
{
oled.setTextCursor(0, 26);
len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.voltage);
len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.bus->voltage);
oled.writeString(buf, len);
}
@@ -164,7 +164,7 @@ void ExtOled::process_1s()
(abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros
oled.writeString(buf, len);
oled.setTextCursor(42, 26);
len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.voltage);
len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.bus->voltage);
oled.writeString(buf, len);
// load data
+4 -4
View File
@@ -140,8 +140,8 @@ void LoadOutput::control()
// 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->voltage > port->sink_voltage_max ||
port->voltage > LOW_SIDE_VOLTAGE_MAX)
if (port->bus->voltage > port->sink_voltage_max ||
port->bus->voltage > LOW_SIDE_VOLTAGE_MAX)
{
debounce_counter++;
if (debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag
@@ -178,8 +178,8 @@ void LoadOutput::control()
}
if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) &&
port->voltage < (port->sink_voltage_max - ov_hysteresis) &&
port->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
port->bus->voltage < (port->sink_voltage_max - ov_hysteresis) &&
port->bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
{
dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE);
}
+2 -1
View File
@@ -16,10 +16,12 @@
#include "load.h"
#include "pcb.h"
extern DcBus lv_bus;
extern PowerPort lv_terminal;
extern PowerPort load_terminal;
#if CONFIG_HAS_DCDC_CONVERTER
extern DcBus hv_bus;
extern PowerPort hv_terminal;
extern PowerPort dcdc_lv_port;
extern Dcdc dcdc;
@@ -27,7 +29,6 @@ extern Dcdc dcdc;
#if CONFIG_HAS_PWM_SWITCH
extern PowerPort pwm_terminal;
extern PowerPort pwm_port_int;
extern PwmSwitch pwm_switch;
#endif
+11 -10
View File
@@ -31,11 +31,13 @@
#include "data_objects.h" // for access to internal data via ThingSet
#include "bl_support.h" // Bootloader support from the application side
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
#if CONFIG_HAS_DCDC_CONVERTER
PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port; // internal low voltage side of DC/DC 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
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID);
#elif CONFIG_HV_TERMINAL_BATTERY
@@ -46,13 +48,12 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif
#if CONFIG_HAS_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 pwm_terminal(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
PowerPort load_terminal; // load terminal (also connected to lv_bus)
PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus)
LoadOutput load(&load_terminal);
#endif
@@ -152,7 +153,7 @@ int main()
#endif
#if CONFIG_HAS_PWM_SWITCH
bat_terminal.pass_voltage_targets(&pwm_port_int);
bat_terminal.pass_voltage_targets(&pwm_terminal);
#endif
eeprom_update();
@@ -187,10 +188,10 @@ void system_control()
daq_update();
// alerts should trigger only for transients, so update based on actual voltage
daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8);
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal);
ports_update_current_limits(&pwm_terminal, &bat_terminal, &load_terminal);
pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
+10 -9
View File
@@ -28,11 +28,13 @@
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "data_objects.h" // for access to internal data via ThingSet
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
#if CONFIG_HAS_DCDC_CONVERTER
PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port; // internal low voltage side of DC/DC 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
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID);
#elif CONFIG_HV_TERMINAL_BATTERY
@@ -43,13 +45,12 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif
#if CONFIG_HAS_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 pwm_terminal(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
PowerPort load_terminal; // load terminal (also connected to lv_bus)
PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus)
LoadOutput load(&load_terminal);
#endif
@@ -141,10 +142,10 @@ void control_thread()
daq_update();
// alerts should trigger only for transients, so update based on actual voltage
daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8);
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal);
ports_update_current_limits(&pwm_port, &bat_terminal, &load_terminal);
//pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
+2 -2
View File
@@ -45,10 +45,10 @@ void PowerPort::energy_balance()
{
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
if (current >= 0.0F) {
pos_energy_Wh += voltage * current / 3600.0;
pos_energy_Wh += bus->voltage * current / 3600.0;
}
else {
neg_energy_Wh -= voltage * current / 3600.0;
neg_energy_Wh -= bus->voltage * current / 3600.0;
}
}
+23 -3
View File
@@ -15,6 +15,22 @@
#include <stdbool.h>
/**
* These values may be calculated based on external data, if multiple devices are connected to the
* bus.
*/
class DcBus
{
public:
float voltage; ///< Measured bus voltage
float voltage_max; ///< Maximum voltage of a DC bus, equivalent to battery
///< charging target voltage if hard-wired to a battery
float voltage_min; ///< Absolute minimum for both chg and discharging direction
};
// -----------------
// | | >> positive current/power direction
// | o---->----
@@ -48,7 +64,7 @@
class PowerPort
{
public:
float voltage; ///< Measured port voltage
DcBus *bus; ///< Pointer to DC bus containing voltage measurement
float sink_voltage_max; ///< Maximum voltage if connected device acts as a sink
///< (equivalent to battery charging target voltage)
@@ -59,9 +75,9 @@ public:
///< (equal to load reconnect voltage)
float src_voltage_stop; ///< Absolute minimum = load disconnect for batteries
float current; ///< Measured current through this port (positive sign =
float current = 0; ///< Measured current through this port (positive sign =
///< sinking current into the named external device)
float power; ///< Product of current & voltage
float power = 0; ///< Product of port current and bus voltage
float pos_current_limit; ///< Maximum positive current (valid values >= 0.0)
float pos_droop_res; ///< control voltage = nominal voltage - droop_res * current
@@ -72,6 +88,10 @@ public:
float pos_energy_Wh; ///< Cumulated sunk energy since last counter reset (Wh)
float neg_energy_Wh; ///< Cumulated sourced energy since last counter reset (Wh)
PowerPort(DcBus *dc_bus) :
bus(dc_bus)
{}
/** Initialize power port for solar panel connection
*
* @param max_abs_current Maximum input current allowed by PCB (as a positive value)
+13 -16
View File
@@ -152,10 +152,9 @@ bool PwmSwitch::signal_high()
return pwm_signal_high();
}
PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int)
PwmSwitch::PwmSwitch(PowerPort *pwm_port)
{
terminal = pwm_terminal;
port_int = pwm_port_int;
port = pwm_port;
off_timestamp = -10000; // start immediately
@@ -172,20 +171,19 @@ PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int)
void PwmSwitch::control()
{
if (_pwm_active) {
if (port_int->pos_current_limit == 0 || terminal->neg_current_limit == 0
|| terminal->current > 0 // discharging battery into solar panel --> stop
|| port_int->voltage < 9.0 // not enough voltage for MOSFET drivers anymore
if (port->neg_current_limit == 0
|| port->current > 0 // discharging battery into solar panel --> stop
|| port->bus->voltage < 9.0 // not enough voltage for MOSFET drivers anymore
|| enabled == false)
{
pwm_signal_stop();
off_timestamp = uptime();
print_info("PWM charger stop.\n");
}
else if (port_int->voltage > (port_int->sink_voltage_max -
port_int->pos_droop_res * port_int->current) // voltage above target
|| port_int->current > port_int->pos_current_limit // port current limit exceeded
|| port_int->current > PWM_CURRENT_MAX // PCB current limit exceeded
|| terminal->current < terminal->neg_current_limit) // input current (negative signs) above limit
else if (port->bus->voltage > (port->sink_voltage_max -
port->pos_droop_res * port->current) // voltage above target
|| port->current < port->neg_current_limit // port current limit exceeded
|| port->current < -PWM_CURRENT_MAX) // PCB current limit exceeded
{
// decrease power, as limits were reached
@@ -218,11 +216,10 @@ void PwmSwitch::control()
}
}
else {
if (port_int->pos_current_limit > 0 // charging allowed
&& port_int->voltage < port_int->sink_voltage_max
&& port_int->voltage > port_int->sink_voltage_min
&& terminal->neg_current_limit < 0 // discharging allowed
&& terminal->voltage > port_int->voltage + offset_voltage_start
if (port->pos_current_limit > 0 // charging allowed
&& port->bus->voltage < port->bus->voltage_max
&& port->bus->voltage > port->bus->voltage_min
&& ext_voltage > port->bus->voltage + offset_voltage_start
&& uptime() > (off_timestamp + restart_interval)
&& enabled == true)
{
+4 -3
View File
@@ -26,7 +26,7 @@ class PwmSwitch
public:
/** Initialization of PWM switch struct
*/
PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int);
PwmSwitch(PowerPort *pwm_port);
/** Main control function for the PWM switching algorithm
*/
@@ -56,8 +56,9 @@ public:
*/
float get_duty_cycle();
PowerPort *terminal; ///< Pointer to external power port (terminal)
PowerPort *port_int; ///< Pointer to internal power port (junction with load and battery)
PowerPort *port; ///< Pointer to internal power port (junction with load and battery)
float ext_voltage; ///< Voltage measurement at terminal (external)
bool enabled; ///< Can be used to disable the PWM power stage
float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V)