-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
317 lines (236 loc) · 10.4 KB
/
main.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
import nlopt
import math
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as scipy_interpolate
import scipy.optimize as optimize
from cubic_bezier_planner import calc_6points_bezier_path
from cubic_bezier_planner import calc_4points_bezier_path
from cubic_spline_planner import calc_spline_course
class Path:
def __init__(self, x, y, yaw, k):
self.x = x
self.y = y
self.yaw = yaw
self.k = k
class Spline:
def __init__(self, ax, ay, bound):
#input waypoint coordinates
ayaw, k = self.calc_yaw_curvature(ax, ay)
self.waypoints = Path(ax, ay, ayaw, k)
# defines and sets left and right boundary lines
self.bound = bound
lax, lay, rax, ray = self.init_boundary()
self.left_bound = Path(lax, lay, None, None)
self.right_bound = Path(rax, ray, None, None)
# default unoptimized cubic bezier path
# bx, by, byaw, bk, _ = calc_spline_course(ax, ay, 0.1)
bx, by = self.cubic_bezier_path(ax, ay)
byaw, bk = self.calc_yaw_curvature(bx, by)
self.default_path = Path(bx, by, byaw, bk)
# optimized path
self.optimized_path = Path([], [], [], [])
# Calculates the first derivative of input arrays
def calc_d(self, x, y):
dx = []
dy = []
for i in range(0, len(x)-1):
dx.append(x[i+1] - x[i])
dy.append(y[i+1] - y[i])
dx.append(dx[-1])
dy.append(dy[-1])
return dx, dy
# Calculates yaw and curvature given input path
def calc_yaw_curvature(self, x, y):
dx, dy = self.calc_d(x,y)
ddx, ddy = self.calc_d(dx, dy)
yaw = []
k = []
for i in range(0, len(x)):
yaw.append(math.atan2(dy[i], dx[i]))
k.append( (ddy[i] * dx[i] - ddx[i] * dy[i]) / ((dx[i]**2 + dy[i]**2)**(3/2)) )
return yaw, k
# Calculates total distance of the path
def calc_path_dist(self, x, y):
dx = np.absolute(self.calc_d(np.zeros(len(x)), x))
dy = np.absolute(self.calc_d(np.zeros(len(y)), y))
ddist = np.hypot(dx, dy)
return np.sum(ddist)
# Bezier path one as per the approach suggested in
# https://users.soe.ucsc.edu/~elkaim/Documents/camera_WCECS2008_IEEE_ICIAR_58.pdf
def cubic_bezier_path(self, ax, ay):
dyaw, _ = self.calc_yaw_curvature(ax, ay)
cx = []
cy = []
ayaw = dyaw.copy()
for n in range(1, len(ax)-1):
yaw = 0.5*(dyaw[n] + dyaw[n-1])
ayaw[n] = yaw
last_ax = ax[0]
last_ay = ay[0]
last_ayaw = ayaw[0]
# for n waypoints, there are n-1 bezier curves
for i in range(len(ax)-1):
path, ctr_points = calc_4points_bezier_path(last_ax, last_ay, ayaw[i], ax[i+1], ay[i+1], ayaw[i+1], 2.0)
cx = np.concatenate((cx, path.T[0][:-2]))
cy = np.concatenate((cy, path.T[1][:-2]))
cyaw, k = self.calc_yaw_curvature(cx, cy)
last_ax = path.T[0][-1]
last_ay = path.T[1][-1]
return cx, cy
# Approximated quintic bezier path with curvature continuity
def quintic_bezier_path(self, ax, ay, offsets):
dyaw, _ = self.calc_yaw_curvature(ax, ay)
cx = []
cy = []
ayaw = dyaw.copy()
for n in range(1, len(ax)-1):
yaw = 0.5*(dyaw[n] + dyaw[n-1])
ayaw[n] = yaw
last_ax = ax[0]
last_ay = ay[0]
last_ayaw = ayaw[0]
# for n waypoints, there are n-1 bezier curves
for i in range(len(ax)-1):
path, ctr_points = calc_6points_bezier_path(last_ax, last_ay, ayaw[i], ax[i+1], ay[i+1], ayaw[i+1], offsets[i])
cx = np.concatenate((cx, path.T[0][:-2]))
cy = np.concatenate((cy, path.T[1][:-2]))
cyaw, k = self.calc_yaw_curvature(cx, cy)
last_ax = path.T[0][-1]
last_ay = path.T[1][-1]
return cx, cy
# Objective function of cost to be minimized
def cubic_objective_func(self, deviation):
ax = self.waypoints.x.copy()
ay = self.waypoints.y.copy()
for n in range(0, len(deviation)):
ax[n+1] -= deviation[n]*np.sin(self.waypoints.yaw[n+1])
ay[n+1] += deviation[n]*np.cos(self.waypoints.yaw[n+1])
bx, by = self.cubic_bezier_path(ax, ay)
yaw, k = self.calc_yaw_curvature(bx, by)
# cost of curvature continuity
t = np.zeros((len(k)))
dk = self.calc_d(t, k)
absolute_dk = np.absolute(dk)
continuity_cost = 10.0 * np.mean(absolute_dk)
# curvature cost
absolute_k = np.absolute(k)
curvature_cost = 14.0 * np.mean(absolute_k)
# cost of deviation from input waypoints
absolute_dev = np.absolute(deviation)
deviation_cost = 1.0 * np.mean(absolute_dev)
distance_cost = 0.5 * self.calc_path_dist(bx, by)
return curvature_cost + deviation_cost + distance_cost + continuity_cost
# Objective function for quintic bezier
def quintic_objective_func(self, params):
ax = self.waypoints.x.copy()
ay = self.waypoints.y.copy()
# calculate offsets and input waypoints
offsets = params[ (len(self.waypoints.yaw)-2): ]
deviation = params[ :(len(self.waypoints.yaw)-2) ]
for n in range(0, len(self.waypoints.yaw)-2):
ax[n+1] -= deviation[n]*np.sin(self.waypoints.yaw[n+1])
ay[n+1] += deviation[n]*np.cos(self.waypoints.yaw[n+1])
bx, by = self.quintic_bezier_path(ax, ay, offsets)
yaw, k = self.calc_yaw_curvature(bx, by)
# cost of distance
distance_cost = 0.5 * self.calc_path_dist(bx, by)
# curvature cost
absolute_k = np.absolute(k)
curvature_cost = 25.0 * np.mean(absolute_k)
# cost of deviation from input waypoints
absolute_dev = np.absolute(deviation)
deviation_cost = 1.0 * np.mean(absolute_dev)
return curvature_cost + deviation_cost + distance_cost
# Determines position of boundary lines for visualization
def init_boundary(self):
rax = []
ray = []
lax = []
lay = []
for n in range(0, len(self.waypoints.yaw)):
lax.append(self.waypoints.x[n] - self.bound*np.sin(self.waypoints.yaw[n]))
lay.append(self.waypoints.y[n] + self.bound*np.cos(self.waypoints.yaw[n]))
rax.append(self.waypoints.x[n] + self.bound*np.sin(self.waypoints.yaw[n]))
ray.append(self.waypoints.y[n] - self.bound*np.cos(self.waypoints.yaw[n]))
return lax, lay, rax, ray
# Minimize objective function using scipy optimize minimize
def optimize_min_cubic(self):
print("Attempting optimization minima")
initial_guess = [0, 0, 0, 0, 0]
bnds = ((-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound))
result = optimize.minimize(self.cubic_objective_func, initial_guess, bounds=bnds)
ax = self.waypoints.x.copy()
ay = self.waypoints.y.copy()
if result.success:
print("optimized true")
deviation = result.x
for n in range(0, len(deviation)):
ax[n+1] -= deviation[n]*np.sin(self.waypoints.yaw[n+1])
ay[n+1] += deviation[n]*np.cos(self.waypoints.yaw[n+1])
x, y = self.cubic_bezier_path(ax, ay)
yaw, k = self.calc_yaw_curvature(x, y)
self.optimized_path = Path(x, y, yaw, k)
else:
print("optimization failure, defaulting")
exit()
# Minimize quintic objective function
def optimize_min_quintic(self):
print("Attempting optimization minima")
initial_guess = [0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
bnds = ((-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound), (-self.bound, self.bound), (0, 1.0), (0, 1.0), (0, 1.0), (0, 1.0), (0, 1.0), (0, 1.0))
result = optimize.minimize(self.quintic_objective_func, initial_guess, bounds=bnds)
ax = self.waypoints.x.copy()
ay = self.waypoints.y.copy()
if result.success:
print("optimized true")
params = result.x
# collects offsets for individual bezier curves
offsets = params[ (len(self.waypoints.yaw)-2): ]
deviation = params[ :(len(self.waypoints.yaw)-2) ]
# updated set of waypoints
for n in range(0, len(self.waypoints.yaw)-2):
ax[n+1] -= deviation[n]*np.sin(self.waypoints.yaw[n+1])
ay[n+1] += deviation[n]*np.cos(self.waypoints.yaw[n+1])
x, y = self.quintic_bezier_path(ax, ay, offsets)
yaw, k = self.calc_yaw_curvature(x, y)
self.optimized_path = Path(x, y, yaw, k)
else:
print("optimization failure, defaulting")
exit()
def main():
# define input path
ax = [0.0, 2.3, 6.25, 8.6, 8.2, 5.3, 2.6]
ay = [0.0, 7.16, 13.68, 22.3, 30.64, 39.6, 50.4]
boundary = 2.5
spline = Spline(ax, ay, boundary)
# spline.optimize_min_quintic()
# Path plot
plt.subplots(1)
plt.plot(spline.left_bound.x, spline.left_bound.y, '--r', alpha=0.5, label="left boundary")
plt.plot(spline.right_bound.x, spline.right_bound.y, '--g', alpha=0.5, label="right boundary")
plt.plot(spline.default_path.x, spline.default_path.y, '.y', label="default")
plt.plot(spline.optimized_path.x, spline.optimized_path.y, '-m', label="optimized")
plt.plot(spline.waypoints.x, spline.waypoints.y, '.', label="waypoints")
plt.grid(True)
plt.legend()
plt.axis("equal")
# Heading plot
plt.subplots(1)
plt.plot([np.rad2deg(iyaw) for iyaw in spline.default_path.yaw], ".y", label="original")
plt.plot([np.rad2deg(iyaw) for iyaw in spline.optimized_path.yaw], "-m", label="optimized")
plt.grid(True)
plt.legend()
plt.xlabel("line length[m]")
plt.ylabel("yaw angle[deg]")
# Curvature plot
plt.subplots(1)
plt.plot(spline.default_path.k, ".y", label="original")
plt.plot(spline.optimized_path.k, "-m", label="optimized")
plt.grid(True)
plt.legend()
plt.xlabel("line length[m]")
plt.ylabel("curvature [1/m]")
plt.show()
if __name__ == '__main__':
main()