Skip to content

Commit

Permalink
Use fixed settings for unit tests
Browse files Browse the repository at this point in the history
In order to ensure better testing use a specific test-settings file and
add the necessary changes to the makefile.
  • Loading branch information
Abász committed May 13, 2023
1 parent 9ddd3a1 commit 7b058fa
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 14 deletions.
39 changes: 36 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
//
// Documentation: https://docs.platformio.org/page/plus/debugging.html
// Configuration: https://docs.platformio.org/page/projectconf/section_env_debug.html

{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/run_e2e_test.out",
"program": "${workspaceFolder}/build/e2e/run_e2e_test.out",
"args": [
"threshold.txt"
],
Expand Down Expand Up @@ -39,7 +40,7 @@
"name": "C++ Test Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests.out",
"program": "${workspaceFolder}/build/unit/tests.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand All @@ -60,6 +61,38 @@
"ignoreFailures": true
}
]
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "${workspaceFolder}/esp-rowing-monitor/.pio/build/esp32/firmware.elf",
"projectEnvName": "esp32",
"toolchainBinDir": "${userHome}/.platformio/packages/[email protected]+2021r2-patch5/bin",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug"
}
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "${workspaceFolder}/esp-rowing-monitor/.pio/build/esp32/firmware.elf",
"projectEnvName": "esp32",
"toolchainBinDir": "${userHome}/.platformio/packages/[email protected]+2021r2-patch5/bin",
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (without uploading)",
"executable": "${workspaceFolder}/esp-rowing-monitor/.pio/build/esp32/firmware.elf",
"projectEnvName": "esp32",
"toolchainBinDir": "${userHome}/.platformio/packages/[email protected]+2021r2-patch5/bin",
"internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual"
}
]
}
}
17 changes: 12 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ TEST_DIR:=test
UNIT_TEST_DIR:=$(TEST_DIR)/unit
BUILD_DIR:=build
RMDIR_COMMAND=rm -f -r
MK_BUILD_DIR_COMMAND=mkdir -p $(BUILD_DIR)/$(dir $<)
MK_BUILD_DIR_COMMAND=mkdir -p $(dir $<)

ifeq ($(OS), Windows_NT)
RMDIR_COMMAND=if exist $(BUILD_DIR) rmdir /q /s
MK_BUILD_DIR_COMMAND=if not exist $(BUILD_DIR)\$(subst /,\,$(dir $<)) mkdir $(BUILD_DIR)\$(subst /,\,$(dir $<))
MK_BUILD_DIR_COMMAND=if not exist $(subst /,\,$(BUILD_DIR))\$(subst /,\,$(dir $<)) mkdir $(subst /,\,$(BUILD_DIR))\$(subst /,\,$(dir $<))
endif

INCLUDES:=-include test/Arduino.h -Itest
Expand All @@ -21,20 +21,27 @@ DEFINES:=-D LOCAL_SSID=testSSID -D PASSPHRASE=testPhrase
DEPFLAGS=-MT $@ -MMD -MP -MF $(BUILD_DIR)/$*.d

E2E_SRCS:=$(wildcard $(TEST_DIR)/*.cpp) $(wildcard $(LIB_DIR)/utils/*series.cpp) $(wildcard $(LIB_DIR)/rower/*.cpp)
E2E_OBJS:=$(E2E_SRCS:%.cpp=$(BUILD_DIR)/%.o)
E2E_OBJS:=$(E2E_SRCS:%.cpp=$(BUILD_DIR)/e2e/%.o)

e2e: BUILD_DIR:=$(BUILD_DIR)/e2e
e2e: $(E2E_OBJS)
@$(CC) $(CPP_FLAGS) $(INCLUDES) $(DEFINES) -o $(BUILD_DIR)/run_e2e_test.out $^

TEST_SRCS:=$(wildcard $(LIB_DIR)/utils/*series.cpp) $(wildcard $(LIB_DIR)/rower/*.cpp) $(UNIT_TEST_DIR)/catch_amalgamated.cpp $(filter-out $(TEST_DIR)/main.cpp, $(wildcard $(TEST_DIR)/*.cpp)) $(wildcard $(TEST_SRC))

TEST_OBJS:=$(TEST_SRCS:%.cpp=$(BUILD_DIR)/%.o)
TEST_OBJS:=$(TEST_SRCS:%.cpp=$(BUILD_DIR)/test/%.o)

test: BUILD_DIR:=$(BUILD_DIR)/test
test: DEFINES+=-D UNIT_TEST
test: $(TEST_OBJS)
@$(CC) $(CPP_FLAGS) $(INCLUDES) $(DEFINES) -o $(BUILD_DIR)/tests.out $^
@./$(BUILD_DIR)/tests.out

$(BUILD_DIR)/%.o: %.cpp
$(BUILD_DIR)/test/%.o: %.cpp
@$(MK_BUILD_DIR_COMMAND)
@$(CC) $(CPP_FLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@ $(DEPFLAGS)

$(BUILD_DIR)/e2e/%.o: %.cpp
@$(MK_BUILD_DIR_COMMAND)
@$(CC) $(CPP_FLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@ $(DEPFLAGS)

Expand Down
2 changes: 0 additions & 2 deletions src/rower/stroke.service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include "Arduino.h"

#include "ArduinoLog.h"

#include "globals.h"
#include "stroke.service.h"

Expand Down
1 change: 0 additions & 1 deletion src/utils/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "Arduino.h"

#include "../settings.h"
#include "macros.h"

using std::string;
Expand Down
6 changes: 5 additions & 1 deletion src/utils/macros.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

#include "../settings.h"
#ifdef UNIT_TEST
#include "../../test/unit/test-settings.h"
#else
#include "../settings.h"
#endif
#include "enums.h"

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
Expand Down
38 changes: 36 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,43 @@ void loop(unsigned long now)
int main(int argc, const char *argv[])
{
auto const args = std::span(argv, size_t(argc));

unsigned long now = 0;

if (args[1] == string("simulate"))
{
auto const minTimeThreshold = 40000UL;
auto const maxTimeThreshold = 45000UL;
unsigned long timeThreshold = maxTimeThreshold;
const size_t impulseCount = 20000;
bool direction = false;
for (size_t i = 0; i < impulseCount; i++)
{
now += timeThreshold;

loop(now);

if (direction)
{
timeThreshold += 1000;
}
if (!direction)
{
timeThreshold -= 1000;
}
if (timeThreshold == minTimeThreshold)
{
direction = true;
}

if (timeThreshold == maxTimeThreshold)
{
direction = false;
}
}

return 0;
}

if (argc < 2 || string(args[1]).empty())
{
for (auto const &deltaTime : testDeltaTimes)
Expand All @@ -51,7 +86,6 @@ int main(int argc, const char *argv[])

std::ifstream deltaTimeStream(args[1]);
printf("Running external file: %s\n", args[1]);

while (deltaTimeStream >> deltaTime)
{
now += deltaTime;
Expand Down
54 changes: 54 additions & 0 deletions test/unit/test-settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "../src/utils/enums.h"

// NOLINTBEGIN(cppcoreguidelines-macro-usage)

#define DEFAULT_CPS_LOGGING_LEVEL ArduinoLogLevel::LogLevelTrace
#define DEFAULT_BLE_SERVICE BleServiceFlag::CpsService
#define ENABLE_WEBSOCKET_MONITOR true
#define ENABLE_BLE_SERVICE true

// Hardware settings
#define SENSOR_PIN_NUMBER GPIO_NUM_26
#define IMPULSES_PER_REVOLUTION 3
#define FLYWHEEL_INERTIA 0.073
#define LED_BLINK_FREQUENCY 1000
#define SPROCKET_RADIUS 1.50
#define CONCEPT_2_MAGIC_NUMBER 2.8

// Sensor signal filter settings
#define ROTATION_DEBOUNCE_TIME_MIN 7
#define ROWING_STOPPED_THRESHOLD_PERIOD 7000

// Drag factor filter settings
#define GOODNESS_OF_FIT_THRESHOLD 0.97
#define MAX_DRAG_FACTOR_RECOVERY_PERIOD 6000
#define LOWER_DRAG_FACTOR_THRESHOLD 75
#define UPPER_DRAG_FACTOR_THRESHOLD 250
#define DRAG_COEFFICIENTS_ARRAY_LENGTH 1

// Stroke phase detection filter settings
#define MINIMUM_POWERED_TORQUE 0
#define MINIMUM_DRAG_TORQUE 0
#define STROKE_DETECTION_TYPE STROKE_DETECTION_TORQUE
#define MINIMUM_RECOVERY_SLOPE_MARGIN 0.0000022 // Only relevant if STROKE_DETECTION_TYPE is either BOTH or TORQUE
#define MINIMUM_RECOVERY_SLOPE 0.01 // Only relevant if STROKE_DETECTION_TYPE is either BOTH or SLOPE
#define STROKE_DEBOUNCE_TIME 300
#define IMPULSE_DATA_ARRAY_LENGTH 7
// #define FLOATING_POINT_PRECISION PRECISION_DOUBLE

// Network settings
#define PORT 80

// Device power management settings
#define BATTERY_PIN_NUMBER GPIO_NUM_4
#define VOLTAGE_DIVIDER_RATIO 2
#define BATTERY_VOLTAGE_MIN 3.3
#define BATTERY_VOLTAGE_MAX 4.00
#define BATTERY_LEVEL_ARRAY_LENGTH 5
#define INITIAL_BATTERY_LEVEL_MEASUREMENT_COUNT 10
#define BATTERY_MEASUREMENT_FREQUENCY 10
#define DEEP_SLEEP_TIMEOUT 4

// NOLINTEND(cppcoreguidelines-macro-usage)

0 comments on commit 7b058fa

Please sign in to comment.