Distinguishing between OV and UV alerts, inhibit added

- During overvoltage alerts, only power sources (PWM switch and DC/DC are
  switched off), undervoltage only switches load off
- Alert inhibit added to prevent switching off during PWM switch startup
This commit is contained in:
Martin Jaeger
2019-11-05 18:43:33 +01:00
parent 7874b8b4d6
commit 5e3181f5e5
5 changed files with 77 additions and 56 deletions
+49 -43
View File
@@ -19,6 +19,7 @@
#endif
#include "main.h"
#include "debug.h"
#include "adc_dma.h"
#include "pcb.h" // contains defines for pins
@@ -95,30 +96,29 @@ void adc_update_value(unsigned int pos)
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
}
// check if limit violation alert callback is set for this ADC channel
if (adc_alerts[pos].callback != NULL) {
uint32_t err_flag = 0;
if (adc_readings[pos] > adc_alerts[pos].upper_limit) {
err_flag |= adc_alerts[pos].err_flag_upper;
adc_alerts[pos].debounce_counter++;
}
else if (adc_readings[pos] < adc_alerts[pos].lower_limit) {
err_flag |= adc_alerts[pos].err_flag_lower;
adc_alerts[pos].debounce_counter++;
}
else {
adc_alerts[pos].debounce_counter = 0;
}
// minimum debouncing that a single spike on ADC measurement doesn't trigger the alert
if (adc_alerts[pos].debounce_counter > 1) {
dev_stat.set_error(err_flag);
adc_alerts[pos].debounce_ms++;
if (adc_alerts[pos].callback_upper != NULL &&
adc_readings[pos] > adc_alerts[pos].upper_limit)
{
if (adc_alerts[pos].debounce_ms > 1) {
// create function pointer and call function
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback);
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback_upper);
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) {
// 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;
}
}
//----------------------------------------------------------------------------
@@ -230,7 +230,7 @@ void update_measurements()
// else: keep previous setting
}
void lv_voltage_alert()
void high_voltage_alert()
{
// disable any sort of input
#if FEATURE_DCDC_CONVERTER
@@ -242,39 +242,45 @@ void lv_voltage_alert()
// do not use enter_state function, as we don't want to wait entire recharge delay
charger.state = CHG_STATE_IDLE;
// disable load output
if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE)) {
load.emergency_stop(LOAD_STATE_OFF_OVERVOLTAGE);
}
else if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) {
load.emergency_stop(LOAD_STATE_OFF_SHORT_CIRCUIT);
}
else {
load.emergency_stop(LOAD_STATE_DISABLED);
}
charger.port->neg_current_limit = 0;
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);
}
void adc_set_alerts()
void low_voltage_alert()
{
// disable load output
load.emergency_stop(LOAD_STATE_OFF_OVERCURRENT);
charger.port->neg_current_limit = 0;
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);
}
void adc_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;
}
void adc_set_lv_alerts(float upper, float lower)
{
int vcc = VREFINT_VALUE * VREFINT_CAL /
(adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST));
// LV side (battery) overvoltage alert
adc_alerts[ADC_POS_V_BAT].upper_limit =
(uint16_t)((bat_conf.voltage_absolute_max * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
adc_alerts[ADC_POS_V_BAT].err_flag_upper = ERR_BAT_OVERVOLTAGE;
(uint16_t)((upper * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
adc_alerts[ADC_POS_V_BAT].callback_upper = (void *) &high_voltage_alert;
// LV side (battery) undervoltage alert
adc_alerts[ADC_POS_V_BAT].lower_limit =
(uint16_t)((bat_conf.voltage_absolute_min * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
adc_alerts[ADC_POS_V_BAT].err_flag_lower = ERR_BAT_UNDERVOLTAGE;
adc_alerts[ADC_POS_V_BAT].callback = (void *) &lv_voltage_alert;
// printf("v_max %.3f v_min %.3f upper: %u lower %u, vcc %d\n", bat_conf.voltage_absolute_max,
// bat_conf.voltage_absolute_min, adc_alerts[ADC_POS_V_BAT].upper_limit,
// adc_alerts[ADC_POS_V_BAT].lower_limit, vcc);
(uint16_t)((lower * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
adc_alerts[ADC_POS_V_BAT].callback_lower = (void *) &low_voltage_alert;
}
#ifndef UNIT_TEST
+16 -6
View File
@@ -31,12 +31,12 @@
/** Struct to definie upper and lower limit alerts for any ADC channel
*/
typedef struct {
void *callback = NULL; ///< Function to be called when limits are exceeded
//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
uint32_t err_flag_upper = 0; ///< Error flag to raise at upper limit
uint32_t err_flag_lower = 0; ///< Error flag to raise at lower limit
int debounce_counter; ///< Can be used to prevent triggering at fist occurence
int debounce_ms = 0; ///< Milliseconds delay for triggering alert
} AdcAlert;
/** Sets offset to actual measured value, i.e. sets zero current point.
@@ -65,8 +65,18 @@ void dma_setup(void);
*/
void adc_update_value(unsigned int pos);
/** Set limits where an alert should be triggered after reading the ADC values
/** Set lv side (battery) voltage limits where an alert should be triggered
*
* @param upper Upper voltage limit
* @param lower Lower voltage limit
*/
void adc_set_alerts();
void adc_set_lv_alerts(float upper, float lower);
/** Add an inhibit delay to the alerts to disable it temporarily
*
* @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);
#endif /* ADC_DMA */
+3 -1
View File
@@ -165,7 +165,9 @@ int main()
load.state_machine();
adc_set_alerts(); // update regularly to cover changed configurations
// update regularly to cover changed battery configurations
adc_set_lv_alerts(bat_conf.voltage_absolute_max * charger.num_batteries,
bat_conf.voltage_absolute_min * charger.num_batteries);
eeprom_update();
+4
View File
@@ -18,6 +18,8 @@
#include "config.h"
#include "pcb.h"
#include "adc_dma.h"
#include <stdio.h>
#include <time.h> // for time(NULL) function
@@ -203,6 +205,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);
pwm_signal_start(1);
printf("PWM charger start.\n");
}
+5 -6
View File
@@ -70,7 +70,7 @@ void adc_alert_undervoltage_triggering()
{
dev_stat.clear_error(ERR_ANY_ERROR);
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
adc_set_alerts();
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
@@ -81,9 +81,7 @@ void adc_alert_undervoltage_triggering()
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
adc_update_value(ADC_POS_V_BAT);
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
TEST_ASSERT_EQUAL(false, pwm_switch.active());
TEST_ASSERT_EQUAL(DCDC_STATE_OFF, dcdc.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
// reset values
adcval.battery_voltage = 13;
@@ -99,10 +97,12 @@ void adc_alert_overvoltage_triggering()
{
dev_stat.clear_error(ERR_ANY_ERROR);
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
adc_set_alerts();
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
dcdc.state = DCDC_STATE_MPPT;
// overvoltage test
adcval.battery_voltage = bat_conf.voltage_absolute_max + 0.1;
prepare_adc_readings(adcval);
@@ -112,7 +112,6 @@ void adc_alert_overvoltage_triggering()
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
TEST_ASSERT_EQUAL(false, pwm_switch.active());
TEST_ASSERT_EQUAL(DCDC_STATE_OFF, dcdc.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
// reset values
adcval.battery_voltage = 12;