I want to make a node which should rotate the robot in a circle and then stop at its initial position. Currently I am able to move the turtlebot in a circle continuosly but dont how to stop it at its initial position. Please help.

asked 2020-10-08 02:04:19 -0600

sahil gravatar image
#!/usr/bin/env python
  import rospy
  from geometry_msgs.msg import Twist
  from turtlesim.msg import Pose

  value = 0
  initialPos = 0

  def callback(data) :
        global initialPos
        pos = initialPos
        if data.theta == pos :
             rospy.signal_shutdown('finished')

   def initialValue(data) :
        global initialPos
        initialPos = data.x

    def main() :
        global initialPos
        global value
        rospy.init_node('turtle_rotate', anonymous = True)  
        var_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
        rospy.Subscriber('/turtle1/pose', Pose, initialValue)
        rate = rospy.Rate(5)
        while not rospy.is_shutdown() : 
               obj_msg = Twist()
               obj_msg.linear.x = 1.0
               obj_msg.linear.z = 0.0
               obj_msg.angular.z = 0.5
               var_pub.publish(obj_msg)
               var_sub = rospy.Subscriber('/turtle1/pose', Pose, callback)
               rate.sleep()

     if __name__=='__main__':
        try:
           main()
        except rospy.ROSInterruptException:
           pass
edit retag flag offensive close merge delete

Comments

move the var_sub callback to outside of the while loop. I am not looking at the correctness of your solution, but you should not have a subscriber in a loop like yours.

Procópio gravatar image Procópio  ( 2020-10-08 10:49:25 -0600 )edit

ohkk. Thankyou.

sahil gravatar image sahil  ( 2020-10-08 21:56:49 -0600 )edit