Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions Control/move_to_pose/move_to_pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
Author: Daniel Ingram (daniel-s-ingram)
Atsushi Sakai (@Atsushi_twi)
Seied Muhammad Yazdian (@Muhammad-Yazdian)
Wang Zheng (@Aglargil)

P. I. Corke, "Robotics, Vision & Control", Springer 2017, ISBN 978-3-319-54413-7

"""

import matplotlib.pyplot as plt
import numpy as np
from random import random
import sys
import pathlib

sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
from utils.angle import angle_mod
from random import random


class PathFinderController:
"""
Expand Down Expand Up @@ -71,13 +77,18 @@ def calc_control_command(self, x_diff, y_diff, theta, theta_goal):
# from 0 rad to 2*pi rad with slight turn

rho = np.hypot(x_diff, y_diff)
alpha = angle_mod(np.arctan2(y_diff, x_diff) - theta)
beta = angle_mod(theta_goal - theta - alpha)
v = self.Kp_rho * rho
w = self.Kp_alpha * alpha - self.Kp_beta * beta

alpha = angle_mod(np.arctan2(y_diff, x_diff) - theta)
beta = angle_mod(theta_goal - theta - alpha)
if alpha > np.pi / 2 or alpha < -np.pi / 2:
# recalculate alpha to make alpha in the range of [-pi/2, pi/2]
alpha = angle_mod(np.arctan2(-y_diff, -x_diff) - theta)
beta = angle_mod(theta_goal - theta - alpha)
w = self.Kp_alpha * alpha - self.Kp_beta * beta
v = -v
else:
w = self.Kp_alpha * alpha - self.Kp_beta * beta

return rho, v, w

Expand Down Expand Up @@ -111,8 +122,7 @@ def move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal):
x_diff = x_goal - x
y_diff = y_goal - y

rho, v, w = controller.calc_control_command(
x_diff, y_diff, theta, theta_goal)
rho, v, w = controller.calc_control_command(x_diff, y_diff, theta, theta_goal)

if abs(v) > MAX_LINEAR_SPEED:
v = np.sign(v) * MAX_LINEAR_SPEED
Expand All @@ -126,10 +136,22 @@ def move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal):

if show_animation: # pragma: no cover
plt.cla()
plt.arrow(x_start, y_start, np.cos(theta_start),
np.sin(theta_start), color='r', width=0.1)
plt.arrow(x_goal, y_goal, np.cos(theta_goal),
np.sin(theta_goal), color='g', width=0.1)
plt.arrow(
x_start,
y_start,
np.cos(theta_start),
np.sin(theta_start),
color="r",
width=0.1,
)
plt.arrow(
x_goal,
y_goal,
np.cos(theta_goal),
np.sin(theta_goal),
color="g",
width=0.1,
)
plot_vehicle(x, y, theta, x_traj, y_traj)


Expand All @@ -144,16 +166,16 @@ def plot_vehicle(x, y, theta, x_traj, y_traj): # pragma: no cover
p2 = np.matmul(T, p2_i)
p3 = np.matmul(T, p3_i)

plt.plot([p1[0], p2[0]], [p1[1], p2[1]], 'k-')
plt.plot([p2[0], p3[0]], [p2[1], p3[1]], 'k-')
plt.plot([p3[0], p1[0]], [p3[1], p1[1]], 'k-')
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], "k-")
plt.plot([p2[0], p3[0]], [p2[1], p3[1]], "k-")
plt.plot([p3[0], p1[0]], [p3[1], p1[1]], "k-")

plt.plot(x_traj, y_traj, 'b--')
plt.plot(x_traj, y_traj, "b--")

# for stopping simulation with the esc key.
plt.gcf().canvas.mpl_connect(
'key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
"key_release_event", lambda event: [exit(0) if event.key == "escape" else None]
)

plt.xlim(0, 20)
plt.ylim(0, 20)
Expand All @@ -162,26 +184,31 @@ def plot_vehicle(x, y, theta, x_traj, y_traj): # pragma: no cover


def transformation_matrix(x, y, theta):
return np.array([
[np.cos(theta), -np.sin(theta), x],
[np.sin(theta), np.cos(theta), y],
[0, 0, 1]
])
return np.array(
[
[np.cos(theta), -np.sin(theta), x],
[np.sin(theta), np.cos(theta), y],
[0, 0, 1],
]
)


def main():

for i in range(5):
x_start = 20.0 * random()
y_start = 20.0 * random()
theta_start: float = 2 * np.pi * random() - np.pi
x_goal = 20 * random()
y_goal = 20 * random()
theta_goal = 2 * np.pi * random() - np.pi
print(f"Initial x: {round(x_start, 2)} m\nInitial y: {round(y_start, 2)} m\nInitial theta: {round(theta_start, 2)} rad\n")
print(f"Goal x: {round(x_goal, 2)} m\nGoal y: {round(y_goal, 2)} m\nGoal theta: {round(theta_goal, 2)} rad\n")
print(
f"Initial x: {round(x_start, 2)} m\nInitial y: {round(y_start, 2)} m\nInitial theta: {round(theta_start, 2)} rad\n"
)
print(
f"Goal x: {round(x_goal, 2)} m\nGoal y: {round(y_goal, 2)} m\nGoal theta: {round(theta_goal, 2)} rad\n"
)
move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal)


if __name__ == '__main__':
if __name__ == "__main__":
main()