88import scipy .interpolate
99import matplotlib .pyplot as plt
1010import math
11- # import unicycle_model
11+ from matplotrecorder import matplotrecorder
1212
1313L = 1.0
1414ds = 0.1
@@ -88,20 +88,23 @@ def calc_diff(target, x, y, yaw):
8888def calc_J (target , p , h , k0 ):
8989 xp , yp , yawp = generate_trajectory (p [0 , 0 ] + h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
9090 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
9194 d1 = np .matrix (dp / h [0 , 0 ]).T
9295
9396 xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ] + h [1 , 0 ], p [2 , 0 ], k0 )
9497 dp = calc_diff (target , xp , yp , yawp )
9598 # xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0] - h[1, 0], p[2, 0], k0)
9699 # dn = calc_diff(target, xn, yn, yawn)
97- # d2 = np.matrix((dp - dn) / 2.0 * h[1 , 0]).T
100+ # d2 = np.matrix((dp - dn) / ( 2.0 * h[2 , 0]) ).T
98101 d2 = np .matrix (dp / h [1 , 0 ]).T
99102
100103 xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 , 0 ], k0 )
101104 dp = calc_diff (target , xp , yp , yawp )
102105 # xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0], p[2, 0] - h[2, 0], k0)
103106 # dn = calc_diff(target, xn, yn, yawn)
104- # d3 = np.matrix((dp - dn) / 2.0 * h[2, 0]).T
107+ # d3 = np.matrix((dp - dn) / ( 2.0 * h[2, 0]) ).T
105108 d3 = np .matrix (dp / h [2 , 0 ]).T
106109 # print(d1, d2, d3)
107110
@@ -111,52 +114,74 @@ def calc_J(target, p, h, k0):
111114 return J
112115
113116
117+ def selection_learning_param (dp , p , k0 , target ):
118+
119+ mincost = float ("inf" )
120+ mina = 1.0
121+
122+ for a in np .arange (1.0 , 10.0 , 0.5 ):
123+ 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
126+ cost = np .linalg .norm (dc )
127+ # print(a, cost)
128+
129+ if cost <= mincost :
130+ mina = a
131+ mincost = cost
132+
133+ # print(mincost, mina)
134+ # input()
135+
136+ return mina
137+
138+
114139def optimize_trajectory (target , k0 ):
115140
116- p = np .matrix ([5 .0 , 0.0 , 0.0 ]).T
117- h = np .matrix ([0.1 , 0.003 , 0.003 ]).T
141+ p = np .matrix ([6 .0 , 0.0 , 0.0 ]).T
142+ h = np .matrix ([0.1 , 0.002 , 0.002 ]).T
118143
119144 for i in range (1000 ):
120145 xc , yc , yawc = generate_trajectory (p [0 ], p [1 ], p [2 ], k0 )
121146 dc = np .matrix (calc_diff (target , xc , yc , yawc )).T
122147
123- if np .linalg .norm (dc ) <= 0.1 :
148+ cost = np .linalg .norm (dc )
149+ if cost <= 0.05 :
150+ print ("cost is:" + str (cost ))
124151 break
125152
126153 J = calc_J (target , p , h , k0 )
127-
128154 dp = - np .linalg .inv (J ) * dc
155+ alpha = selection_learning_param (dp , p , k0 , target )
129156
130- p += np .array (dp )
157+ p += alpha * np .array (dp )
158+ # print(p.T)
131159
132- # print(p)
133- # plt.clf()
134- # plot_arrow(target.x, target.y, target.yaw)
135- # plt.plot(xc, yc, "-r")
136- # plt.axis("equal")
137- # plt.grid(True)
138- # # plt.show()
139- # plt.pause(0.1)
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 ()
140167
168+ plt .clf ()
141169 plot_arrow (target .x , target .y , target .yaw )
142170 plt .plot (xc , yc , "-r" )
143171 plt .axis ("equal" )
144172 plt .grid (True )
173+ matplotrecorder .save_frame ()
145174
146175 print ("done" )
147176
148177
149178def test_optimize_trajectory ():
150179
151180 target = State (x = 5.0 , y = 2.0 , yaw = math .radians (00.0 ))
152- k0 = 0.0
153- # s = 5.0 # [m]
154- # km = math.radians(30.0)
155- # kf = math.radians(-30.0)
181+ k0 = - 0.3
156182
157183 optimize_trajectory (target , k0 )
158-
159- # x, y = generate_trajectory(s, km, kf, k0)
184+ matplotrecorder .save_movie ("animation.gif" , 0.1 )
160185
161186 # plt.plot(x, y, "-r")
162187 plot_arrow (target .x , target .y , target .yaw )
0 commit comments