Ask Your Question

How to publish 2D array of int type

asked 2017-11-11 08:54:30 -0600

CROS gravatar image

updated 2017-11-11 11:36:49 -0600

How does one go about publishing a 2D array of the integer type in ROS? I am using C++ for this node i am writing.

#include "ros/ros.h"
#include <sstream>

int main(int argc, char **argv)
 ros::init(argc, argv, "talker");
 ros::NodeHandle n;
 ros::Publisher chatter_pub = n.advertise<std_msgs::int>("chatter", 1000); //Is this correct?
 ros::Rate loop_rate(10);

 //define array
 data int[2][2];
 data[0][0] = 12;
 data[1][1] = 23;
  while (ros::ok())
  return 0;

I have tried many things by now and I am really out of options. Anybody that can demonstrate this?

Simply said: I would like the above code to be checked and edited so that it compiles without errors and that it sends a 2D array of type integer.

edit retag flag offensive close merge delete


I have tried many things by now

then please tell us about that, as otherwise we run the risk of suggesting something you've already tried.

Please see the Support Guidelines.

gvdhoorn gravatar imagegvdhoorn ( 2017-11-11 11:07:17 -0600 )edit

@gdvhoorn, well honestly I would like the above way to work. This would be most convenient.

CROS gravatar imageCROS ( 2017-11-11 11:35:50 -0600 )edit

Have you worked with ROS before? I only ask because it seems strange to expect to be able to publish arbitrary primitive data types. It's possible, but typically you go through / use specialised message structures.

And you didn't answer my question: what have you tried already that didn't work?

gvdhoorn gravatar imagegvdhoorn ( 2017-11-11 14:19:09 -0600 )edit

I have worked a little with ROS before, but that was only using simple string messages. And using predefined messages aswell. I have tried using int as shown above, I tried something similar with int16 and something with int16MultiArray. I have not tried making a specialized message..

CROS gravatar imageCROS ( 2017-11-12 08:57:33 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-11-12 09:07:58 -0600

gvdhoorn gravatar image

If you really need a 2D array (instead of a list or vector), take a look at #q234028. In essence: you'd use std_msgs/Int32MultiArray with suitable values for size and stride.

Another approach -- which would let you publish (but not subscribe to) actual int[][] arrays -- could be to use message traits.

edit flag offensive delete link more


And just a note: using something like Int*MultiArray as a message doesn't convey much semantics. If the array is really actually encoding some other domain concept, it might be better to explicitly model that (with a custom message) and send that.

gvdhoorn gravatar imagegvdhoorn ( 2017-11-12 09:11:50 -0600 )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

1 follower


Asked: 2017-11-11 08:54:30 -0600

Seen: 734 times

Last updated: Nov 12 '17