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

Revision history [back]

This tutorial might be what you're after. In general, arrays are not much different than anything else--you just need a msg that has an array in it. Suppose e.g. you have a message file, your_package/Foo.msg:

int32[100] some_integers # array of 100 int32's
float64[] some_floats # variable-sized array of floats

Then a publisher would look like:

from your_package.msg import Foo
mypub = rospy.Publisher('mytopic', Foo)
msg_to_send = Foo()
msg_to_send.some_integers = range(100)
msg_to_send.some_floats = [1.0, -3.14, 42.0]
mypub.publish(msg_to_send)

A subscriber could look like:

from your_package.msg import Foo

def mytopic_callback(msg):
    print "Here are some integers:", str(msg.some_integers)
    print "Here are some floats:", str(msg.some_floats)

mysub = rospy.Subscriber('mytopic', Foo, mytopic_callback)

This isn't fully working code--you'll need to initialize the nodes, spin, etc but hopefully you get the idea.