Skip to content

Commit 82a5ded

Browse files
authored
Merge pull request #7 from KatLab-MiyazakiUniv/ticket-KL25-31
#KL25-31 累計距離を測るクラスを作る
2 parents bc69083 + af5efe8 commit 82a5ded

File tree

8 files changed

+168
-4
lines changed

8 files changed

+168
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(INCLUDE_DIRS
3737
${PROJECT_MODULE_DIR}
3838
${PROJECT_MODULE_DIR}/CameraCapture
3939
${PROJECT_MODULE_DIR}/calculators
40+
${PROJECT_MODULE_DIR}/common
4041
${PROJECT_MODULE_DIR}/API
4142
${PROJECT_TEST_DIR}
4243
${PROJECT_TEST_DIR}/dummy
@@ -80,4 +81,4 @@ target_link_libraries(${PROJECT_NAME}_test
8081

8182
# テストの登録
8283
include(GoogleTest)
83-
gtest_discover_tests(${PROJECT_NAME}_test)
84+
gtest_discover_tests(${PROJECT_NAME}_test)

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ RUN apt-get update && apt-get install -y \
88
git \
99
cmake \
1010
sudo \
11+
clang-format \
1112
libopencv-dev \
1213
&& gem install shell \
1314
&& apt-get clean && rm -rf /var/lib/apt/lists/*
1415

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

1819
# RasPike-ART を clone
1920
# libraspike-artは,RasPike-ART内で運営がリンクで紐づけていたため,直接cloneする必要がある

Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,38 @@ format:
8888
# 指定ファイルがある場合、そのファイルにclang-formatを適用する
8989
ifdef FILES
9090
clang-format -i -style=file $(FILES)
91+
@ echo "フォーマットを適用しました: $(FILES)"
9192
# ない場合、変更されたファイルのうち、cpp、hファイルにclang-formatを適用する
9293
else
93-
git diff origin/main --name-only | awk '/\.cpp$$|\.h$$/ {print $$1}' | xargs clang-format -i -style=file
94+
# 変更されたファイルと未追跡のファイル (新規追加など) の両方を検出
95+
@{ \
96+
CANDIDATE_FILES=$$( (git diff origin/main --name-only; git ls-files --others --exclude-standard) | grep -E '\.cpp$$|\.h$$' || true ); \
97+
ACTUALLY_FORMATTED_FILES=""; \
98+
FORMATTED_COUNT=0; \
99+
if [ -n "$$CANDIDATE_FILES" ]; then \
100+
for FILE_PATH in $$CANDIDATE_FILES; do \
101+
REPLACEMENTS_XML=$$(clang-format -style=file --output-replacements-xml "$$FILE_PATH" 2>/dev/null); \
102+
if echo "$$REPLACEMENTS_XML" | grep -q "<replacement "; then \
103+
clang-format -i -style=file "$$FILE_PATH"; \
104+
if [ -z "$$ACTUALLY_FORMATTED_FILES" ]; then \
105+
ACTUALLY_FORMATTED_FILES="$$FILE_PATH"; \
106+
else \
107+
ACTUALLY_FORMATTED_FILES="$${ACTUALLY_FORMATTED_FILES}\n$$FILE_PATH"; \
108+
fi; \
109+
FORMATTED_COUNT=$$(($$FORMATTED_COUNT + 1)); \
110+
fi; \
111+
done; \
112+
if [ $$FORMATTED_COUNT -gt 0 ]; then \
113+
echo "以下のファイルにフォーマットを適用しました ($$FORMATTED_COUNT 件):"; \
114+
printf "%b\n" "$$ACTUALLY_FORMATTED_FILES"; \
115+
else \
116+
echo "検査したファイルは全てフォーマット済みでした。適用された変更はありません。"; \
117+
fi; \
118+
else \
119+
echo "フォーマットをチェックする対象の .cpp または .h ファイルがありません。"; \
120+
fi \
121+
}
94122
endif
95123

96124
format-check:
97-
find ./test ./modules -type f -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror *.h *.cpp
125+
find ./tests ./modules -type f -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror *.h *.cpp

Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ APPL_DIRS += \
1515
$(mkfile_path)modules\
1616
$(mkfile_path)modules/API\
1717
$(mkfile_path)modules/calculators\
18+
$(mkfile_path)modules/common\
1819
$(mkfile_path)modules/CameraCapture
1920

2021
INCLUDES += \
2122
-I$(mkfile_path)modules\
2223
-I$(mkfile_path)modules/API\
2324
-I$(mkfile_path)modules/calculators\
25+
-I$(mkfile_path)modules/common\
2426
-I$(mkfile_path)modules/CameraCapture \
2527
-I/usr/include/opencv4
2628

modules/calculators/Mileage.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
** @file Mileage.cpp
3+
** @brief 走行距離を計算するクラス
4+
** @author molpui0726
5+
**/
6+
7+
#include "Mileage.h"
8+
9+
// MotorController でモーターの角位置を int32_t 型で取得しているため、
10+
// double ではなく int32_t 型を使用する
11+
double Mileage::calculateWheelMileage(int32_t angle)
12+
{
13+
// タイヤの累計走行距離 = 2 * π * タイヤの半径 * (タイヤの回転角度 / 360[deg])
14+
return 2.0 * M_PI * RADIUS * static_cast<double>(angle) / 360.0;
15+
}
16+
17+
double Mileage::calculateMileage(int32_t rightAngle, int32_t leftAngle)
18+
{
19+
// 右タイヤの累計走行距離を計算
20+
double rightWheelMileage = calculateWheelMileage(rightAngle);
21+
// 左タイヤの累計走行距離を計算
22+
double leftWheelMileage = calculateWheelMileage(leftAngle);
23+
// 走行体全体の累計走行距離 = (右タイヤの累計走行距離 + 左タイヤの累計走行距離) / 2
24+
return (rightWheelMileage + leftWheelMileage) / 2.0;
25+
}

modules/calculators/Mileage.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
** @file Mileage.h
3+
** @brief 走行距離を計算するクラス
4+
** @author molpui0726
5+
**/
6+
7+
#ifndef MILEAGE_H
8+
#define MILEAGE_H
9+
10+
#include <cmath>
11+
#include "SystemInfo.h"
12+
13+
class Mileage {
14+
public:
15+
/**
16+
** @brief タイヤの累計走行距離を計算する
17+
** @param angle タイヤの回転角度[deg]
18+
** @return タイヤの累計走行距離[mm]
19+
**/
20+
static double calculateWheelMileage(int32_t angle);
21+
22+
/**
23+
** @brief 走行体全体の累計走行距離を計算する
24+
** @param rightAngle 右タイヤの回転角度[deg]
25+
** @param leftAngle 左タイヤの回転角度[deg]
26+
** @return 走行体全体の累計走行距離[mm]
27+
**/
28+
static double calculateMileage(int32_t rightAngle, int32_t leftAngle);
29+
30+
private:
31+
Mileage(); // インスタンス化を禁止する
32+
};
33+
34+
#endif

modules/common/SystemInfo.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @file SystemInfo.h
3+
* @brief 走行システムで統一する情報をまとめたファイル
4+
* @author molpui0726
5+
*/
6+
7+
#ifndef SYSTEM_INFO_H
8+
#define SYSTEM_INFO_H
9+
10+
static constexpr double RADIUS = 28.0; // 車輪の半径[mm]
11+
static constexpr double TREAD = 112.0; // 走行体のトレッド幅(両輪の間の距離)[mm]
12+
13+
#endif

tests/MileageTest.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @file MileageTest.cpp
3+
* @brief 走行距離の測定用クラスのテスト
4+
* @author molpui0726
5+
*/
6+
7+
#include <cmath>
8+
#include <gtest/gtest.h>
9+
#include "Mileage.h"
10+
11+
namespace etrobocon2025_test {
12+
TEST(MileageTest, CalculateMileagePlus)
13+
{
14+
int32_t rightAngle = 30;
15+
int32_t leftAngle = 40;
16+
17+
// 計算過程
18+
// 1.右車輪の累計走行距離を算出
19+
// double rightWheelMileage = 2.0 * rightAngle * radius * M_PI / 360.0;
20+
// M_PI = 3.14: rightWheelMileage = 2.0 * 30.0 * 28.0 * 3.14 / 360.0 = 14.6533...
21+
// M_PI = 3.15: rightWheelMileage = 2.0 * 30.0 * 28.0 * 3.15 / 360.0 = 14.70
22+
// 2.左車輪の累計走行距離を算出
23+
// double leftWheelMileage = 2.0 * leftAngle * radius * M_PI / 360.0;
24+
// M_PI = 3.14: leftWheelMileage = 2.0 * 40.0 * 28.0 * 3.14 / 360.0 = 19.53777...
25+
// M_PI = 3.15: leftWheelMileage = 2.0 * 40.0 * 28.0 * 3.15 / 360.0 = 19.60
26+
// 3.両車輪の累計走行距離の平均を算出
27+
// double expected = (rightWheelMileage + leftWheelMileage) / 2.0;
28+
// M_PI = 3.14: (14.6533... + 19.5377...) / 2 = 17.09555...
29+
// M_PI = 3.15: (14.70 + 19.60) / 2 = 17.15
30+
double expected_min = 17.09555;
31+
double expected_max = 17.15;
32+
33+
// actual: 17.104227
34+
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
35+
36+
// M_PIが3.14で計算した値よりも大きいアサーションと、3.15で計算した値よりも小さいアサーションを両方満たすか
37+
EXPECT_LT(expected_min, actual);
38+
EXPECT_GT(expected_max, actual);
39+
}
40+
41+
TEST(MileageTest, CalculateMileageMinus)
42+
{
43+
int32_t rightAngle = -30;
44+
int32_t leftAngle = -40;
45+
double expected_min = -17.15;
46+
double expected_max = -17.09555;
47+
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
48+
EXPECT_LT(expected_min, actual);
49+
EXPECT_GT(expected_max, actual);
50+
}
51+
52+
TEST(MileageTest, CalculateMileageZero)
53+
{
54+
int32_t rightAngle = 0;
55+
int32_t leftAngle = 0;
56+
double expected = 0.0;
57+
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
58+
EXPECT_DOUBLE_EQ(expected, actual);
59+
}
60+
} // namespace etrobocon2025_test

0 commit comments

Comments
 (0)