0
$\begingroup$

Rosanswers logo

Hi! I need to publish a matrix as a ROS message and I was looking around for common types of messages to see how 2d arrays were used. I checked on the following links

http://ros.org/wiki/std_msgs

http://www.ros.org/wiki/common_msgs

I found many similar, but I couldn't find one with a 2D array or a matrix, only 1d arrays. Is that because I can just use a msg that has an array?

Any tutorials on this?

Just out of curiosity,trying to understand how the ROS message system handles the different possible sizes of arrays: I have seen other people using Int32MultiArray, for example, And they do

 array.data.push_back(value);

So the message does not really contain a C++ int[] but something like a vector container, and that is how it can handle different sizes?

Thanks!


Originally posted by fersarr on ROS Answers with karma: 57 on 2013-05-10

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I am posting this because it took me some time to get it working and may be it can help someone in the future:

I was able to send and receive my custom message matrix.msg

Header header
uint32 size #es cuadrada
int32[] data

By following this tutorial http://alexsleat.co.uk/2011/07/02/ros-publishing-and-subscribing-to-arrays/.

Arrays are stored as vector and that's why size doesn't matter. To iterate through them be sure to use const_iterator

vector<int>::const_iterator it;
it=matrix.data.begin();

To print the received matrix values looking like a matrix

int count=0;
for(int i=0;i<size*size;i++){
        if(count%input.size==0)
            cout<<endl;
        cout<<matrix.data[i]<<" ";
        cuenta++;
    }

Originally posted by fersarr with karma: 57 on 2013-05-10

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.