Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ set(PROJECT_TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
# 必要なインクルードディレクトリの設定 (適宜登録)
set(INCLUDE_DIRS
${PROJECT_MODULE_DIR}
${PROJECT_MODULE_DIR}/API
${PROJECT_TEST_DIR}
${PROJECT_TEST_DIR}/dummy
${PROJECT_TEST_DIR}/helpers
)
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この括弧は一個下げた方が気持ちいい


# ソースファイルの登録 (適宜登録)
file(GLOB_RECURSE PROJECT_SOURCES
Expand All @@ -53,6 +56,11 @@ file(GLOB TEST_SOURCES "${PROJECT_TEST_DIR}/*Test.cpp")

# テスト実行可能ファイルの作成
add_executable(${PROJECT_NAME}_test ${TEST_SOURCES})
target_include_directories(${PROJECT_NAME}_test
PRIVATE
${PROJECT_TEST_DIR}/dummy # spikeapi ダミー用
)

target_link_libraries(${PROJECT_NAME}_test
PRIVATE
${PROJECT_NAME}_impl
Expand Down
12 changes: 10 additions & 2 deletions Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ USE_RASPIKE_ART=1

mkfile_path := $(dir $(lastword $(MAKEFILE_LIST)))


# Shellスクリプトで、moduleディレクトリ中のソースコード名をオブジェクトファイル名に変換している
SRCS := $(shell find ${mkfile_path}modules -name '*.cpp')
OBJS := $(notdir $(SRCS:.cpp=.o))
Expand All @@ -12,7 +11,16 @@ SRCLANG := c++

APPL_LIBS += -lm

APPL_DIRS += $(mkfile_path)modules
APPL_DIRS += \
$(mkfile_path)modules\
$(mkfile_path)modules/API




INCLUDES += \
-I$(mkfile_path)modules\
-I$(mkfile_path)modules/API



Comment on lines +24 to +26
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どうせコンフリクトするところだから直さなくてもいい気がするけど、
ここはいらないかな

Suggested change

138 changes: 138 additions & 0 deletions modules/API/ColorSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* @file ColorSensor.h
* @brief カラーセンサクラス (ラッパクラス)
* @author HaruArima08
*/

#ifndef COLOR_SENSOR_H_
#define COLOR_SENSOR_H_

#include "spikeapi.h"
#include "spike/pup/colorsensor.h"
#include "Port.h"

/**
* SPIKE カラーセンサクラス
*/
class ColorSensor {
public:
struct RGB {
uint16_t r;
uint16_t g;
uint16_t b;
};

struct HSV {
uint16_t h;
uint8_t s;
uint8_t v;
};

/**
* コンストラクタ
* @param port PUPポートID
*/
ColorSensor(Port port)
{
pupDevicePointer = pup_color_sensor_get_device(static_cast<pbio_port_id_t>(port));
}

/**
* カラーセンサのRGB値を取得する
* @param 値を設定するRGB構造体、各色10ビット
* @return -
*/
void getRGB(RGB& rgb) const
Comment on lines +40 to +44
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいけど説明に引数名がないかな。

Suggested change
/**
* カラーセンサのRGB値を取得する
* @param 値を設定するRGB構造体各色10ビット
* @return -
*/
void getRGB(RGB& rgb) const
/**
* カラーセンサのRGB値を取得する
* @param rgb 値を設定するRGB構造体各色10ビット
* @return -
*/
void getRGB(RGB& rgb) const

{
pup_color_rgb_t pup_rgb = pup_color_sensor_rgb(pupDevicePointer);
rgb.r = pup_rgb.r;
rgb.g = pup_rgb.g;
rgb.b = pup_rgb.b;
}

/**
* カラーセンサで色を測定する
* @param surface trueならば表面の色から、falseならば他の光源の色を検出する
* @return 色(hsvによる表現)
*/
void getColor(HSV& hsv, bool surface = true) const
Comment on lines +53 to +58
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引数の説明にhsvがない

Suggested change
/**
* カラーセンサで色を測定する
* @param surface trueならば表面の色からfalseならば他の光源の色を検出する
* @return hsvによる表現
*/
void getColor(HSV& hsv, bool surface = true) const
/**
* カラーセンサで色を測定する
* @param surface trueならば表面の色からfalseならば他の光源の色を検出する
* @param hsv なんちゃら
* @return hsvによる表現
*/
void getColor(HSV& hsv, bool surface = true) const

{
pup_color_hsv_t pup_hsv = pup_color_sensor_color(pupDevicePointer, surface);
hsv.h = pup_hsv.h;
hsv.s = pup_hsv.s;
hsv.v = pup_hsv.v;
}

/**
* カラーセンサで色を測定する(近似なし)
* @param surface trueならば表面の色から、falseならば他の光源の色を検出する
* @return 色(hsvによる表現)
*/
Comment on lines +66 to +71
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここも引数hsvの説明がないな

void getHSV(HSV& hsv, bool surface = true) const
{
pup_color_hsv_t pup_hsv = pup_color_sensor_hsv(pupDevicePointer, surface);
hsv.h = pup_hsv.h;
hsv.s = pup_hsv.s;
hsv.v = pup_hsv.v;
}

/**
* センサーの発する光を表面がどの程度反射するかを測定する
* @return どの程度反射しているか(%)
*/
int32_t getReflection() const { return pup_color_sensor_reflection(pupDevicePointer); }

/**
* 周囲の光の強度を測定する
* @return 周囲の光の強度(%)
*/
int32_t getAmbient() const { return pup_color_sensor_ambient(pupDevicePointer); }

/**
* カラーセンサのライトを設定する
* @param bv1 輝度1
* @param bv2 輝度2
* @param bv3 輝度3
* @return -
*/
void setLight(int32_t bv1, int32_t bv2, int32_t bv3) const
{
pup_color_sensor_light_set(pupDevicePointer, bv1, bv2, bv3);
}

/**
* カラーセンサのライトを点灯する
* @param -
* @return -
*/
void lightOn() const { pup_color_sensor_light_on(pupDevicePointer); }

/**
* カラーセンサのライトを消灯する
* @param -
* @return -
*/
void lightOff() const { pup_color_sensor_light_off(pupDevicePointer); }

/**
* カラーセンサが検知する色を設定する
* @param size カラーの配列のサイズ
* @param colors カラーの配列
* @return -
*/
void setDetectableColors(int32_t size, pup_color_hsv_t* colors) const
{
pup_color_sensor_detectable_colors(size, colors);
}

/**
* インスタンス生成が正常にできたかどうかを確認するための共通メソッド
* pupDevicePointerがNULLの場合にtrueとなる
*/
bool hasError() { return pupDevicePointer == 0; }

private:
pup_device_t* pupDevicePointer;
}; // class ColorSensor

#endif
26 changes: 26 additions & 0 deletions modules/API/Port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file Port.h
* @brief ポート関連定義
* @author HaruArima08
*/

#ifndef PORT_H_
#define PORT_H_
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hの後に_って他のクラスつけてたっけ?他のインクルードガードをPORT_Hにすることは多分ないから大丈夫だと思うけど、一応確認。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ダミーの方は PORT_H になってるからミス?
多分ビルド対象外になってたはずだから、PORT_H でも大丈夫なはず

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同じ名前が重複するとインクルードガード効かずビルドが通らなかったので、クラスの方とダミーのほうで分けて書いてました。
分かりやすくクラスのほうは _ 外して、ダミーは先頭に DUMMY_ を足してみました。


#include "spikeapi.h"
#include "pbio/port.h"

/**
* モータ/センサポート関連定義
*/

enum class Port {
PORT_A = PBIO_PORT_ID_A, /**< SPIKE ポートA */
PORT_B = PBIO_PORT_ID_B, /**< SPIKE ポートB */
PORT_C = PBIO_PORT_ID_C, /**< SPIKE ポートC */
PORT_D = PBIO_PORT_ID_D, /**< SPIKE ポートD */
PORT_E = PBIO_PORT_ID_E, /**< SPIKE ポートE */
PORT_F = PBIO_PORT_ID_F /**< SPIKE ポートF */
};

#endif
126 changes: 126 additions & 0 deletions tests/ColorMeasureTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* @file ColorMeasureTest.cpp
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ColorSensor.cppじゃないのは何で?理由があるならいいけど、ないならソースファイルと統一してもらいたい🙏

Suggested change
* @file ColorMeasureTest.cpp
* @file ColorSensorTest.cpp

* @brief ColorSensorクラスの値取得をテストする
* @author HaruArima08
*/

#include "ColorSensor.h"
#include <gtest/gtest.h>

// pup_color_rgb_tの比較用関数
bool eqRgb(pup_color_rgb_t rgb1, pup_color_rgb_t rgb2)
{
return rgb1.r == rgb2.r && rgb1.g == rgb2.g && rgb1.b == rgb2.b;
}

// pup_color_hsv_tの比較用関数
bool eqHsv(pup_color_hsv_t hsv1, pup_color_hsv_t hsv2)
{
return hsv1.h == hsv2.h && hsv1.s == hsv2.s && hsv1.v == hsv2.v;
}

namespace etrobocon2025_test {

// RGB値取得テスト
TEST(RgbColorTest, getRGB)
{
ColorSensor colorSensor(Port::PORT_E);
ColorSensor::RGB rgb;
colorSensor.getRGB(rgb);
pup_color_rgb_t actual = { rgb.r, rgb.g, rgb.b };

// テスト用のRGB期待値の定義
pup_color_rgb_t expectedColors[] = {
{ 32, 36, 40 }, // black
{ 416, 404, 584 }, // white
{ 444, 76, 76 }, // red
{ 480, 432, 284 }, // yellow
{ 16, 300, 140 }, // green
{ 324, 368, 576 } // blue
};

bool match = false;
for(const auto& expected : expectedColors) {
if(eqRgb(actual, expected)) {
match = true;
break;
}
}

ASSERT_TRUE(match) << "RGB値が期待値に一致しません: "
<< "r=" << (int)actual.r << ", g=" << (int)actual.g
<< ", b=" << (int)actual.b;
}

// HSV値取得テスト (近似あり)
TEST(HsvColorApproxiTest, getColor)
{
ColorSensor colorSensor(Port::PORT_E);
ColorSensor::HSV hsv1;
colorSensor.getColor(hsv1);
pup_color_hsv_t actual = { hsv1.h, hsv1.s, hsv1.v };

// テスト用のHSV期待値の定義
pup_color_hsv_t expectedColors[] = {
{ 0, 83, 111 }, // red
{ 45, 41, 120 }, // yellow
{ 150, 94, 75 }, // green
{ 226, 44, 144 }, // blue
{ 245, 45, 146 }, // white
};

bool match = false;
for(const auto& expected : expectedColors) {
if(eqHsv(actual, expected)) {
match = true;
break;
}
}

ASSERT_TRUE(match) << "HSV値が期待値に一致しません: "
<< "h=" << (int)actual.h << ", s=" << (int)actual.s
<< ", v=" << (int)actual.v;
}

// HSV値取得テスト (近似なし)
TEST(HsvColorTest, getHSV)
{
ColorSensor colorSensor(Port::PORT_E);
ColorSensor::HSV hsv2;
colorSensor.getHSV(hsv2);
pup_color_hsv_t actual = { hsv2.h, hsv2.s, hsv2.v };

// テスト用のHSV期待値の定義
pup_color_hsv_t expectedColors[] = {
{ 210, 20, 10 }, // black
{ 245, 45, 146 }, // white
{ 0, 83, 111 }, // red
{ 45, 41, 120 }, // yellow
{ 150, 94, 75 }, // green
{ 226, 44, 144 } // blue
};

bool match = false;
for(const auto& expected : expectedColors) {
if(eqHsv(actual, expected)) {
match = true;
break;
}
}

ASSERT_TRUE(match) << "HSV値が期待値に一致しません: "
<< "h=" << (int)actual.h << ", s=" << (int)actual.s
<< ", v=" << (int)actual.v;
}

// 反射率取得テスト
TEST(ReflectionTest, getReflection)
{
ColorSensor colorSensor(Port::PORT_E);
int32_t actual = colorSensor.getReflection();
int32_t expected = 42; // ダミーの期待値

ASSERT_EQ(actual, expected) << "反射値が期待値に一致しません: " << actual;
}

} // namespace etrobocon2025_test
21 changes: 21 additions & 0 deletions tests/dummy/pbio/port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file port.h
* @brief ポート関連定義(ダミー)
* @author HaruArima08
*/

#ifndef PORT_H
#define PORT_H

// 型定義(ColorSensorで使われる)
typedef int pbio_port_id_t;

// ダミーのポートID定数定義
#define PBIO_PORT_ID_A 0
#define PBIO_PORT_ID_B 1
#define PBIO_PORT_ID_C 2
#define PBIO_PORT_ID_D 3
#define PBIO_PORT_ID_E 4
#define PBIO_PORT_ID_F 5

#endif // PORT_H
Loading