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

subscribing and publishing to a twist message [closed]

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

uzair gravatar image

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

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
    #added
    from geometry_msgs.msg import Twist 


    def callback(data):

    global msg
    if data.data=="Unknown":

      pub_ = rospy.Publisher('cmd_vel', Twist)
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        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))
        pub_.publish(msg)

    elif data.data=="Check":
        rospy.loginfo("Classifiers output: %s in check" % data.data)
    else:      
        rospy.loginfo("Classifiers output: %s and not unknown or check" % data.data)


def listener():

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


if __name__ == '__main__':


     listener()
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

Comments

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

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

sorry i didnt know about the indentation..

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

1 Answer

Sort by ยป oldest newest most voted
0

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

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

Comments

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 -0600 )edit
1

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 -0600 )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 -0600 )edit

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

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

Question Tools

1 follower

Stats

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

Seen: 15,005 times

Last updated: Feb 22 '14