I have a robot control program. The robot receives the coordinates of the target and, knowing its coordinates (x, y, theta), must turn to the target. To rotate, it needs to send the command self.geometry_msg.angular.z with a negative value (to rotate clockwise) or positive (counterclockwise). The question is how to understand which way to turn him? Now he does not always correctly select the shortest angle of rotation. How to fix it? Formula with math.atan2 found on the Internet.
angle = math.atan2(y_goal - self.y, x_goal - self.x) - self.theta if abs(angle) > 0.1: self.geometry_msg.angular.z = 0.5*angle/abs(angle) self.geometry_msg.linear.x = 0.3 else: self.geometry_msg.angular.z = 0 self.geometry_msg.linear.x = 1 self.velocity_publisher.publish(self.geometry_msg) x_goal - x target coordinate
y_goal - y coordinate of the target
self.x - the x coordinate of the robot
self.y - y coordinate of the robot
self.theta - rotation angle of the robot relative to OX
Theta angle is counted from 0 to 6.28 (counterclockwise) and up to -6.28 (hourly)
abs (angle)> 0.1 this condition can be changed, I just do not need a high accuracy of rotation, quite an accuracy of 0.1 (in radians).
math.atan2(y_goal - self.y, x_goal - self.x)and for the secondarcctg2( x_goal - self.x, y_goal - self.y). it will add to you accuracy. Perhaps this fact in the library took into account ... and maybe not. - nick_n_aself.thetainto a directing vector, then the sign of the coordinate axis Z of the vector product of this vector will give the desired direction of rotation. - Fat-Zer