-
Notifications
You must be signed in to change notification settings - Fork 0
/
planner.py
212 lines (185 loc) · 7.67 KB
/
planner.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
from argparse import Namespace
from numba import njit
from pyglet.gl import GL_POINTS
import numpy as np
"""
Planner Helpers
"""
@njit(fastmath=False, cache=True)
def nearest_point_on_trajectory(point, trajectory):
"""
Return the nearest point along the given piecewise linear trajectory.
Same as nearest_point_on_line_segment, but vectorized. This method is quite fast, time constraints should
not be an issue so long as trajectories are not insanely long.
Order of magnitude: trajectory length: 1000 --> 0.0002 second computation (5000fps)
point: size 2 numpy array
trajectory: Nx2 matrix of (x,y) trajectory waypoints
- these must be unique. If they are not unique, a divide by 0 error will destroy the world
"""
diffs = trajectory[1:,:] - trajectory[:-1,:]
l2s = diffs[:,0]**2 + diffs[:,1]**2
# this is equivalent to the elementwise dot product
# dots = np.sum((point - trajectory[:-1,:]) * diffs[:,:], axis=1)
dots = np.empty((trajectory.shape[0]-1, ))
for i in range(dots.shape[0]):
dots[i] = np.dot((point - trajectory[i, :]), diffs[i, :])
t = dots / l2s
t[t<0.0] = 0.0
t[t>1.0] = 1.0
# t = np.clip(dots / l2s, 0.0, 1.0)
projections = trajectory[:-1,:] + (t*diffs.T).T
# dists = np.linalg.norm(point - projections, axis=1)
dists = np.empty((projections.shape[0],))
for i in range(dists.shape[0]):
temp = point - projections[i]
dists[i] = np.sqrt(np.sum(temp*temp))
min_dist_segment = np.argmin(dists)
return projections[min_dist_segment], dists[min_dist_segment], t[min_dist_segment], min_dist_segment
@njit(fastmath=False, cache=True)
def first_point_on_trajectory_intersecting_circle(point, radius, trajectory, t=0.0, wrap=False):
"""
starts at beginning of trajectory, and find the first point one radius away from the given point along the trajectory.
Assumes that the first segment passes within a single radius of the point
http://codereview.stackexchange.com/questions/86421/line-segment-to-circle-collision-algorithm
"""
start_i = int(t)
start_t = t % 1.0
first_t = None
first_i = None
first_p = None
trajectory = np.ascontiguousarray(trajectory)
for i in range(start_i, trajectory.shape[0]-1):
start = trajectory[i,:]
end = trajectory[i+1,:]+1e-6
V = np.ascontiguousarray(end - start)
a = np.dot(V,V)
b = 2.0*np.dot(V, start - point)
c = np.dot(start, start) + np.dot(point,point) - 2.0*np.dot(start, point) - radius*radius
discriminant = b*b-4*a*c
if discriminant < 0:
continue
# print "NO INTERSECTION"
# else:
# if discriminant >= 0.0:
discriminant = np.sqrt(discriminant)
t1 = (-b - discriminant) / (2.0*a)
t2 = (-b + discriminant) / (2.0*a)
if i == start_i:
if t1 >= 0.0 and t1 <= 1.0 and t1 >= start_t:
first_t = t1
first_i = i
first_p = start + t1 * V
break
if t2 >= 0.0 and t2 <= 1.0 and t2 >= start_t:
first_t = t2
first_i = i
first_p = start + t2 * V
break
elif t1 >= 0.0 and t1 <= 1.0:
first_t = t1
first_i = i
first_p = start + t1 * V
break
elif t2 >= 0.0 and t2 <= 1.0:
first_t = t2
first_i = i
first_p = start + t2 * V
break
# wrap around to the beginning of the trajectory if no intersection is found1
if wrap and first_p is None:
for i in range(-1, start_i):
start = trajectory[i % trajectory.shape[0],:]
end = trajectory[(i+1) % trajectory.shape[0],:]+1e-6
V = end - start
a = np.dot(V,V)
b = 2.0*np.dot(V, start - point)
c = np.dot(start, start) + np.dot(point,point) - 2.0*np.dot(start, point) - radius*radius
discriminant = b*b-4*a*c
if discriminant < 0:
continue
discriminant = np.sqrt(discriminant)
t1 = (-b - discriminant) / (2.0*a)
t2 = (-b + discriminant) / (2.0*a)
if t1 >= 0.0 and t1 <= 1.0:
first_t = t1
first_i = i
first_p = start + t1 * V
break
elif t2 >= 0.0 and t2 <= 1.0:
first_t = t2
first_i = i
first_p = start + t2 * V
break
return first_p, first_i, first_t
@njit(fastmath=False, cache=True)
def get_actuation(pose_theta, lookahead_point, position, lookahead_distance, wheelbase):
"""
Returns actuation
"""
waypoint_y = np.dot(np.array([np.sin(-pose_theta), np.cos(-pose_theta)]), lookahead_point[0:2]-position)
speed = lookahead_point[2]
if np.abs(waypoint_y) < 1e-6:
return speed, 0.
radius = 1/(2.0*waypoint_y/lookahead_distance**2)
steering_angle = np.arctan(wheelbase/radius)
return speed, steering_angle
class PurePursuitPlanner:
"""
Example Planner
"""
def __init__(self, conf, wb):
self.wheelbase = wb
self.conf = conf
self.load_waypoints(conf)
self.max_reacquire = 20.
self.drawn_waypoints = []
def load_waypoints(self, conf):
"""
loads waypoints
"""
self.waypoints = np.loadtxt(conf.wpt_path, delimiter=conf.wpt_delim, skiprows=conf.wpt_rowskip)
def render_waypoints(self, e):
"""
update waypoints being drawn by EnvRenderer
"""
#points = self.waypoints
points = np.vstack((self.waypoints[:, self.conf.wpt_xind], self.waypoints[:, self.conf.wpt_yind])).T
scaled_points = 50.*points
for i in range(points.shape[0]):
if len(self.drawn_waypoints) < points.shape[0]:
b = e.batch.add(1, GL_POINTS, None, ('v3f/stream', [scaled_points[i, 0], scaled_points[i, 1], 0.]),
('c3B/stream', [183, 193, 222]))
self.drawn_waypoints.append(b)
else:
self.drawn_waypoints[i].vertices = [scaled_points[i, 0], scaled_points[i, 1], 0.]
def _get_current_waypoint(self, waypoints, lookahead_distance, position, theta):
"""
gets the current waypoint to follow
"""
wpts = np.vstack((self.waypoints[:, self.conf.wpt_xind], self.waypoints[:, self.conf.wpt_yind])).T
nearest_point, nearest_dist, t, i = nearest_point_on_trajectory(position, wpts)
if nearest_dist < lookahead_distance:
lookahead_point, i2, t2 = first_point_on_trajectory_intersecting_circle(position, lookahead_distance, wpts, i+t, wrap=True)
if i2 == None:
return None
current_waypoint = np.empty((3, ))
# x, y
current_waypoint[0:2] = wpts[i2, :]
# speed
current_waypoint[2] = waypoints[i, self.conf.wpt_vind]
return current_waypoint
elif nearest_dist < self.max_reacquire:
return np.append(wpts[i, :], waypoints[i, self.conf.wpt_vind])
else:
return None
def plan(self, pose_x, pose_y, pose_theta, lookahead_distance, vgain):
"""
gives actuation given observation
"""
position = np.array([pose_x, pose_y])
lookahead_point = self._get_current_waypoint(self.waypoints, lookahead_distance, position, pose_theta)
if lookahead_point is None:
return 4.0, 0.0
speed, steering_angle = get_actuation(pose_theta, lookahead_point, position, lookahead_distance, self.wheelbase)
speed = vgain * speed
return speed, steering_angle