Overvoltage alert improved for PWM switching
Previously, the alert was sometimes erroneously triggered in PWM charger absorption phase (constant voltage, PWM switching). Now the overvoltage alert is inhibited during turn-on in each PWM cycle by an interrupt.
This commit is contained in:
+34
-25
@@ -60,7 +60,8 @@ float load_current_offset;
|
||||
// for ADC and DMA
|
||||
volatile uint16_t adc_readings[NUM_ADC_CH] = {0};
|
||||
volatile uint32_t adc_filtered[NUM_ADC_CH] = {0};
|
||||
volatile AdcAlert adc_alerts[NUM_ADC_CH] = {0};
|
||||
volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {0};
|
||||
volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {0};
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
@@ -79,8 +80,9 @@ void calibrate_current_sensors()
|
||||
|
||||
void adc_update_value(unsigned int pos)
|
||||
{
|
||||
// low pass filter with filter constant c = 1/16
|
||||
// low pass filter with filter constant c = 1/(2^ADC_FILTER_CONST)
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
// see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf
|
||||
|
||||
#if FEATURE_PWM_SWITCH == 1
|
||||
if (pos == ADC_POS_V_SOLAR || pos == ADC_POS_I_SOLAR) {
|
||||
@@ -96,28 +98,35 @@ void adc_update_value(unsigned int pos)
|
||||
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
|
||||
}
|
||||
|
||||
adc_alerts[pos].debounce_ms++;
|
||||
if (adc_alerts[pos].callback_upper != NULL &&
|
||||
adc_readings[pos] > adc_alerts[pos].upper_limit)
|
||||
// check upper alerts
|
||||
adc_alerts_upper[pos].debounce_ms++;
|
||||
if (adc_alerts_upper[pos].callback != NULL &&
|
||||
adc_readings[pos] > adc_alerts_upper[pos].limit)
|
||||
{
|
||||
if (adc_alerts[pos].debounce_ms > 1) {
|
||||
if (adc_alerts_upper[pos].debounce_ms > 1) {
|
||||
// create function pointer and call function
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback_upper);
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts_upper[pos].callback);
|
||||
alert();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts[pos].callback_lower != NULL &&
|
||||
adc_readings[pos] < adc_alerts[pos].lower_limit)
|
||||
{
|
||||
if (adc_alerts[pos].debounce_ms > 1) {
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback_lower);
|
||||
alert();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts[pos].debounce_ms > 0) {
|
||||
else if (adc_alerts_upper[pos].debounce_ms > 0) {
|
||||
// reset debounce ms counter only if already close to triggering to allow setting negative
|
||||
// values to specify a one-time inhibit delay
|
||||
adc_alerts[pos].debounce_ms = 0;
|
||||
adc_alerts_upper[pos].debounce_ms = 0;
|
||||
}
|
||||
|
||||
// same for lower alerts
|
||||
adc_alerts_lower[pos].debounce_ms++;
|
||||
if (adc_alerts_lower[pos].callback != NULL &&
|
||||
adc_readings[pos] < adc_alerts_lower[pos].limit)
|
||||
{
|
||||
if (adc_alerts_lower[pos].debounce_ms > 1) {
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts_lower[pos].callback);
|
||||
alert();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts_lower[pos].debounce_ms > 0) {
|
||||
adc_alerts_lower[pos].debounce_ms = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +256,7 @@ void high_voltage_alert()
|
||||
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
|
||||
|
||||
print_error("High voltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts[ADC_POS_V_BAT].upper_limit);
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts_upper[ADC_POS_V_BAT].limit);
|
||||
}
|
||||
|
||||
void low_voltage_alert()
|
||||
@@ -259,14 +268,14 @@ void low_voltage_alert()
|
||||
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
|
||||
|
||||
print_error("Low voltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts[ADC_POS_V_BAT].lower_limit);
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts_lower[ADC_POS_V_BAT].limit);
|
||||
}
|
||||
|
||||
void adc_alert_inhibit(int adc_pos, int timeout_ms)
|
||||
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms)
|
||||
{
|
||||
// set negative value so that we get a final debouncing of this timeout + the original
|
||||
// delay in the alert function (currently only waiting for 2 samples = 2 ms)
|
||||
adc_alerts[adc_pos].debounce_ms = -timeout_ms;
|
||||
adc_alerts_upper[adc_pos].debounce_ms = -timeout_ms;
|
||||
}
|
||||
|
||||
void adc_set_lv_alerts(float upper, float lower)
|
||||
@@ -275,14 +284,14 @@ void adc_set_lv_alerts(float upper, float lower)
|
||||
(adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST));
|
||||
|
||||
// LV side (battery) overvoltage alert
|
||||
adc_alerts[ADC_POS_V_BAT].upper_limit =
|
||||
adc_alerts_upper[ADC_POS_V_BAT].limit =
|
||||
(uint16_t)((upper * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
|
||||
adc_alerts[ADC_POS_V_BAT].callback_upper = (void *) &high_voltage_alert;
|
||||
adc_alerts_upper[ADC_POS_V_BAT].callback = (void *) &high_voltage_alert;
|
||||
|
||||
// LV side (battery) undervoltage alert
|
||||
adc_alerts[ADC_POS_V_BAT].lower_limit =
|
||||
adc_alerts_lower[ADC_POS_V_BAT].limit =
|
||||
(uint16_t)((lower * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
|
||||
adc_alerts[ADC_POS_V_BAT].callback_lower = (void *) &low_voltage_alert;
|
||||
adc_alerts_lower[ADC_POS_V_BAT].callback = (void *) &low_voltage_alert;
|
||||
}
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
+4
-7
@@ -31,12 +31,9 @@
|
||||
/** Struct to definie upper and lower limit alerts for any ADC channel
|
||||
*/
|
||||
typedef struct {
|
||||
//bool enable = false; ///< Can be used to temporarily disable this alert
|
||||
void *callback_upper = NULL; ///< Function to be called when limits are exceeded
|
||||
void *callback_lower = NULL; ///< Function to be called when limits are exceeded
|
||||
uint16_t upper_limit = UINT16_MAX; ///< ADC reading for upper limit
|
||||
uint16_t lower_limit = 0; ///< ADC reading for lower limit
|
||||
int debounce_ms = 0; ///< Milliseconds delay for triggering alert
|
||||
void *callback = NULL; ///< Function to be called when limits are exceeded
|
||||
uint16_t limit = 0; ///< ADC reading for lower limit
|
||||
int debounce_ms = 0; ///< Milliseconds delay for triggering alert
|
||||
} AdcAlert;
|
||||
|
||||
/** Sets offset to actual measured value, i.e. sets zero current point.
|
||||
@@ -77,6 +74,6 @@ void adc_set_lv_alerts(float upper, float lower);
|
||||
* @param adc_pos The position of the ADC measurement channel
|
||||
* @param timeout_ms Timeout in milliseconds
|
||||
*/
|
||||
void adc_alert_inhibit(int adc_pos, int timeout_ms);
|
||||
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms);
|
||||
|
||||
#endif /* ADC_DMA */
|
||||
|
||||
@@ -337,6 +337,12 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) &&
|
||||
port->voltage < (bat_conf->voltage_absolute_max - 0.5) * num_batteries)
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
}
|
||||
|
||||
// state machine
|
||||
switch (state) {
|
||||
case CHG_STATE_IDLE: {
|
||||
|
||||
+24
-5
@@ -58,9 +58,15 @@ void pwm_signal_init_registers(int freq_Hz)
|
||||
// CCxP: Active high polarity on OCx (default = 0)
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4P); // PB1 / TIM3_CH4: high-side
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // edge-aligned mode
|
||||
// Interrupt on timer update
|
||||
TIM3->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM3->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM3_IRQn, 1);
|
||||
NVIC_EnableIRQ(TIM3_IRQn);
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
@@ -73,6 +79,14 @@ void pwm_signal_init_registers(int freq_Hz)
|
||||
TIM3->ARR = _pwm_resolution - 1;
|
||||
}
|
||||
|
||||
extern "C" void TIM3_IRQHandler(void)
|
||||
{
|
||||
TIM3->SR &= ~TIM_SR_UIF; // clear update interrupt flag
|
||||
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
|
||||
adc_upper_alert_inhibit(ADC_POS_V_BAT, 10);
|
||||
}
|
||||
|
||||
void pwm_signal_set_duty_cycle(float duty)
|
||||
{
|
||||
TIM3->CCR4 = _pwm_resolution * duty;
|
||||
@@ -94,6 +108,10 @@ void pwm_signal_start(float pwm_duty)
|
||||
{
|
||||
pwm_signal_set_duty_cycle(pwm_duty);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // edge-aligned mode
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CCxE = 1: Enable the output on OCx
|
||||
// CCxP = 0: Active high polarity on OCx (default)
|
||||
@@ -106,6 +124,7 @@ void pwm_signal_start(float pwm_duty)
|
||||
|
||||
void pwm_signal_stop()
|
||||
{
|
||||
TIM3->CR1 &= ~(TIM_CR1_CEN);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
TIM3->CCR4 = 0;
|
||||
_pwm_active = false;
|
||||
@@ -212,8 +231,8 @@ void PwmSwitch::control()
|
||||
&& time(NULL) > (off_timestamp + restart_interval)
|
||||
&& enabled == true)
|
||||
{
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 100 ms
|
||||
adc_alert_inhibit(ADC_POS_V_BAT, 100);
|
||||
// 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);
|
||||
pwm_signal_start(1);
|
||||
print_info("PWM charger start.\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user