etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
Distinguisher.cpp
[詳解]
1 #include "Distinguisher.h"
2 #include <cmath>
3 
5 {
6  setRawColor2Rgb();
7  convertRgbToHsv(rgb);
8  distingishColor();
9  return color;
10 }
11 
12 void Distinguisher::distingishColor()
13 {
14  color = Color::NONE;
15  double min_distance = threshold_distance;
16  std::int8_t black_threthold = 10;
17  std::int8_t white_threthold = 40;
18 
19  judgement(RED, min_distance);
20  judgement(AltRED, min_distance);
21  judgement(BLUE, min_distance);
22  judgement(YELLOW, min_distance);
23  judgement(GREEN, min_distance);
24  judgement(GREY, min_distance);
25  judgement(BLACK, min_distance);
26  judgement(WHITE, min_distance);
27  if(color == Color::YELLOW) {
28  black_threthold = 30;
29  white_threthold = 50;
30  } else if(color == Color::GREEN) {
31  white_threthold = 35;
32  if(hsv.s < 40) {
33  color = Color::WHITE;
34  }
35  }
36  if(hsv.v < black_threthold) {
37  color = Color::BLACK;
38  } else if(hsv.v > white_threthold) {
39  color = Color::WHITE;
40  }
41 }
42 
43 void Distinguisher::judgement(const Hsv& hsv_, double& min)
44 {
45  double tmp = distanceColor(hsv_);
46  if(min < tmp) return;
47  if(hsv_.start_h < hsv.h && hsv_.end_h > hsv.h) {
48  min = tmp;
49  color = hsv_.color;
50  }
51 }
52 
53 double Distinguisher::distanceColor(Hsv target_color)
54 {
55  std::uint16_t pow_h = std::pow(hsv.h - target_color.h, 2);
56  std::uint16_t pow_s = std::pow(hsv.s - target_color.s, 2);
57  std::uint16_t pow_v = std::pow(hsv.v - target_color.v, 2);
58  return std::sqrt(pow_h + pow_s + pow_v);
59 }
60 
61 void Distinguisher::setRawColor2Rgb()
62 {
63  std::uint32_t total_r, total_g, total_b;
64  std::int8_t times = 10;
65 
66  total_r = total_g = total_b = 0;
67  for(int i = 0; i < times; i++) {
68  controller.getRawColor(rgb.r, rgb.g, rgb.b);
69  total_r += rgb.r;
70  total_g += rgb.g;
71  total_b += rgb.b;
72  }
73  rgb.r = total_r / times;
74  rgb.g = total_g / times;
75  rgb.b = total_b / times;
76 }
77 
78 void Distinguisher::convertRgbToHsv(Rgb rgb_)
79 {
80  double max = std::max((std::max(rgb_.r, rgb_.g)), rgb_.b);
81  double min = std::min((std::min(rgb_.r, rgb_.g)), rgb_.b);
82  hsv.v = max / 256 * 100;
83 
84  if(max == min) {
85  hsv.h = 0;
86  hsv.s = 0;
87  } else {
88  if(max == rgb_.r)
89  hsv.h = 60.0 * (rgb_.g - rgb_.b) / (max - min) + 0;
90  else if(max == rgb_.g)
91  hsv.h = 60.0 * (rgb_.b - rgb_.r) / (max - min) + 120.0;
92  else if(max == rgb_.b)
93  hsv.h = 60.0 * (rgb_.r - rgb_.g) / (max - min) + 240.0;
94 
95  if(hsv.h > 360.0) {
96  hsv.h = hsv.h - 360.0;
97  } else if(hsv.h < 0) {
98  hsv.h = hsv.h + 360.0;
99  }
100  hsv.s = (max - min) / max * 100.0;
101  }
102 }
double start_h
Definition: Distinguisher.h:38
void getRawColor(uint16_t &r, uint16_t &g, uint16_t &b)
Definition: Controller.cpp:66
double threshold_distance
Definition: Distinguisher.h:71
double s
Definition: Distinguisher.h:35
Color
Definition: Distinguisher.h:16
double end_h
Definition: Distinguisher.h:39
std::uint16_t g
Definition: Distinguisher.h:44
double v
Definition: Distinguisher.h:36
走行体のカラーセンサを用いて、RGB情報より色を推定するクラス。
std::uint16_t r
Definition: Distinguisher.h:43
色とHsv情報を保持するクラス。
Definition: Distinguisher.h:21
Color getColor()
double h
Definition: Distinguisher.h:34
Color color
Definition: Distinguisher.h:33
std::uint16_t b
Definition: Distinguisher.h:45