Skip to content

Commit dc879da

Browse files
authored
Using utility rot_mat_2d (AtsushiSakai#683)
1 parent 31178c8 commit dc879da

File tree

10 files changed

+70
-43
lines changed

10 files changed

+70
-43
lines changed

Localization/ensemble_kalman_filter/ensemble_kalman_filter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
1111
"""
1212

13-
import math
13+
import sys
14+
import os
15+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
1416

17+
import math
1518
import matplotlib.pyplot as plt
1619
import numpy as np
17-
from scipy.spatial.transform import Rotation as Rot
20+
21+
from utils.angle import rot_mat_2d
1822

1923
# Simulation parameter
2024
Q_sim = np.diag([0.2, np.deg2rad(1.0)]) ** 2
@@ -168,8 +172,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
168172
x = [a * math.cos(it) for it in t]
169173
y = [b * math.sin(it) for it in t]
170174
angle = math.atan2(eig_vec[1, big_ind], eig_vec[0, big_ind])
171-
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
172-
fx = np.stack([x, y]).T @ rot
175+
fx = np.stack([x, y]).T @ rot_mat_2d(angle)
173176

174177
px = np.array(fx[:, 0] + xEst[0, 0]).flatten()
175178
py = np.array(fx[:, 1] + xEst[1, 0]).flatten()

Localization/extended_kalman_filter/extended_kalman_filter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
77
"""
88

9-
import math
9+
import sys
10+
import os
11+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
1012

13+
import math
1114
import matplotlib.pyplot as plt
1215
import numpy as np
13-
from scipy.spatial.transform import Rotation as Rot
16+
17+
from utils.angle import rot_mat_2d
1418

1519
# Covariance for EKF simulation
1620
Q = np.diag([
@@ -149,8 +153,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
149153
x = [a * math.cos(it) for it in t]
150154
y = [b * math.sin(it) for it in t]
151155
angle = math.atan2(eigvec[1, bigind], eigvec[0, bigind])
152-
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
153-
fx = rot @ (np.array([x, y]))
156+
fx = rot_mat_2d(angle) @ (np.array([x, y]))
154157
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
155158
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
156159
plt.plot(px, py, "--r")

Localization/particle_filter/particle_filter.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
author: Atsushi Sakai (@Atsushi_twi)
66
77
"""
8+
import sys
9+
import os
10+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
811

912
import math
1013

1114
import matplotlib.pyplot as plt
1215
import numpy as np
13-
from scipy.spatial.transform import Rotation as Rot
16+
17+
from utils.angle import rot_mat_2d
1418

1519
# Estimation parameter of PF
1620
Q = np.diag([0.2]) ** 2 # range error
@@ -189,10 +193,9 @@ def plot_covariance_ellipse(x_est, p_est): # pragma: no cover
189193
x = [a * math.cos(it) for it in t]
190194
y = [b * math.sin(it) for it in t]
191195
angle = math.atan2(eig_vec[1, big_ind], eig_vec[0, big_ind])
192-
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
193-
fx = rot.dot(np.array([[x, y]]))
194-
px = np.array(fx[0, :] + x_est[0, 0]).flatten()
195-
py = np.array(fx[1, :] + x_est[1, 0]).flatten()
196+
fx = rot_mat_2d(angle) @ np.array([[x, y]])
197+
px = np.array(fx[:, 0] + x_est[0, 0]).flatten()
198+
py = np.array(fx[:, 1] + x_est[1, 0]).flatten()
196199
plt.plot(px, py, "--r")
197200

198201

Localization/unscented_kalman_filter/unscented_kalman_filter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
77
"""
88

9+
import sys
10+
import os
11+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
12+
913
import math
1014

1115
import matplotlib.pyplot as plt
1216
import numpy as np
13-
from scipy.spatial.transform import Rotation as Rot
1417
import scipy.linalg
1518

19+
from utils.angle import rot_mat_2d
20+
1621
# Covariance for UKF simulation
1722
Q = np.diag([
1823
0.1, # variance of location on x-axis
@@ -182,8 +187,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
182187
x = [a * math.cos(it) for it in t]
183188
y = [b * math.sin(it) for it in t]
184189
angle = math.atan2(eigvec[1, bigind], eigvec[0, bigind])
185-
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
186-
fx = rot @ np.array([x, y])
190+
fx = rot_mat_2d(angle) @ np.array([x, y])
187191
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
188192
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
189193
plt.plot(px, py, "--r")

Mapping/rectangle_fitting/rectangle_fitting.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
1313
"""
1414

15+
import sys
16+
import os
17+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
18+
19+
1520
import matplotlib.pyplot as plt
1621
import numpy as np
1722
import itertools
1823
from enum import Enum
19-
from scipy.spatial.transform import Rotation as Rot
24+
25+
from utils.angle import rot_mat_2d
2026

2127
from Mapping.rectangle_fitting.simulator \
2228
import VehicleSimulator, LidarSimulator
@@ -104,8 +110,7 @@ def _rectangle_search(self, x, y):
104110
min_cost = (-float('inf'), None)
105111
for theta in np.arange(0.0, np.pi / 2.0 - d_theta, d_theta):
106112

107-
rot = Rot.from_euler('z', theta).as_matrix()[0:2, 0:2]
108-
c = X @ rot
113+
c = X @ rot_mat_2d(theta)
109114
c1 = c[:, 0]
110115
c2 = c[:, 1]
111116

Mapping/rectangle_fitting/simulator.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
author: Atsushi Sakai
66
77
"""
8+
import sys
9+
import os
10+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
811

912
import numpy as np
1013
import matplotlib.pyplot as plt
1114
import math
1215
import random
13-
from scipy.spatial.transform import Rotation as Rot
16+
17+
from utils.angle import rot_mat_2d
1418

1519

1620
class VehicleSimulator:
@@ -41,8 +45,7 @@ def plot(self):
4145
plt.plot(gx, gy, "--b")
4246

4347
def calc_global_contour(self):
44-
rot = Rot.from_euler('z', self.yaw).as_matrix()[0:2, 0:2]
45-
gxy = np.stack([self.vc_x, self.vc_y]).T @ rot
48+
gxy = np.stack([self.vc_x, self.vc_y]).T @ rot_mat_2d(self.yaw)
4649
gx = gxy[:, 0] + self.x
4750
gy = gxy[:, 1] + self.y
4851

@@ -137,11 +140,5 @@ def ray_casting_filter(theta_l, range_l, angle_resolution):
137140
return rx, ry
138141

139142

140-
def main():
141-
print("start!!")
142-
143-
print("done!!")
144-
145-
146143
if __name__ == '__main__':
147144
main()

PathPlanning/GridBasedSweepCPP/grid_based_sweep_coverage_path_planner.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
author: Atsushi Sakai
55
"""
66

7-
import math
8-
import os
97
import sys
8+
import os
9+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
10+
11+
import math
1012
from enum import IntEnum
1113

1214
import numpy as np
13-
from scipy.spatial.transform import Rotation as Rot
1415
import matplotlib.pyplot as plt
1516

17+
from utils.angle import rot_mat_2d
18+
1619
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
1720
"/../../Mapping/")
1821

@@ -148,16 +151,14 @@ def convert_grid_coordinate(ox, oy, sweep_vec, sweep_start_position):
148151
tx = [ix - sweep_start_position[0] for ix in ox]
149152
ty = [iy - sweep_start_position[1] for iy in oy]
150153
th = math.atan2(sweep_vec[1], sweep_vec[0])
151-
rot = Rot.from_euler('z', th).as_matrix()[0:2, 0:2]
152-
converted_xy = np.stack([tx, ty]).T @ rot
154+
converted_xy = np.stack([tx, ty]).T @ rot_mat_2d(th)
153155

154156
return converted_xy[:, 0], converted_xy[:, 1]
155157

156158

157159
def convert_global_coordinate(x, y, sweep_vec, sweep_start_position):
158160
th = math.atan2(sweep_vec[1], sweep_vec[0])
159-
rot = Rot.from_euler('z', -th).as_matrix()[0:2, 0:2]
160-
converted_xy = np.stack([x, y]).T @ rot
161+
converted_xy = np.stack([x, y]).T @ rot_mat_2d(-th)
161162
rx = [ix + sweep_start_position[0] for ix in converted_xy[:, 0]]
162163
ry = [iy + sweep_start_position[1] for iy in converted_xy[:, 1]]
163164
return rx, ry

PathPlanning/HybridAStar/car.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
77
"""
88

9+
import sys
10+
import os
11+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
12+
913
from math import cos, sin, tan, pi
1014

1115
import matplotlib.pyplot as plt
1216
import numpy as np
13-
from scipy.spatial.transform import Rotation as Rot
17+
18+
from utils.angle import rot_mat_2d
1419

1520
WB = 3.0 # rear to front wheel
1621
W = 2.0 # width of car
@@ -45,7 +50,7 @@ def check_car_collision(x_list, y_list, yaw_list, ox, oy, kd_tree):
4550

4651
def rectangle_check(x, y, yaw, ox, oy):
4752
# transform obstacles to base link frame
48-
rot = Rot.from_euler('z', yaw).as_matrix()[0:2, 0:2]
53+
rot = rot_mat_2d(yaw)
4954
for iox, ioy in zip(ox, oy):
5055
tx = iox - x
5156
ty = ioy - y

PathPlanning/InformedRRTStar/informed_rrt_star.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
https://arxiv.org/pdf/1404.2334.pdf
1010
1111
"""
12+
import sys
13+
import os
14+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
1215

1316
import copy
1417
import math
1518
import random
1619

1720
import matplotlib.pyplot as plt
18-
from scipy.spatial.transform import Rotation as Rot
1921
import numpy as np
2022

23+
from utils.angle import rot_mat_2d
24+
2125
show_animation = True
2226

2327

@@ -308,8 +312,7 @@ def plot_ellipse(xCenter, cBest, cMin, e_theta): # pragma: no cover
308312
t = np.arange(0, 2 * math.pi + 0.1, 0.1)
309313
x = [a * math.cos(it) for it in t]
310314
y = [b * math.sin(it) for it in t]
311-
rot = Rot.from_euler('z', -angle).as_matrix()[0:2, 0:2]
312-
fx = rot @ np.array([x, y])
315+
fx = rot_mat_2d(-angle) @ np.array([x, y])
313316
px = np.array(fx[0, :] + cx).flatten()
314317
py = np.array(fx[1, :] + cy).flatten()
315318
plt.plot(cx, cy, "xc")

SLAM/GraphBasedSLAM/graph_based_slam.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
(http://www2.informatik.uni-freiburg.de/~stachnis/pdf/grisetti10titsmag.pdf)
1111
1212
"""
13+
import sys
14+
import os
15+
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../utils/")
1316

1417
import copy
1518
import itertools
@@ -63,7 +66,7 @@ def cal_observation_sigma():
6366
return sigma
6467

6568

66-
def calc_rotational_matrix(angle):
69+
def calc_3d_rotational_matrix(angle):
6770
return Rot.from_euler('z', angle).as_matrix()
6871

6972

@@ -82,8 +85,8 @@ def calc_edge(x1, y1, yaw1, x2, y2, yaw2, d1,
8285
edge.e[1, 0] = y2 - y1 - tmp3 + tmp4
8386
edge.e[2, 0] = 0
8487

85-
Rt1 = calc_rotational_matrix(tangle1)
86-
Rt2 = calc_rotational_matrix(tangle2)
88+
Rt1 = calc_3d_rotational_matrix(tangle1)
89+
Rt2 = calc_3d_rotational_matrix(tangle2)
8790

8891
sig1 = cal_observation_sigma()
8992
sig2 = cal_observation_sigma()

0 commit comments

Comments
 (0)