Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do i make it publish?

I am currently Trying to make this rosnode work, but I aren't sure why i am not able to do the first publish in this code, being the pub.publish(String("start")) But able to do the ones in the callback, what is wrong?

#!/usr/bin/env python

import rospy
from std_msgs.msg import Empty
from std_msgs.msg import Int8
from std_msgs.msg import String
import numpy as np

rospy.init_node('Node', anonymous=True)
pub = rospy.Publisher('/status', String, queue_size=1000)
pub.publish(String("start"))

def callback(data):
    print "Message received"
    pub.publish(String("New - status"))

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.Subscriber('control', Empty, callback)
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    print "Running"
    listener()

How do i make it publish?

I am currently Trying to make this rosnode work, but I aren't sure why i am not able to do the first publish in this code, being the pub.publish(String("start")) But able to do the ones in the callback, what is wrong?

#!/usr/bin/env python

import rospy
from std_msgs.msg import Empty
from std_msgs.msg import Int8
from std_msgs.msg import String
import numpy as np

rospy.init_node('Node', anonymous=True)
pub = rospy.Publisher('/status', String, queue_size=1000)
pub.publish(String("start"))

def callback(data):
    print "Message received"
    pub.publish(String("New - status"))

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.Subscriber('control', Empty, callback)
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    print "Running"
    listener()

How do i make it publish?Publish a message from global scope in python

I am currently Trying to make this rosnode work, but I aren't sure why i am not able to do the first publish in this code, being the pub.publish(String("start")) But able to do the ones in the callback, what is wrong?

#!/usr/bin/env python

import rospy
from std_msgs.msg import Empty
from std_msgs.msg import Int8
from std_msgs.msg import String
import numpy as np

rospy.init_node('Node', anonymous=True)
pub = rospy.Publisher('/status', String, queue_size=1000)
pub.publish(String("start"))

def callback(data):
    print "Message received"
    pub.publish(String("New - status"))

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.Subscriber('control', Empty, callback)
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    print "Running"
    listener()