Discharge and load output control improvements
- Charge/discharge control in charger class sets global error flags - Load state machine analyzes error flags to distinguish reasons for switching load off - Temperature errors now identified correctly (issue #59) - Delay before load re-connect after low voltage or overcurrent event now configurable - Unit tests added
This commit is contained in:
+59
-39
@@ -18,6 +18,9 @@
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
|
||||
#include "log.h"
|
||||
extern LogData log_data;
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
@@ -265,57 +268,72 @@ void Charger::enter_state(int next_state)
|
||||
|
||||
void Charger::discharge_control(BatConf *bat_conf)
|
||||
{
|
||||
// load output state is defined by battery bus dis_allowed flag
|
||||
if (port->neg_current_limit < 0 // discharging allowed
|
||||
&& port->voltage < num_batteries *
|
||||
(bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res))
|
||||
{
|
||||
// low state of charge
|
||||
port->neg_current_limit = 0;
|
||||
num_deep_discharges++;
|
||||
// load output state is defined by battery negative current limit
|
||||
if (port->neg_current_limit < 0) {
|
||||
// discharging currently allowed. see if that's still valid:
|
||||
if (port->voltage < num_batteries *
|
||||
(bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res))
|
||||
{
|
||||
// low state of charge
|
||||
port->neg_current_limit = 0;
|
||||
num_deep_discharges++;
|
||||
log_data.error_flags |= (1 << ERR_BAT_UNDERVOLTAGE);
|
||||
|
||||
if (usable_capacity < 0.1) {
|
||||
// reset to measured value if discharged the first time
|
||||
if (first_full_charge_reached) {
|
||||
usable_capacity = discharged_Ah;
|
||||
if (usable_capacity < 0.1) {
|
||||
// reset to measured value if discharged the first time
|
||||
if (first_full_charge_reached) {
|
||||
usable_capacity = discharged_Ah;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// slowly adapt new measurements with low-pass filter
|
||||
usable_capacity =
|
||||
0.8 * usable_capacity +
|
||||
0.2 * discharged_Ah;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// slowly adapt new measurements with low-pass filter
|
||||
usable_capacity =
|
||||
0.8 * usable_capacity +
|
||||
0.2 * discharged_Ah;
|
||||
}
|
||||
|
||||
// simple SOH estimation
|
||||
soh = usable_capacity / bat_conf->nominal_capacity;
|
||||
}
|
||||
else if (port->neg_current_limit < 0
|
||||
&& (bat_temperature > bat_conf->discharge_temp_max
|
||||
|| bat_temperature < bat_conf->discharge_temp_min))
|
||||
{
|
||||
// temperature limits exceeded
|
||||
port->neg_current_limit = 0;
|
||||
// simple SOH estimation
|
||||
soh = usable_capacity / bat_conf->nominal_capacity;
|
||||
}
|
||||
else if (bat_temperature > bat_conf->discharge_temp_max) {
|
||||
port->neg_current_limit = 0;
|
||||
log_data.error_flags |= (1 << ERR_BAT_DIS_OVERTEMP);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->discharge_temp_min) {
|
||||
port->neg_current_limit = 0;
|
||||
log_data.error_flags |= (1 << ERR_BAT_DIS_UNDERTEMP);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// discharging currently not allowed. should we allow it?
|
||||
if (port->voltage >= num_batteries *
|
||||
(bat_conf->voltage_load_reconnect - port->current * port->neg_droop_res)
|
||||
&& bat_temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->discharge_temp_min + 1)
|
||||
{
|
||||
// discharge current is stored as absolute value in bat_conf, but defined
|
||||
// as negative current for power port
|
||||
port->neg_current_limit = -bat_conf->discharge_current_max;
|
||||
|
||||
if (port->voltage >= num_batteries *
|
||||
(bat_conf->voltage_load_reconnect - port->current * port->neg_droop_res)
|
||||
&& bat_temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->discharge_temp_min + 1)
|
||||
{
|
||||
// discharge current is stored as absolute value in bat_conf, but defined
|
||||
// as negative current for dc_bus
|
||||
port->neg_current_limit = -bat_conf->discharge_current_max;
|
||||
// delete all discharge error flags
|
||||
log_data.error_flags &= ~(1 << ERR_BAT_DIS_OVERTEMP);
|
||||
log_data.error_flags &= ~(1 << ERR_BAT_DIS_UNDERTEMP);
|
||||
log_data.error_flags &= ~(1 << ERR_BAT_UNDERVOLTAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Charger::charge_control(BatConf *bat_conf)
|
||||
{
|
||||
// check battery temperature for charging direction
|
||||
if (bat_temperature > bat_conf->charge_temp_max
|
||||
|| bat_temperature < bat_conf->charge_temp_min)
|
||||
{
|
||||
if (bat_temperature > bat_conf->charge_temp_max) {
|
||||
port->pos_current_limit = 0;
|
||||
log_data.error_flags |= (1 << ERR_BAT_CHG_OVERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->charge_temp_min) {
|
||||
port->pos_current_limit = 0;
|
||||
log_data.error_flags |= (1 << ERR_BAT_CHG_UNDERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
|
||||
@@ -331,6 +349,8 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
port->pos_current_limit = bat_conf->charge_current_max;
|
||||
full = false;
|
||||
log_data.error_flags &= ~(1 << ERR_BAT_CHG_OVERTEMP);
|
||||
log_data.error_flags &= ~(1 << ERR_BAT_CHG_UNDERTEMP);
|
||||
enter_state(CHG_STATE_BULK);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -34,8 +34,6 @@
|
||||
#define BATTERY_NUM_CELLS 6 ///< For lead-acid batteries: 6 for 12V system, 12 for 24V system
|
||||
#define BATTERY_CAPACITY 40 ///< Cell capacity or sum of parallel cells capacity (Ah)
|
||||
|
||||
#define LOAD_OVERCURRENT_RECOVERY_TIMEOUT (5*30) ///< Seconds to wait until load is switched back on after an overcurrent event
|
||||
|
||||
#define DEVICE_ID 12345678
|
||||
|
||||
#define THINGSET_USER_PASSWORD "user123"
|
||||
|
||||
@@ -97,6 +97,8 @@ const data_object_t data_objects[] = {
|
||||
{0x43, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_reconnect), "LoadReconnect_V"},
|
||||
//{0x44, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_disconnect), "UsbDisconnect_V"},
|
||||
//{0x45, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_reconnect), "UsbReconnect_V"},
|
||||
{0x46, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(load.overcurrent_recovery_delay), "LoadOCRecovery_s"},
|
||||
{0x47, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(load.lvd_recovery_delay), "LoadUVRecovery_s"},
|
||||
|
||||
// other configuration items
|
||||
//{0x33, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 2, (void*) &(??), "WarningIndicator"}, // can be set externally
|
||||
@@ -118,7 +120,7 @@ const data_object_t data_objects[] = {
|
||||
// using IDs >= 0x70 except for high priority data objects
|
||||
|
||||
// high priority data objects (low IDs)
|
||||
{0x04, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.switch_state), "LoadState"},
|
||||
{0x04, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.state), "LoadState"},
|
||||
{0x05, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.usb_state), "UsbState"},
|
||||
{0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t
|
||||
|
||||
|
||||
+44
-33
@@ -191,14 +191,15 @@ void LoadOutput::usb_set(bool enabled) {;}
|
||||
LoadOutput::LoadOutput(PowerPort *pwr_port)
|
||||
{
|
||||
port = pwr_port;
|
||||
current_max = LOAD_CURRENT_MAX;
|
||||
enable = true;
|
||||
usb_enable = true;
|
||||
switch_set(false);
|
||||
usb_set(false);
|
||||
switch_state = LOAD_STATE_DISABLED;
|
||||
state = LOAD_STATE_DISABLED;
|
||||
usb_state = LOAD_STATE_DISABLED;
|
||||
junction_temperature = 25;
|
||||
overcurrent_recovery_delay = 5*60;
|
||||
lvd_recovery_delay = 60*60;
|
||||
|
||||
// analog comparator to detect short circuits and trigger immediate load switch-off
|
||||
short_circuit_comp_init();
|
||||
@@ -215,11 +216,11 @@ void LoadOutput::usb_state_machine()
|
||||
break;
|
||||
case LOAD_STATE_ON:
|
||||
// currently still same cut-off SOC limit as the load
|
||||
if (switch_state == LOAD_STATE_OFF_LOW_SOC) {
|
||||
if (state == LOAD_STATE_OFF_LOW_SOC) {
|
||||
usb_set(false);
|
||||
usb_state = LOAD_STATE_OFF_LOW_SOC;
|
||||
}
|
||||
else if (switch_state == LOAD_STATE_OFF_OVERCURRENT) {
|
||||
else if (state == LOAD_STATE_OFF_OVERCURRENT) {
|
||||
usb_set(false);
|
||||
usb_state = LOAD_STATE_OFF_OVERCURRENT;
|
||||
}
|
||||
@@ -230,7 +231,7 @@ void LoadOutput::usb_state_machine()
|
||||
break;
|
||||
case LOAD_STATE_OFF_LOW_SOC:
|
||||
// currently still same cut-off SOC limit as the load
|
||||
if (switch_state == LOAD_STATE_ON) {
|
||||
if (state == LOAD_STATE_ON) {
|
||||
if (usb_enable == true) {
|
||||
usb_set(true);
|
||||
usb_state = LOAD_STATE_ON;
|
||||
@@ -241,7 +242,7 @@ void LoadOutput::usb_state_machine()
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_OFF_OVERCURRENT:
|
||||
if (switch_state != LOAD_STATE_OFF_OVERCURRENT) {
|
||||
if (state != LOAD_STATE_OFF_OVERCURRENT) {
|
||||
usb_state = LOAD_STATE_DISABLED;
|
||||
}
|
||||
break;
|
||||
@@ -250,57 +251,67 @@ void LoadOutput::usb_state_machine()
|
||||
|
||||
void LoadOutput::state_machine()
|
||||
{
|
||||
//printf("Load State: %d\n", switch_state);
|
||||
switch (switch_state) {
|
||||
//printf("Load State: %d\n", state);
|
||||
switch (state) {
|
||||
case LOAD_STATE_DISABLED:
|
||||
if (enable == true && port->pos_current_limit > 0) {
|
||||
switch_set(true);
|
||||
switch_state = LOAD_STATE_ON;
|
||||
if (enable == true) {
|
||||
if (port->pos_current_limit > 0) {
|
||||
switch_set(true);
|
||||
state = LOAD_STATE_ON;
|
||||
}
|
||||
else {
|
||||
if (log_data.error_flags & (1 << ERR_BAT_UNDERVOLTAGE)) {
|
||||
state = LOAD_STATE_OFF_LOW_SOC;
|
||||
}
|
||||
else {
|
||||
// must be battery over/under temperature then
|
||||
state = LOAD_STATE_OFF_BAT_TEMP;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_ON:
|
||||
if (enable == false) {
|
||||
switch_set(false);
|
||||
switch_state = LOAD_STATE_DISABLED;
|
||||
state = LOAD_STATE_DISABLED;
|
||||
}
|
||||
else if (port->pos_current_limit == 0) { // float == is allowed for 0.0
|
||||
lvd_timestamp = time(NULL);
|
||||
switch_set(false);
|
||||
switch_state = LOAD_STATE_OFF_LOW_SOC;
|
||||
// find out reason why load current is not allowed
|
||||
if (log_data.error_flags & (1 << ERR_BAT_UNDERVOLTAGE)) {
|
||||
lvd_timestamp = time(NULL);
|
||||
state = LOAD_STATE_OFF_LOW_SOC;
|
||||
}
|
||||
else {
|
||||
// must be battery over/under temperature then
|
||||
state = LOAD_STATE_OFF_BAT_TEMP;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_OFF_LOW_SOC:
|
||||
// wait at least one hour
|
||||
if (port->pos_current_limit > 0
|
||||
&& time(NULL) - lvd_timestamp > 60*60)
|
||||
{
|
||||
if (enable == true) {
|
||||
switch_set(true);
|
||||
switch_state = LOAD_STATE_ON;
|
||||
}
|
||||
else {
|
||||
switch_state = LOAD_STATE_DISABLED;
|
||||
}
|
||||
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
|
||||
if (time(NULL) > overcurrent_timestamp + LOAD_OVERCURRENT_RECOVERY_TIMEOUT) {
|
||||
switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
if (time(NULL) - overcurrent_timestamp > overcurrent_recovery_delay) {
|
||||
state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_OFF_OVERVOLTAGE:
|
||||
if (port->voltage < port->sink_voltage_max &&
|
||||
port->voltage < LOW_SIDE_VOLTAGE_MAX)
|
||||
if (port->voltage < (port->sink_voltage_max - 0.5) &&
|
||||
port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5))
|
||||
{
|
||||
switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_OFF_SHORT_CIRCUIT:
|
||||
// stay here until the charge controller is reset or load is switched off remotely
|
||||
if (enable == false) {
|
||||
short_circuit = false;
|
||||
switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
state = LOAD_STATE_DISABLED; // switch to normal mode again
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -312,7 +323,7 @@ void LoadOutput::state_machine()
|
||||
void LoadOutput::control()
|
||||
{
|
||||
if (short_circuit) {
|
||||
switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT;
|
||||
state = LOAD_STATE_OFF_SHORT_CIRCUIT;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -326,7 +337,7 @@ void LoadOutput::control()
|
||||
if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP) {
|
||||
switch_set(false);
|
||||
usb_set(false);
|
||||
switch_state = LOAD_STATE_OFF_OVERCURRENT;
|
||||
state = LOAD_STATE_OFF_OVERCURRENT;
|
||||
overcurrent_timestamp = time(NULL);
|
||||
}
|
||||
|
||||
@@ -338,7 +349,7 @@ void LoadOutput::control()
|
||||
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
|
||||
switch_set(false);
|
||||
usb_set(false);
|
||||
switch_state = LOAD_STATE_OFF_OVERVOLTAGE;
|
||||
state = LOAD_STATE_OFF_OVERVOLTAGE;
|
||||
overcurrent_timestamp = time(NULL);
|
||||
log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE);
|
||||
}
|
||||
|
||||
+10
-5
@@ -39,7 +39,8 @@ enum LoadState {
|
||||
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)
|
||||
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 output type
|
||||
@@ -79,15 +80,19 @@ public:
|
||||
*/
|
||||
void control();
|
||||
|
||||
uint16_t switch_state; ///< Current state of load output switch
|
||||
uint16_t state; ///< Current state of load output switch
|
||||
uint16_t usb_state; ///< Current state of USB output
|
||||
|
||||
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
|
||||
///< measurement of (external) load output terminal
|
||||
|
||||
float current_max; ///< maximum allowed current
|
||||
time_t overcurrent_timestamp; ///< time at which an overcurrent event occured
|
||||
time_t lvd_timestamp; ///< stores when the load was disconnected last time
|
||||
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
|
||||
|
||||
time_t lvd_timestamp; ///< Time when last low voltage disconnect happened
|
||||
int lvd_recovery_delay; ///< Seconds before we re-enable the load after a low voltage
|
||||
///< disconnect
|
||||
|
||||
float junction_temperature; ///< calculated using thermal model based on current and ambient
|
||||
///< temperature measurement (unit: °C)
|
||||
|
||||
@@ -32,10 +32,14 @@
|
||||
|
||||
/** Error Flags
|
||||
*/
|
||||
enum error_flag_t {
|
||||
enum ErrorFlag {
|
||||
ERR_HS_MOSFET_SHORT = 0, ///< Short-circuit in HS MOSFET
|
||||
ERR_BAT_OVERVOLTAGE,
|
||||
ERR_BAT_UNDERVOLTAGE,
|
||||
ERR_BAT_DIS_OVERTEMP,
|
||||
ERR_BAT_DIS_UNDERTEMP,
|
||||
ERR_BAT_CHG_OVERTEMP,
|
||||
ERR_BAT_CHG_UNDERTEMP
|
||||
};
|
||||
|
||||
/** Log Data
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ int main()
|
||||
eeprom_update();
|
||||
|
||||
leds_update_1s();
|
||||
leds_update_soc(charger.soc, load.switch_state == LOAD_STATE_OFF_LOW_SOC);
|
||||
leds_update_soc(charger.soc, load.state == LOAD_STATE_OFF_LOW_SOC);
|
||||
|
||||
uext_process_1s();
|
||||
ts_interfaces.process_1s();
|
||||
|
||||
@@ -71,7 +71,7 @@ void uext_process_1s()
|
||||
oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
|
||||
if (load.switch_state == LOAD_STATE_ON) {
|
||||
if (load.state == LOAD_STATE_ON) {
|
||||
oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
else {
|
||||
|
||||
+1
-3
@@ -63,7 +63,5 @@ int main()
|
||||
half_brigde_tests();
|
||||
dcdc_tests();
|
||||
log_tests();
|
||||
|
||||
// TODO
|
||||
//load_tests();
|
||||
load_tests();
|
||||
}
|
||||
@@ -12,3 +12,5 @@ void half_brigde_tests();
|
||||
void dcdc_tests();
|
||||
|
||||
void log_tests();
|
||||
|
||||
void load_tests();
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
|
||||
#include "tests.h"
|
||||
#include "adc_dma_stub.h"
|
||||
#include "adc_dma.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#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;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 10;
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
|
||||
}
|
||||
|
||||
void disabled_to_off_low_soc_if_error_flag_set()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
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);
|
||||
}
|
||||
|
||||
void disabled_to_off_bat_temp_if_error_flag_set()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 0;
|
||||
|
||||
// overtemp
|
||||
log_data.error_flags = 1 << ERR_BAT_CHG_OVERTEMP;
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_BAT_TEMP, load.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);
|
||||
}
|
||||
|
||||
void off_low_soc_to_on_after_delay()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
load.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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void off_overcurrent_to_on_after_delay()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
load.state = LOAD_STATE_OFF_OVERCURRENT;
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void off_overvoltage_to_on_at_lower_voltage()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 10;
|
||||
load.state = LOAD_STATE_OFF_OVERVOLTAGE;
|
||||
port.voltage = port.sink_voltage_max + 0.1;
|
||||
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
|
||||
|
||||
port.voltage = port.sink_voltage_max - 0.1; // test hysteresis
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
|
||||
|
||||
port.voltage = port.sink_voltage_max - 0.6;
|
||||
load.state_machine();
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
|
||||
}
|
||||
|
||||
void off_short_circuit_to_disabled()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 10;
|
||||
load.state = LOAD_STATE_OFF_SHORT_CIRCUIT;
|
||||
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state);
|
||||
|
||||
load.enable = false; // this is like a manual reset
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
|
||||
}
|
||||
|
||||
void on_to_off_low_soc_if_error_flag_set()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
load.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);
|
||||
}
|
||||
|
||||
void on_to_off_bat_temp_if_error_flag_set()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 0;
|
||||
|
||||
load.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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void on_to_disabled_if_enable_false()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 10;
|
||||
|
||||
load.state = LOAD_STATE_ON;
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
|
||||
|
||||
load.enable = false;
|
||||
load.state_machine();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state);
|
||||
}
|
||||
|
||||
void control_off_overvoltage()
|
||||
{
|
||||
PowerPort port;
|
||||
LoadOutput load(&port);
|
||||
port.init_load(14.6);
|
||||
port.pos_current_limit = 10;
|
||||
port.voltage = 14.7;
|
||||
load.state = LOAD_STATE_ON;
|
||||
|
||||
// increase debounce counter to 1 before limit
|
||||
for (int i = 0; i < CONTROL_FREQUENCY; i++) {
|
||||
load.control();
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
|
||||
load.control(); // once more
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state);
|
||||
}
|
||||
|
||||
extern float mcu_temp;
|
||||
|
||||
void control_off_overcurrent()
|
||||
{
|
||||
PowerPort port;
|
||||
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;
|
||||
load.state = LOAD_STATE_ON;
|
||||
mcu_temp = 25;
|
||||
load.junction_temperature = 25;
|
||||
|
||||
load.control();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
void load_tests()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
// state_machine tests
|
||||
RUN_TEST(disabled_to_on_if_everything_fine);
|
||||
RUN_TEST(disabled_to_off_low_soc_if_error_flag_set);
|
||||
RUN_TEST(disabled_to_off_bat_temp_if_error_flag_set);
|
||||
RUN_TEST(off_low_soc_to_on_after_delay);
|
||||
RUN_TEST(off_overcurrent_to_on_after_delay);
|
||||
RUN_TEST(off_overvoltage_to_on_at_lower_voltage);
|
||||
RUN_TEST(off_short_circuit_to_disabled);
|
||||
RUN_TEST(on_to_off_low_soc_if_error_flag_set);
|
||||
RUN_TEST(on_to_off_bat_temp_if_error_flag_set);
|
||||
RUN_TEST(on_to_disabled_if_enable_false);
|
||||
|
||||
// control tests
|
||||
RUN_TEST(control_off_overvoltage);
|
||||
RUN_TEST(control_off_overcurrent);
|
||||
|
||||
// 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();
|
||||
}
|
||||
Reference in New Issue
Block a user