ADC alerts and emergency stop functions added

Check for overvoltage/undervoltage conditions after each ADC reading cycle and
trigger alert (stopping DC/DC, PWM switch and load output) if limits exceeded.
This commit is contained in:
Martin Jaeger
2019-11-04 18:12:00 +01:00
parent 7412b11002
commit 7874b8b4d6
11 changed files with 198 additions and 3 deletions
+73 -2
View File
@@ -59,7 +59,7 @@ 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 int num_adc_conversions;
volatile AdcAlert adc_alerts[NUM_ADC_CH] = {0};
extern DeviceStatus dev_stat;
@@ -94,8 +94,32 @@ void adc_update_value(unsigned int pos)
// adc_readings: 12-bit ADC values left-aligned in uint16_t
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);
// create function pointer and call function
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback);
alert();
}
}
}
//----------------------------------------------------------------------------
void update_measurements()
@@ -206,6 +230,53 @@ void update_measurements()
// else: keep previous setting
}
void lv_voltage_alert()
{
// disable any sort of input
#if FEATURE_DCDC_CONVERTER
dcdc.emergency_stop();
#endif
#if FEATURE_PWM_SWITCH
pwm_switch.emergency_stop();
#endif
// 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;
}
void adc_set_alerts()
{
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;
// 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);
}
#ifndef UNIT_TEST
void dma_setup()
+15
View File
@@ -28,6 +28,17 @@
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
/** 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
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
} AdcAlert;
/** Sets offset to actual measured value, i.e. sets zero current point.
*
* All input/output switches and consumers should be switched off before calling this function
@@ -54,4 +65,8 @@ void dma_setup(void);
*/
void adc_update_value(unsigned int pos);
/** Set limits where an alert should be triggered after reading the ADC values
*/
void adc_set_alerts();
#endif /* ADC_DMA */
+1
View File
@@ -351,6 +351,7 @@ void Charger::charge_control(BatConf *bat_conf)
full = false;
dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP);
dev_stat.clear_error(ERR_BAT_CHG_UNDERTEMP);
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
enter_state(CHG_STATE_BULK);
}
break;
+8
View File
@@ -137,6 +137,7 @@ int Dcdc::check_start_conditions()
hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing
lvs->voltage > ls_voltage_max ||
lvs->voltage < ls_voltage_min ||
dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) ||
time(NULL) < (off_timestamp + restart_interval))
{
return 0; // no energy transfer allowed
@@ -269,6 +270,13 @@ void Dcdc::test()
}
}
void Dcdc::emergency_stop()
{
half_bridge_stop();
state = DCDC_STATE_OFF;
off_timestamp = time(NULL);
}
void Dcdc::self_destruction()
{
print_error("Charge controller self-destruction called!\n");
+6
View File
@@ -88,6 +88,12 @@ public:
*/
void test();
/** Fast emergency stop function
*
* May be called from an ISR which detected overvoltage / overcurrent conditions
*/
void emergency_stop();
/** Prevent overcharging of battery in case of shorted HS MOSFET
*
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
+10 -1
View File
@@ -260,7 +260,9 @@ void LoadOutput::state_machine()
switch (state) {
case LOAD_STATE_DISABLED:
if (enable == true) {
if (port->pos_current_limit > 0) {
if (port->pos_current_limit > 0 &&
!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE))
{
switch_set(true);
state = LOAD_STATE_ON;
}
@@ -407,3 +409,10 @@ void LoadOutput::control()
debounce_counter = 0;
}
}
void LoadOutput::emergency_stop(uint16_t next_state)
{
switch_set(false);
leds_flicker(LED_LOAD);
state = next_state;
}
+8
View File
@@ -80,6 +80,14 @@ public:
*/
void control();
/** Fast emergency stop function
*
* May be called from an ISR which detected overvoltage / overcurrent conditions
*
* @param next_state Select state the load should go to
*/
void emergency_stop(uint16_t next_state = LOAD_STATE_DISABLED);
uint16_t state; ///< Current state of load output switch
uint16_t usb_state; ///< Current state of USB output
+2
View File
@@ -165,6 +165,8 @@ int main()
load.state_machine();
adc_set_alerts(); // update regularly to cover changed configurations
eeprom_update();
leds_update_1s();
+6
View File
@@ -209,6 +209,12 @@ void PwmSwitch::control()
}
}
void PwmSwitch::emergency_stop()
{
pwm_signal_stop();
off_timestamp = time(NULL);
}
float PwmSwitch::get_duty_cycle()
{
return pwm_signal_get_duty_cycle();
+6
View File
@@ -41,6 +41,12 @@ public:
*/
void control();
/** Fast emergency stop function
*
* May be called from an ISR which detected overvoltage / overcurrent conditions
*/
void emergency_stop();
/** Read the general on/off status of PWM switching
*
* @returns true if on
+63
View File
@@ -8,6 +8,7 @@
static AdcValues adcval;
extern uint32_t adc_filtered[NUM_ADC_CH];
extern AdcAlert adc_alerts[NUM_ADC_CH];
// testing only for 2 values
void check_filtering()
@@ -65,6 +66,65 @@ void check_temperature_readings()
TEST_ASSERT_EQUAL_FLOAT(adcval.bat_temperature, round(charger.bat_temperature * 10) / 10);
}
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();
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
// undervoltage test
adcval.battery_voltage = bat_conf.voltage_absolute_min - 0.1;
prepare_adc_readings(adcval);
adc_update_value(ADC_POS_V_BAT);
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);
// reset values
adcval.battery_voltage = 13;
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
charger.discharge_control(&bat_conf);
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
}
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();
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
// overvoltage test
adcval.battery_voltage = bat_conf.voltage_absolute_max + 0.1;
prepare_adc_readings(adcval);
adc_update_value(ADC_POS_V_BAT);
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
adc_update_value(ADC_POS_V_BAT);
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;
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
charger.charge_control(&bat_conf);
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
}
/** ADC conversion test
*
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
@@ -94,5 +154,8 @@ void adc_tests()
//RUN_TEST(check_temperature_readings); // TODO
RUN_TEST(adc_alert_undervoltage_triggering);
RUN_TEST(adc_alert_overvoltage_triggering);
UNITY_END();
}