ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Can any one help me to code turtle robot which moves only one revolution in circular path?

asked 2020-10-17 14:05:17 -0500

Ajay134 gravatar image

updated 2020-10-19 10:05:34 -0500

This my code I tried so many times but failed to get exact output

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose

import sys

robot_x = 0
distance = 0


def pose_callback(pose):
    global robot_x
    rospy.loginfo("Robot X = %f\n",pose.x)
    robot_x = pose.x

def move_turtle(lin_vel,ang_vel):

    global robot_x

    rospy.init_node('move_turtle', anonymous=True)
    pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)

    rospy.Subscriber('/turtle1/pose',Pose, pose_callback)

    rate = rospy.Rate(10) # 10hz

    vel = Twist()
    while not rospy.is_shutdown():

    vel.linear.x = lin_vel
    vel.linear.y = 0
    vel.linear.z = 0

    vel.angular.x = 0
    vel.angular.y = 0
    vel.angular.z = ang_vel



        rospy.loginfo("Linear Vel = %f: Angular Vel = %f",lin_vel,ang_vel)
        global distance  
        distance = 2*(3.1415)*(lin_vel/ang_vel)

        if(robot_x == distance):

             rospy.loginfo("Robot Reached destination")
         rospy.logwarn("Stopping robot")

             break


        pub.publish(vel)

        rate.sleep()

if __name__ == '_main_':
    try:
        move_turtle(float(sys.argv[1]),float(sys.argv[2]))
    except rospy.ROSInterruptException:
        pass
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-10-19 10:12:14 -0500

Couple of quick comments:

  1. This robot_x == distance is probably not a good idea. Those are both floating point numbers and they are essentially never going to be identically equal. Instead, you should do something along the lines of math.abs(robot_x - distance) < 1e-6.
  2. Once the turtlesim receives a twist, it keeps moving at the commanded twist for one second. Meaning, if you've detected that you've met the criteria mentioned in the previous point, rather than just breaking from your loop you might instead prefer to actually send a zero twist to stop the turtle (otherwise it will continue moving even after you've reached your stopping condition)
  3. There seem to be some indentation issues with your code. I'm going to assume those are copy-paste/formatting errors. However, be sure to double-check the indentation is correct.
edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-10-17 14:05:17 -0500

Seen: 117 times

Last updated: Oct 19 '20