Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(INCLUDE_DIRS
${PROJECT_MODULE_DIR}
${PROJECT_MODULE_DIR}/CameraCapture
${PROJECT_MODULE_DIR}/calculators
${PROJECT_MODULE_DIR}/common
${PROJECT_MODULE_DIR}/API
${PROJECT_TEST_DIR}
${PROJECT_TEST_DIR}/dummy
Expand Down Expand Up @@ -80,4 +81,4 @@ target_link_libraries(${PROJECT_NAME}_test

# テストの登録
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}_test)
gtest_discover_tests(${PROJECT_NAME}_test)
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ RUN apt-get update && apt-get install -y \
git \
cmake \
sudo \
clang-format \
libopencv-dev \
&& gem install shell \
&& apt-get clean && rm -rf /var/lib/apt/lists/*

# ビルド時に日本語が含まれていたらエラーになるので,C.UTF-8を指定
ENV LANG=C.UTF-8
ENV LANG=C.UTF-8

# RasPike-ART を clone
# libraspike-artは,RasPike-ART内で運営がリンクで紐づけていたため,直接cloneする必要がある
Expand Down
32 changes: 30 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,38 @@ format:
# 指定ファイルがある場合、そのファイルにclang-formatを適用する
ifdef FILES
clang-format -i -style=file $(FILES)
@ echo "フォーマットを適用しました: $(FILES)"
# ない場合、変更されたファイルのうち、cpp、hファイルにclang-formatを適用する
else
git diff origin/main --name-only | awk '/\.cpp$$|\.h$$/ {print $$1}' | xargs clang-format -i -style=file
# 変更されたファイルと未追跡のファイル (新規追加など) の両方を検出
@{ \
CANDIDATE_FILES=$$( (git diff origin/main --name-only; git ls-files --others --exclude-standard) | grep -E '\.cpp$$|\.h$$' || true ); \
ACTUALLY_FORMATTED_FILES=""; \
FORMATTED_COUNT=0; \
if [ -n "$$CANDIDATE_FILES" ]; then \
for FILE_PATH in $$CANDIDATE_FILES; do \
REPLACEMENTS_XML=$$(clang-format -style=file --output-replacements-xml "$$FILE_PATH" 2>/dev/null); \
if echo "$$REPLACEMENTS_XML" | grep -q "<replacement "; then \
clang-format -i -style=file "$$FILE_PATH"; \
if [ -z "$$ACTUALLY_FORMATTED_FILES" ]; then \
ACTUALLY_FORMATTED_FILES="$$FILE_PATH"; \
else \
ACTUALLY_FORMATTED_FILES="$${ACTUALLY_FORMATTED_FILES}\n$$FILE_PATH"; \
fi; \
FORMATTED_COUNT=$$(($$FORMATTED_COUNT + 1)); \
fi; \
done; \
if [ $$FORMATTED_COUNT -gt 0 ]; then \
echo "以下のファイルにフォーマットを適用しました ($$FORMATTED_COUNT 件):"; \
printf "%b\n" "$$ACTUALLY_FORMATTED_FILES"; \
else \
echo "検査したファイルは全てフォーマット済みでした。適用された変更はありません。"; \
fi; \
else \
echo "フォーマットをチェックする対象の .cpp または .h ファイルがありません。"; \
fi \
}
endif

format-check:
find ./test ./modules -type f -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror *.h *.cpp
find ./tests ./modules -type f -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror *.h *.cpp
2 changes: 2 additions & 0 deletions Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ APPL_DIRS += \
$(mkfile_path)modules\
$(mkfile_path)modules/API\
$(mkfile_path)modules/calculators\
$(mkfile_path)modules/common\
$(mkfile_path)modules/CameraCapture

INCLUDES += \
-I$(mkfile_path)modules\
-I$(mkfile_path)modules/API\
-I$(mkfile_path)modules/calculators\
-I$(mkfile_path)modules/common\
-I$(mkfile_path)modules/CameraCapture \
-I/usr/include/opencv4

Expand Down
25 changes: 25 additions & 0 deletions modules/calculators/Mileage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
** @file Mileage.cpp
** @brief 走行距離を計算するクラス
** @author molpui0726
**/

#include "Mileage.h"

// MotorController でモーターの角位置を int32_t 型で取得しているため、
// double ではなく int32_t 型を使用する
double Mileage::calculateWheelMileage(int32_t angle)
{
// タイヤの累計走行距離 = 2 * π * タイヤの半径 * (タイヤの回転角度 / 360[deg])
return 2.0 * M_PI * RADIUS * static_cast<double>(angle) / 360.0;
}

double Mileage::calculateMileage(int32_t rightAngle, int32_t leftAngle)
{
// 右タイヤの累計走行距離を計算
double rightWheelMileage = calculateWheelMileage(rightAngle);
// 左タイヤの累計走行距離を計算
double leftWheelMileage = calculateWheelMileage(leftAngle);
// 走行体全体の累計走行距離 = (右タイヤの累計走行距離 + 左タイヤの累計走行距離) / 2
return (rightWheelMileage + leftWheelMileage) / 2.0;
}
34 changes: 34 additions & 0 deletions modules/calculators/Mileage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
** @file Mileage.h
** @brief 走行距離を計算するクラス
** @author molpui0726
**/

#ifndef MILEAGE_H
#define MILEAGE_H

#include <cmath>
#include "SystemInfo.h"

class Mileage {
public:
/**
** @brief タイヤの累計走行距離を計算する
** @param angle タイヤの回転角度[deg]
** @return タイヤの累計走行距離[mm]
**/
static double calculateWheelMileage(int32_t angle);

/**
** @brief 走行体全体の累計走行距離を計算する
** @param rightAngle 右タイヤの回転角度[deg]
** @param leftAngle 左タイヤの回転角度[deg]
** @return 走行体全体の累計走行距離[mm]
**/
static double calculateMileage(int32_t rightAngle, int32_t leftAngle);

private:
Mileage(); // インスタンス化を禁止する
};

#endif
13 changes: 13 additions & 0 deletions modules/common/SystemInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file SystemInfo.h
* @brief 走行システムで統一する情報をまとめたファイル
* @author molpui0726
*/

#ifndef SYSTEM_INFO_H
#define SYSTEM_INFO_H

static constexpr double RADIUS = 28.0; // 車輪の半径[mm]
static constexpr double TREAD = 112.0; // 走行体のトレッド幅(両輪の間の距離)[mm]

#endif
60 changes: 60 additions & 0 deletions tests/MileageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file MileageTest.cpp
* @brief 走行距離の測定用クラスのテスト
* @author molpui0726
*/

#include <cmath>
#include <gtest/gtest.h>
#include "Mileage.h"

namespace etrobocon2025_test {
TEST(MileageTest, CalculateMileagePlus)
{
int32_t rightAngle = 30;
int32_t leftAngle = 40;

// 計算過程
// 1.右車輪の累計走行距離を算出
// double rightWheelMileage = 2.0 * rightAngle * radius * M_PI / 360.0;
// M_PI = 3.14: rightWheelMileage = 2.0 * 30.0 * 28.0 * 3.14 / 360.0 = 14.6533...
// M_PI = 3.15: rightWheelMileage = 2.0 * 30.0 * 28.0 * 3.15 / 360.0 = 14.70
// 2.左車輪の累計走行距離を算出
// double leftWheelMileage = 2.0 * leftAngle * radius * M_PI / 360.0;
// M_PI = 3.14: leftWheelMileage = 2.0 * 40.0 * 28.0 * 3.14 / 360.0 = 19.53777...
// M_PI = 3.15: leftWheelMileage = 2.0 * 40.0 * 28.0 * 3.15 / 360.0 = 19.60
// 3.両車輪の累計走行距離の平均を算出
// double expected = (rightWheelMileage + leftWheelMileage) / 2.0;
// M_PI = 3.14: (14.6533... + 19.5377...) / 2 = 17.09555...
// M_PI = 3.15: (14.70 + 19.60) / 2 = 17.15
double expected_min = 17.09555;
double expected_max = 17.15;

// actual: 17.104227
double actual = Mileage::calculateMileage(rightAngle, leftAngle);

// M_PIが3.14で計算した値よりも大きいアサーションと、3.15で計算した値よりも小さいアサーションを両方満たすか
EXPECT_LT(expected_min, actual);
EXPECT_GT(expected_max, actual);
}

TEST(MileageTest, CalculateMileageMinus)
{
int32_t rightAngle = -30;
int32_t leftAngle = -40;
double expected_min = -17.15;
double expected_max = -17.09555;
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
EXPECT_LT(expected_min, actual);
EXPECT_GT(expected_max, actual);
}

TEST(MileageTest, CalculateMileageZero)
{
int32_t rightAngle = 0;
int32_t leftAngle = 0;
double expected = 0.0;
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
EXPECT_DOUBLE_EQ(expected, actual);
}
} // namespace etrobocon2025_test