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

Float64MultiArray message type does not show up in published topic

asked 2022-02-06 13:21:35 -0500

distro gravatar image

I am trying to do a test where I publish an array to a topic /chatter2s. Here is my publisher below:

#!/usr/bin/python3
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
    pub.publish(data_to_send)
    rospy.spin()

do_it()

Here is my listener:

#!/usr/bin/python3
import rospy
from std_msgs.msg import Float64MultiArray


def callback(msg):
    rospy.loginfo(msg)

rospy.init_node("array_listener")
rospy.Subscriber("chatters2",Float64MultiArray, callback)
#rospy.sleep(3.0)
rospy.spin()

The problem is, when I run these two codes in their respective terminals, they seem to run but then nothing shows up for the listener, I do a rostopic echo /chatters2 and dont see anything coming out for that either. I check the rqt_graph and I see both nodes as well as the chatter topic, so these nodes seem to be active. whats going on?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-02-06 13:40:43 -0500

updated 2022-02-07 12:44:42 -0500

The rospy.spin() in your listener is waiting for a callback to be executed. By writing that line, you are essentially 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()
edit flag offensive delete link more

Comments

Also, since you mention ROS-melodic, you may want to change the python3 to python2

Akhil Kurup gravatar image Akhil Kurup  ( 2022-02-06 13:41:33 -0500 )edit

@Akhil Kurup Thanks! Could you please look at a related question I asked here if possible.

distro gravatar image distro  ( 2022-02-08 16:04:20 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-02-06 13:21:35 -0500

Seen: 293 times

Last updated: Feb 07 '22