Merge pull request #67 from LibreSolar/load-protection-improvements

This commit is contained in:
Martin Jäger
2019-11-01 08:15:08 +01:00
committed by GitHub
17 changed files with 237 additions and 150 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
+21 -64
View File
@@ -53,10 +53,6 @@
AnalogOut ref_i_dcdc(PIN_REF_I_DCDC);
#endif
#ifdef PIN_TEMP_INT_PD
DigitalInOut temp_pd(PIN_TEMP_INT_PD);
#endif
float solar_current_offset;
float load_current_offset;
@@ -66,7 +62,7 @@ volatile uint32_t adc_filtered[NUM_ADC_CH] = {0};
//volatile int num_adc_conversions;
extern LogData log_data;
extern float mcu_temp;
extern float internal_temp;
void calibrate_current_sensors()
{
@@ -146,7 +142,6 @@ void update_measurements()
#if defined PIN_ADC_TEMP_BAT || defined PIN_ADC_TEMP_FETS
float v_temp, rts;
#endif
float bat_temp = 25.0;
#ifdef PIN_ADC_TEMP_BAT
// battery temperature calculation
@@ -155,10 +150,19 @@ void update_measurements()
// Temperature calculation using Beta equation for 10k thermistor
// (25°C reference temperature for Beta equation assumed)
bat_temp = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C
#endif
float bat_temp = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C
detect_battery_temperature(&charger, bat_temp);
if (bat_temp > -50) {
// external sensor connected: take measured value
charger.bat_temperature = bat_temp;
charger.ext_temp_sensor = true;
}
else {
// no external sensor: assume typical room temperature
charger.bat_temperature = 25;
charger.ext_temp_sensor = false;
}
#endif
#ifdef PIN_ADC_TEMP_FETS
// MOSFET temperature calculation
@@ -169,64 +173,17 @@ void update_measurements()
// internal MCU temperature
uint16_t adcval = (adc_filtered[ADC_POS_TEMP_MCU] >> (4 + ADC_FILTER_CONST)) * vcc / VREFINT_VALUE;
mcu_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) / (TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
//printf("TS_CAL1:%d TS_CAL2:%d ADC:%d, temp_int:%f\n", TS_CAL1, TS_CAL2, adcval, meas->temp_int);
}
internal_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) /
(TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
void detect_battery_temperature(Charger *charger, float bat_temp)
{
#ifdef PIN_TEMP_INT_PD
// state machine for external sensor detection
enum temp_sense_state {
TSENSE_STATE_CHECK,
TSENSE_STATE_CHECK_WAIT,
TSENSE_STATE_MEASURE,
TSENSE_STATE_MEASURE_WAIT
};
static temp_sense_state ts_state = TSENSE_STATE_CHECK;
static int sensor_change_counter = 0;
switch (ts_state) {
case TSENSE_STATE_CHECK:
if (bat_temp < -50) {
charger->ext_temp_sensor = false;
temp_pd.output();
temp_pd = 0;
ts_state = TSENSE_STATE_CHECK_WAIT;
}
else {
charger->ext_temp_sensor = true;
ts_state = TSENSE_STATE_MEASURE;
}
break;
case TSENSE_STATE_CHECK_WAIT:
sensor_change_counter++;
if (sensor_change_counter > 5) {
sensor_change_counter = 0;
ts_state = TSENSE_STATE_MEASURE;
}
break;
case TSENSE_STATE_MEASURE:
charger->bat_temperature = charger->bat_temperature * 0.8 + bat_temp * 0.2;
//printf("Battery temperature: %.2f (%s sensor)\n", bat_temp, (charger->ext_temp_sensor ? "external" : "internal"));
temp_pd.input();
ts_state = TSENSE_STATE_MEASURE_WAIT;
break;
case TSENSE_STATE_MEASURE_WAIT:
sensor_change_counter++;
if (sensor_change_counter > 10) {
sensor_change_counter = 0;
ts_state = TSENSE_STATE_CHECK;
}
break;
if (internal_temp > 80) {
log_data.error_flags |= 1U << ERR_INT_OVERTEMP;
}
#else
if (bat_temp > -50) {
charger->bat_temperature = bat_temp;
else if (internal_temp < 70 && (log_data.error_flags & (1U << ERR_INT_OVERTEMP))) {
// remove error flag with hysteresis of 10°C
log_data.error_flags &= ~(1U << ERR_INT_OVERTEMP);
}
#endif
// else: keep previous setting
}
#ifndef UNIT_TEST
-4
View File
@@ -34,10 +34,6 @@
*/
void calibrate_current_sensors();
/** Detects if external temperature sensor is attached, otherwise takes internal sensor
*/
void detect_battery_temperature(Charger *bat, float bat_temp);
/** Updates structures with data read from ADC
*/
void update_measurements();
+2 -2
View File
@@ -36,7 +36,7 @@ const char* const firmware_commit = COMMIT_HASH;
uint32_t device_id = DEVICE_ID; // from config.h
extern uint32_t timestamp;
float mcu_temp;
float internal_temp;
/** Data Objects
*
@@ -131,7 +131,7 @@ const data_object_t data_objects[] = {
{0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.current), "Load_A"},
{0x74, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(charger.bat_temperature), "Bat_degC"},
{0x75, TS_OUTPUT, TS_READ_ALL, TS_T_BOOL, 1, (void*) &(charger.ext_temp_sensor), "BatTempExt"},
{0x76, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(mcu_temp), "MCU_degC"},
{0x76, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(internal_temp), "Int_degC"},
#if defined(PIN_ADC_TEMP_FETS) && FEATURE_DCDC_CONVERTER
{0x77, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Mosfet_degC"},
#endif
+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 */
+98 -48
View File
@@ -20,11 +20,12 @@
#include "hardware.h"
#include "leds.h"
#include "log.h"
#include "debug.h"
volatile bool short_circuit = false;
extern LogData log_data;
extern float mcu_temp;
extern float internal_temp;
#ifndef UNIT_TEST
@@ -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
}
@@ -197,9 +208,9 @@ LoadOutput::LoadOutput(PowerPort *pwr_port)
usb_set(false);
state = LOAD_STATE_DISABLED;
usb_state = LOAD_STATE_DISABLED;
junction_temperature = 25;
overcurrent_recovery_delay = 5*60;
lvd_recovery_delay = 60*60;
junction_temperature = 25; // starting point: 25°C
overcurrent_recovery_delay = 5*60; // default: 5 minutes
lvd_recovery_delay = 60*60; // default: 1 hour
// analog comparator to detect short circuits and trigger immediate load switch-off
short_circuit_comp_init();
@@ -207,22 +218,28 @@ LoadOutput::LoadOutput(PowerPort *pwr_port)
void LoadOutput::usb_state_machine()
{
// We don't have any overcurrent detection mechanism for USB and the buck converter IC should
// reduce output power itself in case of failure. Battey overvoltage should also be handled by
// the DC/DC up to a certain degree.
// So, we don't use above failure states for the USB output. Only low SOC and exceeded
// temperature limits force the USB output to be turned off.
switch (usb_state) {
case LOAD_STATE_DISABLED:
if (usb_enable == true) {
usb_set(true);
usb_state = LOAD_STATE_ON;
if (port->pos_current_limit > 0) {
usb_set(true);
usb_state = LOAD_STATE_ON;
}
else if (state == LOAD_STATE_OFF_LOW_SOC || state == LOAD_STATE_OFF_TEMPERATURE) {
usb_state = state;
}
}
break;
case LOAD_STATE_ON:
// currently still same cut-off SOC limit as the load
if (state == LOAD_STATE_OFF_LOW_SOC) {
if (state == LOAD_STATE_OFF_LOW_SOC || state == LOAD_STATE_OFF_TEMPERATURE) {
usb_set(false);
usb_state = LOAD_STATE_OFF_LOW_SOC;
}
else if (state == LOAD_STATE_OFF_OVERCURRENT) {
usb_set(false);
usb_state = LOAD_STATE_OFF_OVERCURRENT;
usb_state = state;
}
else if (usb_enable == false) {
usb_set(false);
@@ -230,19 +247,9 @@ void LoadOutput::usb_state_machine()
}
break;
case LOAD_STATE_OFF_LOW_SOC:
// currently still same cut-off SOC limit as the load
if (state == LOAD_STATE_ON) {
if (usb_enable == true) {
usb_set(true);
usb_state = LOAD_STATE_ON;
}
else {
usb_state = LOAD_STATE_DISABLED;
}
}
break;
case LOAD_STATE_OFF_OVERCURRENT:
if (state != LOAD_STATE_OFF_OVERCURRENT) {
case LOAD_STATE_OFF_TEMPERATURE:
if (state == LOAD_STATE_ON || state == LOAD_STATE_DISABLED) {
// also go back to normal operation with the USB charging port
usb_state = LOAD_STATE_DISABLED;
}
break;
@@ -251,7 +258,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) {
@@ -265,7 +271,7 @@ void LoadOutput::state_machine()
}
else {
// must be battery over/under temperature then
state = LOAD_STATE_OFF_BAT_TEMP;
state = LOAD_STATE_OFF_TEMPERATURE;
}
}
}
@@ -284,19 +290,23 @@ void LoadOutput::state_machine()
}
else {
// must be battery over/under temperature then
state = LOAD_STATE_OFF_BAT_TEMP;
state = LOAD_STATE_OFF_TEMPERATURE;
}
}
break;
case LOAD_STATE_OFF_LOW_SOC:
// wait at least one hour
if (time(NULL) - lvd_timestamp > lvd_recovery_delay) {
// wait at least configured time
if (time(NULL) - lvd_timestamp > lvd_recovery_delay &&
!(log_data.error_flags & (1 << ERR_BAT_UNDERVOLTAGE)))
{
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 +314,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,9 +322,17 @@ 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;
case LOAD_STATE_OFF_TEMPERATURE:
if (!(log_data.error_flags & (1U << ERR_INT_OVERTEMP)) &&
port->pos_current_limit > 0)
{
state = LOAD_STATE_DISABLED;
}
break;
}
usb_state_machine();
@@ -323,24 +342,54 @@ 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;
}
// junction temperature calculation model for overcurrent detection
junction_temperature = junction_temperature + (
mcu_temp - junction_temperature +
internal_temp - junction_temperature +
port->current * port->current /
(LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * (MOSFET_MAX_JUNCTION_TEMP - 25)
(LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) *
(MOSFET_MAX_JUNCTION_TEMP - INTERNAL_MAX_REFERENCE_TEMP)
) / (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);
return;
}
// internal overtemperature
if (log_data.error_flags & (1U << ERR_INT_OVERTEMP)) {
switch_set(false);
state = LOAD_STATE_OFF_TEMPERATURE;
print_error("Device overtemperature detected\n");
return;
}
// overcurrent detected because of voltage dip by 25% for around 100ms
if (port->voltage < voltage_prev * 0.75 && voltage_prev != 0) {
switch_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);
return;
}
voltage_prev = port->voltage;
static int debounce_counter = 0;
if (port->voltage > port->sink_voltage_max ||
port->voltage > LOW_SIDE_VOLTAGE_MAX)
@@ -348,10 +397,11 @@ void LoadOutput::control()
debounce_counter++;
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);
return;
}
}
else {
+3 -1
View File
@@ -40,7 +40,7 @@ enum LoadState {
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)
LOAD_STATE_OFF_BAT_TEMP ///< Off because of battery over/under-temperature
LOAD_STATE_OFF_TEMPERATURE ///< Off because of battery or charge controller temperature
};
/** Load output type
@@ -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
+3 -3
View File
@@ -23,7 +23,7 @@
#include <math.h> // for fabs function
#include <stdio.h>
extern float mcu_temp;
extern float internal_temp;
//----------------------------------------------------------------------------
// must be called exactly once per second, otherwise energy calculation gets wrong
@@ -112,7 +112,7 @@ void log_update_min_max_values(LogData *log_data)
log_data->bat_temp_max = charger.bat_temperature;
}
if (mcu_temp > log_data->int_temp_max) {
log_data->int_temp_max = mcu_temp;
if (internal_temp > log_data->int_temp_max) {
log_data->int_temp_max = internal_temp;
}
}
+7 -1
View File
@@ -39,7 +39,13 @@ 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)
ERR_INT_OVERTEMP ///< Charge controller internal temperature too high
};
/** Log Data
+8
View File
@@ -41,6 +41,14 @@
*/
#define MOSFET_MAX_JUNCTION_TEMP 120
/** Internal reference temperature at full load (°C)
*
* This value is used for model-based control of overcurrent protection. It represents
* the steady-state internal temperature for max. continuous current at ambient
* temperature of 25°C.
*/
#define INTERNAL_MAX_REFERENCE_TEMP 50
/** Thermal time constant junction to ambient (s)
*
* This value is used for model-based control of overcurrent protection. It does not reflect
+2
View File
@@ -60,6 +60,8 @@
#define PIN_REF_I_DCDC PA_4
// Internal NTC temperature currently ignored by firmware as it is similar to MCU temperature and
// does not reflect external battery temperature. Feature will be removed in future HW revisions.
#define PIN_TEMP_INT_PD PA_8
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
+2
View File
@@ -62,6 +62,8 @@
#define PIN_REF_I_DCDC PA_4
// Internal NTC temperature currently ignored by firmware as it is similar to MCU temperature and
// does not reflect external battery temperature. Feature will be removed in future HW revisions.
#define PIN_TEMP_INT_PD PA_8
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
-1
View File
@@ -24,7 +24,6 @@ typedef struct {
float dcdc_current;
float load_current;
float bat_temperature;
float mcu_temperature;
float internal_temperature;
} adc_values_t;
-1
View File
@@ -50,7 +50,6 @@ void adc_tests()
adcval.dcdc_current = 3;
adcval.internal_temperature = 25;
adcval.load_current = 1;
adcval.mcu_temperature = 25;
adcval.solar_voltage = 30;
prepare_adc_readings(adcval);
+82 -21
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;
@@ -39,6 +28,7 @@ void disabled_to_off_low_soc_if_error_flag_set()
log_data.error_flags = 1 << ERR_BAT_UNDERVOLTAGE;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state);
}
void disabled_to_off_bat_temp_if_error_flag_set()
@@ -51,13 +41,15 @@ void disabled_to_off_bat_temp_if_error_flag_set()
// overtemp
log_data.error_flags = 1 << ERR_BAT_CHG_OVERTEMP;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_BAT_TEMP, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state);
// undertemp
load.state = LOAD_STATE_DISABLED;
log_data.error_flags = 1 << ERR_BAT_CHG_UNDERTEMP;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_BAT_TEMP, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state);
}
void off_low_soc_to_on_after_delay()
@@ -66,16 +58,19 @@ void off_low_soc_to_on_after_delay()
LoadOutput load(&port);
port.init_load(14.6);
load.state = LOAD_STATE_OFF_LOW_SOC;
load.usb_state = LOAD_STATE_OFF_LOW_SOC;
port.pos_current_limit = 10;
load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay + 1;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state);
load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay - 1;
load.state_machine();
load.state_machine(); // call twice as it goes through disabled state
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
}
void off_overcurrent_to_on_after_delay()
@@ -84,16 +79,19 @@ void off_overcurrent_to_on_after_delay()
LoadOutput load(&port);
port.init_load(14.6);
load.state = LOAD_STATE_OFF_OVERCURRENT;
load.usb_state = LOAD_STATE_ON;
port.pos_current_limit = 10;
load.overcurrent_timestamp = time(NULL) - load.overcurrent_recovery_delay + 1;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overcurrent
load.overcurrent_timestamp = time(NULL) - load.overcurrent_recovery_delay - 1;
load.state_machine();
load.state_machine(); // call twice as it goes through disabled state
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
}
void off_overvoltage_to_on_at_lower_voltage()
@@ -103,10 +101,12 @@ void off_overvoltage_to_on_at_lower_voltage()
port.init_load(14.6);
port.pos_current_limit = 10;
load.state = LOAD_STATE_OFF_OVERVOLTAGE;
load.usb_state = LOAD_STATE_ON;
port.voltage = port.sink_voltage_max + 0.1;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage
port.voltage = port.sink_voltage_max - 0.1; // test hysteresis
load.state_machine();
@@ -116,6 +116,7 @@ void off_overvoltage_to_on_at_lower_voltage()
load.state_machine();
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
}
void off_short_circuit_to_disabled()
@@ -125,13 +126,16 @@ void off_short_circuit_to_disabled()
port.init_load(14.6);
port.pos_current_limit = 10;
load.state = LOAD_STATE_OFF_SHORT_CIRCUIT;
load.usb_state = LOAD_STATE_ON;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage
load.enable = false; // this is like a manual reset
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
}
void on_to_off_low_soc_if_error_flag_set()
@@ -140,11 +144,13 @@ void on_to_off_low_soc_if_error_flag_set()
LoadOutput load(&port);
port.init_load(14.6);
load.state = LOAD_STATE_ON;
load.usb_state = LOAD_STATE_ON;
port.pos_current_limit = 0;
log_data.error_flags = 1 << ERR_BAT_UNDERVOLTAGE;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state);
}
void on_to_off_bat_temp_if_error_flag_set()
@@ -155,14 +161,17 @@ void on_to_off_bat_temp_if_error_flag_set()
port.pos_current_limit = 0;
load.state = LOAD_STATE_ON;
load.usb_state = LOAD_STATE_ON;
log_data.error_flags = 1 << ERR_BAT_DIS_OVERTEMP;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_BAT_TEMP, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state);
load.state = LOAD_STATE_ON;
log_data.error_flags = 1 << ERR_BAT_DIS_UNDERTEMP;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_BAT_TEMP, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state);
}
void on_to_disabled_if_enable_false()
@@ -173,12 +182,19 @@ void on_to_disabled_if_enable_false()
port.pos_current_limit = 10;
load.state = LOAD_STATE_ON;
load.usb_state = LOAD_STATE_ON;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
load.enable = false;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state);
load.usb_enable = false;
load.state_machine();
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.usb_state);
}
void control_off_overvoltage()
@@ -189,6 +205,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,9 +216,10 @@ 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;
extern float internal_temp;
void control_off_overcurrent()
{
@@ -208,20 +227,62 @@ 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;
mcu_temp = 25;
load.voltage_prev = port.voltage;
internal_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);
port.current = 0;
load.voltage_prev = port.voltage;
load.state = LOAD_STATE_ON;
internal_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 control_off_temperature()
{
PowerPort port;
LoadOutput load(&port);
port.init_load(14.6);
port.current = 0;
load.voltage_prev = port.voltage;
load.state = LOAD_STATE_ON;
internal_temp = 25;
load.junction_temperature = 25;
log_data.error_flags = 1U << ERR_INT_OVERTEMP;
load.control();
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state);
}
void load_tests()
@@ -243,10 +304,10 @@ void load_tests()
// control tests
RUN_TEST(control_off_overvoltage);
RUN_TEST(control_off_overcurrent);
RUN_TEST(control_off_voltage_dip);
RUN_TEST(control_off_temperature);
// ToDo: What to do if port current is above the limit, but the hardware can still handle it?
// ToDo: Additional USB output state machine tests
UNITY_END();
}
+2 -2
View File
@@ -106,11 +106,11 @@ void log_new_bat_temp_max()
TEST_ASSERT_EQUAL(45, log_data.bat_temp_max);
}
extern float mcu_temp;
extern float internal_temp;
void log_new_int_temp_max()
{
mcu_temp = 22;
internal_temp = 22;
log_update_min_max_values(&log_data);
TEST_ASSERT_EQUAL(22, log_data.int_temp_max);
}
+1 -1
View File
@@ -33,7 +33,7 @@ void energy_calculation_init()
adcval.dcdc_current = dcdc_current_sun;
adcval.internal_temperature = 25;
adcval.load_current = load_current;
adcval.mcu_temperature = 25;
adcval.internal_temperature = 25;
adcval.solar_voltage = 30;
// insert values into ADC functions