-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcontroller.py
225 lines (172 loc) · 6.61 KB
/
controller.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
from time import time
import numpy as np
from constants import STEER_MAX, STEER_MIN, DEFAULT_ANGLE, HALF_CAR_WIDTH, FRONT_DISTANCE
from math import atan2, degrees, hypot
######## General functions ########
def angle_to_point(point):
angle = -degrees(atan2(point[1],point[0]))
return angle
def sort_cones(cones):
dists = np.sqrt(np.sum(np.square(cones), axis=1))
sorted_indexes = dists.argsort()
return cones[sorted_indexes[::]] # sort cones based on dist
def normalize_angle(angle):
# normalize over 90 degree angles
angle = min(STEER_MAX, angle)
angle = max(STEER_MIN, angle)
return angle
######## One Cone ########
def gonna_hit(cone):
x,y = cone
if y < FRONT_DISTANCE:
return (x > - HALF_CAR_WIDTH or x < HALF_CAR_WIDTH)
else:
return False
def one_cone(blue, yellow):
# TODO: calculate mid to return to viewer
blue_size = blue.shape[0]
yellow_size = yellow.shape[0]
if blue_size > yellow_size: # one blue cone
if gonna_hit(blue[0]):
return STEER_MIN, [] # steer away
else:
return DEFAULT_ANGLE, []
else: # one yellow cone
if gonna_hit(yellow[0]):
return STEER_MAX , []# steer away
else:
return DEFAULT_ANGLE, []
######## One side ########
def one_side(blue, yellow):
if blue.shape[0] > yellow.shape[0]: # only blue
cones = sort_cones(blue)[:2] # get closest to car
else: # only yellow
cones = sort_cones(yellow)[:2] # get closest to car
(x1,y1), (x2,y2) = cones
dist_x = hypot(x2, x1)
dist_y = hypot(y2, y1)
angle = angle_to_point([dist_x, dist_y])
if angle > 0:
angle = STEER_MAX
else:
angle = STEER_MIN
return angle, [[dist_x,dist_y]] # steer in the angle of the two cones
######## One on one side ########
def one_on_one_side(blue, yellow):
if blue.shape[0] > yellow.shape[0]:
blue = sort_cones(blue)[:1] # get closest to car
else:
yellow = sort_cones(yellow)[:1] # get closest to car
mid = (blue + yellow) / 2
return angle_to_point(mid[0]), mid # steer to mid
######## One on each side ########
def one_on_each_side(blue, yellow):
mid = (blue + yellow) / 2
return angle_to_point(mid[0]), mid # steer to mid
######## All cones ########
def lots_of_cones(blue, yellow):
main_blue = sort_cones(blue)[:2]
main_yellow = sort_cones(yellow)[:2]
all_cones = np.concatenate((main_blue,main_yellow))
mid = np.average(all_cones, axis=0)
return angle_to_point(mid), [mid] # steer to mid
'''
Parameters:
blue - np.array( [ [x1,y1], [x2,y2], ... ] )
yellow - np.array( [ [x1,y1], [x2,y2], ... ] )
'''
def run(blue, yellow):
time_start = time()
blue_size = blue.shape[0]
yellow_size = yellow.shape[0]
total_size = blue_size + yellow_size
print_info = f"Controller:\n Input:\n Blue size: {blue_size:>5}\n Yellow size: {yellow_size:>3}\n "
if total_size == 0: # no cones
print_info += "Control Mode: no cones"
angle = DEFAULT_ANGLE
mid = []
elif total_size == 1: # one cone
angle, mid = one_cone(blue, yellow)
print_info += "Control Mode: one cone{'' if angle == DEFAULT_ANGLE else ' - gonna hit'}"
elif blue_size == 0 or yellow_size == 0: # cones on only one side
print_info += "Control Mode: one side only"
angle, mid = one_side(blue, yellow)
elif blue_size == 1 and yellow_size == 1:
print_info += "Control Mode: one on each side"
angle, mid = one_on_each_side(blue, yellow)
elif blue_size == 1 or yellow_size == 1:
print_info += "Control Mode: lots on one side and one on the other"
angle, mid = one_on_one_side(blue, yellow)
else: # one or more cones on both sides
print_info += "Control Mode: lots of cones"
angle, mid = lots_of_cones(blue, yellow)
# print("Angle before normalization", angle)
print_info += f"\n Angle before normalization: {angle:>5.2f}\n"
angle = normalize_angle(angle)
print_info += f" Angle after normalization: {angle:>6.2f}\n Time: {(time() - time_start)*1000:>5.0f} ms \n"
return angle, mid, print_info
if __name__ == "__main__":
def test(blue, yellow, expected, test_name):
result = run(blue,yellow)
print(f"{test_name} : \nresult: {result} Expected {expected}")
print("Succeed test" if result == expected else "\n\nFailed test\n\n")
print()
### No cone test
expected = 0.0
yel = np.array( [] )
blue = np.array( [] )
test(blue, yel, expected=0.0, test_name="no cone")
### One code tests
# blue
yel = np.array( [] )
blue = np.array( [ [1,1] ] )
test(blue, yel, expected=STEER_MAX, test_name="one cone : blue")
# yellow
yel = np.array( [ [1,1] ] )
blue = np.array( [] )
test(blue, yel, expected=STEER_MIN, test_name="one cone : yellow")
### One side tests
# blue
yel = np.array( [] )
blue = np.array( [ [2,1], [1,1] ] )
test(blue, yel, expected=STEER_MAX, test_name="one side : blue")
# yellow
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [] )
test(blue, yel, expected=STEER_MIN, test_name="one side : yellow")
### Only one cone on one side tests
# blue
yel = np.array( [[-1, 1]] )
blue = np.array( [ [2,1], [1,1] ] )
test(blue, yel, expected=0.0, test_name="only one cone on one side : blue")
# yellow
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [[-1, 1]] )
test(blue, yel, expected=-0.0, test_name="only one cone on one side : yellow")
### Only one cone on one side tests
# blue
yel = np.array( [[-1, 1]] )
blue = np.array( [ [2,1], [1,1] ] )
test(blue, yel, expected=0.0, test_name="only one cone on one side : blue")
# yellow
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [[-1, 1]] )
test(blue, yel, expected=-0.0, test_name="only one cone on one side : yellow")
### One cone on each side tests
# blue
yel = np.array( [[-1, 1]] )
blue = np.array( [ [1,1] ] )
test(blue, yel, expected=0.0, test_name="one cone on each side : blue")
# yellow
yel = np.array( [ [1,1] ] )
blue = np.array( [ [-1, 1] ] )
test(blue, yel, expected=-0.0, test_name="one cone on each side : yellow")
### Lots of cones tests
# blue
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [ [-2,1], [-1,1] ] )
test(blue, yel, expected=0.0, test_name="lots of cones : blue")
# yellow
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [ [-2,1], [-1,1] ] )
test(blue, yel, expected=-0.0, test_name="lots of cones : yellow")