Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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/*
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
24 changes: 24 additions & 0 deletions modules/calculators/Mileage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
** @file Mileage.cpp
** @brief 走行距離を計算するクラス
** @author molpui0726
**/

#include "Mileage.h"
#include "../common/SystemInfo.h"

double Mileage::calculateWheelMileage(int angle)
{
// タイヤの累計走行距離 = 2 * π * タイヤの半径 * (タイヤの回転角度 / 360[deg])
return 2.0 * M_PI * RADIUS * static_cast<double>(angle) / 360.0;
}

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

#ifndef MILEAGE_H
#define MILEAGE_H

#include <cmath>

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

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

private:
Mileage(); // インスタンス化を禁止する
};
#endif
14 changes: 14 additions & 0 deletions modules/common/SystemInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @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]
static bool shouldGetRunLogs = false; // 実行ログを取得するかのフラグ

#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 "Mileage.h"
#include <cmath>
#include <gtest/gtest.h>

namespace etrobocon2025_test {
TEST(MileageTest, CalculateMileage)
{
double rightAngle = 30.0;
double leftAngle = 40.0;

// 計算過程
// 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)
{
double rightAngle = -30.0;
double leftAngle = -40.0;
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)
{
double rightAngle = 0.0;
double leftAngle = 0.0;
double expected = 0.0;
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
EXPECT_DOUBLE_EQ(expected, actual);
}
} // namespace etrobocon2025_test