PWM switch also implemented as class

This commit is contained in:
Martin Jaeger
2019-10-20 22:01:45 +02:00
parent b5c7f396e1
commit 8558cef9cb
8 changed files with 208 additions and 162 deletions
+11 -6
View File
@@ -99,11 +99,15 @@ void update_measurements()
(float)(((adc_filtered[ADC_POS_V_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
ADC_GAIN_V_BAT / 1000.0;
#if FEATURE_DCDC_CONVERTER
hv_bus.voltage =
(float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
ADC_GAIN_V_SOLAR / 1000.0;
#ifdef ADC_OFFSET_V_SOLAR
hv_bus.voltage = hv_bus.voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hv_bus.voltage);
#endif
#if FEATURE_PWM_SWITCH
pwm_sw_ext.voltage = lv_bus.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;
#endif
load_terminal.current =
@@ -112,10 +116,12 @@ void update_measurements()
#if FEATURE_PWM_SWITCH
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation
hv_terminal.current = - pwm_switch_get_duty_cycle() * (
pwm_terminal.current = - pwm_switch.get_duty_cycle() * (
(float)(((adc_filtered[ADC_POS_I_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset);
lv_terminal.current = -hv_terminal.current - load_terminal.current;
pwm_port_int.current = pwm_terminal.current;
lv_terminal.current = -pwm_port_int.current - load_terminal.current;
pwm_terminal.power = pwm_sw_ext.voltage * pwm_terminal.current;
#endif
#if FEATURE_DCDC_CONVERTER
dcdc_port_lv.current =
@@ -127,9 +133,8 @@ void update_measurements()
dcdc_port_lv.power = lv_bus.voltage * dcdc_port_lv.current;
dcdc_port_hv.power = hv_bus.voltage * dcdc_port_hv.current;
#endif
hv_terminal.power = hv_bus.voltage * hv_terminal.current;
#endif
lv_terminal.power = lv_bus.voltage * lv_terminal.current;
load_terminal.power = lv_bus.voltage * load_terminal.current;
+3
View File
@@ -48,6 +48,9 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode)
restart_interval = 60;
off_timestamp = -10000; // start immediately
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / hs_voltage_max, 0.97);
}
int Dcdc::duty_cycle_delta()
+47 -54
View File
@@ -54,9 +54,13 @@ Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT);
#endif
#if FEATURE_PWM_SWITCH
DcBus hv_bus;
PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT)
PwmSwitch pwm_switch = {};
DcBus pwm_sw_ext;
PowerPort pwm_terminal(&pwm_sw_ext); // external terminal of PWM switch port (normally solar)
PowerPort pwm_port_int(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int);
PowerPort &solar_terminal = pwm_terminal;
#else
PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h
#endif
#if FEATURE_LOAD_OUTPUT
@@ -64,9 +68,7 @@ PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_b
LoadOutput load(&load_terminal);
#endif
// configure actual terminal connections as defined in config.h
PowerPort &bat_terminal = BATTERY_TERMINAL;
PowerPort &solar_terminal = SOLAR_TERMINAL;
PowerPort &bat_terminal = BATTERY_TERMINAL; // defined in config.h
#ifdef GRID_TERMINAL
PowerPort &grid_terminal = GRID_TERMINAL;
#endif
@@ -84,45 +86,6 @@ time_t timestamp; // current unix timestamp (independent of time(NULL), as it
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200);
/** High priority function for DC/DC control and safety functions
*
* Called by control timer with 10 Hz frequency (see hardware.cpp).
*/
void system_control()
{
static int counter = 0;
// convert ADC readings to meaningful measurement values
update_measurements();
#if FEATURE_PWM_SWITCH
pwm_switch_control(&pwm_switch, &hv_terminal, &bat_terminal);
leds_set_charging(pwm_switch_enabled());
#endif
#if FEATURE_DCDC_CONVERTER
dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
#endif
load.control(bat_conf.voltage_absolute_max * charger.num_batteries);
if (counter % CONTROL_FREQUENCY == 0) {
// called once per second (this timer is much more accurate than time(NULL) based on LSI)
// see also here: https://github.com/ARMmbed/mbed-os/issues/9065
timestamp++;
counter = 0;
// energy + soc calculation must be called exactly once per second
solar_terminal.energy_balance();
bat_terminal.energy_balance();
load_terminal.energy_balance();
log_update_energy(&log_data);
log_update_min_max_values(&log_data);
charger.update_soc(&bat_conf);
}
counter++;
}
/** Main function including initialization and continuous loop
*/
int main()
@@ -132,15 +95,6 @@ int main()
battery_conf_init(&bat_conf, BATTERY_TYPE, BATTERY_NUM_CELLS, BATTERY_CAPACITY);
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
#if FEATURE_DCDC_CONVERTER
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / dcdc.hs_voltage_max, 0.97);
#endif
#if FEATURE_PWM_SWITCH
pwm_switch_init(&pwm_switch);
#endif
// Configuration from EEPROM
data_objects_read_eeprom();
ts.set_conf_callback(data_objects_update_conf); // after each configuration change, data should be written back to EEPROM
@@ -218,4 +172,43 @@ int main()
}
}
/** High priority function for DC/DC / PWM control and safety functions
*
* Called by control timer with 10 Hz frequency (see hardware.cpp).
*/
void system_control()
{
static int counter = 0;
// convert ADC readings to meaningful measurement values
update_measurements();
#if FEATURE_PWM_SWITCH
pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
#if FEATURE_DCDC_CONVERTER
dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
#endif
load.control(bat_conf.voltage_absolute_max * charger.num_batteries);
if (counter % CONTROL_FREQUENCY == 0) {
// called once per second (this timer is much more accurate than time(NULL) based on LSI)
// see also here: https://github.com/ARMmbed/mbed-os/issues/9065
timestamp++;
counter = 0;
// energy + soc calculation must be called exactly once per second
solar_terminal.energy_balance();
bat_terminal.energy_balance();
load_terminal.energy_balance();
log_update_energy(&log_data);
log_update_min_max_values(&log_data);
charger.update_soc(&bat_conf);
}
counter++;
}
#endif
+16 -11
View File
@@ -24,16 +24,27 @@
#include "pwm_switch.h"
#include "half_bridge.h"
#include "load.h"
extern DcBus hv_bus;
extern PowerPort dcdc_port_hv;
extern PowerPort hv_terminal;
#include "pcb.h"
extern DcBus lv_bus;
extern PowerPort dcdc_port_lv;
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 Dcdc dcdc;
#endif
#if FEATURE_PWM_SWITCH
extern DcBus pwm_sw_ext;
extern PowerPort pwm_terminal;
extern PowerPort pwm_port_int;
extern PwmSwitch pwm_switch;
#endif
extern PowerPort &bat_terminal;
extern PowerPort &solar_terminal;
@@ -42,10 +53,4 @@ extern Charger charger;
extern BatConf bat_conf;
extern BatConf bat_conf_user;
#ifndef CHARGER_TYPE_PWM
extern Dcdc dcdc;
#endif
extern LoadOutput load;
extern PwmSwitch pwm_switch;
+9 -2
View File
@@ -22,9 +22,11 @@
// specify features of charge controller
#define FEATURE_DCDC_CONVERTER 1
#define FEATURE_PWM_SWITCH 0
#define FEATURE_PWM_SWITCH 1
#define FEATURE_LOAD_OUTPUT 1
#define PWM_FREQUENCY 50 // kHz
#define PWM_DEADTIME 230 // ns
#define PWM_TIM 3 // use TIM3 timer
#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current
@@ -85,6 +87,12 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset
// trick to pass unit tests for MPPT
#define ADC_POS_I_SOLAR ADC_POS_I_DCDC
#define ADC_GAIN_I_SOLAR ADC_GAIN_I_DCDC
// position in the array written by the DMA controller
enum {
ADC_POS_V_BAT, // ADC 0 (PA_0)
@@ -97,5 +105,4 @@ enum {
NUM_ADC_CH // trick to get the number of elements
};
#endif
+71 -53
View File
@@ -14,21 +14,21 @@
* limitations under the License.
*/
#ifndef UNIT_TEST
#include "pwm_switch.h"
#include "config.h"
#include "pcb.h"
#include <stdio.h>
#include <time.h> // for time(NULL) function
#if FEATURE_PWM_SWITCH
static int _pwm_resolution;
static bool _pwm_active;
static bool _enabled;
#ifndef UNIT_TEST
void pwm_switch_init_registers(int freq_Hz)
void pwm_signal_init_registers(int freq_Hz)
{
// Enable peripheral clock of GPIOB
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
@@ -69,31 +69,26 @@ void pwm_switch_init_registers(int freq_Hz)
TIM3->ARR = _pwm_resolution - 1;
}
void pwm_switch_set_duty_cycle(float duty)
void pwm_signal_set_duty_cycle(float duty)
{
TIM3->CCR4 = _pwm_resolution * duty;
}
void pwm_switch_duty_cycle_step(int delta)
void pwm_signal_duty_cycle_step(int delta)
{
if ((int)TIM3->CCR4 + delta <= _pwm_resolution && (int)TIM3->CCR4 + delta >= 0) {
TIM3->CCR4 += delta;
}
}
bool pwm_switch_enabled()
{
return _enabled;
}
float pwm_switch_get_duty_cycle()
float pwm_signal_get_duty_cycle()
{
return (float)(TIM3->CCR4) / _pwm_resolution;
}
void pwm_switch_start(float pwm_duty)
void pwm_signal_start(float pwm_duty)
{
pwm_switch_set_duty_cycle(pwm_duty);
pwm_signal_set_duty_cycle(pwm_duty);
// Capture/Compare Enable Register
// CCxE = 1: Enable the output on OCx
@@ -102,86 +97,109 @@ void pwm_switch_start(float pwm_duty)
// CCxNP = 0: Active high polarity on OC1N (default)
TIM3->CCER |= TIM_CCER_CC4E;
_enabled = true;
_pwm_active = true;
}
void pwm_switch_stop()
void pwm_signal_stop()
{
TIM3->CCER &= ~(TIM_CCER_CC4E);
TIM3->CCR4 = 0;
_enabled = false;
_pwm_active = false;
}
void pwm_switch_init(PwmSwitch *pwm_switch)
#else
// dummy functions for unit tests
float pwm_signal_get_duty_cycle() { return 0; }
void pwm_signal_set_duty_cycle(float duty) {;}
void pwm_signal_duty_cycle_step(int delta) {;}
void pwm_signal_init_registers(int freq_Hz) {;}
void pwm_signal_start(float pwm_duty) {;}
void pwm_signal_stop() {;}
#endif
bool PwmSwitch::active()
{
pwm_switch->off_timestamp = -10000; // start immediately
return _pwm_active;
}
PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int)
{
terminal = pwm_terminal;
port_int = pwm_port_int;
off_timestamp = -10000; // start immediately
// calibration parameters
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?
offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset
restart_interval = 60; // s When should we retry to start charging after low solar power cut-off?
pwm_switch_init_registers(PWM_FREQUENCY);
pwm_signal_init_registers(PWM_FREQUENCY);
pwm_switch->enabled = true; // enable charging in general
_enabled = false; // still disable actual switch
enabled = true; // enable charging in general
_pwm_active = false; // still disable actual switch
}
void pwm_switch_control(PwmSwitch *pwm_switch, PowerPort *solar, PowerPort *bat)
void PwmSwitch::control()
{
if (_enabled) {
if (bat->chg_current_limit == 0 || solar->dis_current_limit == 0
|| solar->current > 0 // discharging battery into solar panel --> stop
|| pwm_switch->enabled == false)
if (_pwm_active) {
if (port_int->chg_current_limit == 0 || terminal->dis_current_limit == 0
|| terminal->current > 0 // discharging battery into solar panel --> stop
|| enabled == false)
{
pwm_switch_stop();
pwm_switch->off_timestamp = time(NULL);
pwm_signal_stop();
off_timestamp = time(NULL);
printf("PWM charger stop.\n");
}
else if (bat->bus->voltage > (bat->bus->chg_voltage_target - bat->chg_droop_res * bat->current) // output voltage above target
|| bat->current > bat->chg_current_limit // output current limit exceeded
|| solar->current < solar->dis_current_limit) // input current (negative signs) above limit
else if (port_int->bus->voltage > (port_int->bus->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
{
// 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) {
if (pwm_signal_get_duty_cycle() > 0.95) {
// prevent very short off periods
pwm_switch_set_duty_cycle(0.95);
pwm_signal_set_duty_cycle(0.95);
}
else if (pwm_switch_get_duty_cycle() < 0.05) {
else if (pwm_signal_get_duty_cycle() < 0.05) {
// prevent very short on periods and switch completely off instead
pwm_switch_stop();
pwm_switch->off_timestamp = time(NULL);
pwm_signal_stop();
off_timestamp = time(NULL);
printf("PWM charger stop, no further derating possible.\n");
}
else {
pwm_switch_duty_cycle_step(-1);
pwm_signal_duty_cycle_step(-1);
}
}
else {
if (pwm_switch_get_duty_cycle() > 0.95) {
if (pwm_signal_get_duty_cycle() > 0.95) {
// prevent very short off periods and switch completely on instead
pwm_switch_set_duty_cycle(1);
pwm_signal_set_duty_cycle(1);
}
else {
pwm_switch_duty_cycle_step(1);
pwm_signal_duty_cycle_step(1);
}
}
}
else {
if (bat->chg_current_limit > 0 // charging allowed
&& bat->bus->voltage < bat->bus->chg_voltage_target
&& bat->bus->voltage > bat->bus->chg_voltage_min
&& solar->dis_current_limit < 0 // dischraging allowed
&& 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)
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
&& terminal->dis_current_limit < 0 // dischraging allowed
&& terminal->bus->voltage > port_int->bus->voltage + offset_voltage_start
&& time(NULL) > (off_timestamp + restart_interval)
&& enabled == true)
{
pwm_switch_start(1);
pwm_signal_start(1);
printf("PWM charger start.\n");
}
}
}
#endif /* FEATURE_PWM_SWITCH */
float PwmSwitch::get_duty_cycle()
{
return pwm_signal_get_duty_cycle();
}
#endif /* UNIT_TEST */
#endif /* FEATURE_PWM_SWITCH */
+27 -26
View File
@@ -30,35 +30,36 @@
*
* Contains all data belonging to the PWM switching sub-component.
*/
typedef struct {
class PwmSwitch
{
public:
/** Initialization of PWM switch struct
*/
PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int);
/** Main control function for the PWM switching algorithm
*/
void control();
/** Read the on/off status of the PWM switch
*
* @returns true if on
*/
bool active();
/** Read the currently set duty cycle
*
* @returns Duty cycle between 0.0 and 1.0
*/
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)
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)
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);
/** 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, PowerPort *solar_bus, PowerPort *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
*
* @returns Duty cycle between 0.0 and 1.0
*/
float pwm_switch_get_duty_cycle();
};
#endif /* PWM_SWITCH_H */
+24 -10
View File
@@ -17,24 +17,38 @@
#include "tests.h"
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus); // 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
DcBus lv_bus;
PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter
PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT)
PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus)
PowerPort &bat_terminal = lv_terminal;
PowerPort &solar_terminal = hv_terminal;
Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT);
#endif
PwmSwitch pwm_switch = {}; // only necessary for PWM charger
#if FEATURE_PWM_SWITCH
DcBus pwm_sw_ext;
PowerPort pwm_terminal(&pwm_sw_ext); // external terminal of PWM switch port (normally solar)
PowerPort pwm_port_int(&lv_bus); // internal side of PWM switch
PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int);
PowerPort &solar_terminal = pwm_terminal;
#else
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)
LoadOutput load(&load_terminal);
#endif
PowerPort &bat_terminal = BATTERY_TERMINAL; // defined in config.h
#ifdef GRID_TERMINAL
PowerPort &grid_terminal = GRID_TERMINAL;
#endif
Charger charger(&lv_terminal);
LoadOutput load(&load_terminal);
BatConf bat_conf; // actual (used) battery configuration
BatConf bat_conf_user; // temporary storage where the user can write to