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

Revision history [back]

click to hide/show revision 1
initial version

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function.

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function.function so you're not re-instantiating it every time your a message is published on the /ccc topic.

For example:

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


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        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("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move creation publisher
     rospy.spin()


if __name__ == '__main__':
    listener()

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function so you're not re-instantiating it every time your a message is published on the /ccc topic.topic. Although, what you most likely would want to do is create a class to do this since the use of globals isn't always the greatest of ideas.

For example:

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


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        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("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move creation publisher
     rospy.spin()


if __name__ == '__main__':
    listener()

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function so you're not re-instantiating it every time your a message is published on the /ccc topic. Although, what you most likely would want to do is create a class to do this since the use of globals isn't always the greatest of ideas.

For example:

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


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        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("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move creation instantiation of publisher
     rospy.spin()


if __name__ == '__main__':
    listener()

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function so you're not re-instantiating it every time your a message is published on the /ccc topic.

For example:

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


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        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("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move instantiation of publisher
     rospy.spin()


if __name__ == '__main__':
    listener()

Although, what you most likely would want to do is create a class to do this since the use of globals isn't always the greatest of ideas.

For example:

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


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        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("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move instantiation of publisher
     rospy.spin()


if __name__ == '__main__':
    listener()