11"""
2+
23Move to specified pose
4+
35Author: Daniel Ingram (daniel-s-ingram)
6+ Atsushi Sakai(@Atsushi_twi)
47
58P. I. Corke, "Robotics, Vision & Control", Springer 2017, ISBN 978-3-319-54413-7
6- """
79
8- from __future__ import print_function , division
10+ """
911
1012import matplotlib .pyplot as plt
1113import numpy as np
12-
13- from math import cos , sin , sqrt , atan2 , pi
1414from random import random
1515
16+ # simulation parameters
1617Kp_rho = 9
1718Kp_alpha = 15
1819Kp_beta = - 3
1920dt = 0.01
2021
21- #Corners of triangular vehicle when pointing to the right (0 radians)
22- p1_i = np .array ([0.5 , 0 , 1 ]).T
23- p2_i = np .array ([- 0.5 , 0.25 , 1 ]).T
24- p3_i = np .array ([- 0.5 , - 0.25 , 1 ]).T
25-
26- x_traj = []
27- y_traj = []
22+ show_animation = True
2823
29- plt .ion ()
3024
3125def move_to_pose (x_start , y_start , theta_start , x_goal , y_goal , theta_goal ):
3226 """
@@ -44,7 +38,9 @@ def move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal):
4438 x_diff = x_goal - x
4539 y_diff = y_goal - y
4640
47- rho = sqrt (x_diff ** 2 + y_diff ** 2 )
41+ x_traj , y_traj = [], []
42+
43+ rho = np .sqrt (x_diff ** 2 + y_diff ** 2 )
4844 while rho > 0.001 :
4945 x_traj .append (x )
5046 y_traj .append (y )
@@ -54,63 +50,78 @@ def move_to_pose(x_start, y_start, theta_start, x_goal, y_goal, theta_goal):
5450
5551 """
5652 Restrict alpha and beta (angle differences) to the range
57- [-pi, pi] to prevent unstable behavior e.g. difference going
53+ [-pi, pi] to prevent unstable behavior e.g. difference going
5854 from 0 rad to 2*pi rad with slight turn
59- """
55+ """
6056
61- rho = sqrt (x_diff ** 2 + y_diff ** 2 )
62- alpha = (atan2 (y_diff , x_diff ) - theta + pi )% (2 * pi ) - pi
63- beta = (theta_goal - theta - alpha + pi )% (2 * pi ) - pi
57+ rho = np .sqrt (x_diff ** 2 + y_diff ** 2 )
58+ alpha = (np .arctan2 (y_diff , x_diff ) -
59+ theta + np .pi ) % (2 * np .pi ) - np .pi
60+ beta = (theta_goal - theta - alpha + np .pi ) % (2 * np .pi ) - np .pi
6461
65- v = Kp_rho * rho
66- w = Kp_alpha * alpha + Kp_beta * beta
62+ v = Kp_rho * rho
63+ w = Kp_alpha * alpha + Kp_beta * beta
6764
68- if alpha > pi / 2 or alpha < - pi / 2 :
65+ if alpha > np . pi / 2 or alpha < - np . pi / 2 :
6966 v = - v
7067
71- theta = theta + w * dt
72- x = x + v * cos (theta )* dt
73- y = y + v * sin (theta )* dt
68+ theta = theta + w * dt
69+ x = x + v * np . cos (theta ) * dt
70+ y = y + v * np . sin (theta ) * dt
7471
75- plot_vehicle (x , y , theta , x_traj , y_traj )
72+ if show_animation :
73+ plt .cla ()
74+ plt .arrow (x_start , y_start , np .cos (theta_start ),
75+ np .sin (theta_start ), color = 'r' , width = 0.1 )
76+ plt .arrow (x_goal , y_goal , np .cos (theta_goal ),
77+ np .sin (theta_goal ), color = 'g' , width = 0.1 )
78+ plot_vehicle (x , y , theta , x_traj , y_traj )
7679
7780
7881def plot_vehicle (x , y , theta , x_traj , y_traj ):
82+ # Corners of triangular vehicle when pointing to the right (0 radians)
83+ p1_i = np .array ([0.5 , 0 , 1 ]).T
84+ p2_i = np .array ([- 0.5 , 0.25 , 1 ]).T
85+ p3_i = np .array ([- 0.5 , - 0.25 , 1 ]).T
86+
7987 T = transformation_matrix (x , y , theta )
8088 p1 = np .matmul (T , p1_i )
8189 p2 = np .matmul (T , p2_i )
8290 p3 = np .matmul (T , p3_i )
8391
84- plt .cla ()
85-
8692 plt .plot ([p1 [0 ], p2 [0 ]], [p1 [1 ], p2 [1 ]], 'k-' )
8793 plt .plot ([p2 [0 ], p3 [0 ]], [p2 [1 ], p3 [1 ]], 'k-' )
8894 plt .plot ([p3 [0 ], p1 [0 ]], [p3 [1 ], p1 [1 ]], 'k-' )
8995
90- plt .arrow (x_start , y_start , cos (theta_start ), sin (theta_start ), color = 'r' , width = 0.1 )
91- plt .arrow (x_goal , y_goal , cos (theta_goal ), sin (theta_goal ), color = 'g' , width = 0.1 )
9296 plt .plot (x_traj , y_traj , 'b--' )
9397
9498 plt .xlim (0 , 20 )
9599 plt .ylim (0 , 20 )
96100
97- plt .show ()
98101 plt .pause (dt )
99102
103+
100104def transformation_matrix (x , y , theta ):
101105 return np .array ([
102- [cos (theta ), - sin (theta ), x ],
103- [sin (theta ), cos (theta ), y ],
106+ [np . cos (theta ), - np . sin (theta ), x ],
107+ [np . sin (theta ), np . cos (theta ), y ],
104108 [0 , 0 , 1 ]
105- ])
109+ ])
110+
111+
112+ def main ():
113+ x_start = 20 * random ()
114+ y_start = 20 * random ()
115+ theta_start = 2 * np .pi * random () - np .pi
116+ x_goal = 20 * random ()
117+ y_goal = 20 * random ()
118+ theta_goal = 2 * np .pi * random () - np .pi
119+ print ("Initial x: %.2f m\n Initial y: %.2f m\n Initial theta: %.2f rad\n " %
120+ (x_start , y_start , theta_start ))
121+ print ("Goal x: %.2f m\n Goal y: %.2f m\n Goal theta: %.2f rad\n " %
122+ (x_goal , y_goal , theta_goal ))
123+ move_to_pose (x_start , y_start , theta_start , x_goal , y_goal , theta_goal )
124+
106125
107126if __name__ == '__main__' :
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
114- print ("Initial x: %.2f m\n Initial y: %.2f m\n Initial theta: %.2f rad\n " % (x_start , y_start , theta_start ))
115- print ("Goal x: %.2f m\n Goal y: %.2f m\n Goal theta: %.2f rad\n " % (x_goal , y_goal , theta_goal ))
116- move_to_pose (x_start , y_start , theta_start , x_goal , y_goal , theta_goal )
127+ main ()
0 commit comments