Load protection improvements

- Switched back to older mbed version, as update broke comparator function
- Added voltage dip detection to disconnect load if charge controller is connected to small
  battery or with bad wires and short circuit detection doesn't trigger
- Added more error flags to diagnose reasons for load disconnect
This commit is contained in:
Martin Jaeger
2019-10-30 17:31:16 +01:00
parent 975c03affd
commit 5b4df98656
6 changed files with 97 additions and 31 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ default_envs =
[env]
# select specific stm32 version, as automatic updates of mbed could break compatibility
platform = ststm32@~5.6.0
platform = ststm32@~5.3.0
#platform = https://github.com/platformio/platform-ststm32.git
framework = mbed
+5
View File
@@ -264,4 +264,9 @@ void leds_update_soc(int soc, bool load_off_low_soc)
#endif
}
#else
// dummy functions
void leds_flicker(int led, int timeout) {}
#endif /* UNIT_TEST */
+52 -16
View File
@@ -20,6 +20,7 @@
#include "hardware.h"
#include "leds.h"
#include "log.h"
#include "debug.h"
volatile bool short_circuit = false;
@@ -97,7 +98,7 @@ static void short_circuit_comp_init()
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
// select VREFINT divider as negative input
COMP2->CSR |= COMP_INPUT_MINUS_VREFINT;
COMP2->CSR |= COMP_INPUT_MINUS_1_4VREFINT;
// propagate comparator value to LPTIM input
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
@@ -149,19 +150,29 @@ void LoadOutput::switch_set(bool enabled)
#ifdef PIN_LOAD_EN
DigitalOut load_enable(PIN_LOAD_EN);
if (enabled) load_enable = 1;
else load_enable = 0;
if (enabled) {
load_enable = 1;
}
else {
load_enable = 0;
}
#endif
#ifdef PIN_LOAD_DIS
DigitalOut load_disable(PIN_LOAD_DIS);
#ifdef PIN_I_LOAD_COMP
if (enabled) lptim_init();
#else
if (enabled) load_disable = 0;
#endif
else load_disable = 1;
printf("Load enabled = %d\n", enabled);
#ifdef PIN_I_LOAD_COMP
if (enabled) {
lptim_init();
}
#else
if (enabled) {
load_disable = 0;
}
#endif
else {
load_disable = 1;
}
print_info("Load enabled = %d\n", enabled);
#endif
}
@@ -251,7 +262,6 @@ void LoadOutput::usb_state_machine()
void LoadOutput::state_machine()
{
//printf("Load State: %d\n", state);
switch (state) {
case LOAD_STATE_DISABLED:
if (enable == true) {
@@ -289,14 +299,16 @@ void LoadOutput::state_machine()
}
break;
case LOAD_STATE_OFF_LOW_SOC:
// wait at least one hour
// wait configured time
if (time(NULL) - lvd_timestamp > lvd_recovery_delay) {
state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
case LOAD_STATE_OFF_OVERCURRENT:
// wait some time
// wait configured time
if (time(NULL) - overcurrent_timestamp > overcurrent_recovery_delay) {
log_data.error_flags &= ~(1 << ERR_LOAD_OVERCURRENT);
log_data.error_flags &= ~(1 << ERR_LOAD_VOLTAGE_DIP);
state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -304,6 +316,7 @@ void LoadOutput::state_machine()
if (port->voltage < (port->sink_voltage_max - 0.5) &&
port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5))
{
log_data.error_flags &= ~(1 << ERR_LOAD_OVERVOLTAGE);
state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -311,6 +324,7 @@ void LoadOutput::state_machine()
// stay here until the charge controller is reset or load is switched off remotely
if (enable == false) {
short_circuit = false;
log_data.error_flags &= ~(1 << ERR_LOAD_SHORT_CIRCUIT);
state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -323,7 +337,11 @@ void LoadOutput::state_machine()
void LoadOutput::control()
{
if (short_circuit) {
state = LOAD_STATE_OFF_SHORT_CIRCUIT;
if (state != LOAD_STATE_OFF_SHORT_CIRCUIT) {
state = LOAD_STATE_OFF_SHORT_CIRCUIT;
log_data.error_flags |= (1 << ERR_LOAD_SHORT_CIRCUIT);
print_error("Load short circuit detected\n");
}
return;
}
@@ -334,13 +352,30 @@ void LoadOutput::control()
(LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * (MOSFET_MAX_JUNCTION_TEMP - 25)
) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY);
if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP) {
if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP
|| port->current > LOAD_CURRENT_MAX * 2)
{
switch_set(false);
usb_set(false);
leds_flicker(LED_LOAD);
state = LOAD_STATE_OFF_OVERCURRENT;
log_data.error_flags |= (1 << ERR_LOAD_OVERCURRENT);
print_error("Load overcurrent detected\n");
overcurrent_timestamp = time(NULL);
}
// overcurrent detected because of voltage dip by 25%
if (port->voltage < voltage_prev * 0.75 && voltage_prev != 0) {
switch_set(false);
usb_set(false);
leds_flicker(LED_LOAD);
state = LOAD_STATE_OFF_OVERCURRENT;
log_data.error_flags |= (1 << ERR_LOAD_VOLTAGE_DIP);
print_error("Load voltage dip detected\n");
overcurrent_timestamp = time(NULL);
}
voltage_prev = port->voltage;
static int debounce_counter = 0;
if (port->voltage > port->sink_voltage_max ||
port->voltage > LOW_SIDE_VOLTAGE_MAX)
@@ -349,9 +384,10 @@ void LoadOutput::control()
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
switch_set(false);
usb_set(false);
print_error("Load overvoltage detected\n");
state = LOAD_STATE_OFF_OVERVOLTAGE;
overcurrent_timestamp = time(NULL);
log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE);
log_data.error_flags |= (1 << ERR_LOAD_OVERVOLTAGE);
}
}
else {
+2
View File
@@ -86,6 +86,8 @@ public:
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
///< measurement of (external) load output terminal
float voltage_prev; ///< voltage from previous call of control function
time_t overcurrent_timestamp; ///< Time when last overcurrent event occured
int overcurrent_recovery_delay; ///< Seconds before we attempt to re-enable the load
///< after an overcurrent event
+6 -1
View File
@@ -39,7 +39,12 @@ enum ErrorFlag {
ERR_BAT_DIS_OVERTEMP,
ERR_BAT_DIS_UNDERTEMP,
ERR_BAT_CHG_OVERTEMP,
ERR_BAT_CHG_UNDERTEMP
ERR_BAT_CHG_UNDERTEMP,
ERR_LOAD_OVERVOLTAGE, ///< To high voltage for load so that it was switched off
ERR_LOAD_SHORT_CIRCUIT, ///< Short circuit detected by load port
ERR_LOAD_OVERCURRENT, ///< Long-term overcurrent of charge controller
ERR_LOAD_VOLTAGE_DIP ///< Overcurrent identified via voltage dip (may be because of
///< too small battery)
};
/** Log Data
+31 -13
View File
@@ -9,17 +9,6 @@
#include "main.h"
/*
enum LoadState {
LOAD_STATE_DISABLED = 0, ///< Actively disabled
LOAD_STATE_ON, ///< Normal state: On
LOAD_STATE_OFF_LOW_SOC, ///< Off to protect battery (overrules target setting)
LOAD_STATE_OFF_OVERCURRENT, ///< Off to protect charge controller (overrules target setting)
LOAD_STATE_OFF_OVERVOLTAGE, ///< Off to protect loads (overrules target setting)
LOAD_STATE_OFF_SHORT_CIRCUIT ///< Off to protect charge controller (overrules target setting)
};
*/
void disabled_to_on_if_everything_fine()
{
PowerPort port;
@@ -189,6 +178,8 @@ void control_off_overvoltage()
port.pos_current_limit = 10;
port.voltage = 14.7;
load.state = LOAD_STATE_ON;
load.voltage_prev = port.voltage;
log_data.error_flags = 0;
// increase debounce counter to 1 before limit
for (int i = 0; i < CONTROL_FREQUENCY; i++) {
@@ -198,6 +189,7 @@ void control_off_overvoltage()
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
load.control(); // once more
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
TEST_ASSERT_EQUAL(1 << ERR_LOAD_OVERVOLTAGE, log_data.error_flags);
}
extern float mcu_temp;
@@ -208,20 +200,45 @@ void control_off_overcurrent()
LoadOutput load(&port);
port.init_load(14.6);
port.pos_current_limit = 10; // this is currently not considered, as it is lower than hardware limit
port.current = LOAD_CURRENT_MAX * 2;
port.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately
port.voltage = 14;
load.state = LOAD_STATE_ON;
load.voltage_prev = port.voltage;
mcu_temp = 25;
load.junction_temperature = 25;
log_data.error_flags = 0;
load.control();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
// 2x current = 4x heat generation: Should definitely trigger after waiting one time constant
// almost 2x current = 4x heat generation: Should definitely trigger after waiting one time constant
int trigger_steps = MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY;
for (int i = 0; i <= trigger_steps; i++) {
load.control();
}
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
TEST_ASSERT_EQUAL(1 << ERR_LOAD_OVERCURRENT, log_data.error_flags);
}
void control_off_voltage_dip()
{
PowerPort port;
LoadOutput load(&port);
port.init_load(14.6);
load.voltage_prev = port.voltage;
load.state = LOAD_STATE_ON;
mcu_temp = 25;
load.junction_temperature = 25;
log_data.error_flags = 0;
port.voltage = 14;
load.control();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
port.voltage = 14 * 0.74;
load.control();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
TEST_ASSERT_EQUAL(1 << ERR_LOAD_VOLTAGE_DIP, log_data.error_flags);
}
void load_tests()
@@ -243,6 +260,7 @@ void load_tests()
// control tests
RUN_TEST(control_off_overvoltage);
RUN_TEST(control_off_overcurrent);
RUN_TEST(control_off_voltage_dip);
// ToDo: What to do if port current is above the limit, but the hardware can still handle it?