diff --git a/src/dc_bus.cpp b/src/dc_bus.cpp index 436ae99..19dd019 100644 --- a/src/dc_bus.cpp +++ b/src/dc_bus.cpp @@ -16,12 +16,12 @@ #include "dc_bus.h" -void dc_bus_init_solar(DcBus *bus) +void dc_bus_init_solar(DcBus *bus, float max_abs_current) { bus->dis_allowed = true; // PV panel may provide power to solar input of DC/DC bus->dis_voltage_start = 16.0; bus->dis_voltage_stop = 14.0; - bus->dis_current_max = -18.0; + bus->dis_current_max = -max_abs_current; bus->chg_allowed = false; } diff --git a/src/dc_bus.h b/src/dc_bus.h index 26c82f8..67d7061 100644 --- a/src/dc_bus.h +++ b/src/dc_bus.h @@ -56,8 +56,11 @@ typedef struct { } DcBus; /** Initialize dc bus for solar panel connection + * + * @param bus The DC bus struct to initialize + * @param max_abs_current Maximum input current allowed by PCB (as a positive value) */ -void dc_bus_init_solar(DcBus *bus); +void dc_bus_init_solar(DcBus *bus, float max_abs_current); /** Initialize dc bus for nanogrid connection */ diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 15a89df..86fa72e 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -19,6 +19,8 @@ #include "pcb.h" #include "log.h" +#ifndef CHARGER_TYPE_PWM + #include "half_bridge.h" #include // for time(NULL) function @@ -181,7 +183,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hs, DcBus *ls) } } -void Dcdcest(Dcdc *dcdc, DcBus *hs, DcBus *ls) +void dcdc_test(Dcdc *dcdc, DcBus *hs, DcBus *ls) { if (half_bridge_enabled()) { if (half_bridge_get_duty_cycle() > 0.5) { @@ -225,3 +227,5 @@ void dcdc_self_destruction() //half_bridge_start(0); // now the fuse should be triggered and we disappear } + +#endif /* CHARGER_TYPE_PWM */ diff --git a/src/dcdc.h b/src/dcdc.h index b8ba934..aab7769 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -107,7 +107,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *high_side, DcBus *low_side); * @param high_side High-side power port (e.g. solar input for typical MPPT charge controller application) * @param low_side Low-side power port (e.g. battery output for typical MPPT charge controller application) */ -void Dcdcest(Dcdc *dcdc, DcBus *high_side, DcBus *low_side); +void dcdc_test(Dcdc *dcdc, DcBus *high_side, DcBus *low_side); /** Prevent overcharging of battery in case of shorted HS MOSFET * diff --git a/src/main.cpp b/src/main.cpp index 71cf3b2..4902939 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -117,7 +117,7 @@ int main() #else // MPPT dcdc_init(&dcdc); half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage - #endif +#endif // Configuration from EEPROM data_objects_read_eeprom(); @@ -145,6 +145,10 @@ int main() uext_init(); init_watchdog(10); // 10s should be enough for communication ports +#ifdef CHARGER_TYPE_PWM + dc_bus_init_solar(&hv_bus, PWM_CURRENT_MAX); + bat_bus = &lv_bus; +#else // Setup of DC/DC power stage switch(dcdc.mode) { case MODE_NANOGRID: @@ -152,14 +156,15 @@ int main() bat_bus = &lv_bus; break; case MODE_MPPT_BUCK: // typical MPPT charge controller operation - dc_bus_init_solar(&hv_bus); + dc_bus_init_solar(&hv_bus, DCDC_CURRENT_MAX); bat_bus = &lv_bus; break; case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel - dc_bus_init_solar(&lv_bus); + dc_bus_init_solar(&lv_bus, DCDC_CURRENT_MAX); bat_bus = &hv_bus; break; } +#endif charger_detect_num_batteries(&charger, &bat_conf, bat_bus); // check if we have 24V instead of 12V system battery_init_dc_bus(bat_bus, &bat_conf, charger.num_batteries); diff --git a/src/pcbs/pwm_2420_lus_0v2.h b/src/pcbs/pwm_2420_lus_0v2.h index e9420a4..e8eac5d 100644 --- a/src/pcbs/pwm_2420_lus_0v2.h +++ b/src/pcbs/pwm_2420_lus_0v2.h @@ -25,9 +25,10 @@ #define CHARGER_TYPE_PWM 1 // PWM charge controller instead of MPPT #define PWM_TIM 3 // use TIM3 timer +#define PWM_FREQUENCY 25 // Hz -#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current -#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current +#define PWM_CURRENT_MAX 20 // PCB maximum PWM switch (solar) current +#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current #define LOW_SIDE_VOLTAGE_MAX 30 // Maximum voltage at battery port (V) #define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 256cf0f..33412aa 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -21,8 +21,6 @@ #include "pcb.h" #include // for time(NULL) function -#include // for fabs function - // timer used for PWM generation has to be globally selected for the used PCB #if (PWM_TIM == 3) && defined(CHARGER_TYPE_PWM) @@ -46,7 +44,7 @@ void pwm_switch_init_registers(int freq_Hz) GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos; // Set timer clock to 10 kHz - TIM3->PSC = SystemCoreClock / 10000; + TIM3->PSC = SystemCoreClock / 10000 - 1; // Capture/Compare Mode Register 1 // OCxM = 110: Select PWM mode 1 on OCx @@ -68,7 +66,8 @@ void pwm_switch_init_registers(int freq_Hz) _pwm_resolution = 10000 / freq_Hz; // Auto Reload Register - TIM3->ARR = _pwm_resolution; + // Period goes from 0 to ARR (including ARR value), so substract 1 clock cycle + TIM3->ARR = _pwm_resolution - 1; } void pwm_switch_set_duty_cycle(float duty) @@ -78,7 +77,7 @@ void pwm_switch_set_duty_cycle(float duty) void pwm_switch_duty_cycle_step(int delta) { - if ((int)TIM3->CCR4 + delta <= (_pwm_resolution + 1) && (int)TIM3->CCR4 + delta >= 0) { + if ((int)TIM3->CCR4 + delta <= _pwm_resolution && (int)TIM3->CCR4 + delta >= 0) { TIM3->CCR4 += delta; } } @@ -88,9 +87,9 @@ bool pwm_switch_enabled() return _enabled; } -float pwm_switch_get_duty_cycle() { - // TODO: check why we might get > 100 - return (float)(TIM3->CCR4) / (_pwm_resolution); +float pwm_switch_get_duty_cycle() +{ + return (float)(TIM3->CCR4) / _pwm_resolution; } void pwm_switch_start(float pwm_duty) @@ -110,57 +109,71 @@ void pwm_switch_start(float pwm_duty) void pwm_switch_stop() { TIM3->CCER &= ~(TIM_CCER_CC4E); - + TIM3->CCR4 = 0; _enabled = false; } void pwm_switch_init(PwmSwitch *pwm_switch) { - pwm_switch->solar_current_max = DCDC_CURRENT_MAX; - pwm_switch->solar_current_min = 0.05; - - //pwm_switch->pwm_delta = 1; pwm_switch->off_timestamp = -10000; // start immediately // calibration parameters - //pwm_switch->offset_voltage_start = 3.0; // V charging switched on if Vsolar > Vbat + offset - //pwm_switch->offset_voltage_stop = 1.0; // V charging switched off if Vsolar < Vbat + offset + pwm_switch->offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset pwm_switch->restart_interval = 60; // s When should we retry to start charging after low solar power cut-off? - pwm_switch_init_registers(20); + pwm_switch_init_registers(PWM_FREQUENCY); pwm_switch->enabled = true; // enable charging in general _enabled = false; // still disable actual switch } -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_port, DcBus *bat_port) +void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus) { if (_enabled) { - if (bat_port->chg_allowed == false || solar_port->dis_allowed == false - || (solar_port->voltage < solar_port->dis_voltage_stop && solar_port->current > 0) + if (bat_bus->chg_allowed == false || solar_bus->dis_allowed == false + || solar_bus->current > 0 // discharging battery into solar panel --> stop || pwm_switch->enabled == false) { pwm_switch_stop(); pwm_switch->off_timestamp = time(NULL); printf("PWM charger stop.\n"); } - else if (bat_port->voltage > (bat_port->chg_voltage_target - bat_port->chg_droop_res * bat_port->current) // output voltage above target - || bat_port->current > bat_port->chg_current_max // output current limit exceeded - || solar_port->current < solar_port->dis_current_max) // input current (negative signs) above limit + else if (bat_bus->voltage > (bat_bus->chg_voltage_target - bat_bus->chg_droop_res * bat_bus->current) // output voltage above target + || bat_bus->current > bat_bus->chg_current_max // output current limit exceeded + || solar_bus->current < solar_bus->dis_current_max) // input current (negative signs) above limit { - if (pwm_switch_get_duty_cycle() > 0.9) + // The gate driver switch-off time is quite high (fall time around 1ms), so very short + // on or off periods (duty cycle close to 0 and 1) should be avoided + if (pwm_switch_get_duty_cycle() > 0.95) { + // prevent very short off periods + pwm_switch_set_duty_cycle(0.95); + } + else if (pwm_switch_get_duty_cycle() < 0.05) { + // prevent very short on periods and switch completely off instead + pwm_switch_stop(); + pwm_switch->off_timestamp = time(NULL); + printf("PWM charger stop, no further derating possible.\n"); + } + else { pwm_switch_duty_cycle_step(-1); + } } else { - pwm_switch_duty_cycle_step(1); + if (pwm_switch_get_duty_cycle() > 0.95) { + // prevent very short off periods and switch completely on instead + pwm_switch_set_duty_cycle(1); + } + else { + pwm_switch_duty_cycle_step(1); + } } } else { - if (bat_port->chg_allowed == true - && bat_port->voltage < bat_port->chg_voltage_target - && bat_port->voltage > bat_port->chg_voltage_min - && solar_port->dis_allowed == true - && solar_port->voltage > solar_port->dis_voltage_start + if (bat_bus->chg_allowed == true + && bat_bus->voltage < bat_bus->chg_voltage_target + && bat_bus->voltage > bat_bus->chg_voltage_min + && solar_bus->dis_allowed == true + && solar_bus->voltage > bat_bus->voltage + pwm_switch->offset_voltage_start && time(NULL) > (pwm_switch->off_timestamp + pwm_switch->restart_interval) && pwm_switch->enabled == true) { @@ -170,14 +183,6 @@ void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_port, DcBus *bat_por } } -#else - -// dummy functions for non-PWM charge controllers -void pwm_switch_init(PwmSwitch *pwm_switch) {} -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_port, DcBus *bat_port) {} -void pwm_switch_duty_cycle_step(int delta) {} -bool pwm_switch_enabled() {return false;} - -#endif +#endif /* CHARGER_TYPE_PWM */ #endif /* UNIT_TEST */ diff --git a/src/pwm_switch.h b/src/pwm_switch.h index a501dc3..95f26ec 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -27,36 +27,31 @@ /** PWM charger type * - * Contains all data belonging to the PWM switching sub-component of the PCB, incl. - * actual measurements and calibration parameters. + * Contains all data belonging to the PWM switching sub-component. */ typedef struct { bool enabled; ///< Can be used to disable the PWM power stage - - // actual measurements - float solar_current; - float temp_mosfets; - float solar_power; - - float solar_current_max; // MOSFET maximum (continuous) - float solar_current_min; // --> if lower, charger is switched off - - int pwm_delta; // direction of PWM change - int off_timestamp; // time when charger was switched off last time - - // calibration parameters - float offset_voltage_start; // V charging switched on if Vsolar > Vbat + offset - float offset_voltage_stop; // V charging switched off if Vsolar < Vbat + offset - int restart_interval; // s When should we retry to start charging after low solar power cut-off? + 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) + int off_timestamp; ///< Time when charger was switched off last time } PwmSwitch; - +/** Initialization of PWM switch struct + */ void pwm_switch_init(PwmSwitch *pwm_switch); -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_port, DcBus *bat_port); - -void pwm_switch_duty_cycle_step(int delta); +/** Main control function for the PWM switching algorithm + * + * @param pwm_switch PWM switch struct + * @param solar_bus Solar connector DC bus struct + * @param bat_bus Battery connector DC bus struct + */ +void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus); +/** Read the on/off status of the PWM switch + * + * @returns true if enabled + */ bool pwm_switch_enabled(); /** Read the currently set duty cycle diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 7af97f6..c112aff 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -105,7 +105,11 @@ void uext_process_1s() oled.drawRect(66, 4, 2, 5, 1); // bar 5 // solar panel data - if (half_bridge_enabled() || pwm_switch_enabled()) { +#ifdef CHARGER_TYPE_PWM + if (pwm_switch_enabled()) { +#else + if (half_bridge_enabled()) { +#endif tmp = - hv_bus.voltage * hv_bus.current; oled.setTextCursor(0, 18); oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros