Skip to content

Commit b926a80

Browse files
committed
first release lookuptable_generator
1 parent 7297d5a commit b926a80

File tree

4 files changed

+117
-13
lines changed

4 files changed

+117
-13
lines changed
40.7 KB
Loading
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Lookup Table generation for model predictive trajectory generator
3+
4+
author: Atsushi Sakai
5+
"""
6+
from matplotlib import pyplot as plt
7+
import numpy as np
8+
import math
9+
import model_predictive_trajectory_generator as planner
10+
import motion_model
11+
12+
13+
def calc_states_list():
14+
maxyaw = math.radians(-30.0)
15+
16+
x = np.arange(1.0, 30.0, 5.0)
17+
y = np.arange(0.0, 20.0, 2.0)
18+
yaw = np.arange(-maxyaw, maxyaw, maxyaw)
19+
20+
states = []
21+
for iyaw in yaw:
22+
for iy in y:
23+
for ix in x:
24+
states.append([ix, iy, iyaw])
25+
# print(len(states))
26+
27+
return states
28+
29+
30+
def search_nearest_one_from_lookuptable(tx, ty, tyaw, lookuptable):
31+
32+
mind = float("inf")
33+
minid = -1
34+
35+
for (i, table) in enumerate(lookuptable):
36+
37+
dx = tx - table[0]
38+
dy = ty - table[1]
39+
dyaw = tyaw - table[2]
40+
d = math.sqrt(dx**2 + dy**2 + dyaw**2)
41+
if d <= mind:
42+
minid = i
43+
mind = d
44+
45+
# print(minid)
46+
47+
return lookuptable[minid]
48+
49+
50+
def generate_lookup_table():
51+
52+
states = calc_states_list()
53+
k0 = 0.0
54+
55+
# x, y, yaw, s, km, kf
56+
lookuptable = [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]]
57+
58+
for state in states:
59+
bestp = search_nearest_one_from_lookuptable(
60+
state[0], state[1], state[2], lookuptable)
61+
# print(bestp)
62+
63+
target = motion_model.State(x=state[0], y=state[1], yaw=state[2])
64+
init_p = np.matrix(
65+
[math.sqrt(state[0]**2 + state[1]**2), bestp[4], bestp[5]]).T
66+
67+
x, y, yaw, p = planner.optimize_trajectory(target, k0, init_p)
68+
69+
if x is not None:
70+
print("find good path")
71+
lookuptable.append(
72+
[x[-1], y[-1], yaw[-1], float(p[0]), float(p[1]), float(p[2])])
73+
74+
print("finish lookuptable generation")
75+
76+
for table in lookuptable:
77+
xc, yc, yawc = motion_model.generate_trajectory(
78+
table[3], table[4], table[5], k0)
79+
plt.plot(xc, yc, "-r")
80+
xc, yc, yawc = motion_model.generate_trajectory(
81+
table[3], -table[4], -table[5], k0)
82+
plt.plot(xc, yc, "-r")
83+
84+
plt.grid(True)
85+
plt.axis("equal")
86+
plt.show()
87+
88+
print("Done")
89+
90+
91+
def main():
92+
generate_lookup_table()
93+
94+
95+
if __name__ == '__main__':
96+
main()

PathPlanning/ModelPredictiveTrajectoryGenerator/model_predictive_trajectory_generator.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from matplotrecorder import matplotrecorder
1212

1313
# optimization parameter
14-
maxiter = 1000
15-
h = np.matrix([0.1, 0.002, 0.002]).T # parameter sampling distanse
14+
maxiter = 100
15+
h = np.matrix([0.1, 0.001, 0.001]).T # parameter sampling distanse
1616

1717
matplotrecorder.donothing = True
18+
show_graph = False
1819

1920

2021
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
@@ -69,7 +70,7 @@ def selection_learning_param(dp, p, k0, target):
6970

7071
mincost = float("inf")
7172
mina = 1.0
72-
maxa = 5.0
73+
maxa = 2.0
7374
da = 0.5
7475

7576
for a in np.arange(mina, maxa, da):
@@ -108,24 +109,29 @@ def optimize_trajectory(target, k0, p):
108109
# print(dc.T)
109110

110111
cost = np.linalg.norm(dc)
111-
print("cost is:" + str(cost))
112112
if cost <= 0.05:
113-
print("cost is:" + str(cost))
114-
print(p)
113+
print("path is ok cost is:" + str(cost))
115114
break
116115

117116
J = calc_J(target, p, h, k0)
118-
dp = - np.linalg.inv(J) * dc
117+
try:
118+
dp = - np.linalg.inv(J) * dc
119+
except np.linalg.linalg.LinAlgError:
120+
print("cannot calc path LinAlgError")
121+
xc, yc, yawc, p = None, None, None, None
122+
break
119123
alpha = selection_learning_param(dp, p, k0, target)
120124

121125
p += alpha * np.array(dp)
122126
# print(p.T)
123127

124-
show_trajectory(target, xc, yc)
125-
126-
show_trajectory(target, xc, yc)
128+
if show_graph:
129+
show_trajectory(target, xc, yc)
130+
else:
131+
xc, yc, yawc, p = None, None, None, None
132+
print("cannot calc path")
127133

128-
print("done")
134+
return xc, yc, yawc, p
129135

130136

131137
def test_optimize_trajectory():
@@ -136,7 +142,9 @@ def test_optimize_trajectory():
136142

137143
init_p = np.matrix([6.0, 0.0, 0.0]).T
138144

139-
optimize_trajectory(target, k0, init_p)
145+
x, y, yaw, p = optimize_trajectory(target, k0, init_p)
146+
147+
show_trajectory(target, x, y)
140148
matplotrecorder.save_movie("animation.gif", 0.1)
141149

142150
# plt.plot(x, y, "-r")

PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def generate_trajectory(s, km, kf, k0):
4646
kk = np.array([k0, km, kf])
4747
t = np.arange(0.0, time, time / n)
4848
kp = scipy.interpolate.spline(tk, kk, t, order=2)
49-
dt = time / n
49+
dt = float(time / n)
5050

5151
# plt.plot(t, kp)
5252
# plt.show()

0 commit comments

Comments
 (0)