Skip to content

Commit 5cb2e45

Browse files
committed
add: カラーセンサのラッパクラスの追加
1 parent cbde8c8 commit 5cb2e45

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

Makefile.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ SRCLANG := c++
1212

1313
APPL_LIBS += -lm
1414

15-
APPL_DIRS += $(mkfile_path)modules
15+
APPL_DIRS += \
16+
$(mkfile_path)modules\
17+
$(mkfile_path)modules/API
18+
19+
1620

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

modules/API/ColorSensor.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @file ColorSensor.h
3+
* @brief カラーセンサクラス
4+
* @author HaruArima08
5+
*/
6+
7+
#ifndef COLOR_SENSOR_H_
8+
#define COLOR_SENSOR_H_
9+
10+
#include "spikeapi.h"
11+
#include "spike/pup/colorsensor.h"
12+
#include "Port.h"
13+
/**
14+
* SPIKE カラーセンサクラス
15+
*/
16+
class ColorSensor {
17+
public:
18+
struct RGB {
19+
uint16_t r;
20+
uint16_t g;
21+
uint16_t b;
22+
};
23+
24+
struct HSV {
25+
uint16_t h;
26+
uint8_t s;
27+
uint8_t v;
28+
};
29+
30+
/**
31+
* コンストラクタ
32+
* @param port PUPポートID
33+
*/
34+
ColorSensor(Port port)
35+
{
36+
pupDevicePointer = pup_color_sensor_get_device(static_cast<pbio_port_id_t>(port));
37+
}
38+
39+
/**
40+
* カラーセンサのRGB値を取得する
41+
* @param 値を設定するRGB構造体、各色8ビット
42+
* @return -
43+
*/
44+
void getRGB(RGB& rgb) const
45+
{
46+
pup_color_rgb_t pup_rgb = pup_color_sensor_rgb(pupDevicePointer);
47+
rgb.r = pup_rgb.r;
48+
rgb.g = pup_rgb.g;
49+
rgb.b = pup_rgb.b;
50+
}
51+
52+
/**
53+
* カラーセンサで色を測定する
54+
* @param surface trueならば表面の色から、falseならば他の光源の色を検出する
55+
* @return 色(hsvによる表現)
56+
*/
57+
void getColor(HSV& hsv, bool surface = true) const
58+
{
59+
pup_color_hsv_t pup_hsv = pup_color_sensor_color(pupDevicePointer, surface);
60+
hsv.h = pup_hsv.h;
61+
hsv.s = pup_hsv.s;
62+
hsv.v = pup_hsv.v;
63+
}
64+
65+
/**
66+
* カラーセンサで色を測定する(近似なし)
67+
* @param surface trueならば表面の色から、falseならば他の光源の色を検出する
68+
* @return 色(hsvによる表現)
69+
*/
70+
void getHSV(HSV& hsv, bool surface = true) const
71+
{
72+
pup_color_hsv_t pup_hsv = pup_color_sensor_hsv(pupDevicePointer, surface);
73+
hsv.h = pup_hsv.h;
74+
hsv.s = pup_hsv.s;
75+
hsv.v = pup_hsv.v;
76+
}
77+
78+
/**
79+
* センサーの発する光を表面がどの程度反射するかを測定する
80+
* @return どの程度反射しているか(%)
81+
*/
82+
int32_t getReflection() const { return pup_color_sensor_reflection(pupDevicePointer); }
83+
84+
/**
85+
* 周囲の光の強度を測定する
86+
* @return 周囲の光の強度(%)
87+
*/
88+
int32_t getAmbient() const { return pup_color_sensor_ambient(pupDevicePointer); }
89+
90+
/**
91+
* カラーセンサのライトを設定する
92+
* @param bv1 輝度1
93+
* @param bv2 輝度2
94+
* @param bv3 輝度3
95+
* @return -
96+
*/
97+
void setLight(int32_t bv1, int32_t bv2, int32_t bv3) const
98+
{
99+
pup_color_sensor_light_set(pupDevicePointer, bv1, bv2, bv3);
100+
}
101+
102+
/**
103+
* カラーセンサのライトを点灯する
104+
* @param -
105+
* @return -
106+
*/
107+
void lightOn() const { pup_color_sensor_light_on(pupDevicePointer); }
108+
109+
/**
110+
* カラーセンサのライトを消灯する
111+
* @param -
112+
* @return -
113+
*/
114+
void lightOff() const { pup_color_sensor_light_off(pupDevicePointer); }
115+
116+
/**
117+
* カラーセンサが検知する色を設定する
118+
* @param size カラーの配列のサイズ
119+
* @param colors カラーの配列
120+
* @return -
121+
*/
122+
void setDetectableColors(int32_t size, pup_color_hsv_t* colors) const
123+
{
124+
pup_color_sensor_detectable_colors(size, colors);
125+
}
126+
127+
/**
128+
* インスタンス生成が正常にできたかどうかを確認するための共通メソッド
129+
* pupDevicePointerがNULLの場合にtrueとなる
130+
*/
131+
bool hasError() { return pupDevicePointer == 0; }
132+
133+
private:
134+
pup_device_t* pupDevicePointer;
135+
}; // class ColorSensor
136+
137+
#endif

modules/API/Port.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file Port.h
3+
* @brief ポート関連定義
4+
* @author HaruArima08
5+
*/
6+
7+
#ifndef PORT_H_
8+
#define PORT_H_
9+
10+
#include "spikeapi.h"
11+
#include "pbio/port.h"
12+
13+
/**
14+
* モータ/センサポート関連定義
15+
*/
16+
17+
enum class Port {
18+
PORT_A = PBIO_PORT_ID_A, /**< SPIKE ポートA */
19+
PORT_B = PBIO_PORT_ID_B, /**< SPIKE ポートB */
20+
PORT_C = PBIO_PORT_ID_C, /**< SPIKE ポートC */
21+
PORT_D = PBIO_PORT_ID_D, /**< SPIKE ポートD */
22+
PORT_E = PBIO_PORT_ID_E, /**< SPIKE ポートE */
23+
PORT_F = PBIO_PORT_ID_F /**< SPIKE ポートF */
24+
};
25+
26+
#endif

0 commit comments

Comments
 (0)