Pointing turtlebot to the target position before moving
I need to move my turtlebot to a specific point with the specified velocity. I don't want my turtlebot to make angular movement while it is moving, because, angular movement in between is causing additional travel time. So I am trying to set the turtle bot point towards the goal before it starts moving, but I couldn't do that. I robot is pointing to completely different direction. I am using gazibo simulation, given below is my code, please review and let me know where I need to make my changes.
def newOdom(msg):
global x
global y
global theta
x = msg.pose.pose.position.x
y = msg.pose.pose.position.y
rot_q = msg.pose.pose.orientation
(roll, pitch, theta) = euler_from_quaternion([rot_q.x, rot_q.y, rot_q.z, rot_q.w])
rospy.init_node("RobotController")
rospy.Subscriber("/robot1/odom", Odometry, newOdom)
pub = rospy.Publisher("/robot1/cmd_vel", Twist, queue_size = 1)
speed = Twist()
r = rospy.Rate(5)
goal = Point()
goal.x = 3.3
goal.y = 3.3
distance=sqrt(pow(goal.x-x,2) + pow(goal.y-y, 2))
inc_x = goal.x-x
inc_y = goal.y-y
angle_to_goal = atan2(inc_y, inc_x)
while abs(angle_to_goal - theta) > 0.1 :
scale=1.5
dir=(angle_to_goal-theta) /abs( angle_to_goal-theta)
angSpeed= min(0.5, abs( angle_to_goal-theta) / scale)
speed.angular.z = dir*angSpeed
speed.linear.x = 0
pub.publish(speed)
r.sleep()
speed.angular.z = 0
while distance >= 0.7:
distance=sqrt(pow(goal.x-x,2) + pow(goal.y-y, 2))
rospy.loginfo(distance)
speed.linear.x = 0.5
speed.angular.z = 0
pub.publish(speed)
r.sleep()
speed.linear.x = 0.0
speed.angular.z =0.0
pub.publish(speed)