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

Revision history [back]

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.