etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
BlockSolver.cpp
[詳解]
1 
6 #include "BlockSolver.h"
7 
9 {
10  // 最初の8番までのライントレース
11  navigator.moveOnLine(500, targetBrightness, 55);
12  moveOnLineToColor(45, targetBrightness - 10, Color::RED, false);
13 
14  // ブロックがある0番までの移動
15  std::vector<int> route{ 8, 12, 13 };
16  moveRoute(route);
17 
18  // ブロック読み取り(青だとする。)
19  getBlockColor();
20  if(blockColor == Color::BLUE) {
21  // 青ブロックを10番まで移動。その後、9に戻る
22  std::vector<int> route2 = { 13, 9, 10 };
23  moveRoute(route2);
24  byeByeBlock();
25  }
26 
27  // 9番までの移動とパーキングの方を向く。
28  std::vector<int> route3 = { 10, 9, 5, 6, 7, 11 };
29  moveRoute(route3);
31  navigator.spin(90, false);
32 }
33 
35 {
36  if(gameColor == Color::RED) {
37  return Selector::BlockColor::Red;
38  } else if(gameColor == Color::BLUE) {
39  return Selector::BlockColor::Blue;
40  } else if(gameColor == Color::GREEN) {
41  return Selector::BlockColor::Green;
42  } else if(gameColor == Color::YELLOW) {
43  return Selector::BlockColor::Yellow;
44  }
45  return Selector::BlockColor::Undefined;
46 }
47 
49 {
50  // 最初の8番までのライントレース
51  //navigator.moveOnLine(500, targetBrightness, 60);
52  moveOnLineToColor(50, targetBrightness - 10, Color::RED, false);
53  std::vector<int> route{ 8, 9, 10, 11 };
54  moveRoute(route);
55  //spinParkingArea();
56 
57  return;
58  Selector selector;
59  selector.setBlockPositionList({ 8, 9, 11, 15 }); // ipcd.getInitialPositionCodeList());
60 
62  while(!selector.isAlreadyAllBlockMoved()) {
63  auto route = selector.exploreNextOperation(nowPlace, Selector::Undefined); // b_color);
64 
65  // 目的地移動前にバックステップを行う必要がある場合
66  if(selector.isBacksteppingBeforeNextOperation()) {
67  byeByeBlock();
68  }
69 
70  // 移動
71  moveRoute(route);
72 
73  if(selector.isMovingWithNext()) {
74  // ブロックまで移動する場合
75  getBlockColor();
76  b_color = convertSelectorColor(blockColor);
77  } else {
78  // ブロックを移動する場合
79  b_color = Selector::Undefined;
80  }
81 
82  // 移動後にバックステップを行う必要がある場合
83  if(selector.isBackstepping()) {
84  byeByeBlock();
85  }
86  }
88 }
89 
91 {
92  std::int8_t returnPlace = blockStrategy.blockArea.colorBlockPlaceArray[nowPlace]
93  ->black_line[getChangeDirection(2)]
94  ->getPlaceCord();
95  moveDirection(returnPlace, true);
96 }
97 
98 void BlockSolver::moveRoute(std::vector<int>& route)
99 {
100  bool isFirst = true;
101  for(auto next_n : route) {
102  if(isFirst == true) {
103  isFirst = false;
104  continue;
105  } else if(next_n == nowPlace) {
106  continue;
107  }
108  controller.tslpTsk(50);
109  moveDirection(next_n);
110  }
111  controller.speakerPlayTone(controller.noteFs4, 300);
112  return;
113 }
114 
116 {
117  std::int8_t diffDirection = static_cast<std::int8_t>(BlockSideBySide::Direction::WEST)
118  - static_cast<std::int8_t>(nowDirection);
120  if(diffDirection == -1 || diffDirection == 3) {
121  // 左方向に行く場合
122  navigator.spin(110, false);
124  } else {
125  // 右方向に行く場合
126  navigator.spin(90, true);
128  }
129 }
130 
131 void BlockSolver::moveDirection(const std::int8_t& nextPlace, bool remove_block)
132 {
133  std::int8_t offset = 5;
134  auto nextDirection = blockStrategy.blockArea.getDirection(nowPlace, nextPlace);
135  auto nowColor = convertColor(blockStrategy.blockArea.colorBlockPlaceArray[nowPlace]->getColor());
136  auto nextColor
137  = convertColor(blockStrategy.blockArea.colorBlockPlaceArray[nextPlace]->getColor());
138 
139  std::int8_t diffDirection
140  = static_cast<std::int8_t>(nextDirection) - static_cast<std::int8_t>(nowDirection);
141  std::int16_t black_line_length = 250;
142  if(nextDirection == BlockSideBySide::Direction::EAST
143  || nextDirection == BlockSideBySide::Direction::WEST) {
144  black_line_length = 300;
145  }
146  if(diffDirection == 0) {
147  // 前方向に行く場合
148  passCircle(nowColor);
149  } else if(diffDirection == -1 || diffDirection == 3) {
150  // 左方向に行く場合
151  passCircle(nowColor);
152  navigator.spin(110, false);
153  // spinToBlack(false);
155  } else if(diffDirection == 1 || diffDirection == -3) {
156  // 右方向に行く場合
157  passCircle(nowColor);
158  navigator.spin(90, true);
159 
160  // spinToBlack(true);
162  } else if(remove_block == false) {
163  // 後方向に行く場合
164  black_line_length -= 100;
165  navigator.spin(170, true);
166 
167  // spinToBlack(true);
169  } else {
170  // 後方向に行く場合
171  black_line_length -= 200;
172  navigator.back(100);
173  // spinToBlack(true);
174  navigator.spin(170, true);
175 
177  }
178  controller.speakerPlayTone(controller.noteFs4, 150);
179  //navigator.moveOnLine(black_line_length - 40, targetBrightness - offset, 30);
180  moveOnLineToColor(30, targetBrightness - offset, nextColor, false);
181  nowPlace = nextPlace;
182 }
183 
184 Color BlockSolver::convertColor(const BlockSideBySide::GameColor& gameColor)
185 {
186  if(gameColor == BlockSideBySide::GameColor::RED) {
187  return Color::RED;
188  } else if(gameColor == BlockSideBySide::GameColor::BLUE) {
189  return Color::BLUE;
190  } else if(gameColor == BlockSideBySide::GameColor::GREEN) {
191  return Color::GREEN;
192  } else if(gameColor == BlockSideBySide::GameColor::YELLOW) {
193  return Color::YELLOW;
194  }
195  return Color::NONE;
196 }
197 
199 {
200  std::int8_t tmp = static_cast<std::int8_t>(nowDirection) + angle;
201  if(tmp > 3) {
202  tmp = tmp % 4;
203  } else if(tmp < 0) {
204  tmp = 4 + angle;
205  }
206  return static_cast<BlockSideBySide::Direction>(tmp);
207 }
208 
209 void BlockSolver::moveOnLineToColor(std::int8_t pwm, std::int16_t target, const Color& circle_color,
210  bool isRight)
211 {
212  std::int8_t buf = 1;
213  controller.tslpTsk(50);
214 
215  if(isRight == true) {
216  buf = -1;
217  }
218  walker.reset();
219  // ライントレースで用いるPID値のセット
220  lineTracer.speedControl.setPid(2.0, 0.8, 0.1, pwm);
221  lineTracer.turnControl.setPid(1.1, 0.1, 0.2, target);
222  Color tmp;
223  std::int8_t count = 0;
224  // navigator.move(15);
225  while(1) {
226  tmp = distinguisher.getColor();
227  // controller.printDisplay(10, "%4d %4d %4d", pwm, turn, target);
228  lineTracer.runLine(walker.get_count_L(), walker.get_count_R(), controller.getBrightness());
229  if(circle_color == tmp) {
230  count++;
231  } else {
232  if(count > 0) {
233  count--;
234  }
235  walker.run(lineTracer.getForward(), lineTracer.getTurn() * buf);
236  }
237  if(count >= 2 || controller.buttonIsPressedBack()) {
238  walker.run(0, 0);
239  break;
240  }
241  controller.tslpTsk(4);
242  }
243  walker.run(0, 0);
244  controller.tslpTsk(50);
245 }
246 
248 {
249  //ブロックの色をチェックして保持する
250  controller.speakerPlayTone(controller.noteFs4, 100);
251  lifter.liftUp(45, 10);
252  blockColor = distinguisher.getColor();
253  lifter.liftDown(0, 3);
254  lifter.liftUp(1.5, 5);
255 }
256 
257 void BlockSolver::passCircle(const Color& circle_color)
258 {
259  controller.speakerPlayTone(controller.noteFs4, 100);
260  navigator.move(90, 5);
261  walker.run(5, 0);
262  Color tmp;
263  std::int8_t count = 0;
264  while(1) {
265  tmp = distinguisher.getColor();
266  if(Color::BLACK == tmp || Color::WHITE == tmp) {
267  count++;
268  } else {
269  if(count > 0) {
270  count--;
271  }
272  }
273  if(count >= 1 || controller.buttonIsPressedBack()) {
274  walker.run(0, 0);
275  break;
276  }
277  controller.tslpTsk(4);
278  }
279  navigator.move(10, 5);
280 }
bool isBackstepping()
Definition: Selector.cpp:294
void speakerPlayTone(uint16_t frequency, int32_t duration)
Definition: Controller.cpp:9
void byeByeBlock()
バックして180ど回転し、来た方向へ戻ります。ブロックを放置するときに使います。
Definition: BlockSolver.cpp:90
ブロック並べ解決
void tslpTsk(int16_t time)
Definition: Controller.cpp:74
std::array< std::shared_ptr< ColorBlockPlace >, 16 > colorBlockPlaceArray
Definition: BlockArea.h:53
void moveOnLine(float distance, std::int16_t target, std::int8_t pwm=20, bool is_leftside_line=true)
指定した距離だけライントレースする
Definition: Navigator.cpp:163
void move(float distance, std::int8_t pwm=20)
指定した距離まで走行体を移動させる
Definition: Navigator.cpp:48
BlockSideBySide::Direction getDirection(std::int8_t n1, std::int8_t n2)
Definition: BlockArea.cpp:3
void setPid(double p_gain_, double i_gain_, double d_gain_, double target_)
Definition: Pid.cpp:40
void spinParkingArea()
Color
Definition: Distinguisher.h:16
void moveDirection(const std::int8_t &nextPlace, bool remove_block=false)
指定した方角へ移動します。
void runLine(int32_t countL, int32_t countR, int16_t light_value)
bool buttonIsPressedBack()
Definition: Controller.cpp:24
BlockSideBySide::Direction getChangeDirection(std::int8_t angle)
次に向かうべき方角を切り替えます。
void run(std::int8_t pwm, std::int8_t turn)
Definition: Walker.cpp:30
void setBlockPositionList(std::vector< std::int8_t > list)
Definition: Selector.cpp:196
void passCircle(const Color &circle_color)
ブロック置き場を乗り越えます。
void run()
ブロック並べを攻略します。
Definition: BlockSolver.cpp:48
int16_t getBrightness()
Definition: Controller.cpp:59
void liftDown(std::uint8_t angle, std::int8_t pwm=20)
Definition: Lifter.cpp:45
void back(float distance, std::int8_t pwm=10)
指定した距離まで走行体を後退させる
Definition: Navigator.cpp:72
void demo()
デモです。アリアに従って移動します。
Definition: BlockSolver.cpp:8
bool isMovingWithNext()
Definition: Selector.cpp:284
uint16_t noteFs4
Definition: Controller.h:41
void spin(float angle, bool clockwise=false, std::int8_t pwm=10)
指定した角度まで走行体を回転させる(基本左エッジ)
Definition: Navigator.cpp:25
std::vector< int > exploreNextOperation(std::int8_t currentPosition, BlockColor color)
Definition: Selector.cpp:3
void reset()
Definition: Walker.cpp:15
std::int32_t get_count_R()
Definition: Walker.cpp:54
void getBlockColor()
ブロックの色を取得し、blockColorに格納します。
void moveOnLineToColor(std::int8_t pwm, std::int16_t target, const Color &circle_color, bool isRight)
指定した色のブロック置き場までライントレースします。
bool isAlreadyAllBlockMoved()
Definition: Selector.cpp:183
TurnControl turnControl
void moveRoute(std::vector< int > &route)
ルートを移動します。
Definition: BlockSolver.cpp:98
bool isBacksteppingBeforeNextOperation()
Definition: Selector.cpp:299
Selector::BlockColor convertSelectorColor(const Color &gameColor)
Definition: BlockSolver.cpp:34
void liftUp(std::uint8_t angle, std::int8_t pwm=20)
Definition: Lifter.cpp:33
BlockSideBySide::Direction nowDirection
今向いてる方角です。
Definition: BlockSolver.h:58
Color getColor()
SpeedControl speedControl
std::int32_t get_count_L()
Definition: Walker.cpp:49
ブロック選択クラス
Definition: Selector.h:113