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

Revision history [back]

The rospy.spin() in your callback is waiting for a callback to be executed. By writing that line, you are essentially a asking ROS to handle that for you. In your publisher, you don't want to wait for callbacks. So you need to omit the spin. You will also need a loop to continually publish your data. From the ROS simple publisher tutorial your example would look like this:

import rospy
from std_msgs.msg import Float64MultiArray
#Float64MultiArray


def do_it():
    data_to_send = Float64MultiArray()
    rospy.init_node("array_publisher")
    pub=rospy.Publisher("chatters2", Float64MultiArray, queue_size=10)

    data_to_send.data = [1.2354567, 99.7890]
    #ata_to_send.data_offset = 0

    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
      pub.publish(data_to_send)
      # rospy.spin()
      r.sleep()

do_it()

The rospy.spin() in your callback listener is waiting for a callback to be executed. By writing that line, you are essentially a asking ROS to handle that for you. In your publisher, you don't want to wait for callbacks. So you need to omit the spin. You will also need a loop to continually publish your data. From the ROS simple publisher tutorial your example would look like this:

import rospy
from std_msgs.msg import Float64MultiArray
#Float64MultiArray


def do_it():
    data_to_send = Float64MultiArray()
    rospy.init_node("array_publisher")
    pub=rospy.Publisher("chatters2", Float64MultiArray, queue_size=10)

    data_to_send.data = [1.2354567, 99.7890]
    #ata_to_send.data_offset = 0

    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
      pub.publish(data_to_send)
      # rospy.spin()
      r.sleep()

do_it()