etrobocon2019 feat.KatLab  ece30a9a007fff7d3ad48592c0d09a74643377bb
Calibrator.cpp
[詳解]
1 
6 #include "Calibrator.h"
7 
9  : controller(controller_),
10  isCameraMode(true),
11  isLeft(true),
12  brightnessOfWhite(0),
13  brightnessOfBlack(0)
14 {
15 }
16 
18 {
19  Display::print(2, "now calibration...");
20 
21  if(!setCameraMode()) {
22  Display::print(2, "Error setCameraMode!");
23  return false;
24  }
25 
26  if(!setLRCourse()) {
27  Display::print(2, "Error setLRCourse!");
28  return false;
29  }
30 
32  Display::print(2, "Error setBrightness White!");
33  return false;
34  }
35 
37  Display::print(2, "Error setBrightness Black!");
38  return false;
39  }
40 
41  Display::print(5, "White: %3d", brightnessOfWhite);
42  Display::print(6, "Black: %3d", brightnessOfBlack);
43 
44  return true;
45 }
46 
48 {
49  char cameraMode[8] = "ON";
50 
51  controller.tslpTsk(500);
52  while(!controller.buttonIsPressedEnter()) {
53  if(isCameraMode) {
54  std::strcpy(cameraMode, "ON");
55  } else {
56  std::strcpy(cameraMode, "OFF");
57  }
58  Display::print(3, "camera system: %s ?", cameraMode);
59 
60  if(controller.buttonIsPressedLeft() || controller.buttonIsPressedRight()) {
61  isCameraMode = !isCameraMode;
62  controller.speakerPlayToneFS6(50);
63  controller.tslpTsk(500);
64  }
65  controller.tslpTsk(4);
66  }
67  Display::print(3, "camera system: %s", cameraMode);
68 
69  controller.speakerPlayToneFS6(100);
70  return true;
71 }
72 
74 {
75  char course[8] = "Left";
76 
77  controller.tslpTsk(500);
78  while(!controller.buttonIsPressedEnter()) {
79  if(isLeft) {
80  std::strcpy(course, "Left");
81  } else {
82  std::strcpy(course, "Right");
83  }
84  Display::print(4, "Set LRCourse: %s ?", course);
85 
86  if(controller.buttonIsPressedLeft() || controller.buttonIsPressedRight()) {
87  isLeft = !isLeft;
88  controller.speakerPlayToneFS6(50);
89  controller.tslpTsk(500);
90  }
91  controller.tslpTsk(4);
92  }
93  Display::print(4, "course: %s", course);
94 
95  controller.speakerPlayToneFS6(100);
96  return true;
97 }
98 
100 {
101  char name[8] = "none";
102 
103  if(brightness == Brightness::WHITE) {
104  std::strcpy(name, "White");
105  } else if(brightness == Brightness::BLACK) {
106  std::strcpy(name, "Black");
107  } else {
108  return false;
109  }
110 
111  controller.tslpTsk(500);
112 
113  while(1) {
114  // ENTERボタンが押されたらループを抜ける
115  if(controller.buttonIsPressedEnter()) {
116  controller.speakerPlayToneFS6(100);
117  break;
118  }
119 
120  int tmpColor = controller.getBrightness();
121  Display::print(5, "Set brightness of %s: %3d ?", name, tmpColor);
122 
123  controller.tslpTsk(4);
124  }
125 
126  controller.speakerPlayToneFS6(200);
127 
128  if(brightness == Brightness::WHITE) {
129  brightnessOfWhite = averageBrightness();
130  } else {
131  brightnessOfBlack = averageBrightness();
132  }
133 
134  return true;
135 }
136 
137 int Calibrator::averageBrightness()
138 {
139  // 4ms毎に10回明るさを取得して、その平均値をメンバ変数に代入する処理
140  int meanBrightness = 0;
141  int times = 10;
142  for(int i = 0; i < times; i++) {
143  meanBrightness += controller.getBrightness();
144  controller.tslpTsk(4);
145  }
146 
147  return meanBrightness / times;
148 }
149 
151 {
152  return isCameraMode;
153 }
154 
156 {
157  return isLeft;
158 }
159 
161 {
162  return brightnessOfWhite;
163 };
164 
166 {
167  return brightnessOfBlack;
168 };
Calibrator(Controller &controller_)
Definition: Calibrator.cpp:8
bool isLeftCourse() const
Definition: Calibrator.cpp:155
int getBlackBrightness() const
Definition: Calibrator.cpp:165
bool setLRCourse()
Definition: Calibrator.cpp:73
bool buttonIsPressedLeft()
Definition: Controller.cpp:61
static void print(int row, const char *format,...)
Definition: Display.cpp:3
bool buttonIsPressedRight()
Definition: Controller.cpp:56
static void speakerPlayToneFS6(int duration)
Definition: Controller.cpp:21
bool setBrightness(Brightness brightness)
Definition: Calibrator.cpp:99
int getBrightness()
Definition: Controller.cpp:71
static void tslpTsk(int time)
Definition: Controller.cpp:163
bool calibration()
falseが返ってきた場合、エラーメッセージがLCDに出ます。スタートを取りやめ、原因をさぐってください。 ...
Definition: Calibrator.cpp:17
bool buttonIsPressedEnter()
Definition: Controller.cpp:41
int getWhiteBrightness() const
Definition: Calibrator.cpp:160
キャリブレーションを行うクラス
bool setCameraMode()
Definition: Calibrator.cpp:47
Brightness
Definition: Calibrator.h:13
bool getCameraMode() const
Definition: Calibrator.cpp:150