subscribing and publishing to a twist message [closed]
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()
Format your code correctly; all lines should be indented an additional 4 spaces.
sorry i didnt know about the indentation..