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

As per @gvdhoorn's comment, adding a delay between between Publisher and publish makes it work.

#!/usr/bin/env python  
import rospy
import std_msgs

if __name__ == "__main__":
    rospy.init_node('test_py')
    pub = rospy.Publisher('/topic', std_msgs.msg.Int32, queue_size=1)
    rospy.sleep(2)
    pub.publish(0)

As per @gvdhoorn's comment, adding a delay between between Publisher and publish makes it work.

#!/usr/bin/env python  
import rospy
import std_msgs

if __name__ == "__main__":
    rospy.init_node('test_py')
    pub = rospy.Publisher('/topic', std_msgs.msg.Int32, queue_size=1)
    rospy.sleep(2)
    pub.publish(0)

Or better yet, check that a connection is established first

#!/usr/bin/env python  
import rospy
import std_msgs

if __name__ == "__main__":
    rospy.init_node('test_py')
    pub = rospy.Publisher('/topic', std_msgs.msg.Int32, queue_size=1)
    while pub.get_num_connections() == 0:
        print("no connections")
        rospy.sleep(0.2)
    print("num connections = " + str(pub.get_num_connections()))
    pub.publish(0)