55"""
66
77import numpy as np
8- import scipy .interpolate
98import matplotlib .pyplot as plt
109import math
10+ import motion_model
1111from matplotrecorder import matplotrecorder
1212
13- L = 1.0
14- ds = 0.1
13+ # optimization parameter
14+ maxiter = 1000
15+ h = np .matrix ([0.1 , 0.002 , 0.002 ]).T # parameter sampling distanse
1516
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 , yaw = [state .x ], [state .y ], [state .yaw ]
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- yaw .append (state .yaw )
48-
49- return x , y , yaw
50-
51-
52- def update (state , v , delta , dt , L ):
53-
54- state .v = v
55- state .x = state .x + state .v * math .cos (state .yaw ) * dt
56- state .y = state .y + state .v * math .sin (state .yaw ) * dt
57- state .yaw = state .yaw + state .v / L * math .tan (delta ) * dt
58- state .yaw = pi_2_pi (state .yaw )
59-
60- return state
61-
62-
63- def pi_2_pi (angle ):
64- while (angle > math .pi ):
65- angle = angle - 2.0 * math .pi
66-
67- while (angle < - math .pi ):
68- angle = angle + 2.0 * math .pi
69-
70- return angle
17+ # matplotrecorder.donothing = True
7118
7219
7320def plot_arrow (x , y , yaw , length = 1.0 , width = 0.5 , fc = "r" , ec = "k" ):
@@ -81,35 +28,39 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
8128
8229
8330def calc_diff (target , x , y , yaw ):
84- d = np .array ([x [- 1 ] - target .x , y [- 1 ] - target .y , yaw [- 1 ] - target .yaw ])
31+ d = np .array ([target .x - x [- 1 ],
32+ target .y - y [- 1 ],
33+ motion_model .pi_2_pi (target .yaw - yaw [- 1 ])])
34+
8535 return d
8636
8737
8838def calc_J (target , p , h , k0 ):
89- xp , yp , yawp = generate_trajectory (p [0 , 0 ] + h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
90- dp = calc_diff (target , xp , yp , yawp )
91- # xn, yn, yawn = generate_trajectory(p[0, 0] - h[0, 0], p[1, 0], p[2, 0], k0)
92- # dn = calc_diff(target, xn, yn, yawn)
93- # d1 = np.matrix((dp - dn) / (2.0 * h[1, 0])).T
94- d1 = np .matrix (dp / h [0 , 0 ]).T
95-
96- xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ] + h [1 , 0 ], p [2 , 0 ], k0 )
97- dp = calc_diff (target , xp , yp , yawp )
98- # xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0] - h[1, 0], p[2, 0], k0)
99- # dn = calc_diff(target, xn, yn, yawn)
100- # d2 = np.matrix((dp - dn) / (2.0 * h[2, 0])).T
101- d2 = np .matrix (dp / h [1 , 0 ]).T
102-
103- xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 , 0 ], k0 )
104- dp = calc_diff (target , xp , yp , yawp )
105- # xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0], p[2, 0] - h[2, 0], k0)
106- # dn = calc_diff(target, xn, yn, yawn)
107- # d3 = np.matrix((dp - dn) / (2.0 * h[2, 0])).T
108- d3 = np .matrix (dp / h [2 , 0 ]).T
109- # print(d1, d2, d3)
39+ xp , yp , yawp = motion_model .generate_last_state (
40+ p [0 , 0 ] + h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
41+ dp = calc_diff (target , [xp ], [yp ], [yawp ])
42+ xn , yn , yawn = motion_model .generate_last_state (
43+ p [0 , 0 ] - h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
44+ dn = calc_diff (target , [xn ], [yn ], [yawn ])
45+ d1 = np .matrix ((dp - dn ) / (2.0 * h [1 , 0 ])).T
46+
47+ xp , yp , yawp = motion_model .generate_last_state (
48+ p [0 , 0 ], p [1 , 0 ] + h [1 , 0 ], p [2 , 0 ], k0 )
49+ dp = calc_diff (target , [xp ], [yp ], [yawp ])
50+ xn , yn , yawn = motion_model .generate_last_state (
51+ p [0 , 0 ], p [1 , 0 ] - h [1 , 0 ], p [2 , 0 ], k0 )
52+ dn = calc_diff (target , [xn ], [yn ], [yawn ])
53+ d2 = np .matrix ((dp - dn ) / (2.0 * h [2 , 0 ])).T
54+
55+ xp , yp , yawp = motion_model .generate_last_state (
56+ p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 , 0 ], k0 )
57+ dp = calc_diff (target , [xp ], [yp ], [yawp ])
58+ xn , yn , yawn = motion_model .generate_last_state (
59+ p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] - h [2 , 0 ], k0 )
60+ dn = calc_diff (target , [xn ], [yn ], [yawn ])
61+ d3 = np .matrix ((dp - dn ) / (2.0 * h [2 , 0 ])).T
11062
11163 J = np .hstack ((d1 , d2 , d3 ))
112- # print(J)
11364
11465 return J
11566
@@ -118,15 +69,17 @@ def selection_learning_param(dp, p, k0, target):
11869
11970 mincost = float ("inf" )
12071 mina = 1.0
72+ maxa = 5.0
73+ da = 0.5
12174
122- for a in np .arange (1.0 , 10.0 , 0.5 ):
75+ for a in np .arange (mina , maxa , da ):
12376 tp = p [:, :] + a * dp
124- xc , yc , yawc = generate_trajectory (tp [0 ], tp [1 ], tp [2 ], k0 )
125- dc = np .matrix (calc_diff (target , xc , yc , yawc )).T
77+ xc , yc , yawc = motion_model .generate_last_state (
78+ tp [0 ], tp [1 ], tp [2 ], k0 )
79+ dc = np .matrix (calc_diff (target , [xc ], [yc ], [yawc ])).T
12680 cost = np .linalg .norm (dc )
127- # print(a, cost)
12881
129- if cost <= mincost :
82+ if cost <= mincost and a != 0.0 :
13083 mina = a
13184 mincost = cost
13285
@@ -136,18 +89,29 @@ def selection_learning_param(dp, p, k0, target):
13689 return mina
13790
13891
139- def optimize_trajectory (target , k0 ):
92+ def show_trajectory (target , xc , yc ):
93+
94+ plt .clf ()
95+ plot_arrow (target .x , target .y , target .yaw )
96+ plt .plot (xc , yc , "-r" )
97+ plt .axis ("equal" )
98+ plt .grid (True )
99+ plt .pause (0.1 )
100+ matplotrecorder .save_frame ()
101+
140102
141- p = np .matrix ([6.0 , 0.0 , 0.0 ]).T
142- h = np .matrix ([0.1 , 0.002 , 0.002 ]).T
103+ def optimize_trajectory (target , k0 , p ):
143104
144- for i in range (1000 ):
145- xc , yc , yawc = generate_trajectory (p [0 ], p [1 ], p [2 ], k0 )
105+ for i in range (maxiter ):
106+ xc , yc , yawc = motion_model . generate_trajectory (p [0 ], p [1 ], p [2 ], k0 )
146107 dc = np .matrix (calc_diff (target , xc , yc , yawc )).T
108+ # print(dc.T)
147109
148110 cost = np .linalg .norm (dc )
111+ print ("cost is:" + str (cost ))
149112 if cost <= 0.05 :
150113 print ("cost is:" + str (cost ))
114+ print (p )
151115 break
152116
153117 J = calc_J (target , p , h , k0 )
@@ -157,30 +121,22 @@ def optimize_trajectory(target, k0):
157121 p += alpha * np .array (dp )
158122 # print(p.T)
159123
160- plt .clf ()
161- plot_arrow (target .x , target .y , target .yaw )
162- plt .plot (xc , yc , "-r" )
163- plt .axis ("equal" )
164- plt .grid (True )
165- plt .pause (0.1 )
166- matplotrecorder .save_frame ()
124+ show_trajectory (target , xc , yc )
167125
168- plt .clf ()
169- plot_arrow (target .x , target .y , target .yaw )
170- plt .plot (xc , yc , "-r" )
171- plt .axis ("equal" )
172- plt .grid (True )
173- matplotrecorder .save_frame ()
126+ show_trajectory (target , xc , yc )
174127
175128 print ("done" )
176129
177130
178131def test_optimize_trajectory ():
179132
180- target = State (x = 5.0 , y = 2.0 , yaw = math .radians (00.0 ))
181- k0 = - 0.3
133+ # target = motion_model.State(x=5.0, y=2.0, yaw=math.radians(00.0))
134+ target = motion_model .State (x = 5.0 , y = 2.0 , yaw = math .radians (90.0 ))
135+ k0 = 0.0
136+
137+ init_p = np .matrix ([6.0 , 0.0 , 0.0 ]).T
182138
183- optimize_trajectory (target , k0 )
139+ optimize_trajectory (target , k0 , init_p )
184140 matplotrecorder .save_movie ("animation.gif" , 0.1 )
185141
186142 # plt.plot(x, y, "-r")
@@ -200,7 +156,7 @@ def test_trajectory_generate():
200156 # plt.plot(t, kp)
201157 # plt.show()
202158
203- x , y = generate_trajectory (s , km , kf , k0 )
159+ x , y = motion_model . generate_trajectory (s , km , kf , k0 )
204160
205161 plt .plot (x , y , "-r" )
206162 plt .axis ("equal" )
0 commit comments