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

How to pass arrays in custom messages

asked 2011-09-02 09:29:54 -0500

Gideon gravatar image

updated 2011-09-05 03:19:32 -0500

joq gravatar image

Hello all, I have a node publishing temperature data from a string of 8 batteries written in python. Each battery has 8 temperature sensors, and so rather than building a message with 64 fields I have it broken down into 8 arrays each of length 8. I have written a healthMap node in C++ and I want to subscribe to the temperature data. What should my message structure look like? Python wants to use a tuple and C++ wants a vector. Does anyone have a code example that illustrates how to approach this? I also have little experience working with vectors in C++, so hopefully you could give me a sketch of what the subscriber looks like as well. Sorry for the long-winded question



edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-09-02 09:53:50 -0500

Thomas D gravatar image

updated 2011-09-02 10:04:38 -0500

This is similar to a previous question. There is a good answer there that has some example code and points to this tutorial. The tutorial has example code for publishers and subscribers.

For C++ vectors you can do something like the following (someone correct my C++ if I'm wrong please):

std::vector<double>::iterator my_iterator;
int i = 0;
for (my_iterator = msg.vector_field.begin(); msg.vector_field.end(); my_iterator++)
    my_local_battery_value[i] = msg.vector_field[my_iterator];

or follow this page.

edit flag offensive delete link more


Thanks. If my subscriber in written in C++, do I manually copy all of the fields to my local variables with a for loop?
Gideon gravatar image Gideon  ( 2011-09-02 10:06:19 -0500 )edit
so something like this wouldnt work?
Gideon gravatar image Gideon  ( 2011-09-02 10:16:38 -0500 )edit
Would this work? for(int i = 0; i<numTemps;i++){ temp[i] = msg->temp[i]; }
Gideon gravatar image Gideon  ( 2011-09-02 10:18:28 -0500 )edit
Well, you can also just copy the whole vector, no need for a for loop. Or ideally subscribe a shared pointer, that you store, then no copying is needed at all.
dornhege gravatar image dornhege  ( 2011-09-02 10:25:33 -0500 )edit
It should definitely be possible. It might even be easier than my little example code (I just haven't worked with vectors that much either).
Thomas D gravatar image Thomas D  ( 2011-09-02 10:26:29 -0500 )edit
You could replace your code with temp = msg.temp; which would copy the vector. If that's even necessary depends on what you want to do with it.
dornhege gravatar image dornhege  ( 2011-09-02 10:30:58 -0500 )edit
Ignore this ... triggered the 5 comments bug.
dornhege gravatar image dornhege  ( 2011-09-02 10:31:34 -0500 )edit

Question Tools

1 follower


Asked: 2011-09-02 09:29:54 -0500

Seen: 7,295 times

Last updated: Sep 02 '11