Skip to content

Commit 98441a2

Browse files
Fixed unstable behavior
1 parent df8e91d commit 98441a2

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

PathTracking/move_to_pose/move_to_pose.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from math import cos, sin, sqrt, atan2, pi
1414
from random import random
1515

16-
Kp_rho = 3
17-
Kp_alpha = 5
18-
Kp_beta = -2
16+
Kp_rho = 9
17+
Kp_alpha = 15
18+
Kp_beta = -3
1919
dt = 0.01
2020

2121
#Corners of triangular vehicle when pointing to the right (0 radians)
@@ -40,45 +40,62 @@ def move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal):
4040
x = x_start
4141
y = y_start
4242
theta = theta_start
43-
while True:
43+
44+
x_diff = x_goal - x
45+
y_diff = y_goal - y
46+
47+
rho = sqrt(x_diff**2 + y_diff**2)
48+
while rho > 0.001:
49+
x_traj.append(x)
50+
y_traj.append(y)
51+
4452
x_diff = x_goal - x
4553
y_diff = y_goal - y
4654

55+
"""
56+
Restrict alpha and beta (angle differences) to the range
57+
[-pi, pi] to prevent unstable behavior e.g. difference going
58+
from 0 rad to 2*pi rad with slight turn
59+
"""
60+
4761
rho = sqrt(x_diff**2 + y_diff**2)
48-
alpha = atan2(y_diff, x_diff) - theta
49-
beta = theta_goal - theta - alpha
62+
alpha = (atan2(y_diff, x_diff) - theta + pi)%(2*pi) - pi
63+
beta = (theta_goal - theta - alpha + pi)%(2*pi) - pi
5064

5165
v = Kp_rho*rho
5266
w = Kp_alpha*alpha + Kp_beta*beta
5367

54-
if alpha > pi/2 or alpha < -pi/2:
68+
if alpha > pi/2 or alpha < -pi/2:
5569
v = -v
5670

5771
theta = theta + w*dt
5872
x = x + v*cos(theta)*dt
5973
y = y + v*sin(theta)*dt
60-
x_traj.append(x)
61-
y_traj.append(y)
6274

63-
plot_vehicle(x, y, theta, x_start, y_start, x_goal, y_goal, theta_goal, x_traj, y_traj)
75+
plot_vehicle(x, y, theta, x_traj, y_traj)
6476

6577

66-
def plot_vehicle(x, y, theta, x_start, y_start, x_goal, y_goal, theta_goal, x_traj, y_traj):
78+
def plot_vehicle(x, y, theta, x_traj, y_traj):
6779
T = transformation_matrix(x, y, theta)
6880
p1 = np.matmul(T, p1_i)
6981
p2 = np.matmul(T, p2_i)
7082
p3 = np.matmul(T, p3_i)
83+
7184
plt.cla()
85+
7286
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], 'k-')
7387
plt.plot([p2[0], p3[0]], [p2[1], p3[1]], 'k-')
7488
plt.plot([p3[0], p1[0]], [p3[1], p1[1]], 'k-')
75-
plt.plot(x_start, y_start, 'r*')
89+
90+
plt.arrow(x_start, y_start, cos(theta_start), sin(theta_start), color='r', width=0.1)
7691
plt.arrow(x_goal, y_goal, cos(theta_goal), sin(theta_goal), color='g', width=0.1)
7792
plt.plot(x_traj, y_traj, 'b--')
93+
7894
plt.xlim(0, 20)
7995
plt.ylim(0, 20)
96+
8097
plt.show()
81-
plt.pause(dt/10)
98+
plt.pause(dt)
8299

83100
def transformation_matrix(x, y, theta):
84101
return np.array([
@@ -88,12 +105,12 @@ def transformation_matrix(x, y, theta):
88105
])
89106

90107
if __name__ == '__main__':
91-
x_start = 4 #20*random()
92-
y_start = 15 #20*random()
93-
theta_start = pi/2 #2*pi*random() - pi
94-
x_goal = 13 #20*random()
95-
y_goal = 3 #20*random()
96-
theta_goal = -pi/2 #2*pi*random() - pi
108+
x_start = 20*random()
109+
y_start = 20*random()
110+
theta_start = 2*pi*random() - pi
111+
x_goal = 20*random()
112+
y_goal = 20*random()
113+
theta_goal = 2*pi*random() - pi
97114
print("Initial x: %.2f m\nInitial y: %.2f m\nInitial theta: %.2f rad\n" % (x_start, y_start, theta_start))
98115
print("Goal x: %.2f m\nGoal y: %.2f m\nGoal theta: %.2f rad\n" % (x_goal, y_goal, theta_goal))
99116
move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal)

0 commit comments

Comments
 (0)