Ask Your Question

Using Float32MultiArray with RosSerial

asked 2019-03-10 09:03:13 -0500

meat030 gravatar image

Hi All,

I'm using an OpenCR board by robotis. It is an STM32 based micro.

I have been going through their example of using rosserial to publish data to a host machine and have been successful in doing that for floats.

I am struggling to figure out how to use Float32MultiArrays with Rosserial. When I attempt to read the topic I'm publishing .. I get nothing. The ros serial server actually complains about the system bing out of sync. I suspect I have issues initialising my multi array

currently the way I'm setting up my array looks like this

I intend to have 4 elements in my array val 1 , val 2 , val3 , spare , <--- all are float32

my_status_msg.layout.dim =  (std_msgs::MultiArrayDimension *) malloc(sizeof(std_msgs::MultiArrayDimension));
 my_status_msg.layout.dim[0].label = dim0_label;

my_status_msg.layout.dim[0].size = 4;
my_status_msg.layout.dim[0].stride = 1*4;
my_status_msg.layout.data_offset = 0; = (std_msgs::Float32MultiArray::_data_type*)malloc(sizeof(std_msgs::Float32MultiArray::_data_type));

why is it so difficult to get arrays working in rosserial. There is hardly any complete examples in my search.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-03-10 10:15:15 -0500

Arrays in messages work differently in rosserial than regular ROS due to the lack of the std::vector type, they are implemented using a pointer and a separate length variable. There is a brief description of this here.

To populate an array in a message using rosserial you need to assign a value to the pointer, then set the elements of the array and also set the length, as shown below. Your code above is just lacks setting the length variable, so it still has a value of zero. This is why the array you're receiving doesn't contain anything.

float data[4];
my_msg.array = data;  // assign pointer to array.
my_msg.array[0] = 1.0;  // set elements
my_msg.array[1] = 2.0;
my_msg.array[2] = 3.0;
my_msg.array[3] = 4.0;
my_msg.array_length = 4;  // define the length of the array

Hope this helps.

edit flag offensive delete link more

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: 2019-03-10 09:03:13 -0500

Seen: 451 times

Last updated: Mar 10 '19