From 7412b11002fcd31373647e66c2255bd929bc4a83 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Mon, 4 Nov 2019 12:35:37 +0100 Subject: [PATCH 1/3] ADC filtering in separate function + unit-tests --- src/adc_dma.cpp | 40 +++++++++++++---------- src/adc_dma.h | 4 +++ src/pwm_switch.cpp | 11 +++++++ src/pwm_switch.h | 8 ++++- test/adc_dma_stub.cpp | 22 +++++++++---- test/adc_dma_stub.h | 6 ++-- test/{tests_adc.cpp => tests_adc_dma.cpp} | 35 ++++++++++++++++++-- test/tests_device_status.cpp | 1 + test/tests_power_port.cpp | 4 ++- 9 files changed, 99 insertions(+), 32 deletions(-) rename test/{tests_adc.cpp => tests_adc_dma.cpp} (69%) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 5a4b1a1..8a491a7 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -76,6 +76,27 @@ void calibrate_current_sensors() load_current_offset = -(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0; } +void adc_update_value(unsigned int pos) +{ + // low pass filter with filter constant c = 1/16 + // y(n) = c * x(n) + (c - 1) * y(n-1) + +#if FEATURE_PWM_SWITCH == 1 + if (pos == ADC_POS_V_SOLAR || pos == ADC_POS_I_SOLAR) { + // only read input voltage and current when switch is on or permanently off + if (pwm_switch.signal_high() || pwm_switch.active() == false) { + adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST); + } + } + else +#endif + { + // 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); + } +} + + //---------------------------------------------------------------------------- void update_measurements() { @@ -231,26 +252,9 @@ extern "C" void DMA1_Channel1_IRQHandler(void) { if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1 { - // low pass filter with filter constant c = 1/16 - // y(n) = c * x(n) + (c - 1) * y(n-1) -#ifdef CHARGER_TYPE_PWM for (unsigned int i = 0; i < NUM_ADC_CH; i++) { - if (i == ADC_POS_V_SOLAR || i == ADC_POS_I_SOLAR) { - // only read input voltage and current when switch is on or permanently off - if (GPIOB->IDR & GPIO_PIN_1 || pwm_switch_enabled() == false) { - adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST); - } - } - else { - adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST); - } + adc_update_value(i); } -#else - for (unsigned int i = 0; i < NUM_ADC_CH; i++) { - // adc_readings: 12-bit ADC values left-aligned in uint16_t - adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST); - } -#endif } DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers } diff --git a/src/adc_dma.h b/src/adc_dma.h index 5e3c4a4..a496707 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -50,4 +50,8 @@ void adc_setup(void); */ void dma_setup(void); +/** Read, filter and check raw ADC readings stored by DMA controller + */ +void adc_update_value(unsigned int pos); + #endif /* ADC_DMA */ diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index e1a3759..6c4f12d 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -108,6 +108,11 @@ void pwm_signal_stop() _pwm_active = false; } +bool pwm_signal_high() +{ + return (GPIOB->IDR & GPIO_PIN_1); +} + #else // dummy functions for unit tests @@ -117,6 +122,7 @@ 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() {;} +bool pwm_signal_high() { return false; } #endif @@ -125,6 +131,11 @@ bool PwmSwitch::active() return _pwm_active; } +bool PwmSwitch::signal_high() +{ + return pwm_signal_high(); +} + PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int) { terminal = pwm_terminal; diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 082c654..8b4ea34 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -41,12 +41,18 @@ public: */ void control(); - /** Read the on/off status of the PWM switch + /** Read the general on/off status of PWM switching * * @returns true if on */ bool active(); + /** Read the current high or low state of the PWM signal + * + * @returns true if high, false if low + */ + bool signal_high(); + /** Read the currently set duty cycle * * @returns Duty cycle between 0.0 and 1.0 diff --git a/test/adc_dma_stub.cpp b/test/adc_dma_stub.cpp index 4b13bdd..8f199da 100644 --- a/test/adc_dma_stub.cpp +++ b/test/adc_dma_stub.cpp @@ -22,14 +22,22 @@ #include #include +extern uint16_t adc_readings[NUM_ADC_CH]; extern uint32_t adc_filtered[NUM_ADC_CH]; - -void prepare_adc_readings(adc_values_t values) +void prepare_adc_readings(AdcValues values) { - adc_filtered[ADC_POS_VREF_MCU] = (uint32_t)(1.224 / 3.3 * 4096) << (4 + ADC_FILTER_CONST); - adc_filtered[ADC_POS_V_SOLAR] = (uint32_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST); - adc_filtered[ADC_POS_V_BAT] = (uint32_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST); - adc_filtered[ADC_POS_I_DCDC] = (uint32_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST); - adc_filtered[ADC_POS_I_LOAD] = (uint32_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST); + adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4; + adc_readings[ADC_POS_V_SOLAR] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << 4; + adc_readings[ADC_POS_V_BAT] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << 4; + adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4; + adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4; +} + +void prepare_adc_filtered() +{ + // initialize also filtered values + for (int i = 0; i < NUM_ADC_CH; i++) { + adc_filtered[i] = adc_readings[i] << ADC_FILTER_CONST; + } } diff --git a/test/adc_dma_stub.h b/test/adc_dma_stub.h index ec9bb33..558f1aa 100644 --- a/test/adc_dma_stub.h +++ b/test/adc_dma_stub.h @@ -25,6 +25,8 @@ typedef struct { float load_current; float bat_temperature; float internal_temperature; -} adc_values_t; +} AdcValues; -void prepare_adc_readings(adc_values_t values); +void prepare_adc_readings(AdcValues values); + +void prepare_adc_filtered(); diff --git a/test/tests_adc.cpp b/test/tests_adc_dma.cpp similarity index 69% rename from test/tests_adc.cpp rename to test/tests_adc_dma.cpp index b4a7f0b..fcfc265 100644 --- a/test/tests_adc.cpp +++ b/test/tests_adc_dma.cpp @@ -5,7 +5,34 @@ #include "main.h" -static adc_values_t adcval; +static AdcValues adcval; + +extern uint32_t adc_filtered[NUM_ADC_CH]; + +// testing only for 2 values +void check_filtering() +{ + // reset values + adc_filtered[ADC_POS_VREF_MCU] = 0; + adc_filtered[ADC_POS_V_SOLAR] = 0; + + // collect 1000 samples to update filtered values + for (int s = 0; s < 1000; s++) { + for (int i = 0; i < NUM_ADC_CH; i++) { + adc_update_value(i); + } + } + + uint32_t adc_filtered_bak[NUM_ADC_CH]; + adc_filtered_bak[ADC_POS_VREF_MCU] = adc_filtered[ADC_POS_VREF_MCU]; + adc_filtered_bak[ADC_POS_V_SOLAR] = adc_filtered[ADC_POS_V_SOLAR]; + + // overwrite filtered values + prepare_adc_filtered(); + + TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_VREF_MCU], adc_filtered_bak[ADC_POS_VREF_MCU]); + TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_V_SOLAR], adc_filtered_bak[ADC_POS_V_SOLAR]); +} void check_solar_terminal_readings() { @@ -53,11 +80,13 @@ void adc_tests() adcval.solar_voltage = 30; prepare_adc_readings(adcval); + UNITY_BEGIN(); + + RUN_TEST(check_filtering); + // call original update_measurements function update_measurements(); - UNITY_BEGIN(); - RUN_TEST(check_solar_terminal_readings); RUN_TEST(check_bat_terminal_readings); RUN_TEST(check_load_terminal_readings); diff --git a/test/tests_device_status.cpp b/test/tests_device_status.cpp index f0b6d57..5f39773 100644 --- a/test/tests_device_status.cpp +++ b/test/tests_device_status.cpp @@ -108,6 +108,7 @@ void dev_stat_new_bat_temp_max() void dev_stat_new_int_temp_max() { + dev_stat.int_temp_max = 20; dev_stat.internal_temp = 22; dev_stat.update_min_max_values(); TEST_ASSERT_EQUAL(22, dev_stat.int_temp_max); diff --git a/test/tests_power_port.cpp b/test/tests_power_port.cpp index 9b10d74..d3949f2 100644 --- a/test/tests_power_port.cpp +++ b/test/tests_power_port.cpp @@ -9,7 +9,7 @@ #include "main.h" -static adc_values_t adcval; +static AdcValues adcval; const float dcdc_current_sun = 3; const float load_current = 1; @@ -38,6 +38,7 @@ void energy_calculation_init() // insert values into ADC functions prepare_adc_readings(adcval); + prepare_adc_filtered(); update_measurements(); for (int i = 0; i < 60*60*sun_hours; i++) { @@ -49,6 +50,7 @@ void energy_calculation_init() // disable DC/DC = solar charging adcval.dcdc_current = 0; prepare_adc_readings(adcval); + prepare_adc_filtered(); update_measurements(); for (int i = 0; i < 60*60*night_hours; i++) { From 7874b8b4d67a603ee710fb9fe98aaab47e1e9a07 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Mon, 4 Nov 2019 18:11:55 +0100 Subject: [PATCH 2/3] 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. --- src/adc_dma.cpp | 75 ++++++++++++++++++++++++++++++++++++++++-- src/adc_dma.h | 15 +++++++++ src/bat_charger.cpp | 1 + src/dcdc.cpp | 8 +++++ src/dcdc.h | 6 ++++ src/load.cpp | 11 ++++++- src/load.h | 8 +++++ src/main.cpp | 2 ++ src/pwm_switch.cpp | 6 ++++ src/pwm_switch.h | 6 ++++ test/tests_adc_dma.cpp | 63 +++++++++++++++++++++++++++++++++++ 11 files changed, 198 insertions(+), 3 deletions(-) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 8a491a7..e1e2b73 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -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(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() diff --git a/src/adc_dma.h b/src/adc_dma.h index a496707..86bf73f 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -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 */ diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 5c6d008..1a375fc 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -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; diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 87a60fb..cd1d564 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -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"); diff --git a/src/dcdc.h b/src/dcdc.h index 1d7ac62..93c02de 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -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 diff --git a/src/load.cpp b/src/load.cpp index c5e2f34..a49f7b3 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -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; +} diff --git a/src/load.h b/src/load.h index 34a5fab..f7bc730 100644 --- a/src/load.h +++ b/src/load.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 2f43c44..c375c17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -165,6 +165,8 @@ int main() load.state_machine(); + adc_set_alerts(); // update regularly to cover changed configurations + eeprom_update(); leds_update_1s(); diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 6c4f12d..31f131d 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -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(); diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 8b4ea34..300b531 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -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 diff --git a/test/tests_adc_dma.cpp b/test/tests_adc_dma.cpp index fcfc265..2a68fc2 100644 --- a/test/tests_adc_dma.cpp +++ b/test/tests_adc_dma.cpp @@ -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(); } \ No newline at end of file From 5e3181f5e5f52662474121b20ea704b1cd75ae86 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Tue, 5 Nov 2019 18:43:33 +0100 Subject: [PATCH 3/3] 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 --- src/adc_dma.cpp | 92 ++++++++++++++++++++++-------------------- src/adc_dma.h | 22 +++++++--- src/main.cpp | 4 +- src/pwm_switch.cpp | 4 ++ test/tests_adc_dma.cpp | 11 +++-- 5 files changed, 77 insertions(+), 56 deletions(-) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index e1e2b73..b903915 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -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(adc_alerts[pos].callback); + void (*alert)(void) = reinterpret_cast(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(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 diff --git a/src/adc_dma.h b/src/adc_dma.h index 86bf73f..f8cdcb6 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -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 */ diff --git a/src/main.cpp b/src/main.cpp index c375c17..bb31d4e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 31f131d..bba7f65 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -18,6 +18,8 @@ #include "config.h" #include "pcb.h" +#include "adc_dma.h" + #include #include // 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"); } diff --git a/test/tests_adc_dma.cpp b/test/tests_adc_dma.cpp index 2a68fc2..358a4d0 100644 --- a/test/tests_adc_dma.cpp +++ b/test/tests_adc_dma.cpp @@ -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;