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

Move the turtlebot in infinity shape (two circles) using pose messages?

asked 2021-10-23 04:07:46 -0500

nm gravatar image

updated 2021-10-26 08:52:19 -0500

lucasw gravatar image

I am trying to move the turtlebot in infinity shape (two circles) using pose messages but the turtlebot doesn't move and pose messages don't change. What am I doing wrong?

#!/usr/bin/env python

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)
    pose_subscriber = rospy.Subscriber("/turle1/pose", Pose, poseCallback)
    startx=x
    starty=y
    bgn=0


    while not rospy.is_shutdown():
        pub.publish(move_cmd)
        if(startx == x and starty == y):
            bgn=bgn+1
        if(bgn!=1):
            move_cmd.angular.z=-1
        if(bgn!=2):
            break
    rospy.spin()        

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

Comments

1

Please format your question using 101010 button to show the code. It’s hard to see if it’s an indentation problem for example

osilva gravatar image osilva  ( 2021-10-24 06:30:41 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-10-30 15:18:03 -0500

osilva gravatar image

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
edit flag offensive delete link more

Comments

subscriber should be outside the loop.

skros gravatar image skros  ( 2023-02-21 16:45:05 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-10-23 04:07:46 -0500

Seen: 398 times

Last updated: Oct 30 '21