1+ """
2+ Move to specified pose
3+ Author: Daniel Ingram (daniel-s-ingram)
4+
5+ P. I. Corke, "Robotics, Vision & Control", Springer 2017, ISBN 978-3-319-54413-7
6+ """
7+
8+ from __future__ import print_function , division
9+
10+ import matplotlib .pyplot as plt
11+ import numpy as np
12+
13+ from math import cos , sin , sqrt , atan2 , pi
14+ from random import random
15+
16+ Kp_rho = 9
17+ Kp_alpha = 15
18+ Kp_beta = - 3
19+ dt = 0.01
20+
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 = []
28+
29+ plt .ion ()
30+
31+ def move_to_pose (x_start , y_start , theta_start , x_goal , y_goal , theta_goal ):
32+ """
33+ rho is the distance between the robot and the goal position
34+ alpha is the angle to the goal relative to the heading of the robot
35+ beta is the angle between the robot's position and the goal position plus the goal angle
36+
37+ Kp_rho*rho and Kp_alpha*alpha drive the robot along a line towards the goal
38+ Kp*beta*beta rotates the line so that it is parallel to the goal angle
39+ """
40+ x = x_start
41+ y = y_start
42+ theta = theta_start
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+
52+ x_diff = x_goal - x
53+ y_diff = y_goal - y
54+
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+
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
64+
65+ v = Kp_rho * rho
66+ w = Kp_alpha * alpha + Kp_beta * beta
67+
68+ if alpha > pi / 2 or alpha < - pi / 2 :
69+ v = - v
70+
71+ theta = theta + w * dt
72+ x = x + v * cos (theta )* dt
73+ y = y + v * sin (theta )* dt
74+
75+ plot_vehicle (x , y , theta , x_traj , y_traj )
76+
77+
78+ def plot_vehicle (x , y , theta , x_traj , y_traj ):
79+ T = transformation_matrix (x , y , theta )
80+ p1 = np .matmul (T , p1_i )
81+ p2 = np .matmul (T , p2_i )
82+ p3 = np .matmul (T , p3_i )
83+
84+ plt .cla ()
85+
86+ plt .plot ([p1 [0 ], p2 [0 ]], [p1 [1 ], p2 [1 ]], 'k-' )
87+ plt .plot ([p2 [0 ], p3 [0 ]], [p2 [1 ], p3 [1 ]], 'k-' )
88+ plt .plot ([p3 [0 ], p1 [0 ]], [p3 [1 ], p1 [1 ]], 'k-' )
89+
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 )
92+ plt .plot (x_traj , y_traj , 'b--' )
93+
94+ plt .xlim (0 , 20 )
95+ plt .ylim (0 , 20 )
96+
97+ plt .show ()
98+ plt .pause (dt )
99+
100+ def transformation_matrix (x , y , theta ):
101+ return np .array ([
102+ [cos (theta ), - sin (theta ), x ],
103+ [sin (theta ), cos (theta ), y ],
104+ [0 , 0 , 1 ]
105+ ])
106+
107+ if __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 )
0 commit comments