Skip to content

Commit

Permalink
Unit test attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabooman committed Feb 5, 2024
1 parent 44c0274 commit da8421d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 28 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build and Test

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

This comment has been minimized.

Copy link
@lenvm

lenvm Feb 6, 2024

Collaborator

@Cabooman, please ensure to use checkout@v4, rather than checkout@v2

This comment has been minimized.

Copy link
@Cabooman

Cabooman Feb 7, 2024

Author Collaborator

Roger!


- name: Configure and build with CMake
run: |
mkdir build
cd build
cmake ..
cmake --build .
- name: Run unit tests
run: |
cd build
./Software/test/Debug/events_test.exe
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
# Ignore any files in the build folder
Software/build/

# Ignore CMake build folder
build/

# Ignore unit tests
*.exe
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.10)

project(BatteryEmulator)

add_subdirectory(Software/src/devboard/utils)
add_subdirectory(Software/test)
1 change: 1 addition & 0 deletions Software/src/devboard/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# add_library(utils_library events.cpp)
66 changes: 38 additions & 28 deletions Software/src/devboard/utils/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ 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;
static char event_message[256];

/* Local function prototypes */
static void print_event_message(EVENTS_ENUM_TYPE event);
static void set_event_message(EVENTS_ENUM_TYPE event);
static void update_led_color(EVENTS_ENUM_TYPE event);

/* Exported functions */
Expand All @@ -42,7 +43,10 @@ void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
entries[event].timestamp = time_seconds;
entries[event].data = data;
entries[event].occurences++;
print_event_message(event);
set_event_message(event);
#ifdef DEBUG_VIA_USB
Serial.println(event_message);
#endif
}

void update_event_timestamps(void) {
Expand All @@ -58,69 +62,75 @@ 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
static void set_event_message(EVENTS_ENUM_TYPE event) {
switch (event) {
case EVENT_CAN_FAILURE:
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message), "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.");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"ERROR: Battery raised caution indicator AND requested charge/discharge stop. Inspect battery status!");
break;
case EVENT_LOW_SOH:
Serial.println(
snprintf(
event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"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!");
snprintf(event_message, sizeof(event_message),
"ERROR: CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
break;
case EVENT_CELL_DEVIATION_HIGH:
Serial.println("ERROR: HIGH CELL DEVIATION!!! Inspect battery!");
snprintf(event_message, sizeof(event_message), "ERROR: HIGH CELL DEVIATION!!! Inspect battery!");
break;
case EVENT_UNKNOWN_EVENT_SET:
Serial.println("An unknown event was set! Review your code!");
snprintf(event_message, sizeof(event_message), "An unknown event was set! Review your code!");
break;
case EVENT_DUMMY:
Serial.println("The dummy event was set!");
snprintf(event_message, sizeof(event_message), "The dummy event was set!"); // Don't change this event message!
break;
default:
break;
}
#endif
}
2 changes: 2 additions & 0 deletions Software/src/devboard/utils/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <Arduino.h>
#endif

#include <stdint.h>

typedef enum {
EVENT_CAN_FAILURE = 0u,
EVENT_CAN_WARNING,
Expand Down
13 changes: 13 additions & 0 deletions Software/src/devboard/utils/events_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include "events.cpp"

/* Helper functions */
static void reset_event_msg(void) {
snprintf(event_message, sizeof(event_message), "");
}

TEST(init_events_test) {
init_events();

Expand Down Expand Up @@ -62,4 +67,12 @@ TEST(set_event_test) {
ASSERT_EQ(entries[EVENT_CELL_OVER_VOLTAGE].timestamp, 345);
}

TEST(event_message_test) {
reset_event_msg();

set_event(EVENT_DUMMY, 0); // Set dummy event with no data

ASSERT_STREQ("The dummy event was set!", event_message);
}

TEST_MAIN();
5 changes: 5 additions & 0 deletions Software/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(events_test ../src/devboard/utils/events_test.cpp test_lib.cpp)

target_compile_definitions(events_test PRIVATE UNIT_TEST)

target_link_libraries(events_test) # Link to the library from devboard/utils

0 comments on commit da8421d

Please sign in to comment.