Skip to content

Commit cc3fd0c

Browse files
videh25Videh Patel
and
Videh Patel
authored
Using util.angle_mod in all codes. AtsushiSakai#684 (AtsushiSakai#946)
* switched to using utils.angle_mod() * switched to using utils.angle_mod() * renamed mod2pi to pi_2_pi * Removed linting errors * switched to using utils.angle_mod() * switched to using utils.angle_mod() * renamed mod2pi to pi_2_pi * Removed linting errors * annotation changes and round precision * Reverted to mod2pi --------- Co-authored-by: Videh Patel <[email protected]>
1 parent 748b9be commit cc3fd0c

File tree

17 files changed

+51
-60
lines changed

17 files changed

+51
-60
lines changed

ArmNavigation/n_joint_arm_to_point_control/n_joint_arm_to_point_control.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import numpy as np
1313
from ArmNavigation.n_joint_arm_to_point_control.NLinkArm import NLinkArm
14+
from utils.angle import angle_mod
1415

1516
# Simulation parameters
1617
Kp = 2
@@ -159,9 +160,9 @@ def ang_diff(theta1, theta2):
159160
"""
160161
Returns the difference between two angles in the range -pi to +pi
161162
"""
162-
return (theta1 - theta2 + np.pi) % (2 * np.pi) - np.pi
163+
return angle_mod(theta1 - theta2)
163164

164165

165166
if __name__ == '__main__':
166167
# main()
167-
animation()
168+
animation()

ArmNavigation/two_joint_arm_to_point_control/two_joint_arm_to_point_control.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import matplotlib.pyplot as plt
1616
import numpy as np
1717
import math
18+
from utils.angle import angle_mod
1819

1920

2021
# Simulation parameters
@@ -110,7 +111,7 @@ def plot_arm(theta1, theta2, target_x, target_y): # pragma: no cover
110111

111112
def ang_diff(theta1, theta2):
112113
# Returns the difference between two angles in the range -pi to +pi
113-
return (theta1 - theta2 + np.pi) % (2 * np.pi) - np.pi
114+
return angle_mod(theta1 - theta2)
114115

115116

116117
def click(event): # pragma: no cover

Control/move_to_pose/move_to_pose.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import matplotlib.pyplot as plt
1414
import numpy as np
1515
from random import random
16-
16+
from utils.angle import angle_mod
1717

1818
class PathFinderController:
1919
"""
@@ -71,9 +71,8 @@ def calc_control_command(self, x_diff, y_diff, theta, theta_goal):
7171
# from 0 rad to 2*pi rad with slight turn
7272

7373
rho = np.hypot(x_diff, y_diff)
74-
alpha = (np.arctan2(y_diff, x_diff)
75-
- theta + np.pi) % (2 * np.pi) - np.pi
76-
beta = (theta_goal - theta - alpha + np.pi) % (2 * np.pi) - np.pi
74+
alpha = angle_mod(np.arctan2(y_diff, x_diff) - theta)
75+
beta = angle_mod(theta_goal - theta - alpha)
7776
v = self.Kp_rho * rho
7877
w = self.Kp_alpha * alpha - controller.Kp_beta * beta
7978

@@ -173,16 +172,14 @@ def transformation_matrix(x, y, theta):
173172
def main():
174173

175174
for i in range(5):
176-
x_start = 20 * random()
177-
y_start = 20 * random()
178-
theta_start = 2 * np.pi * random() - np.pi
175+
x_start = 20.0 * random()
176+
y_start = 20.0 * random()
177+
theta_start: float = 2 * np.pi * random() - np.pi
179178
x_goal = 20 * random()
180179
y_goal = 20 * random()
181180
theta_goal = 2 * np.pi * random() - np.pi
182-
print("Initial x: %.2f m\nInitial y: %.2f m\nInitial theta: %.2f rad\n" %
183-
(x_start, y_start, theta_start))
184-
print("Goal x: %.2f m\nGoal y: %.2f m\nGoal theta: %.2f rad\n" %
185-
(x_goal, y_goal, theta_goal))
181+
print(f"Initial x: {round(x_start, 2)} m\nInitial y: {round(y_start, 2)} m\nInitial theta: {round(theta_start, 2)} rad\n")
182+
print(f"Goal x: {round(x_goal, 2)} m\nGoal y: {round(y_goal, 2)} m\nGoal theta: {round(theta_goal, 2)} rad\n")
186183
move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal)
187184

188185

Localization/ensemble_kalman_filter/ensemble_kalman_filter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import math
1717
import matplotlib.pyplot as plt
1818
import numpy as np
19+
from utils.angle import angle_mod
1920

2021
from utils.angle import rot_mat_2d
2122

@@ -179,7 +180,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
179180

180181

181182
def pi_2_pi(angle):
182-
return (angle + math.pi) % (2 * math.pi) - math.pi
183+
return angle_mod(angle)
183184

184185

185186
def main():

PathPlanning/ClosedLoopRRTStar/unicycle_model.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import math
1010
import numpy as np
11+
from utils.angle import angle_mod
1112

1213
dt = 0.05 # [s]
1314
L = 0.9 # [m]
@@ -39,7 +40,7 @@ def update(state, a, delta):
3940

4041

4142
def pi_2_pi(angle):
42-
return (angle + math.pi) % (2 * math.pi) - math.pi
43+
return angle_mod(angle)
4344

4445

4546
if __name__ == '__main__': # pragma: no cover

PathPlanning/ModelPredictiveTrajectoryGenerator/motion_model.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
import numpy as np
33
from scipy.interpolate import interp1d
4+
from utils.angle import angle_mod
45

56
# motion parameter
67
L = 1.0 # wheel base
@@ -18,7 +19,7 @@ def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
1819

1920

2021
def pi_2_pi(angle):
21-
return (angle + math.pi) % (2 * math.pi) - math.pi
22+
return angle_mod(angle)
2223

2324

2425
def update(state, v, delta, dt, L):

PathPlanning/ModelPredictiveTrajectoryGenerator/trajectory_generator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# optimization parameter
2020
max_iter = 100
21-
h = np.array([0.5, 0.02, 0.02]).T # parameter sampling distance
21+
h: np.ndarray = np.array([0.5, 0.02, 0.02]).T # parameter sampling distance
2222
cost_th = 0.1
2323

2424
show_animation = True

PathPlanning/ReedsSheppPath/reeds_shepp_path_planning.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import matplotlib.pyplot as plt
1111
import numpy as np
12+
from utils.angle import angle_mod
1213

1314
show_animation = True
1415

@@ -40,6 +41,9 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
4041
plt.plot(x, y)
4142

4243

44+
def pi_2_pi(x):
45+
return angle_mod(x)
46+
4347
def mod2pi(x):
4448
# Be consistent with fmod in cplusplus here.
4549
v = np.mod(x, np.copysign(2.0 * math.pi, x))
@@ -50,7 +54,6 @@ def mod2pi(x):
5054
v -= 2.0 * math.pi
5155
return v
5256

53-
5457
def straight_left_straight(x, y, phi):
5558
phi = mod2pi(phi)
5659
# only take phi in (0.01*math.pi, 0.99*math.pi) for the sake of speed.
@@ -296,10 +299,6 @@ def interpolate(dist, length, mode, max_curvature, origin_x, origin_y,
296299
return x, y, yaw, 1 if length > 0.0 else -1
297300

298301

299-
def pi_2_pi(angle):
300-
return (angle + math.pi) % (2 * math.pi) - math.pi
301-
302-
303302
def calc_paths(sx, sy, syaw, gx, gy, gyaw, maxc, step_size):
304303
q0 = [sx, sy, syaw]
305304
q1 = [gx, gy, gyaw]

PathTracking/lqr_speed_steer_control/lqr_speed_steer_control.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212
import scipy.linalg as la
1313
import pathlib
14+
from utils.angle import angle_mod
1415
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
1516

1617
from PathPlanning.CubicSpline import cubic_spline_planner
@@ -52,8 +53,7 @@ def update(state, a, delta):
5253

5354

5455
def pi_2_pi(angle):
55-
return (angle + math.pi) % (2 * math.pi) - math.pi
56-
56+
return angle_mod(angle)
5757

5858
def solve_dare(A, B, Q, R):
5959
"""

PathTracking/lqr_steer_control/lqr_steer_control.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212
import sys
1313
import pathlib
14+
from utils.angle import angle_mod
1415
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
1516

1617
from PathPlanning.CubicSpline import cubic_spline_planner
@@ -61,7 +62,7 @@ def PIDControl(target, current):
6162

6263

6364
def pi_2_pi(angle):
64-
return (angle + math.pi) % (2 * math.pi) - math.pi
65+
return angle_mod(angle)
6566

6667

6768
def solve_DARE(A, B, Q, R):

PathTracking/model_predictive_speed_and_steer_control/model_predictive_speed_and_steer_control.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212
import sys
1313
import pathlib
14+
from utils.angle import angle_mod
1415
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
1516

1617
from PathPlanning.CubicSpline import cubic_spline_planner
@@ -69,13 +70,7 @@ def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
6970

7071

7172
def pi_2_pi(angle):
72-
while(angle > math.pi):
73-
angle = angle - 2.0 * math.pi
74-
75-
while(angle < -math.pi):
76-
angle = angle + 2.0 * math.pi
77-
78-
return angle
73+
return angle_mod(angle)
7974

8075

8176
def get_linear_model_matrix(v, phi, delta):

PathTracking/rear_wheel_feedback/rear_wheel_feedback.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import matplotlib.pyplot as plt
99
import math
1010
import numpy as np
11+
from utils.angle import angle_mod
1112

1213
from scipy import interpolate
1314
from scipy import optimize
@@ -51,21 +52,21 @@ def __init__(self, x, y):
5152
self.ddY = self.Y.derivative(2)
5253

5354
self.length = s[-1]
54-
55+
5556
def calc_yaw(self, s):
5657
dx, dy = self.dX(s), self.dY(s)
5758
return np.arctan2(dy, dx)
58-
59+
5960
def calc_curvature(self, s):
6061
dx, dy = self.dX(s), self.dY(s)
6162
ddx, ddy = self.ddX(s), self.ddY(s)
6263
return (ddy * dx - ddx * dy) / ((dx ** 2 + dy ** 2)**(3 / 2))
63-
64+
6465
def __find_nearest_point(self, s0, x, y):
6566
def calc_distance(_s, *args):
6667
_x, _y= self.X(_s), self.Y(_s)
6768
return (_x - args[0])**2 + (_y - args[1])**2
68-
69+
6970
def calc_distance_jacobian(_s, *args):
7071
_x, _y = self.X(_s), self.Y(_s)
7172
_dx, _dy = self.dX(_s), self.dY(_s)
@@ -76,7 +77,7 @@ def calc_distance_jacobian(_s, *args):
7677

7778
def calc_track_error(self, x, y, s0):
7879
ret = self.__find_nearest_point(s0, x, y)
79-
80+
8081
s = ret[0][0]
8182
e = ret[1]
8283

@@ -96,13 +97,7 @@ def pid_control(target, current):
9697
return a
9798

9899
def pi_2_pi(angle):
99-
while(angle > math.pi):
100-
angle = angle - 2.0 * math.pi
101-
102-
while(angle < -math.pi):
103-
angle = angle + 2.0 * math.pi
104-
105-
return angle
100+
return angle_mod(angle)
106101

107102
def rear_wheel_feedback_control(state, e, k, yaw_ref):
108103
v = state.v
@@ -170,7 +165,7 @@ def simulate(path_ref, goal):
170165
plt.plot(path_ref.X(s0), path_ref.Y(s0), "xg", label="target")
171166
plt.axis("equal")
172167
plt.grid(True)
173-
plt.title("speed[km/h]:{:.2f}, target s-param:{:.2f}".format(round(state.v * 3.6, 2), s0))
168+
plt.title(f"speed[km/h]:{round(state.v * 3.6, 2):.2f}, target s-param:{s0:.2f}")
174169
plt.pause(0.0001)
175170

176171
return t, x, y, yaw, v, goal_flag
@@ -184,7 +179,7 @@ def calc_target_speed(state, yaw_ref):
184179
if switch:
185180
state.direction *= -1
186181
return 0.0
187-
182+
188183
if state.direction != 1:
189184
return -target_speed
190185

PathTracking/stanley_controller/stanley_controller.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import matplotlib.pyplot as plt
1414
import sys
1515
import pathlib
16+
from utils.angle import angle_mod
1617
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
1718

1819
from PathPlanning.CubicSpline import cubic_spline_planner
@@ -26,7 +27,7 @@
2627
show_animation = True
2728

2829

29-
class State(object):
30+
class State:
3031
"""
3132
Class representing the state of a vehicle.
3233
@@ -38,7 +39,7 @@ class State(object):
3839

3940
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
4041
"""Instantiate the object."""
41-
super(State, self).__init__()
42+
super().__init__()
4243
self.x = x
4344
self.y = y
4445
self.yaw = yaw
@@ -106,13 +107,7 @@ def normalize_angle(angle):
106107
:param angle: (float)
107108
:return: (float) Angle in radian in [-pi, pi]
108109
"""
109-
while angle > np.pi:
110-
angle -= 2.0 * np.pi
111-
112-
while angle < -np.pi:
113-
angle += 2.0 * np.pi
114-
115-
return angle
110+
return angle_mod(angle)
116111

117112

118113
def calc_target_index(state, cx, cy):

SLAM/EKFSLAM/ekf_slam.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import matplotlib.pyplot as plt
1010
import numpy as np
11+
from utils.angle import angle_mod
1112

1213
# EKF state covariance
1314
Cx = np.diag([0.5, 0.5, np.deg2rad(30.0)]) ** 2
@@ -192,7 +193,7 @@ def jacob_h(q, delta, x, i):
192193

193194

194195
def pi_2_pi(angle):
195-
return (angle + math.pi) % (2 * math.pi) - math.pi
196+
return angle_mod(angle)
196197

197198

198199
def main():

SLAM/FastSLAM1/fast_slam1.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import matplotlib.pyplot as plt
1212
import numpy as np
13+
from utils.angle import angle_mod
1314

1415
# Fast SLAM covariance
1516
Q = np.diag([3.0, np.deg2rad(10.0)]) ** 2
@@ -321,7 +322,7 @@ def motion_model(x, u):
321322

322323

323324
def pi_2_pi(angle):
324-
return (angle + math.pi) % (2 * math.pi) - math.pi
325+
return angle_mod(angle)
325326

326327

327328
def main():

0 commit comments

Comments
 (0)