forked from KOSS-ROKO/Team_RoKo_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerA.py
1044 lines (857 loc) · 40.4 KB
/
ControllerA.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import time
from Robo import Robo
from Head import Head
from Motion import Motion
import Distance
# Par 4
class Act:
TEESHOTA = 1 # 1. 맨 처음 티샷
SECSHOT = 2
WALK_BALL = 3 # 2. 공까지 걸어가기 (걸음수)
PUTTING_POS = 4 # 3. 퍼팅 위치에 서기
PUTTING = 5 # 4. 퍼팅
HOLEIN = 6 # 5. 홀인
class ControllerA:
def __init__(self):
# act = Act.TEESHOTB
pass
act = Act.TEESHOTA
robo = Robo()
@classmethod
def start(self):
act = self.act
robo = self.robo
head = Head()
motion = Motion()
#=======================================================#
# Head def #
#=======================================================#
def big_UD(object="ball"):
big_ud_angle = 100
# big UD head
while True:
is_object_in_frame, Distance.Head_ud_angle = head.big_UD_head(object, big_ud_angle)
if is_object_in_frame == True:
big_ud_angle = Distance.Head_ud_angle
return "Success"
elif is_object_in_frame == False:
big_ud_angle = Distance.Head_ud_angle
if Distance.Head_ud_angle == 55: # Distance.Head_UD_Middle_Value_Measures - 100 + 10 + 45: # big ud 한 사이클이 끝남. / 9는 바뀔 수 있는 값
#Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures # 고개값을 다시 정면100으로
return "Except"
else:
continue
def small_UD(object="ball"):
small_ud_angle = 55
while True:
is_horizontal_middle, small_ud_temp = head.small_UD_head(object, small_ud_angle)
if is_horizontal_middle == True: #최종 중앙 맞춰짐
return "Success"
elif is_horizontal_middle == False:
small_ud_angle = small_ud_temp
continue
else: # is_horizontal_middle == Except_
return "Except"
def big_LR(object="ball"):
Distance.head_lr_angle = 100
max_right_flag = 0
print("THis is ", object)
# big LR head
while True:
is_object_in_frame, small_lr_temp, max_right_flag = head.big_LR_head(object, Distance.head_lr_angle, max_right_flag)
if small_lr_temp == 190: #왼쪽 max까지 갔는데 공 못찾으면
Distance.head_lr_angle = 100
robo._motion.head("DEFAULT", 2)
return "Except"
if is_object_in_frame == True:
return "Success"
elif is_object_in_frame == False:
Distance.head_lr_angle = small_lr_temp
print("head_lr_angle : ", Distance.head_lr_angle)
continue
#고개 정면 코드 추가하기
def ball_small_LR(object="ball"): # ball은 small lr끝난뒤 몸 돌리고 고개 default함
Distance.head_lr_angle = 100
cnt = 0
while True:
cnt += 1
print(cnt)
print("---------start small lr head")
is_vertical_middle, small_lr_temp = head.small_LR_head(object, Distance.head_lr_angle)
if cnt > 40:
print("small lr,,, cnt > 40,,, except")
return "Except"
if is_vertical_middle == True:
return "Success" #break
elif is_vertical_middle == False:
Distance.head_lr_angle = small_lr_temp
continue
else : # is_vertical_middle == Except_
return "Except"
def small_LR(object="ball2"): # ball은 small lr끝난뒤 몸, 고개 그대로, 끝.
#Distance.head_lr_angle = 100
while True:
print("---------start small lr head")
is_vertical_middle, small_lr_temp = head.small_LR_head(object, Distance.head_lr_angle)
if is_vertical_middle == True:
return "Success" #break
elif is_vertical_middle == False:
Distance.head_lr_angle = small_lr_temp
continue
else : # is_vertical_middle == Except_
return "Except"
def UD_for_dist(object="ball"): # small ud head 변형
small_ud_angle = Distance.Head_ud_angle
time.sleep(0.5)
# 거리를 위한 고개 각도 내리기
while True:
print("---------start ud for dist")
is_horizontal_middle, small_ud_temp = head.head_for_dist(object, small_ud_angle)
# 100 이상에서 검출될 경우 예외처리
if small_ud_temp >= 100 :
small_ud_temp = 91
Distance.Head_ud_angle = 91
motion.head("DOWN", 9)
continue
if is_horizontal_middle == True: #최종 중앙 맞춰짐
Distance.Head_ud_angle = small_ud_temp
if Distance.Head_ud_angle >= 90:
time.sleep(3)
continue
print("head UD angle : ", Distance.Head_ud_angle)
break
elif is_horizontal_middle == False:
small_ud_angle = small_ud_temp
Distance.Head_ud_angle = small_ud_temp
continue
# def holecup_UD_for_dist(): # small ud head 변형
# small_ud_angle = 10
# # 거리를 위한 고개 각도 올리기
# while True:
# print("---------start HOlECUP ud for dist")
# is_horizontal_middle, small_ud_temp = head.head_for_dist("holecup", small_ud_angle)
# if is_horizontal_middle == True: #최종 중앙 맞춰짐
# #act = Act.PUTTING_POS
# #variable.Head_ud_angle =
# Distance.Head_ud_angle = small_ud_temp
# print("holecup ud angle : ",Distance.Head_ud_angle)
# break
# elif is_horizontal_middle == False:
# if small_ud_angle > small_ud_temp : # 홀컵중점 바껴서 갇히는 현상 해결
# Distance.Head_ud_angle = small_ud_temp #(상관없) 더 최근 고개값 = 더 먼 거리값
# break
# small_ud_angle = small_ud_temp
# Distance.Head_ud_angle = small_ud_temp
# continue
###########
def ball_pos(role):
time.sleep(0.2)
motion.head ("DEFAULT", 63)
time.sleep(1)
print("++++++++++++++++++")
print("ball pos: ", role)
print("++++++++++++++++++")
is_center = False
if role == 'p41':
x,y = reference_point = [430, 306] # par3 1st teeshot # p41
v,w = 0,5
rectangle_coordinates = [x-v, y-w, x+w, y-w, x+w, y+v, x-v, y+v]
elif role == 'p42':
x,y = reference_point = [420, 310] # par4 2nd teeshot # p42
v,w = 0,5
rectangle_coordinates = [x-v, y-w, x+w, y-w, x+w, y+v, x-v, y+v]
elif role == 'p43':
x,y = reference_point = [390, 310] # par4 2nd teeshot # p42
v,w = 0,5
rectangle_coordinates = [x-v, y-w, x+w, y-w, x+w, y+v, x-v, y+v]
elif role == 'pr':
x,y = reference_point = [362, 312] # right putting shot # pr
v,w = 3,3
rectangle_coordinates = [x-v, y-w, x+w, y-w, x+w, y+v, x-v, y+v]
pr = 0
side_left_cnt = 0
side_right_cnt = 0
while not is_center:
red_center = robo._image_processor.detect_ball('call_midpoint')
x1, y1, x2, y2, x3, y3, x4, y4 = rectangle_coordinates
print("현재 빨간공 중심: ", red_center ,"목표 지점: ",reference_point)
if(red_center == None):
print("지금 화면안에 빨간 공 안보임")
motion.walk("BACKWARD")
time.sleep(3)
continue
else:
if(x1 <= red_center[0] <= x2 and y1 <= red_center[1] <= y4):
print("성공함요")
break
dx = red_center[0] - reference_point[0]
dy = red_center[1] - reference_point[1]
print("중앙에서 떨어진 거리: ", dx, dy, abs(dx),abs(dy))
# print("dx//30: ",dx//30 ,"dy//30",dy//30)
if(abs(dx)>=30):
if (dx<0):
motion.walk_side("LEFT10")
side_left_cnt += 1
if side_left_cnt % 2 == 0 and role is'pr':
motion.turn("LEFT",10)
time.sleep(0.5)
print("3")
else:
motion.walk_side("RIGHT10")
side_right_cnt += 1
if side_right_cnt % 2 == 0 and role is 'pr':
motion.turn("RIGHT",10)
time.sleep(0.5)
print("4")
elif(abs(dy)>=30):
if (dy<0):
motion.walk("2JFORWARD")
print("1")
else:
motion.walk("2JBACKWARD")
print("2")
else:
is_center = True
Distance.Head_ud_angle = 63
def Set_holecup_right():
time.sleep(0.1)
motion.head("DEFAULT", 0) # 고개 디폴트
time.sleep(0.1)
is_left = False
motion.Rarm("DOWN")
time.sleep(1)
for i in range(0,5):
time.sleep(0.1)
print("왼쪽에 있는지 확인")
is_holecup = robo._image_processor.detect_holecup()
print("CHECK HOLCUP : ", is_holecup)
if is_holecup:
print("왼편에 있음")
is_left = True
break
time.sleep(0.1)
motion.head("RIGHT",30)
time.sleep(0.5)
print("is_left: ", is_left)
motion.head("RIGHT",90) # 머리 오른쪽 90도
time.sleep(1)
print("팔 내리고 머리 90으로 돌림")
if is_left:
while True:
is_holecup = robo._image_processor.detect_holecup()
print("홀컵 있는지 확인", is_holecup)
if is_holecup:
print("찾음")
break
time.sleep(0.1)
robo._motion.holecup_turn('LEFT', 20)
print("완쪽으로 몸 돌리기")
time.sleep(1)
else:
while True:
time.sleep(0.1)
robo._motion.holecup_turn('RIGHT', 10)
print("오른쪽으로 몸 돌리기")
time.sleep(1)
is_holecup = robo._image_processor.detect_holecup()
print("홀컵있는지 확인", is_holecup)
if is_holecup:
print("찾음")
is_left = True
break
while True:
time.sleep(0.2)
# mid = 340
# min = mid - 8
# max = mid + 8
# mid = 230
mid = 315
min = mid - 15
max = mid + 15
holecup_midpoint = robo._image_processor.detect_holecup("call_toppoint")
print("홀컵 중앙은", holecup_midpoint, "목푤는 : ", min, max)
if is_left and holecup_midpoint == (0,0):
robo._motion.holecup_turn('LEFT', 10)
if min<=holecup_midpoint[0] <= max:
print("범위안에 들어옴 종료 성공")
robo._motion.Rarm('RESET')
time.sleep(0.5)
motion.head("DEFAULT", 0) # 고개 디폴트
time.sleep(0.5)
break
elif holecup_midpoint[0] >= max:
if holecup_midpoint[0] > max + 150:
print("RIGHT 회전하고 쉬기")
time.sleep(0.1)
robo._motion.holecup_turn('RIGHT', 20)
time.sleep(0.1)
else:
print("RIGHT 회전하고 쉬기")
time.sleep(0.1)
robo._motion.holecup_turn('RIGHT', 5)
time.sleep(0.1)
elif min>=holecup_midpoint[0]:
if min-150>=holecup_midpoint[0]:
print("왼쪽 회전하고 쉬기")
time.sleep(0.1)
robo._motion.holecup_turn('LEFT', 20)
time.sleep(0.1)
else:
print("왼쪽 회전하고 쉬기")
time.sleep(0.2)
robo._motion.holecup_turn('LEFT', 5)
time.sleep(0.1)
def Set_holecup_left():
time.sleep(0.5)
motion.head("DEFAULT", 0) # 고개 디폴트
### 고개 내리는 거
#motion.head("DOWN", 60) # 고개 디폴트
time.sleep(1)
is_right = False
#print("is_left: ", is_right)
for i in range(0,3):
time.sleep(0.1)
is_holecup = robo._image_processor.detect_holecup()
print(i,"HOLCUP은: ", is_holecup)
if is_holecup:
print("right편에 있음")
is_right = True
break
time.sleep(0.1)
motion.head("LEFT",30)
time.sleep(1)
print("is_left: ", is_right)
motion.head("LEFT",90) # 머리 오른쪽 90도
time.sleep(0.2)
print("머리 90으로 돌림")
if is_right:
while True:
is_holecup = robo._image_processor.detect_holecup()
print("홀컵 있는지 확인", is_holecup)
if is_holecup:
print("찾음")
break
time.sleep(0.1)
robo._motion.turn('RIGHT', 20)
print("오른쪽으로 몸 돌리기")
time.sleep(0.1)
else:
while True:
time.sleep(0.1)
is_holecup = robo._image_processor.detect_holecup()
print("홀컵있는지 확인", is_holecup)
if is_holecup:
print("찾음")
is_right = True
break
robo._motion.turn('LEFT', 45)
print("왼쪽으로 몸 돌리기")
time.sleep(0.1)
while True:
time.sleep(0.2)
holecup_midpoint = robo._image_processor.detect_holecup("call_toppoint")
# mid = 360 ###### if body left ++, if body right --
mid = 455
min = mid - 10
max = mid + 10
print("홀컵 중앙은", holecup_midpoint, "목푤는 : ", min, max)
if holecup_midpoint == (0,0):
robo._motion.turn('RIGHT', 20)
time.sleep(0.5)
if min <= holecup_midpoint[0] <= max:
print("범위안에 들어옴 종료 성공")
motion.head("DEFAULT", 0) # 고개 디폴트
time.sleep(1)
break
elif holecup_midpoint[0] > max:
print("오른쪽 5 회전하고 쉬기")
robo._motion.holecup_turn('RIGHT', 5)
time.sleep(0.1)
elif min>holecup_midpoint[0]:
if holecup_midpoint[0] < min-150:
print("왼쪽 10 회전하고 쉬기")
robo._motion.holecup_turn('LEFT', 10)
time.sleep(0.1)
else:
print("왼쪽 5 회전하고 쉬기")
robo._motion.holecup_turn('LEFT', 5)
time.sleep(0.1)
#=======================================================#
# 1. Teeshot A #
#=======================================================#
motion.head("DEFAULT", 0)
time.sleep(1)
if act == Act.TEESHOTA: ##### 1. 시작 및 티샷 #################
print("ACT: ", act, "Teeshot A") # Debug
time.sleep(2)
UD_for_dist("ball")
# length = 거리
ball_dist = Distance.Length_ServoAngle_dict.get(Distance.Head_ud_angle)
print(Distance.Length_ServoAngle_dict)
print("==========================================")
print("ball dist: ", ball_dist , "===========","head angle: ", Distance.Head_ud_angle)
print("==========================================")
motion.head("DEFAULT", 1) # ud for dist 이후 고개 상하 디폴트
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(2)
point = 0 # 점 1, 2, 3 할당
p = 'p41'
if ball_dist < 26: # 점 1
point = 1
p = 'p41'
motion.turn("RIGHT", 25)
time.sleep(1)
elif ball_dist < 46: # 점 2
point = 2
p = 'p42'
motion.walk("FORWARD", ball_dist - 18)
time.sleep(2)
motion.turn("RIGHT", 10)
time.sleep(1)
else : # 점 3
point = 3
p = 'p43'
motion.walk("FORWARD", ball_dist - 18)
ball_pos(p)
time.sleep(1)
# PUTTING
motion.putting("PAR4", 1, 2)
print("putting")
time.sleep(5)
### 점 1,2인 경우엔 walk해서 점 3까지 !
if point==1: # 점 1
motion.turn("LEFT", 15)
time.sleep(0.7)
motion.walk("FORWARD6")
time.sleep(11)
motion.walk("FORWARD2")
time.sleep(6.5)
elif point==2: # 점 2
motion.walk("FORWARD3")
time.sleep(7.5)
else : # 점 3
motion.walk("FORWARD2")
time.sleep(5)
#return True
motion.turn("LEFT", 10)
time.sleep(0.5)
motion.turn("LEFT", 10)
time.sleep(0.5)
motion.walk_side("LEFT120cm")
time.sleep(18)
# turn은 이연 side walk 정확도 높아지면 뺄거임
motion.turn("LEFT", 20)
time.sleep(1)
motion.turn("LEFT", 90)
time.sleep(4)
# motion.turn("LEFT", 10)
# time.sleep(0.5)
self.act = Act.SECSHOT
#=======================================================#
# 3. SECSHOT (두번째 티샷) #
#=======================================================#
elif act == Act.SECSHOT: ##### 1. 시작 및 티샷 #################
print("ACT: ", act) # Debug
print("^^두번째 티샷^^")
motion.head("UP", 9)
time.sleep(1)
motion.head("UP", 9)
# 지금 각도 45도임
# 공 찾고 중앙 맞추기
is_ball = robo._image_processor.detect_ball()
#### False면, big UD LR 해라
if is_ball == False:
motion.head("DEFAULT", 1)
time.sleep(1)
while True:
# big UD head
is_big_UD = big_UD("ball")
time.sleep(0.5)
print("controller === big ud ")
if is_big_UD == "Except" : # big UD 검출안됨 -> big LR 로 넘어감
motion.head("UP", 6)
is_big_LR = big_LR("ball") # big은 알아서 고개 디폴트 함
if is_big_LR == "Except":
motion.walk("BACKWARD")
time.sleep(3)
motion.head("DEFAULT",0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
Distance.head_lr_angle = 100
time.sleep(1)
continue
is_small_LR = ball_small_LR("ball")
if is_small_LR == "Except" :
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
continue
else:
break
else:
ball_small_LR("ball") # small lr 함으로써 중앙 맞춰짐
## 이제 남은 거리만큼 공까지 걷기
# ud_for_dist 하기전에 고개 세팅
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
print("ball detected")
UD_for_dist("ball")
motion.head("DEFAULT", 1) # ud for dist 이후 고개 상하 디폴트
time.sleep(2)
# length = 거리
ball_dist = Distance.Length_ServoAngle_dict.get(Distance.Head_ud_angle)
print("ball distance :", ball_dist)
# 남은 거리 만큼 걷기
if ball_dist > 18:
motion.walk("FORWARD", ball_dist - 18)
time.sleep(1)
elif ball_dist == 18:
print("correct!")
else : # 최소 거리 18보다 더 가까이 있을 경우: 뒷걸음질
motion.walk("BACKWARD", ball_dist - 18)
time.sleep(2)
time.sleep(1)
ball_pos('pr')
time.sleep(1)
Set_holecup_right()
ball_pos('pr')
time.sleep(1)
### 진짜 두번째 TEESHOT
motion.putting("PAR4", 2)
time.sleep(5)
#######
#return True
#######
motion.walk("FORWARD3")
time.sleep(8)
motion.turn("RIGHT", 10)
time.sleep(1)
motion.walk_side("RIGHT120cm")
time.sleep(18)
motion.walk_side("RIGHT70")
time.sleep(5)
motion.turn("RIGHT", 90)
time.sleep(4)
self.act = Act.WALK_BALL
# return True
#=======================================================#
# 2. Walk #
#=======================================================#
elif act == Act.WALK_BALL:
##### 2. 공을 향해 걸어간다 #################
print("^^^^222222")
print("^^^^222222")
print("^^^^222222")
print("^^^^222222")
motion.head("UP", 9)
time.sleep(1)
motion.head("UP", 9)
# 지금 각도 45도임
is_ball = robo._image_processor.detect_ball()
#### False면, big UD LR 해라
if is_ball == False:
motion.head("DEFAULT", 1)
time.sleep(1)
while True:
# big UD head
is_big_UD = big_UD("ball")
time.sleep(0.5)
print("controller === big ud ")
if is_big_UD == "Except" : # big UD 검출안됨 -> big LR 로 넘어감
motion.head("UP", 6)
is_big_LR = big_LR("ball") # big은 알아서 고개 디폴트 함
if is_big_LR == "Except":
motion.walk("BACKWARD")
time.sleep(3)
motion.head("DEFAULT",0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
Distance.head_lr_angle = 100
time.sleep(1)
continue
is_small_LR = ball_small_LR("ball")
if is_small_LR == "Except" :
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
continue
else:
break
else:
ball_small_LR("ball") # small lr 함으로써 중앙 맞춰짐
# ud_for_dist 하기전에 고개 세팅
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(2)
print("ball detected")
UD_for_dist("ball")
motion.head("DEFAULT", 1) # ud for dist 이후 고개 상하 디폴트
time.sleep(1)
# length = 거리
ball_dist = Distance.Length_ServoAngle_dict.get(Distance.Head_ud_angle)
print("ball distance :", ball_dist)
# 무지성 10번 걸은 후, 남은 거리 측정 후 걷기
if ball_dist > 26: # 18+8 (화면에 여유있게 들어오도록)
motion.walk("FORWARD", ball_dist - 26)
elif ball_dist == 26:
print("correct!")
else : # 최소 거리 18보다 더 가까이 있을 경우: 뒷걸음질
motion.walk("BACKWARD", ball_dist - 26)
time.sleep(1)
#---------------------ball dist one more time
'''
# 거리 한 번 더 재는거 뺌
motion.head("DOWN", 45) # 고개 45도로 내리고 공 detect 시작 !
time.sleep(2)
is_ball = robo._image_processor.detect_ball()
### False면, big UD LR 해라
if is_ball == False:
motion.head("DEFAULT", 1)
time.sleep(1)
while True:
# big UD head
is_big_UD = big_UD("ball")
print("controller === big ud ")
if is_big_UD == "Except" : # big UD 검출안됨 -> big LR 로 넘어감
big_LR("ball") # big은 알아서 고개 디폴트 함
is_small_LR = ball_small_LR("ball")
if is_small_LR == "Except" :
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
continue
else:
break
else:
ball_small_LR("ball") # small lr 함으로써 중앙 맞춰짐
# ud_for_dist 하기전에 고개 세팅
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(2)
UD_for_dist("ball")
motion.head("DEFAULT", 1) # ud for dist 이후 고개 상하 디폴트
time.sleep(2)
# length = 거리
ball_dist = Distance.Length_ServoAngle_dict.get(Distance.Head_ud_angle)
print("ball distance :", ball_dist)
if ball_dist > 26: # 18+8 (화면에 여유있게 들어오도록)
motion.walk("FORWARD", ball_dist - 26)
elif ball_dist == 26:
print("correct!")
else : # 최소 거리 18보다 더 가까이 있을 경우: 뒷걸음질
motion.walk("BACKWARD", ball_dist - 26)'''
self.act = Act.PUTTING_POS
#return True
#=======================================================#
# 3. Putting Pos #
#=======================================================#
elif act == Act.PUTTING_POS: ##### 3. 퍼팅 위치에 서기 #################
###### 홀컵 중앙 맞추기 #######################
print("^^^^333333")
print("^^^^333333")
print("^^^^333333")
print("^^^^333333")
time.sleep(1)
motion.head("DOWN", 45) # 고개 45도로 내리고 공 detect 시작 !
time.sleep(1)
Distance.Head_UD_Angle = 55
is_holecup_in_frame = robo._image_processor.detect_holecup()
if is_holecup_in_frame == False:
motion.head("DEFAULT", 1) # 고개 상하 디폴트
print("holecup NONONONONONO")
# big UD head
while True:
is_big_UD = big_UD("holecup")
if is_big_UD == "Except" : # big UD 검출안됨 -> big LR 로 넘어감
print("holecup big UD except")
big_LR("holecup")
is_small_LR = small_LR("holecup")
print("small lr finished")
if is_small_LR == "Except" :
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
continue
else:
break
motion.head("DEFAULT", 2) # after small lr, occur error, so add default 2
'''
#====== holecup 고개 방향만큼 꽃게 걸음 ======#
side_walk = int(abs(100-Distance.head_lr_angle)//10) # 식은 시행착오거치면서 변경예정
print("**꽃게 걸음 시작**")
# side walk 방향 설정
if Distance.head_lr_angle < 100:
# 고개가 왼쪽L이면 오른쪽R으로 side walk해라
print("꽃게 걸음 오른쪽")
side_lr = "RIGHT"
elif Distance.head_lr_angle > 100 :
# 고개가 오른쪽R이면 왼쪽L으로 side walk해라
print("꽃게 걸음 왼쪽")
side_lr = "LEFT"
print("side walk -------", side_walk)
# motion.py에 walk_side for문이 없어서 임시로 여기다 넣음
for _ in range(side_walk):
motion.walk_side(side_lr)
motion.head("DEFAULT", 2) # LR 한 후 고개 디폴트
print("holecup YES")
###### 홀컵 찾음, 중앙 맞췄음. 일직선 맞추고, 이제 거리 재야됨
'''
while True:
# 공 홀컵 일직선 맞추기
print("!!call straight ")
check_straight = head.straight()
if check_straight == True: # 거리 알고리즘으로 넘어감
print("straight true!!")
break
elif check_straight == "Except":
print("straight except")
while True:
motion.head("DEFAULT", 1)
Distance.Head_ud_angle = 100
# 매번 고개 디폴트했다가 54도로 갔다가 하는 거
is_big_UD = big_UD("ball")
if is_big_UD == "Except":
robo._motion.head("UP", 9)
is_big_LR = big_LR("ball") # big은 알아서 고개 디폴트 함
if is_big_LR == "Except":
motion.walk("BACKWARD")
time.sleep(3)
motion.head("DEFAULT",0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
Distance.head_lr_angle = 100
time.sleep(1)
continue
is_small_LR = ball_small_LR("ball")
if is_small_LR == "Except" :
time.sleep(1)
motion.head("DEFAULT", 0)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
time.sleep(1)
continue
else:
break
else:
continue
motion.pose("LEFT")
time.sleep(3)
self.act = Act.PUTTING
# 공 거리는 Act 4(PUTTING)에서 재기
#return True
#=======================================================#
# 4. Putting #
#=======================================================#
elif act == Act.PUTTING: ##### 4. 퍼팅 #################
print("^^^^444444")
print("^^^^444444")
print("^^^^444444")
print("^^^^444444")
'''
# ud_for_dist 하기전에 고개 세팅
motion.head("DEFAULT", 2) # 고개 디폴트
time.sleep(1)
Distance.Head_ud_angle = Distance.Head_UD_Middle_Value_Measures
motion.head("DEFAULT", 1) # 고개 디폴트
time.sleep(1)
UD_for_dist("ball") # 공 거리 재기
motion.head("DEFAULT", 1) # ud for dist 이후 고개 상하 디폴트
time.sleep(2)
# length = 거리
ball_dist = Distance.Length_ServoAngle_dict.get(Distance.Head_ud_angle)
print(Distance.Length_ServoAngle_dict)
print("=====================================")
print("balL dist:" , ball_dist , " head ud angle:", Distance.Head_ud_angle)
print("=====================================")
if ball_dist > 18: # 18+8 (화면에 여유있게 들어오도록)
motion.walk("FORWARD", ball_dist - 18)
elif ball_dist == 18:
print("correct!")
else : # 최소 거리 18보다 더 가까이 있을 경우: 뒷걸음질
motion.walk("BACKWARD", ball_dist - 18)
'''
ball_pos('p42')
Set_holecup_left()
ball_pos('p42')
### 진짜 퍼팅
motion.putting("LEFT", 4, 3)
time.sleep(5)
self.act = Act.HOLEIN
motion.turn("LEFT", 45)
time.sleep(1)
motion.turn("LEFT", 20)
time.sleep(1)
#return True
elif act == Act.HOLEIN: ##### 5. 홀인 #################
print("^^^^^^^^5555555")
print("^^^^^^^^5555555")
print("^^^^^^^^5555555")
print("^^^^^^^^5555555")
motion.head("UP", 9)
time.sleep(1)
motion.head("UP", 9)
# 지금 각도 45도임
###### Find ball for HOLEIN ######
is_ball = robo._image_processor.detect_ball()
### False면, big UD LR 해라
if is_ball == False:
motion.head("DEFAULT", 1)
time.sleep(1)
while True:
print("FAIL ball detect 555")
# big UD head
is_big_UD = big_UD("ball")
print(" === big ud === for holeIN")
if is_big_UD == "Except" : # big UD 검출안됨 -> big LR 로 넘어감
print("big ud except 555")
# big LR 시행하기전에 UD 45도로
#motion.head("DOWN", 45)
big_LR("ball") # big은 알아서 고개 디폴트 함
elif is_big_UD == "Success" : # big ud에서 너무 아래쪽에 공이 검출될까봐 조금만 더 내려줌
motion.head("DOWN", 3)
is_small_LR = ball_small_LR("ball")
if is_small_LR == "Except" :
time.sleep(1)