-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpar4_gamble.py
344 lines (325 loc) · 13 KB
/
par4_gamble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# 2타
# 0번 샷: 오른쪽으로 치우쳐진 왼쪽 샷
# 1번 샷: 정교한 계산을 통해 오른쪽으로 센 샷
# 그 이상: 가까이 있는 노란색을 향한 샷
from Actuator.Motion import Motion
from Sensor.Camera import Camera
from Brain.Robot import Robot
from Brain.Robot_ball_distance import ball_distance
import cv2
import numpy as np
import time
shot_count = 0
plain_frame_count = 0
clockwise = "Left"
shot_direction = "Left"
shot_power = 8
shot_roi = False
r_turn_flag = False
if __name__ == "__main__":
Robot = Robot()
Motion = Motion()
Camera = Camera()
Motion.initial()
Motion.init(True)
Motion.neckup(50)
neck_before_find = 50
time.sleep(1)
# 미션 수행 함수 실행
print("Loop 시작 :)")
while True:
frame = Camera.get_image()
# image process
img = frame.copy()
Robot.is_ball, ballBox1, ballBox2 = Camera.hsvDetect(img)
# Robot.is_bunker, bunkerL, bunkerR = Camera.is_bunker(img)
# Robot.shotzone, hole_frame = Camera.shotzoneChecker(img)
if Robot.is_ball:
plain_frame_count = 0
cv2.rectangle(frame, ballBox1, ballBox2, (0,0,255), 2)
# if Robot.is_bunker:
# cv2.circle(frame, (bunkerL, 5, (255,255,255), -1))
# cv2.circle(frame, (bunkerR, 5, (255,255,255), -1))
# Finite State Machine
# 1. FindBall
if Robot.curr_mission == "FindBall":
# 공이 있으면 공으로 다가간다
if Robot.is_ball:
Robot.curr_mission = "ApproachBall"
# 공이 없으면 계속 공을 찾는다
else:
Robot.curr_mission = "FindBall"
# 2. ApproachBall
elif Robot.curr_mission == "ApproachBall":
(xmin, ymin) = ballBox1
(xmax, ymax) = ballBox2
Robot.robot_ball_distance = ball_distance(Robot.neck_pitch, ymax)
print(f"공으로 다가가는 중: {int(Robot.robot_ball_distance)} cm")
# 공에 가까이 가는 중에 순간 공이 감지되지 않았다면 일단 기다린다
if not Robot.is_ball:
plain_frame_count += 1
if plain_frame_count > 5:
Robot.curr_mission = "FindBall"
plain_frame_count = 0
# 공이 shot 가능한 위치에 있으면 goal을 찾는다
elif 9.5 <= Robot.robot_ball_distance <= 11 and 330 < (xmin+xmax) // 2 < 370:
Robot.curr_mission = "ShortCheck"
plain_frame_count = 0
if shot_count == 1 and not r_turn_flag:
r_turn_flag = True
Motion.circular_orbit("Right")
Motion.circular_orbit("Right")
Robot.curr_mission = "ApproachBall"
Robot.is_ball = False # 다음 state는 approachball이지만 공이 없다고 설정했으므로 한 프레임 대기
# 공이 shot 불가능한 위치에 있으면 공으로 다가간다
else:
Robot.curr_mission = "ApproachBall"
plain_frame_count = 0
# 3. ShortCheck
elif Robot.curr_mission == "ShortCheck":
Robot.shotzone, frame = Camera.shortChecker(img)
if shot_count == 1:
Robot.long_shot = True
Robot.curr_mission = "LongCheck"
shot_direction = "Right"
neck_before_find = Robot.neck_pitch
Robot.neck_pitch = 75
Motion.neckup(75)
elif Robot.shotzone == "!!!Shot!!!":
Robot.long_shot = False
Robot.curr_mission = "Shot"
shot_direction = "Left"
shot_power = 8
elif Robot.shotzone == "!!!R-Shot!!!":
Robot.long_shot = False
Robot.curr_mission = "Shot"
shot_direction = "Right"
shot_power = 2
elif Robot.shotzone == "NoHole":
Robot.curr_mission = "LongCheck"
neck_before_find = Robot.neck_pitch
Motion.neck_pitch = 70
Motion.neckup(70)
elif Robot.shotzone == "R-turn":
Robot.curr_mission = "ApproachGoal"
clockwise = "Right"
Robot.turn_angle = 100 # TODO Short Check turn angle 결정
elif Robot.shotzone == "L-turn":
Robot.curr_mission = "ApproachGoal"
clockwise = "Left"
Robot.turn_angle = 100 # TODO Short Check turn angle 결정
else: # hole in
Robot.curr_mission = "Ceremony"
# 4. LongCheck
elif Robot.curr_mission == "LongCheck":
if shot_count == 0:
Robot.shotzone, frame, shot_power = Camera.longChecker_gamble_firstshot(img)
elif shot_count == 1:
Robot.shotzone, frame, shot_power = Camera.longChecker_R_accurate(img)
else:
Robot.shotzone, frame, shot_power = Camera.longChecker_close(img)
if Robot.shotzone == "!!!Shot!!!":
Robot.long_shot = True
Robot.curr_mission = "Shot"
shot_direction = "Left"
elif Robot.shotzone == "!!!R-Shot!!!":
shot_direction = "Right"
Robot.long_shot = True
Robot.curr_mission = "Shot"
else:
Robot.curr_mission = "ApproachGoal"
Robot.neck_pitch = neck_before_find
Motion.neckup(Robot.neck_pitch)
if Robot.shotzone == "R-turn":
clockwise = "Right"
Robot.turn_angle = 100
elif Robot.shotzone == "R-turn-20":
clockwise = "Right"
Robot.turn_angle = 20
elif Robot.shotzone == "R-turn-10":
clockwise = "Right"
Robot.turn_angle = 10
elif Robot.shotzone == "R-turn-5":
clockwise = "Right"
Robot.turn_angle = 5
elif Robot.shotzone == "L-turn":
clockwise = "Left"
Robot.turn_angle = 100
elif Robot.shotzone == "L-turn-20":
clockwise = "Left"
Robot.turn_angle = 20
elif Robot.shotzone == "L-turn-10":
clockwise = "Left"
Robot.turn_angle = 10
else:
clockwise = "Left"
Robot.turn_angle = 5
# 5. ApproachGoal
elif Robot.curr_mission == "ApproachGoal":
# goal을 찾아 한걸음 움직였으면 공과의 거리를 보정한다
Robot.curr_mission = "ApproachBall"
# 6. Shot
elif Robot.curr_mission == "Shot":
shot_direction = "Left"
# shot을 하면 다음 shot을 위해 공을 찾는다
Robot.curr_mission = "ApproachBall"
shot_count += 1
if shot_count == 2:
shot_roi = True
# 7. Ceremony
else:
print("미션 종료")
break
print(f"현재 상태 {Robot.curr_mission}, neck: {Robot.neck_pitch}")
# show the frame to our screen
cv2.imshow("Frame", frame)
if cv2.waitKey(1) == ord("q"):
break
# motion
# 1. FindBall
if Motion.getRx() and Robot.curr_mission != "ApproachBall":
pass
elif Robot.curr_mission == "FindBall":
if shot_count == 0 and Robot.neck_pitch != 70:
Robot.neck_pitch = 70
Motion.neckup(70)
else:
Motion.init()
Motion.turn("LEFT", 45)
time.sleep(1)
Robot.neck_yaw = 0
# 2. ApproachBall
elif Robot.curr_mission == "ApproachBall":
(xmin, ymin) = ballBox1
(xmax, ymax) = ballBox2
xmean = (xmin + xmax) // 2
ymean = (ymin + ymax) // 2
# 공에 다가가다가 순간 공이 안 보인 경우
if not Robot.is_ball:
pass
# 공 bounding box가 화면 중앙에 오도록 움직이고 shot 가능할때까지 걸어간다
else:
if xmean < 190:
if Motion.getRx():
Motion.init()
Motion.turn("LEFT", 20)
elif xmean > 510:
if Motion.getRx():
Motion.init()
Motion.turn("RIGHT", 20)
elif ymean < 100 and Robot.neck_pitch < 100:
if Motion.getRx():
Motion.init()
Robot.neck_pitch += 5
Motion.neckup(Robot.neck_pitch)
elif ymean > 280 and Robot.neck_pitch > 35:
if Motion.getRx():
Motion.init()
Robot.neck_pitch -= 5
Motion.neckup(Robot.neck_pitch)
elif Robot.robot_ball_distance < 9.5:
if Motion.getRx():
Motion.init()
Motion.step("BACK")
elif Robot.robot_ball_distance > 30 and shot_count==1:
Motion.walk(True)
elif Robot.robot_ball_distance > 18:
Motion.walk()
elif Robot.robot_ball_distance > 13:
if Motion.getRx():
Motion.init()
Motion.step("FRONT", "big")
elif Robot.robot_ball_distance > 11:
if Motion.getRx():
Motion.init()
Motion.step("FRONT", "small")
elif xmean < 340:
if Motion.getRx():
Motion.init()
Motion.crab("LEFT")
time.sleep(0.5)
elif xmean > 360:
if Motion.getRx():
Motion.init()
Motion.crab("RIGHT")
else:
Motion.init()
Robot.neck_yaw = 0
# 3. ShortCheck
elif Robot.curr_mission == "ShortCheck":
if Motion.getRx():
Motion.init()
Robot.neck_pitch = 45
Motion.neckup(45)
time.sleep(1)
# 4. LongCheck
elif Robot.curr_mission == "LongCheck":
if Motion.getRx():
Motion.init()
if shot_count == 1:
Motion.eagle()
Robot.neck_yaw = 90
Motion.view(90)
else:
Robot.neck_yaw = -90
Motion.view(-90)
time.sleep(1)
# 5. ApproachGoal
elif Robot.curr_mission == "ApproachGoal":
if Motion.getRx():
Motion.init()
if shot_count == 1: # 내렸던 골프채 다시 올리기
Motion.init()
Robot.neck_yaw = 0
Motion.view(0)
time.sleep(1)
# hole이 공이 움직일 궤도 왼쪽에 있다면 반시계 방향으로 회전한다
if clockwise == "Left":
if Robot.turn_angle == 100:
Motion.circular_orbit("Left", False)
else:
Motion.circular_orbit_small("Left", Robot.turn_angle)
else:
if Robot.turn_angle == 100:
Motion.circular_orbit("Right", True)
else:
Motion.circular_orbit_small("Right", Robot.turn_angle)
time.sleep(1) # 동작 안정성을 위한 대기
# 6. Shot
elif Robot.curr_mission == "Shot":
if Motion.getRx():
Motion.init()
Robot.neck_yaw = 0
Motion.view(0)
if shot_direction == "Left":
if shot_count == 0:
shot_power = 19
Motion.shot("LEFT", shot_power)
else:
Motion.shot("RIGHT", 94) # TODO 오른쪽 샷 세기 조절
if Robot.long_shot:
if shot_power < 14:
Robot.neck_pitch = 60
elif shot_power < 19:
Robot.neck_pitch = 70
else:
Robot.neck_pitch = 80
Motion.neckup(80)
if shot_direction == "Left":
Motion.turn("LEFT", 45)
Motion.turn("LEFT", 45)
else:
Motion.turn("RIGHT", 60)
Motion.turn("RIGHT", 60)
Motion.turn("RIGHT", 60)
else:
if shot_direction == "Left":
Motion.turn("LEFT", 20)
else:
Motion.turn("RIGHT", 20)
# 7. Ceremony
else:
if Motion.getRx():
Motion.init()
Motion.ceremony()
cv2.destroyAllWindows()