|
| 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 |
0 commit comments