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

Revision history [back]

click to hide/show revision 1
initial version

As explained in the rosserial_wiki on section 2.4 Arrays (and 2.3 Strings), you should store date in a local array and then give the pointer to that array.

So with this code, I could receive 240 values:

#include <ros.h>
#include <std_msgs/Float32MultiArray.h>

ros::NodeHandle  nh;
std_msgs::Float32MultiArray range_msg;
constexpr size_t DATA_SIZE = 240;
float data[DATA_SIZE];
ros::Publisher pub_range("/test",  &range_msg);

void setup()
{
delay(1000);
nh.initNode();

range_msg.data_length = DATA_SIZE;

nh.advertise(pub_range);

}

long range_time;

void loop()
{
//publish the adc value every 50 milliseconds
//since it takes that long for the sensor to stabilize

if ( millis() >= range_time ) {

    for(int i=0;i<DATA_SIZE;i++)
    {
    data[i]=i;
    }

    range_msg.data = data;

    pub_range.publish(&range_msg);

    range_time =  millis() + 50;
}

nh.spinOnce();
}

But I have noticed that I do not get the messages at 20 Hz. Setting a higher baudrate (before initNode) helps a bit:

 nh.getHardware()->setBaud(500000);

And of course, I have increased the buffers sizes to 2048.