Skip to content

Commit 2090361

Browse files
committed
implement trajectory generator
1 parent 744c0c2 commit 2090361

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Model trajectory generator
3+
4+
author: Atsushi Sakai
5+
"""
6+
7+
import numpy as np
8+
import scipy.interpolate
9+
import matplotlib.pyplot as plt
10+
import math
11+
# import unicycle_model
12+
13+
L = 1.0
14+
ds = 0.1
15+
16+
17+
class State:
18+
19+
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
20+
self.x = x
21+
self.y = y
22+
self.yaw = yaw
23+
self.v = v
24+
25+
26+
def generate_trajectory(s, km, kf, k0):
27+
28+
n = s / ds
29+
v = 10.0 / 3.6 # [m/s]
30+
time = s / v # [s]
31+
tk = np.array([0.0, time / 2.0, time])
32+
kk = np.array([k0, km, kf])
33+
t = np.arange(0.0, time, time / n)
34+
kp = scipy.interpolate.spline(tk, kk, t, order=2)
35+
dt = time / n
36+
37+
# plt.plot(t, kp)
38+
# plt.show()
39+
40+
state = State()
41+
x, y = [state.x], [state.y]
42+
43+
for ikp in kp:
44+
state = update(state, v, ikp, dt, L)
45+
x.append(state.x)
46+
y.append(state.y)
47+
48+
return x, y
49+
50+
51+
def update(state, v, delta, dt, L):
52+
53+
state.v = v
54+
state.x = state.x + state.v * math.cos(state.yaw) * dt
55+
state.y = state.y + state.v * math.sin(state.yaw) * dt
56+
state.yaw = state.yaw + state.v / L * math.tan(delta) * dt
57+
state.yaw = pi_2_pi(state.yaw)
58+
59+
return state
60+
61+
62+
def pi_2_pi(angle):
63+
while(angle > math.pi):
64+
angle = angle - 2.0 * math.pi
65+
66+
while(angle < -math.pi):
67+
angle = angle + 2.0 * math.pi
68+
69+
return angle
70+
71+
72+
def test_trajectory_generate():
73+
s = 5.0 # [m]
74+
k0 = 0.0
75+
km = math.radians(30.0)
76+
kf = math.radians(-30.0)
77+
78+
# plt.plot(xk, yk, "xr")
79+
# plt.plot(t, kp)
80+
# plt.show()
81+
82+
x, y = generate_trajectory(s, km, kf, k0)
83+
84+
plt.plot(x, y, "-r")
85+
plt.axis("equal")
86+
plt.grid(True)
87+
plt.show()
88+
89+
90+
def main():
91+
print(__file__ + " start!!")
92+
test_trajectory_generate()
93+
94+
95+
if __name__ == '__main__':
96+
main()

0 commit comments

Comments
 (0)