diff --git a/src/daq.cpp b/src/daq.cpp index 5087236..e7c1d98 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -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 diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 7120ba9..67fc2ad 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -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 diff --git a/src/main.h b/src/main.h index 9c35305..1c5a1f5 100644 --- a/src/main.h +++ b/src/main.h @@ -28,7 +28,6 @@ extern Dcdc dcdc; #endif #if CONFIG_HAS_PWM_SWITCH -extern PowerPort pwm_terminal; extern PwmSwitch pwm_switch; #endif diff --git a/src/main_mbed.cpp b/src/main_mbed.cpp index 882d6b8..9b9765f 100644 --- a/src/main_mbed.cpp +++ b/src/main_mbed.cpp @@ -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(); diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index aec5875..dcc06a0 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -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(); diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 282b3c4..4380ff8 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -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); diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 845bb08..e65a55e 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -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 diff --git a/test/main.cpp b/test/main.cpp index ece9fe2..b38718a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -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