8
8
import scipy .interpolate
9
9
import matplotlib .pyplot as plt
10
10
import math
11
- # import unicycle_model
11
+ from matplotrecorder import matplotrecorder
12
12
13
13
L = 1.0
14
14
ds = 0.1
@@ -88,20 +88,23 @@ def calc_diff(target, x, y, yaw):
88
88
def calc_J (target , p , h , k0 ):
89
89
xp , yp , yawp = generate_trajectory (p [0 , 0 ] + h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
90
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
91
94
d1 = np .matrix (dp / h [0 , 0 ]).T
92
95
93
96
xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ] + h [1 , 0 ], p [2 , 0 ], k0 )
94
97
dp = calc_diff (target , xp , yp , yawp )
95
98
# xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0] - h[1, 0], p[2, 0], k0)
96
99
# 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
98
101
d2 = np .matrix (dp / h [1 , 0 ]).T
99
102
100
103
xp , yp , yawp = generate_trajectory (p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 , 0 ], k0 )
101
104
dp = calc_diff (target , xp , yp , yawp )
102
105
# xn, yn, yawn = generate_trajectory(p[0, 0], p[1, 0], p[2, 0] - h[2, 0], k0)
103
106
# 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
105
108
d3 = np .matrix (dp / h [2 , 0 ]).T
106
109
# print(d1, d2, d3)
107
110
@@ -111,52 +114,74 @@ def calc_J(target, p, h, k0):
111
114
return J
112
115
113
116
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
+
114
139
def optimize_trajectory (target , k0 ):
115
140
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
118
143
119
144
for i in range (1000 ):
120
145
xc , yc , yawc = generate_trajectory (p [0 ], p [1 ], p [2 ], k0 )
121
146
dc = np .matrix (calc_diff (target , xc , yc , yawc )).T
122
147
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 ))
124
151
break
125
152
126
153
J = calc_J (target , p , h , k0 )
127
-
128
154
dp = - np .linalg .inv (J ) * dc
155
+ alpha = selection_learning_param (dp , p , k0 , target )
129
156
130
- p += np .array (dp )
157
+ p += alpha * np .array (dp )
158
+ # print(p.T)
131
159
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 ()
140
167
168
+ plt .clf ()
141
169
plot_arrow (target .x , target .y , target .yaw )
142
170
plt .plot (xc , yc , "-r" )
143
171
plt .axis ("equal" )
144
172
plt .grid (True )
173
+ matplotrecorder .save_frame ()
145
174
146
175
print ("done" )
147
176
148
177
149
178
def test_optimize_trajectory ():
150
179
151
180
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
156
182
157
183
optimize_trajectory (target , k0 )
158
-
159
- # x, y = generate_trajectory(s, km, kf, k0)
184
+ matplotrecorder .save_movie ("animation.gif" , 0.1 )
160
185
161
186
# plt.plot(x, y, "-r")
162
187
plot_arrow (target .x , target .y , target .yaw )
0 commit comments