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

custom message for matrix or 2d array

asked 2013-05-10 20:51:19 -0600

fersarr gravatar image

updated 2013-05-10 21:34:22 -0600

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!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-05-10 22:16:38 -0600

fersarr gravatar image

updated 2013-05-10 22:20:00 -0600

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<int> 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++;
    }
edit flag offensive delete link more
0

answered 2015-06-18 14:39:10 -0600

Fi gravatar image

Hi Ferr; I read your code but I do not completely understand how to work with Multiarray for showing a 2D matrix (m*n) Would you please read what is my problem and help me?

Hi all; As I understand, the 2D matrix in the ROS in not available until now and for this reasn, a definition of a msg that its variable is a msg of 1D should be done. But, I would like to know how I can implement the following code: 1-define a 2D matrix named mat[m][n]; (m*n)

I write the following messages: array 2d.msg and array 1d.msg. Is it a problem in my definition?

array2d.msg array 1d[] mat;

array 1d.msg int8[] a;

2- Implementation of 2 nested for loop The one of simple code for do some mathematical calculation on matrix is presented in the following:

for (i=starti; i<m, i++)="" <br=""> for (j=starti; j<n, j++)="" {mat[i][j]="mat[i+1][j]+2;}&lt;/p">

Clearly, what I want to do is to do some mathematical functions on elements of matrix named mat. I write the simplest code of mathematical calculation in the previous lines. It is your kindness if you translate these lines to ROS code to give an idea to how to implement mathematical calculation with elements of 2D matrix.

Thanks for your help;

Fi

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-10 20:51:19 -0600

Seen: 10,762 times

Last updated: Jun 18 '15