Skip to content

Commit 1de5a4f

Browse files
committed
Issue 9: Introduce unit tests
1 parent b731b3b commit 1de5a4f

25 files changed

+1207
-62
lines changed

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(ecu_sw_bb)
2+
project(kpi_rover_ecu)
33

4-
# Add librobotcontrol subdirectory
5-
option(BUILD_LIBROBOT_CONTROL "Build librobotcontrol using original Makefile" ON)
6-
add_subdirectory(external/librobotcontrol/library)
4+
if(BUILD_VARIANT STREQUAL "Target")
5+
option(BUILD_LIBROBOT_CONTROL "Build librobotcontrol using original Makefile" ON)
6+
add_subdirectory(external/librobotcontrol)
7+
add_subdirectory(src/kpi_rover_ecu)
8+
endif()
79

8-
# Create interface library for librobotcontrol (not needed anymore, using target directly)
9-
add_subdirectory(src/kpi_rover_ecu)
10+
if(BUILD_VARIANT STREQUAL "Tests")
11+
enable_testing()
12+
add_subdirectory(src/kpi_rover_ecu/src)
13+
add_subdirectory(src/kpi_rover_ecu/tests)
14+
endif()

build.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
#!/bin/bash
22

3+
4+
5+
BUILD_VARIANT="Target"
6+
7+
TARGET_OPTIONS="-DBUILD_VARIANT=${BUILD_VARIANT}"
8+
39
# Ensure build directory exists
4-
mkdir -p build/target
10+
mkdir -p build/${BUILD_VARIANT}
511

612
# Check if the "nocache" argument is passed
713
if [[ "$1" == "nocache" ]]; then
814
echo "-- Clearing CMake cache..."
9-
rm -rf build/target/*
15+
rm -rf build/${BUILD_VARIANT}/*
1016
fi
1117

18+
1219
# Run the Docker container with CMake commands
1320
docker run -it --rm \
1421
-u $(id -u ${USER}):$(id -g ${USER}) \
1522
-v $(pwd):/workspace \
1623
kpi-rover-bbb-build \
17-
bash -c "cmake -B build/target -H. && cmake --build build/target -- -j$(nproc)"
24+
bash -c "cmake -B build/${BUILD_VARIANT} -H. -H. ${TARGET_OPTIONS} \
25+
&& cmake --build build/${BUILD_VARIANT} -- -j$(nproc)"

check_cs.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ docker run -it --rm \
55
-v $(pwd):/workspace \
66
kpi-rover-bbb-check \
77
bash -c "
8-
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build/check && ls build/check/compile_commands.json && \
9-
find src/kpi_rover_ecu/ -name '*.cpp' -o -name '*.h' | xargs --no-run-if-empty clang-format --dry-run --Werror; \
10-
find src/kpi_rover_ecu/ -name '*.cpp' -o -name '*.h' | xargs --no-run-if-empty clang-tidy -p=build/check --warnings-as-errors='*'
8+
cmake -DBUILD_VARIANT=Target -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build/check && ls build/check/compile_commands.json && \
9+
find src/kpi_rover_ecu/src src/kpi_rover_ecu/include -name '*.cpp' -o -name '*.h' | xargs --no-run-if-empty clang-format --dry-run --Werror; \
10+
find src/kpi_rover_ecu/src src/kpi_rover_ecu/include -name '*.cpp' -o -name '*.h' | xargs --no-run-if-empty clang-tidy -p=build/check --warnings-as-errors='*'
1111
"
1212

deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [ -z "$BBB_HOST" ]; then
88
fi
99

1010
# Deploy the software using scp
11-
scp build/target/src/kpi_rover_ecu/kpi_rover_ecu "$BBB_HOST":~
11+
scp build/Target/src/kpi_rover_ecu/kpi_rover_ecu "$BBB_HOST":~
1212

1313
# Check if scp was successful
1414
if [ $? -eq 0 ]; then

docker/Dockerfile.check

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ RUN apt-get update -q && \
1414
python3 \
1515
python3-pip \
1616
cmake \
17+
lcov \
18+
gcc \
19+
g++ \
20+
build-essential \
21+
make \
22+
git \
1723
&& rm -rf /var/lib/apt/lists/*
1824

19-
# RUN ln -s /usr/bin/arm-linux-gnueabihf-g++-8 /usr/bin/arm-linux-gnueabihf-g++ && \
20-
# ln -s /usr/bin/arm-linux-gnueabihf-gcc-8 /usr/bin/arm-linux-gnueabihf-gcc && \
21-
# ln -s /usr/bin/arm-linux-gnueabihf-cpp-8 /usr/bin/arm-linux-gnueabihf-cpp
22-
2325
ENV AS=/usr/bin/arm-linux-gnueabihf-as \
2426
AR=/usr/bin/arm-linux-gnueabihf-ar \
2527
CC=/usr/bin/arm-linux-gnueabihf-gcc \
@@ -29,9 +31,4 @@ ENV AS=/usr/bin/arm-linux-gnueabihf-as \
2931

3032
RUN echo "alias ll='ls -alF --color=auto'" >> /etc/bash.bashrc
3133

32-
RUN apt-get update -q && \
33-
apt-get install -y -q \
34-
git \
35-
&& rm -rf /var/lib/apt/lists/*
36-
3734
WORKDIR /workspace

src/kpi_rover_ecu/CMakeLists.txt

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,26 @@ cmake_minimum_required(VERSION 3.10)
22
project(kpi_rover_ecu)
33

44
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
56

67
set(THREADS_PREFER_PTHREAD_FLAG ON)
78
find_package(Threads REQUIRED)
89

9-
# Update include path to be relative to this CMakeLists.txt location
10-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
11-
12-
set(SOURCE_FILES
13-
src/main.cpp
14-
src/motorsController.cpp
15-
src/motor.cpp
16-
src/protocolHandler.cpp
17-
src/TCPTransport.cpp
18-
src/motorConfig.cpp
19-
src/messageQueue.cpp
20-
src/KPIRoverECU.cpp
10+
# Add include directories globally so all targets can find the headers
11+
include_directories(
12+
${CMAKE_CURRENT_SOURCE_DIR}/include
2113
)
2214

23-
set(HEADER_FILES
24-
include/motorsController.h
25-
include/motor.h
26-
include/protocolHandler.h
27-
include/TCPTransport.h
28-
include/ITransport.h
29-
include/motorConfig.h
30-
include/messageQueue.h
31-
include/KPIRoverECU.h
32-
)
15+
add_subdirectory(src)
3316

34-
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES})
17+
add_executable(${PROJECT_NAME} src/main.cpp)
3518

36-
# Add include directories to target properties
37-
target_include_directories(${PROJECT_NAME} PRIVATE
38-
${CMAKE_CURRENT_SOURCE_DIR}/include
39-
)
40-
41-
# Link the pthread library and robotcontrol
42-
target_link_libraries(${PROJECT_NAME} PRIVATE
19+
target_link_libraries(${PROJECT_NAME} PRIVATE
20+
kpi_rover_ecu_core
4321
Threads::Threads
4422
robotcontrol
4523
)
4624

25+
target_include_directories(${PROJECT_NAME} PRIVATE
26+
${CMAKE_CURRENT_SOURCE_DIR}/include
27+
)

src/kpi_rover_ecu/include/motor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef MOTOR_H
22
#define MOTOR_H
33

4-
#include <robotcontrol.h>
4+
55
#include <iostream>
66

77
#define MIN_RPM 8000
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_library(kpi_rover_ecu_core STATIC
2+
KPIRoverECU.cpp
3+
motor.cpp
4+
motorsController.cpp
5+
protocolHandler.cpp
6+
TCPTransport.cpp
7+
motorConfig.cpp
8+
messageQueue.cpp
9+
)
10+
11+
# Include public headers
12+
target_include_directories(kpi_rover_ecu_core PUBLIC
13+
${CMAKE_SOURCE_DIR}/src/kpi_rover_ecu/include
14+
${CMAKE_SOURCE_DIR}/external/librobotcontrol/library/include
15+
)

src/kpi_rover_ecu/src/KPIRoverECU.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "KPIRoverECU.h"
2+
#include <rc/time.h>
23

34
// #include "TCPTransport.h"
45
// #include "config.h"

src/kpi_rover_ecu/src/motor.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
#include "motor.h"
2+
#include <rc/motor.h>
3+
#include <rc/encoder.h>
4+
#include <rc/time.h>
25

36
Motor::Motor(int assignedNumber, bool isInverted) : motorNumber_(assignedNumber), inverted_(isInverted), currentDutyCycle_(0) {}
47

5-
68
int Motor::MotorGo(int newRPM) {
9+
if (newRPM > MAX_RPM ) {
10+
std::cout << "[Warning] RPM out of range" << '\n';
11+
newRPM = MAX_RPM;
12+
}
13+
14+
if (newRPM < MIN_RPM) {
15+
std::cout << "[Warning] RPM out of range" << '\n';
16+
newRPM = MIN_RPM;
17+
}
18+
719
currentDutyCycle_ = GetDC(newRPM);
820
if (inverted_) {
921
currentDutyCycle_ = -currentDutyCycle_;
@@ -23,13 +35,6 @@ int Motor::MotorStop() const {
2335
return -1;
2436
}
2537

26-
rc_usleep(BRAKE_TIME);
27-
28-
if (rc_motor_free_spin(motorNumber_) != 0) {
29-
std::cout << "[ERROR][RC] rc_motor_free_spin" << '\n';
30-
return -1;
31-
}
32-
3338
return 0;
3439
}
3540

0 commit comments

Comments
 (0)