ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

How to recieve an array over Publisher and Subscriber? (Python)

asked 2011-03-17 16:20:55 -0500

updated 2014-01-28 17:09:22 -0500

ngrennan gravatar image

Examples would be useful.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-03-17 18:58:54 -0500

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]

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.

edit flag offensive delete link more


Thanks! This actually helped me undrstand messages a bit more also.
GeniusGeeko gravatar imageGeniusGeeko ( 2011-03-18 02:12:28 -0500 )edit

how to do the same in c++ ?? its tells invalid type even when used std::vector

kk gravatar imagekk ( 2013-09-10 08:07:04 -0500 )edit

@kk did you manage to solve it in c++ im have the same problem, thanks

ctguell gravatar imagectguell ( 2013-11-21 07:17:11 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2011-03-17 16:20:55 -0500

Seen: 13,091 times

Last updated: Mar 17 '11