Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close #KLI-74 走行距離を算出するクラスとそのテストを作成 #9

Merged
merged 6 commits into from
Jun 8, 2024
Merged
25 changes: 25 additions & 0 deletions module/Calculator/Mileage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
** @file Mileage.cpp
** @brief 走行距離を計算するクラス
** @author YKhm20020
**/

#include "Mileage.h"

static constexpr double RADIUS = 50.0; // 車輪の半径[mm]

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 module/Calculator/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
63 changes: 63 additions & 0 deletions test/MileageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @file MileageTest.cpp
* @brief 走行距離の測定用クラスのテスト
* @author YKhm20020
*/

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

namespace etrobocon2024_test {
TEST(MileageTest, calculateMileage)
{
double radius = 50.0;
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 * 50.0 * 3.14 / 360.0 = 26.1666...
// M_PI = 3.15: rightWheelMileage = 2.0 * 30.0 * 50.0 * 3.15 / 360.0 = 26.25
// 2.左車輪の累計走行距離を算出
// double leftWheelMileage = 2.0 * leftAngle * radius * M_PI / 360.0;
// M_PI = 3.14: leftWheelMileage = 2.0 * 40.0 * 50.0 * 3.14 / 360.0 = 32.1111...
// M_PI = 3.15: leftWheelMileage = 2.0 * 40.0 * 50.0 * 3.15 / 360.0 = 35
// 3.両車輪の累計走行距離の平均を算出
// double expected = (rightWheelMileage + leftWheelMileage) / 2.0;
// M_PI = 3.14: (26.1666... + 32.1111...) / 2 = 29.13888...
// M_PI = 3.15: (26.25 + 35) / 2 = 30.625
double expected_min = 29.13888;
double expected_max = 30.625;

// actual: 30.543261909900767
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 radius = 50.0;
double rightAngle = -30.0;
double leftAngle = -40.0;
double expected_min = -30.625;
double expected_max = -29.13888;
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
EXPECT_LT(expected_min, actual);
EXPECT_GT(expected_max, actual);
}

TEST(MileageTest, calculateMileageZero)
{
double radius = 50.0;
double rightAngle = 0.0;
double leftAngle = 0.0;
double expected = 0.0;
double actual = Mileage::calculateMileage(rightAngle, leftAngle);
EXPECT_DOUBLE_EQ(expected, actual);
}
} // namespace etrobocon2024_test
Loading