Ask Your Question
0

Unable to update data field of Float32MultiArray std msg

asked 2019-04-24 13:38:32 -0500

jkarimi91 gravatar image

I am trying to publish a float array. To do this, I am utilizing the Float32MultiArray std message:

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

float values[3] = {1.0, 2.0, 3.0};

ros::NodeHandle nh;
std_msgs::Float32MultiArray array_msg;
ros::Publisher pub("array_publisher", &array_msg);

void setup() {
  nh.initNode();
  nh.advertise(pub);
}

void loop(){
  array_msg.data = values;
  pub.publish(&array_msg);
  nh.spinOnce();
  delay(1000);
}

However, when I rostopic echo the publisher, the message is empty i.e. the data field is unpopulated:

rostopic echo /array_publisher
layout:
  dim: []
  data_offset: 0
data: []
edit retag flag offensive close merge delete

Comments

On arduino, I think you also need to set the array length, but I don't remember how to do it offhand.

ahendrix gravatar imageahendrix ( 2019-04-24 14:31:57 -0500 )edit
1

Take a look at array section on the limitations of rosserial page.

jayess gravatar imagejayess ( 2019-04-24 14:36:11 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-04-25 12:55:11 -0500

jkarimi91 gravatar image

On the arduino side, array messages have an extra variable added to their class definitions used to define their length. In our case, this means that the Float32MultiArray msg has a data and data_length field; in order to set the data field we first need to set the data_length field:

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

int array_length = 3;
float values[array_length] = {1.0, 2.0, 3.0};

ros::NodeHandle nh;
std_msgs::Float32MultiArray array_msg;
ros::Publisher pub("array_publisher", &array_msg);

void setup() {
  nh.initNode();
  nh.advertise(pub);
}

void loop(){
  array_msg.data_length = array_length;
  array_msg.data = values;
  pub.publish(&array_msg);
  nh.spinOnce();
  delay(1000);
}

For more information, see the array section of the rosserial limitations page.

edit flag offensive delete link more

Comments

@jkarimi91If this solved your problem, then please mark your answer as correct by clicking on the check mark next to the answer.

jayess gravatar imagejayess ( 2019-04-25 14:22:37 -0500 )edit

@jayess I don't have enough points to accept or unaccept my own answer

jkarimi91 gravatar imagejkarimi91 ( 2019-04-25 14:32:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-04-24 13:38:32 -0500

Seen: 59 times

Last updated: Apr 25