-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcontroller2.py
248 lines (195 loc) · 6.63 KB
/
controller2.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
from time import time
import numpy as np
from constants import *
from math import atan2, degrees, hypot, inf
from utils import rotate
######## General functions ########
def angle_to_point(point):
return degrees(atan2(point[1],point[0]) )
# Return distance between 2 points
def dist(p1, p2):
return hypot(p1[0]-p2[0], p1[1]-p2[1])
def closest_point(node, nodes):
nodes = np.asarray(nodes)
dist_2 = np.sum((nodes - node)**2, axis=1)
return np.argmax(dist_2)
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 gonna_hit(cone):
x,y = cone
if x < FRONT_DISTANCE:
return (abs(y) < HALF_CAR_WIDTH)
else:
return False
def get_middle(blue, yellow):
# TODO Check if one cone has 2 correspondences
#sorted_blue = sort_cones(blue)
#sorted_yellow = sort_cones(yellow)
middle = []
for b in blue:
min_dist = inf
yellow_selected = None
for y in yellow:
d = dist(b, y)
if MIN_TRACK_WIDTH <= d <= MAX_TRACK_WIDTH:
if d < min_dist:
min_dist = d
yellow_selected = y
if yellow_selected is not None:
middle.append(np.average([b, yellow_selected], axis=0))
return middle
def one_cone(blue, yellow):
if len(blue)>0:
angle = STEER_MIN
elif len(yellow)>0:
angle = STEER_MAX
return angle
def get_fake_mid(cones, color):
mid = []
cones = sort_cones(cones)
angle = 0
size = len(cones)
for i in range(size):
#a = angle_to_point([cones[i+1][0]-cones[i][0], cones[i+1][0]-cones[i][1]])
a = angle_to_point(cones[i])
print("angle:", a)
angle = angle+a
angle = angle/size
print("final angle:", angle)
for i in range(size-1):
m = (cones[i]+cones[i+1])/2
if color == "blue":
of = [0, -HALF_TRACK_WIDTH]
else:
of = [0, HALF_TRACK_WIDTH]
of = rotate(of, math.radians(angle))
print("offset", of)
mid.append(m+of)
return mid
def one_side(blue, yellow):
if len(blue)>0:
angle = STEER_MIN
mid = get_fake_mid(blue, "blue")
elif len(yellow)>0:
angle = STEER_MAX
mid = get_fake_mid(yellow, "yellow")
print("mid:", mid)
return angle, mid
def lots_of_cones(blue, yellow):
print_info = ""
mid = get_middle(blue, yellow)
angle = DEFAULT_ANGLE
i = closest_point([0,0], mid)
closest = mid[i]
look_ahead = dist([0,0], closest)
mid.insert(0, mid.pop(i))
angle = angle_to_point(closest)
for b in blue:
if gonna_hit(b):
angle = STEER_MIN
print_info += "GOING TO HIT BLUE CONE, IGNORING MIDDLE PATH \n"
break
for y in yellow:
if gonna_hit(y):
angle = STEER_MAX
print_info += "GOING TO HIT YELLOW CONE, IGNORING MIDDLE PATH \n"
break
return angle, mid, print_info
'''
Parameters:
blue - np.array( [ [x1,y1], [x2,y2], ... ] )
yellow - np.array( [ [x1,y1], [x2,y2], ... ] )
'''
last_steer = 0
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 \n"
angle = last_steer
mid = []
elif total_size == 1: # one cone only
print_info += "Control Mode: one cone only \n"
angle, mid = one_cone(blue, yellow)
last_steer = angle
mid = []
elif blue_size == 0 or yellow_size == 0: # cones on only one side
print_info += "Control Mode: one side only \n"
angle, mid = one_side(blue, yellow)
last_steer = angle
else: # one or more cones on both sides
print_info += "Control Mode: lots of cones \n"
angle, mid, print_i = lots_of_cones(blue, yellow)
print_info += print_i
last_steer = angle
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=MAX_ANGLE, test_name="one cone : blue")
# yellow
yel = np.array( [ [1,1] ] )
blue = np.array( [] )
test(blue, yel, expected=-MAX_ANGLE, test_name="one cone : yellow")
### One side tests
# blue
yel = np.array( [] )
blue = np.array( [ [2,1], [1,1] ] )
test(blue, yel, expected=MAX_ANGLE, test_name="one side : blue")
# yellow
yel = np.array( [ [2,1], [1,1] ] )
blue = np.array( [] )
test(blue, yel, expected=-MAX_ANGLE, 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")