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

Revision history [back]

Hi @nm there are few issues in your code. I will point a couple to get you started:

pose_subscriber = rospy.Subscriber("/turtle1/pose", Pose, poseCallback) syntax error missing 't'

Replace rospy.spin() with rate.sleep()

I suggest to create a separate node as best practice to subscribe to your publisher but if you have it in the same node, it needs to be inside the while loop otherwise you only call subscribe just once.

You will need to work on the logic when to change direction to do complete infinity loop.

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

x=0
y=0
theta=0

def poseCallback(pose_message):
    global x,y,theta
    x= pose_message.x
    y= pose_message.y
    theta= pose_message.theta

def move_circle():

    rospy.init_node('node_turtle_revolve', anonymous=True)
    # Create a publisher which can "talk" to Turtlesim and tell it to move
    pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=1)

    # Create a Twist message and add linear x and angular z values
    move_cmd = Twist()
    move_cmd.linear.x = 1
    move_cmd.linear.y = 0
    move_cmd.linear.z = 0
    move_cmd.angular.x = 0
    move_cmd.angular.y = 0
    move_cmd.angular.z = 1

    rate = rospy.Rate(10)


    while not rospy.is_shutdown():
        pub.publish(move_cmd)

        pose_subscriber = rospy.Subscriber("/turtle1/pose", Pose, poseCallback)

# Work on the logic how to change direction...

##        startx=x
##        starty=y
##        bgn=0
##
##        print(x,y,bgn)
##        
##        if(startx == x and starty == y):
##            bgn=bgn+1
##        if(bgn!=1):
##            move_cmd.angular.z=-1
##        if(bgn!=2):
##            break

        rate.sleep()
        #rospy.spin()        

if __name__ == '__main__':
    try:
        move_circle()
    except rospy.ROSInterruptException:
        pass