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

subscribing and publishing to a twist message [closed]

asked 2014-02-21 16:29:04 -0500

uzair gravatar image

updated 2016-10-24 08:37:10 -0500

ngrennan gravatar image

I am trying to subscribe to a topic depending on which I have different values to be published to a twist message. I am using the following code. However when I run this node and check the topic with rostopic echo cmd_vel , i see that the value of 2 is not being assigned to msg.linear.x which remains equal to zero. What am I doing wrong?

    #!/usr/bin/env python
    import rospy
    from std_msgs.msg import String
    from geometry_msgs.msg import Twist 

    def callback(data):

    global msg

      pub_ = rospy.Publisher('cmd_vel', Twist)
        rospy.loginfo("Classifiers output: %s in unknown" %
        msg.linear.x = 2
        msg.linear.y = 0
        msg.linear.x = 0
        msg.angular.z = 0
        speed = 0.4 
        rospy.loginfo("checking for cmd" + str(msg.linear))

        rospy.loginfo("Classifiers output: %s in check" %
        rospy.loginfo("Classifiers output: %s and not unknown or check" %

def listener():

     global msg
     rospy.init_node('listener', anonymous=True)
     msg = Twist()
     rospy.Subscriber("chatter", String, callback)

if __name__ == '__main__':

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by uzair
close date 2014-02-22 21:11:57


Format your code correctly; all lines should be indented an additional 4 spaces.

forrestv gravatar image forrestv  ( 2014-02-22 08:16:04 -0500 )edit

sorry i didnt know about the indentation..

uzair gravatar image uzair  ( 2014-02-22 09:07:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-02-21 17:52:51 -0500

forrestv gravatar image

You should be making your publisher within the global scope, not within the callback. (Though that wouldn't create the problem you describe; it would result in no messages being published.)

edit flag offensive delete link more


BUt the message is being published. But linear.x=0 is what i see..I dont understand why

uzair gravatar image uzair  ( 2014-02-22 07:30:13 -0500 )edit

You set msg.linear.x = 2, and then two lines later you reset it to 0

ahendrix gravatar image ahendrix  ( 2014-02-22 09:10:03 -0500 )edit

Hah, yep ... I'm going to blame missing that on the (previously) horrible code formatting.

forrestv gravatar image forrestv  ( 2014-02-22 09:26:36 -0500 )edit

I dont know how i didnt see that! thank you so much guys!

uzair gravatar image uzair  ( 2014-02-22 16:42:13 -0500 )edit

Question Tools

1 follower


Asked: 2014-02-21 16:29:04 -0500

Seen: 14,584 times

Last updated: Feb 22 '14