Rosanswers logo

Hi All

Can we use std_msgs MultiArray in rosserial?
I'm trying following code, but it doesn't work.
The topic has no data.

We are using rosserial of revision 133:db62c6309a50 tip.
Does anyone have suggestion?

#include <ros.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Int16MultiArray.h>

#include <WProgram.h>
#include <Servo.h>

ros::NodeHandle nh;

std_msgs::Int16MultiArray thermo;
ros::Publisher thermo_pub("thermo", &thermo);

char dim0_label[] = "thermo";
void setup()
  thermo.layout.dim = (std_msgs::MultiArrayDimension *)
  malloc(sizeof(std_msgs::MultiArrayDimension) * 2);
  thermo.layout.dim[0].label = dim0_label;
  thermo.layout.dim[0].size = 8;
  thermo.layout.dim[0].stride = 1*8;
  thermo.layout.data_offset = 0;
  thermo.data = (int *)malloc(sizeof(int)*8);

void loop()
  for(int i = 0; i < 8; i++){
    thermo.data[i] = analogRead(i);
  thermo_pub.publish( &thermo );   

the rostopic echo returns

tsuda@tsuda-laptop:~$ rostopic echo /thermo
 dim: []
 data_offset: 0
data: []

Originally posted by atsushi_tsuda on ROS Answers with karma: 91 on 2011-08-23

Post score: 4

Original comments

Comment by dornhege on 2011-08-23:
You can also indent a code block by 4 spaces to get it formatted nicely.


2 Answers 2


Rosanswers logo

Make sure you set the length of your data:

  thermo.layout.dim_length = 1;
  thermo.data_length = 8;

This is necessary since we are using plain old arrays, and not vectors (which would wreak havoc on your AVR by copying, malloc'ing, and other stuffs).

Originally posted by fergs with karma: 13902 on 2011-08-23

This answer was ACCEPTED on the original site

Post score: 5

Original comments

Comment by fergs on 2011-08-24:
Great, if you could click on the checkmark next to my answer it will mark this question as answered. Thanks!

Comment by atsushi_tsuda on 2011-08-23:
Thanks. it does work!!

Comment by achmad_fathoni on 2019-04-24:
Is data_length multiplication of all dimension size?


Rosanswers logo

I spent a day to debug this code. It keeps generate "Lost sync with device, restarting..." . Eventually get rid of line thermo.layout.dim_length = 1; fix my problem. Not quite sure what is happen here. I did not have enough time to investigate this. If anyone know the reason, please reply the post. thanks

Originally posted by jayson ding with karma: 29 on 2014-05-20

This answer was NOT ACCEPTED on the original site

Post score: 1

Original comments

Comment by pwong on 2014-06-06:
Very glad you posted this. I had the same issue. I'm using an arduino uno btw. If I figure out the reason for this, I'll post as well.

Comment by achmad_fathoni on 2019-04-24:
dim_length value should be your array dimension - 1. So in single dimension array, dim_length should be 0, the default number.


Your Answer

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