-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathlookup_table_generator.py
110 lines (75 loc) · 2.71 KB
/
lookup_table_generator.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
"""
Lookup Table generation for model predictive trajectory generator
author: Atsushi Sakai
"""
import sys
import pathlib
path_planning_dir = pathlib.Path(__file__).parent.parent
sys.path.append(str(path_planning_dir))
from matplotlib import pyplot as plt
import numpy as np
import math
from ModelPredictiveTrajectoryGenerator import trajectory_generator,\
motion_model
def calc_states_list(max_yaw=np.deg2rad(-30.0)):
x = np.arange(10.0, 30.0, 5.0)
y = np.arange(0.0, 20.0, 2.0)
yaw = np.arange(-max_yaw, max_yaw, max_yaw)
states = []
for iyaw in yaw:
for iy in y:
for ix in x:
states.append([ix, iy, iyaw])
print("n_state:", len(states))
return states
def search_nearest_one_from_lookup_table(tx, ty, tyaw, lookup_table):
mind = float("inf")
minid = -1
for (i, table) in enumerate(lookup_table):
dx = tx - table[0]
dy = ty - table[1]
dyaw = tyaw - table[2]
d = math.sqrt(dx ** 2 + dy ** 2 + dyaw ** 2)
if d <= mind:
minid = i
mind = d
# print(minid)
return lookup_table[minid]
def save_lookup_table(file_name, table):
np.savetxt(file_name, np.array(table),
fmt='%s', delimiter=",", header="x,y,yaw,s,km,kf", comments="")
print("lookup table file is saved as " + file_name)
def generate_lookup_table():
states = calc_states_list(max_yaw=np.deg2rad(-30.0))
k0 = 0.0
# x, y, yaw, s, km, kf
lookup_table = [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]]
for state in states:
best_p = search_nearest_one_from_lookup_table(
state[0], state[1], state[2], lookup_table)
target = motion_model.State(x=state[0], y=state[1], yaw=state[2])
init_p = np.array(
[np.hypot(state[0], state[1]), best_p[4], best_p[5]]).reshape(3, 1)
x, y, yaw, p = trajectory_generator.optimize_trajectory(target,
k0, init_p)
if x is not None:
print("find good path")
lookup_table.append(
[x[-1], y[-1], yaw[-1], float(p[0, 0]), float(p[1, 0]), float(p[2, 0])])
print("finish lookup table generation")
save_lookup_table("lookup_table.csv", lookup_table)
for table in lookup_table:
x_c, y_c, yaw_c = motion_model.generate_trajectory(
table[3], table[4], table[5], k0)
plt.plot(x_c, y_c, "-r")
x_c, y_c, yaw_c = motion_model.generate_trajectory(
table[3], -table[4], -table[5], k0)
plt.plot(x_c, y_c, "-r")
plt.grid(True)
plt.axis("equal")
plt.show()
print("Done")
def main():
generate_lookup_table()
if __name__ == '__main__':
main()