-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball_utils.py
114 lines (97 loc) · 2.79 KB
/
ball_utils.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
from picamera.array import PiRGBArray
import test_camera
import car
import picamera
import time
threshold = 10
direction = True
rush = False
time_eclipse = 0.15
def load():
global direction, rush, time_eclipse
direction = True
rush = False
time_eclipse = 0.15
def unload():
car.brake()
def center_ball(bound, resolution, go=False):
global direction, time_eclipse
center_x = bound[0] + bound[2] / 2
if resolution[0] / 2 - threshold <= center_x <= resolution[0] / 2 + threshold:
car.brake()
if go:
go_ball(bound)
else:
time_eclipse = 0
return
elif 0 < center_x < resolution[0] / 2 - threshold:
if not direction:
time_eclipse = max(time_eclipse / 2, 0.005)
direction = True
elif center_x > resolution[0] / 2 + threshold:
if direction:
time_eclipse = max(time_eclipse / 2, 0.005)
direction = False
if not rush:
if time_eclipse == 0:
time_eclipse = 0.05
if direction:
car.rotate_right_in_place()
time.sleep(time_eclipse)
car.brake()
else:
car.rotate_left_in_place()
time.sleep(time_eclipse)
car.brake()
# wait for camera stable
time.sleep(0.005)
def go_ball(bound):
global rush
radius = max(bound[2], bound[3]) / 2
center_y = bound[1] + bound[3] / 2
bottom = center_y - radius
car.set_left_wheels_speed(10)
car.set_right_wheels_speed(10)
if not car.registered_infrared_sensor_callback(infrare_handler):
car.on_infrared_sensor_change(infrare_handler)
if bottom <= 0:
rush = True
rush_ball()
else:
rush = True
car.go()
def infrare_handler(tup):
global time_eclipse, rush
left, middle, right = tup
if middle == 0:
time_eclipse = 0
rush = False
car.brake()
car.remove_infrared_sensor_change(infrare_handler)
def rush_ball():
global time_eclipse, rush
car.go()
time.sleep(0.8)
time_eclipse = 0
rush = False
car.brake()
car.remove_infrared_sensor_change(infrare_handler)
def go_door():
pass
if __name__ == '__main__':
camera = picamera.PiCamera()
camera.resolution = (320, 240)
camera.framerate = 60
rawCapture = PiRGBArray(camera, size=(320, 240))
try:
for frame in camera.capture_continuous(rawCapture, format='bgr', use_video_port=True):
_, bound = test_camera.find_circle(frame.array)
center_ball(bound, camera.resolution)
if time_eclipse == 0:
break
rawCapture.truncate()
rawCapture.seek(0)
except KeyboardInterrupt:
car.brake()
car.stop_polling()
print('Manual interrupted')