Skip to content

Commit 5160704

Browse files
committed
second release
1 parent ff63950 commit 5160704

File tree

8 files changed

+164
-0
lines changed

8 files changed

+164
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "PathPlanning/LatticePlanner/matplotrecorder"]
88
path = PathPlanning/LatticePlanner/matplotrecorder
99
url = https://github.com/AtsushiSakai/matplotrecorder.git
10+
[submodule "PathPlanning/ModelPredictiveTrajectoryGenerator/matplotrecorder"]
11+
path = PathPlanning/ModelPredictiveTrajectoryGenerator/matplotrecorder
12+
url = https://github.com/AtsushiSakai/matplotrecorder
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import math
2+
import numpy as np
3+
import scipy.interpolate
4+
5+
# motion parameter
6+
L = 1.0 # wheel base
7+
ds = 0.1 # course distanse
8+
v = 10.0 / 3.6 # velocity [m/s]
9+
10+
11+
class State:
12+
13+
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
14+
self.x = x
15+
self.y = y
16+
self.yaw = yaw
17+
self.v = v
18+
19+
20+
def pi_2_pi(angle):
21+
while(angle > math.pi):
22+
angle = angle - 2.0 * math.pi
23+
24+
while(angle < -math.pi):
25+
angle = angle + 2.0 * math.pi
26+
27+
return angle
28+
29+
30+
def update(state, v, delta, dt, L):
31+
32+
state.v = v
33+
state.x = state.x + state.v * math.cos(state.yaw) * dt
34+
state.y = state.y + state.v * math.sin(state.yaw) * dt
35+
state.yaw = state.yaw + state.v / L * math.tan(delta) * dt
36+
state.yaw = pi_2_pi(state.yaw)
37+
38+
return state
39+
40+
41+
def generate_trajectory(s, km, kf, k0):
42+
43+
n = s / ds
44+
time = s / v # [s]
45+
tk = np.array([0.0, time / 2.0, time])
46+
kk = np.array([k0, km, kf])
47+
t = np.arange(0.0, time, time / n)
48+
kp = scipy.interpolate.spline(tk, kk, t, order=2)
49+
dt = time / n
50+
51+
# plt.plot(t, kp)
52+
# plt.show()
53+
54+
state = State()
55+
x, y, yaw = [state.x], [state.y], [state.yaw]
56+
57+
for ikp in kp:
58+
state = update(state, v, ikp, dt, L)
59+
x.append(state.x)
60+
y.append(state.y)
61+
yaw.append(state.yaw)
62+
63+
return x, y, yaw
64+
65+
66+
def generate_last_state(s, km, kf, k0):
67+
68+
n = s / ds
69+
time = s / v # [s]
70+
tk = np.array([0.0, time / 2.0, time])
71+
kk = np.array([k0, km, kf])
72+
t = np.arange(0.0, time, time / n)
73+
kp = scipy.interpolate.spline(tk, kk, t, order=2)
74+
dt = time / n
75+
76+
# plt.plot(t, kp)
77+
# plt.show()
78+
79+
state = State()
80+
81+
[update(state, v, ikp, dt, L) for ikp in kp]
82+
return state.x, state.y, state.yaw
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
3+
author: Atsushi Sakai
4+
"""
5+
from sklearn.neural_network import MLPRegressor
6+
from matplotlib import pyplot as plt
7+
import numpy as np
8+
import math
9+
import motion_model
10+
11+
12+
def calc_motion_param(maxs, ds, maxk, dk):
13+
s = np.arange(1.0, maxs, ds)
14+
km = np.arange(-maxk, maxk, dk)
15+
kf = np.arange(-maxk, maxk, dk)
16+
motion_param = []
17+
18+
for p1 in s:
19+
for p2 in km:
20+
for p3 in kf:
21+
motion_param.append([p1, p2, p3])
22+
23+
print("motion_param size is:", len(motion_param))
24+
return motion_param
25+
26+
27+
def calc_state_param(motion_param):
28+
print("calc_motion_param")
29+
30+
state_param = []
31+
32+
for p in motion_param:
33+
x, y, yaw = motion_model.generate_last_state(p[0], p[1], p[2], 0.0)
34+
state_param.append([x, y, yaw])
35+
36+
print("calc_state_param done", len(state_param))
37+
38+
return state_param
39+
40+
41+
def learn_motion_model():
42+
43+
# calc motion param
44+
# s, k0, km, kf
45+
maxk = math.radians(45.0)
46+
dk = math.radians(1.0)
47+
maxs = 50.0
48+
ds = 5.0
49+
50+
motion_param = calc_motion_param(maxs, ds, maxk, dk)
51+
state_param = calc_state_param(motion_param)
52+
reg = MLPRegressor(hidden_layer_sizes=(100, 100, 100))
53+
reg.fit(state_param, motion_param)
54+
print(reg.score(state_param, motion_param))
55+
56+
x = 10.0
57+
y = 5.0
58+
yaw = math.radians(00.0)
59+
60+
predict = reg.predict([[x, y, yaw]])
61+
print(predict)
62+
x, y, yaw = motion_model.generate_trajectory(
63+
predict[0, 0], predict[0, 1], predict[0, 2], 0.0)
64+
65+
plt.plot(x, y, "-r", label="trajectory")
66+
plt.axis("equal")
67+
plt.legend()
68+
plt.grid(True)
69+
plt.show()
70+
71+
print("Done")
72+
73+
74+
def main():
75+
learn_motion_model()
76+
77+
78+
if __name__ == '__main__':
79+
main()

0 commit comments

Comments
 (0)