Let PWM switch class inherit from PowerPort

First step of unifying different ports connected to one bus. At a later
step, also the load and charger classes should inherit from PowerPort
class to make overall charge controller structure more clean.
This commit is contained in:
Martin Jaeger
2020-02-07 12:00:59 +01:00
parent f0e99cd80f
commit a7d6dbc3b5
8 changed files with 34 additions and 43 deletions
+3 -3
View File
@@ -170,12 +170,12 @@ 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_terminal.current = -pwm_switch.get_duty_cycle() * (
pwm_switch.current = -pwm_switch.get_duty_cycle() * (
adc_scaled(ADC_POS_I_SOLAR, vcc, ADC_GAIN_I_SOLAR) + solar_current_offset);
lv_terminal.current = -pwm_terminal.current - load_terminal.current;
lv_terminal.current = -pwm_switch.current - load_terminal.current;
pwm_terminal.power = pwm_terminal.bus->voltage * pwm_terminal.current;
pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current;
#endif
#if CONFIG_HAS_DCDC_CONVERTER
+1 -1
View File
@@ -178,7 +178,7 @@ const data_object_t data_objects[] = {
#endif
#if CONFIG_HAS_PWM_SWITCH
TS_DATA_OBJ_BOOL(0x63, "PwmEn", &pwm_switch.enabled,
TS_DATA_OBJ_BOOL(0x63, "PwmEn", &pwm_switch.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
-1
View File
@@ -28,7 +28,6 @@ extern Dcdc dcdc;
#endif
#if CONFIG_HAS_PWM_SWITCH
extern PowerPort pwm_terminal;
extern PwmSwitch pwm_switch;
#endif
+5 -6
View File
@@ -48,8 +48,7 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif
#if CONFIG_HAS_PWM_SWITCH
PowerPort pwm_terminal(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal);
PwmSwitch pwm_switch(&lv_bus);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
@@ -62,7 +61,7 @@ PowerPort &solar_terminal = hv_terminal;
#elif CONFIG_LV_TERMINAL_SOLAR
PowerPort &solar_terminal = lv_terminal;
#elif CONFIG_PWM_TERMINAL_SOLAR
PowerPort &solar_terminal = pwm_terminal;
PowerPort &solar_terminal = pwm_switch;
#endif
#if CONFIG_HV_TERMINAL_NANOGRID
@@ -153,7 +152,7 @@ int main()
#endif
#if CONFIG_HAS_PWM_SWITCH
bat_terminal.pass_voltage_targets(&pwm_terminal);
bat_terminal.pass_voltage_targets(&pwm_switch);
#endif
eeprom_update();
@@ -191,7 +190,7 @@ void system_control()
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_terminal, &bat_terminal, &load_terminal);
ports_update_current_limits(&pwm_switch, &bat_terminal, &load_terminal);
pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
@@ -216,7 +215,7 @@ void system_control()
#endif
#if CONFIG_HAS_PWM_SWITCH
pwm_terminal.energy_balance();
pwm_switch.energy_balance();
#endif
lv_terminal.energy_balance();
+4 -5
View File
@@ -45,8 +45,7 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif
#if CONFIG_HAS_PWM_SWITCH
PowerPort pwm_terminal(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal);
PwmSwitch pwm_switch(&lv_bus);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
@@ -59,7 +58,7 @@ PowerPort &solar_terminal = hv_terminal;
#elif CONFIG_LV_TERMINAL_SOLAR
PowerPort &solar_terminal = lv_terminal;
#elif CONFIG_PWM_TERMINAL_SOLAR
PowerPort &solar_terminal = pwm_terminal;
PowerPort &solar_terminal = pwm_switch;
#endif
#if CONFIG_HV_TERMINAL_NANOGRID
@@ -145,7 +144,7 @@ void control_thread()
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, &bat_terminal, &load_terminal);
ports_update_current_limits(&pwm_switch, &bat_terminal, &load_terminal);
//pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
@@ -170,7 +169,7 @@ void control_thread()
#endif
#if CONFIG_HAS_PWM_SWITCH
pwm_terminal.energy_balance();
pwm_switch.energy_balance();
#endif
lv_terminal.energy_balance();
+16 -17
View File
@@ -152,10 +152,9 @@ bool PwmSwitch::signal_high()
return pwm_signal_high();
}
PwmSwitch::PwmSwitch(PowerPort *pwm_port)
PwmSwitch::PwmSwitch(DcBus *dc_bus) :
PowerPort(dc_bus)
{
port = pwm_port;
off_timestamp = -10000; // start immediately
// calibration parameters
@@ -164,26 +163,26 @@ PwmSwitch::PwmSwitch(PowerPort *pwm_port)
pwm_signal_init_registers(PWM_FREQUENCY);
enabled = true; // enable charging in general
enable = true; // enable charging in general
_pwm_active = false; // still disable actual switch
}
void PwmSwitch::control()
{
if (_pwm_active) {
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)
if (neg_current_limit == 0
|| current > 0 // discharging battery into solar panel --> stop
|| bus->voltage < 9.0 // not enough voltage for MOSFET drivers anymore
|| enable == false)
{
pwm_signal_stop();
off_timestamp = uptime();
print_info("PWM charger stop.\n");
}
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
else if (bus->voltage > (sink_voltage_max -
pos_droop_res * current) // voltage above target
|| current < neg_current_limit // port current limit exceeded
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
{
// decrease power, as limits were reached
@@ -216,12 +215,12 @@ void PwmSwitch::control()
}
}
else {
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
if (pos_current_limit > 0 // charging allowed
&& bus->voltage < bus->voltage_max
&& bus->voltage > bus->voltage_min
&& ext_voltage > bus->voltage + offset_voltage_start
&& uptime() > (off_timestamp + restart_interval)
&& enabled == true)
&& enable == true)
{
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
adc_upper_alert_inhibit(ADC_POS_V_BAT, 50);
+3 -7
View File
@@ -21,12 +21,10 @@
*
* Contains all data belonging to the PWM switching sub-component.
*/
class PwmSwitch
class PwmSwitch : public PowerPort
{
public:
/** Initialization of PWM switch struct
*/
PwmSwitch(PowerPort *pwm_port);
PwmSwitch(DcBus *dc_bus);
/** Main control function for the PWM switching algorithm
*/
@@ -56,11 +54,9 @@ public:
*/
float get_duty_cycle();
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
bool enable; ///< Can be used to disable the PWM power stage
float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V)
int restart_interval; ///< Interval to wait before retrying charging after low solar power cut-off (s)
time_t off_timestamp; ///< Time when charger was switched off last time
+2 -3
View File
@@ -39,8 +39,7 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif
#if CONFIG_HAS_PWM_SWITCH
PowerPort pwm_terminal(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal);
PwmSwitch pwm_switch(&lv_bus);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
@@ -53,7 +52,7 @@ PowerPort &solar_terminal = hv_terminal;
#elif CONFIG_LV_TERMINAL_SOLAR
PowerPort &solar_terminal = lv_terminal;
#elif CONFIG_PWM_TERMINAL_SOLAR
PowerPort &solar_terminal = pwm_terminal;
PowerPort &solar_terminal = pwm_switch;
#endif
#if CONFIG_HV_TERMINAL_NANOGRID