11"""
2+
23Model trajectory generator
34
45author: Atsushi Sakai(@Atsushi_twi)
6+
57"""
68
79import numpy as np
1113
1214# optimization parameter
1315max_iter = 100
14- h = np .matrix ([0.5 , 0.02 , 0.02 ]).T # parameter sampling distanse
16+ h = np .array ([0.5 , 0.02 , 0.02 ]).T # parameter sampling distance
1517cost_th = 0.1
1618
1719show_animation = False
@@ -37,28 +39,28 @@ def calc_diff(target, x, y, yaw):
3739
3840def calc_J (target , p , h , k0 ):
3941 xp , yp , yawp = motion_model .generate_last_state (
40- p [0 , 0 ] + h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
42+ p [0 , 0 ] + h [0 ], p [1 , 0 ], p [2 , 0 ], k0 )
4143 dp = calc_diff (target , [xp ], [yp ], [yawp ])
4244 xn , yn , yawn = motion_model .generate_last_state (
43- p [0 , 0 ] - h [0 , 0 ], p [1 , 0 ], p [2 , 0 ], k0 )
45+ p [0 , 0 ] - h [0 ], p [1 , 0 ], p [2 , 0 ], k0 )
4446 dn = calc_diff (target , [xn ], [yn ], [yawn ])
45- d1 = np .matrix ((dp - dn ) / (2.0 * h [0 , 0 ])).T
47+ d1 = np .array ((dp - dn ) / (2.0 * h [0 ])).reshape ( 3 , 1 )
4648
4749 xp , yp , yawp = motion_model .generate_last_state (
48- p [0 , 0 ], p [1 , 0 ] + h [1 , 0 ], p [2 , 0 ], k0 )
50+ p [0 , 0 ], p [1 , 0 ] + h [1 ], p [2 , 0 ], k0 )
4951 dp = calc_diff (target , [xp ], [yp ], [yawp ])
5052 xn , yn , yawn = motion_model .generate_last_state (
51- p [0 , 0 ], p [1 , 0 ] - h [1 , 0 ], p [2 , 0 ], k0 )
53+ p [0 , 0 ], p [1 , 0 ] - h [1 ], p [2 , 0 ], k0 )
5254 dn = calc_diff (target , [xn ], [yn ], [yawn ])
53- d2 = np .matrix ((dp - dn ) / (2.0 * h [1 , 0 ])).T
55+ d2 = np .array ((dp - dn ) / (2.0 * h [1 ])).reshape ( 3 , 1 )
5456
5557 xp , yp , yawp = motion_model .generate_last_state (
56- p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 , 0 ], k0 )
58+ p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] + h [2 ], k0 )
5759 dp = calc_diff (target , [xp ], [yp ], [yawp ])
5860 xn , yn , yawn = motion_model .generate_last_state (
59- p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] - h [2 , 0 ], k0 )
61+ p [0 , 0 ], p [1 , 0 ], p [2 , 0 ] - h [2 ], k0 )
6062 dn = calc_diff (target , [xn ], [yn ], [yawn ])
61- d3 = np .matrix ((dp - dn ) / (2.0 * h [2 , 0 ])).T
63+ d3 = np .array ((dp - dn ) / (2.0 * h [2 ])).reshape ( 3 , 1 )
6264
6365 J = np .hstack ((d1 , d2 , d3 ))
6466
@@ -73,10 +75,10 @@ def selection_learning_param(dp, p, k0, target):
7375 da = 0.5
7476
7577 for a in np .arange (mina , maxa , da ):
76- tp = p [:, :] + a * dp
78+ tp = p + a * dp
7779 xc , yc , yawc = motion_model .generate_last_state (
7880 tp [0 ], tp [1 ], tp [2 ], k0 )
79- dc = np . matrix ( calc_diff (target , [xc ], [yc ], [yawc ])). T
81+ dc = calc_diff (target , [xc ], [yc ], [yawc ])
8082 cost = np .linalg .norm (dc )
8183
8284 if cost <= mincost and a != 0.0 :
@@ -102,8 +104,7 @@ def show_trajectory(target, xc, yc):
102104def optimize_trajectory (target , k0 , p ):
103105 for i in range (max_iter ):
104106 xc , yc , yawc = motion_model .generate_trajectory (p [0 ], p [1 ], p [2 ], k0 )
105- dc = np .matrix (calc_diff (target , xc , yc , yawc )).T
106- # print(dc.T)
107+ dc = np .array (calc_diff (target , xc , yc , yawc )).reshape (3 , 1 )
107108
108109 cost = np .linalg .norm (dc )
109110 if cost <= cost_th :
@@ -112,7 +113,7 @@ def optimize_trajectory(target, k0, p):
112113
113114 J = calc_J (target , p , h , k0 )
114115 try :
115- dp = - np .linalg .inv (J ) * dc
116+ dp = - np .linalg .inv (J ) @ dc
116117 except np .linalg .linalg .LinAlgError :
117118 print ("cannot calc path LinAlgError" )
118119 xc , yc , yawc , p = None , None , None , None
@@ -137,7 +138,7 @@ def test_optimize_trajectory():
137138 target = motion_model .State (x = 5.0 , y = 2.0 , yaw = np .deg2rad (90.0 ))
138139 k0 = 0.0
139140
140- init_p = np .matrix ([6.0 , 0.0 , 0.0 ]).T
141+ init_p = np .array ([6.0 , 0.0 , 0.0 ]).reshape ( 3 , 1 )
141142
142143 x , y , yaw , p = optimize_trajectory (target , k0 , init_p )
143144
0 commit comments