mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-06 03:50:13 +02:00
Improvements and unit tests
This commit is contained in:
parent
4d95fcfc31
commit
44c02745bf
9 changed files with 425 additions and 2 deletions
|
@ -1,27 +1,48 @@
|
|||
#include "events.h"
|
||||
|
||||
#include "../../../USER_SETTINGS.h"
|
||||
#include "../config.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t timestamp; // Time in seconds since startup when the event occurred
|
||||
uint8_t data; // Custom data passed when setting the event, for example cell number for under voltage
|
||||
uint8_t occurences; // Number of occurrences since startup
|
||||
uint8_t led_color; // Weirdly indented comment
|
||||
} EVENTS_STRUCT_TYPE;
|
||||
|
||||
static EVENTS_STRUCT_TYPE entries[EVENT_NOF_EVENTS];
|
||||
static unsigned long previous_millis = 0;
|
||||
static uint32_t time_seconds = 0;
|
||||
static uint8_t total_led_color = GREEN;
|
||||
|
||||
/* Local function prototypes */
|
||||
static void print_event_message(EVENTS_ENUM_TYPE event);
|
||||
static void update_led_color(EVENTS_ENUM_TYPE event);
|
||||
|
||||
/* Exported functions */
|
||||
void init_events(void) {
|
||||
for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) {
|
||||
entries[i].timestamp = 0;
|
||||
entries[i].data = 0;
|
||||
entries[i].occurences = 0;
|
||||
entries[i].led_color = RED; // Most events are RED
|
||||
}
|
||||
|
||||
// YELLOW events below
|
||||
entries[EVENT_12V_LOW].led_color = YELLOW;
|
||||
entries[EVENT_CAN_WARNING].led_color = YELLOW;
|
||||
entries[EVENT_CELL_DEVIATION_HIGH].led_color = YELLOW;
|
||||
entries[EVENT_KWH_PLAUSIBILITY_ERROR].led_color = YELLOW;
|
||||
}
|
||||
|
||||
void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
|
||||
if (event >= EVENT_NOF_EVENTS) {
|
||||
event = EVENT_UNKNOWN_EVENT_SET;
|
||||
}
|
||||
entries[event].timestamp = time_seconds;
|
||||
entries[event].data = data;
|
||||
entries[event].occurences++;
|
||||
print_event_message(event);
|
||||
}
|
||||
|
||||
void update_event_timestamps(void) {
|
||||
|
@ -31,3 +52,75 @@ void update_event_timestamps(void) {
|
|||
previous_millis = new_millis;
|
||||
}
|
||||
}
|
||||
|
||||
/* Local functions */
|
||||
static void update_led_color(EVENTS_ENUM_TYPE event) {
|
||||
total_led_color = (total_led_color == RED) ? RED : entries[event].led_color;
|
||||
}
|
||||
|
||||
static void print_event_message(EVENTS_ENUM_TYPE event) {
|
||||
#ifndef DEBUG_VIA_USB
|
||||
return;
|
||||
#else
|
||||
switch (event) {
|
||||
case EVENT_CAN_FAILURE:
|
||||
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
|
||||
break;
|
||||
case EVENT_CAN_WARNING:
|
||||
Serial.println("ERROR: High amount of corrupted CAN messages detected. Check CAN wire shielding!");
|
||||
break;
|
||||
case EVENT_WATER_INGRESS:
|
||||
Serial.println("Water leakage inside battery detected. Operation halted. Inspect battery!");
|
||||
break;
|
||||
case EVENT_12V_LOW:
|
||||
Serial.println(
|
||||
"12V battery source below required voltage to safely close contactors. Inspect the supply/battery!");
|
||||
break;
|
||||
case EVENT_SOC_PLAUSIBILITY_ERROR:
|
||||
Serial.println("ERROR: SOC% reported by battery not plausible. Restart battery!");
|
||||
break;
|
||||
case EVENT_KWH_PLAUSIBILITY_ERROR:
|
||||
Serial.println("Warning: kWh remaining reported by battery not plausible. Battery needs cycling.");
|
||||
break;
|
||||
case EVENT_BATTERY_CHG_STOP_REQ:
|
||||
Serial.println("ERROR: Battery raised caution indicator AND requested charge stop. Inspect battery status!");
|
||||
break;
|
||||
case EVENT_BATTERY_DISCHG_STOP_REQ:
|
||||
Serial.println("ERROR: Battery raised caution indicator AND requested discharge stop. Inspect battery status!");
|
||||
break;
|
||||
case EVENT_BATTERY_CHG_DISCHG_STOP_REQ:
|
||||
Serial.println(
|
||||
"ERROR: Battery raised caution indicator AND requested charge/discharge stop. Inspect battery status!");
|
||||
break;
|
||||
case EVENT_LOW_SOH:
|
||||
Serial.println(
|
||||
"ERROR: State of health critically low. Battery internal resistance too high to continue. Recycle battery.");
|
||||
break;
|
||||
case EVENT_HVIL_FAILURE:
|
||||
Serial.println(
|
||||
"ERROR: Battery interlock loop broken. Check that high voltage connectors are seated. Battery will be "
|
||||
"disabled!");
|
||||
break;
|
||||
case EVENT_INTERNAL_OPEN_FAULT:
|
||||
Serial.println("ERROR: High voltage cable removed while battery running. Opening contactors!");
|
||||
break;
|
||||
case EVENT_CELL_UNDER_VOLTAGE:
|
||||
Serial.println("ERROR: CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
|
||||
break;
|
||||
case EVENT_CELL_OVER_VOLTAGE:
|
||||
Serial.println("ERROR: CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
|
||||
break;
|
||||
case EVENT_CELL_DEVIATION_HIGH:
|
||||
Serial.println("ERROR: HIGH CELL DEVIATION!!! Inspect battery!");
|
||||
break;
|
||||
case EVENT_UNKNOWN_EVENT_SET:
|
||||
Serial.println("An unknown event was set! Review your code!");
|
||||
break;
|
||||
case EVENT_DUMMY:
|
||||
Serial.println("The dummy event was set!");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#ifndef __EVENTS_H__
|
||||
#define __EVENTS_H__
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
EVENT_CAN_FAILURE = 0u,
|
||||
|
@ -10,12 +12,16 @@ typedef enum {
|
|||
EVENT_12V_LOW,
|
||||
EVENT_SOC_PLAUSIBILITY_ERROR,
|
||||
EVENT_KWH_PLAUSIBILITY_ERROR,
|
||||
EVENT_BATTERY_CHG_STOP_REQ,
|
||||
EVENT_BATTERY_DISCHG_STOP_REQ,
|
||||
EVENT_BATTERY_CHG_DISCHG_STOP_REQ,
|
||||
EVENT_LOW_SOH,
|
||||
EVENT_HVIL_FAILURE,
|
||||
EVENT_INTERNAL_OPEN_FAULT,
|
||||
EVENT_CELL_UNDER_VOLTAGE,
|
||||
EVENT_CELL_OVER_VOLTAGE,
|
||||
EVENT_CELL_DEVIATION_HIGH,
|
||||
EVENT_UNKNOWN_EVENT_SET,
|
||||
EVENT_DUMMY,
|
||||
EVENT_NOF_EVENTS
|
||||
} EVENTS_ENUM_TYPE;
|
||||
|
|
65
Software/src/devboard/utils/events_test.cpp
Normal file
65
Software/src/devboard/utils/events_test.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
// The test library must be included first!
|
||||
#include "../../../test/test_lib.h"
|
||||
|
||||
#include "events.cpp"
|
||||
|
||||
TEST(init_events_test) {
|
||||
init_events();
|
||||
|
||||
for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) {
|
||||
ASSERT_EQ(entries[i].occurences, 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(update_event_timestamps_test) {
|
||||
// Reset
|
||||
init_events();
|
||||
time_seconds = 0;
|
||||
|
||||
// No delta, so time shouldn't increase
|
||||
test_millis = 0;
|
||||
update_event_timestamps();
|
||||
ASSERT_EQ(time_seconds, 0);
|
||||
|
||||
// Almost time to bump the seconds
|
||||
test_millis = 999;
|
||||
update_event_timestamps();
|
||||
ASSERT_EQ(time_seconds, 0);
|
||||
ASSERT_EQ(previous_millis, 0);
|
||||
|
||||
// millis == 1000, so we should add a second
|
||||
test_millis = 1000;
|
||||
update_event_timestamps();
|
||||
ASSERT_EQ(time_seconds, 1);
|
||||
ASSERT_EQ(previous_millis, 1000);
|
||||
|
||||
// We shouldn't add more seconds until 2000 now
|
||||
test_millis = 1999;
|
||||
update_event_timestamps();
|
||||
ASSERT_EQ(time_seconds, 1);
|
||||
ASSERT_EQ(previous_millis, 1000);
|
||||
test_millis = 2000;
|
||||
update_event_timestamps();
|
||||
ASSERT_EQ(time_seconds, 2);
|
||||
ASSERT_EQ(previous_millis, 2000);
|
||||
}
|
||||
|
||||
TEST(set_event_test) {
|
||||
// Reset
|
||||
init_events();
|
||||
time_seconds = 0;
|
||||
|
||||
// Initially, the event should not have any data or occurences
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].data, 0);
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].occurences, 0);
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].timestamp, 0);
|
||||
// Set current time and overvoltage event for cell 23
|
||||
time_seconds = 345;
|
||||
set_event(EVENT_CELL_OVER_VOLTAGE, 123);
|
||||
// Ensure proper event data
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].data, 123);
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].occurences, 1);
|
||||
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].timestamp, 345);
|
||||
}
|
||||
|
||||
TEST_MAIN();
|
1
Software/src/devboard/utils/test_commands.txt
Normal file
1
Software/src/devboard/utils/test_commands.txt
Normal file
|
@ -0,0 +1 @@
|
|||
g++ events_test.cpp ../../../test/test_lib.cpp -o events_test.exe -DUNIT_TEST -I.
|
Loading…
Add table
Add a link
Reference in a new issue