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

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

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

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

ngrennan gravatar image

Examples would be useful.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
17

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

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.

edit flag offensive delete link more

Comments

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

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

kk gravatar image kk  ( 2013-09-10 08:07:04 -0600 )edit

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

ctguell gravatar image ctguell  ( 2013-11-21 07:17:11 -0600 )edit
1

Question Tools

2 followers

Stats

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

Seen: 30,608 times

Last updated: Mar 17 '11