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

How to publish 2D array of int type

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

CROS gravatar image

updated 2017-11-11 11:36:49 -0500

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>
//include?

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())
  {
    chatter_pub.publish(data);
    ros::spinOnce();
    loop_rate.sleep();
  }
  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

Comments

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 image gvdhoorn  ( 2017-11-11 11:07:17 -0500 )edit

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

CROS gravatar image CROS  ( 2017-11-11 11:35:50 -0500 )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 image gvdhoorn  ( 2017-11-11 14:19:09 -0500 )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 image CROS  ( 2017-11-12 08:57:33 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

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

Comments

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 image gvdhoorn  ( 2017-11-12 09:11:50 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 1,877 times

Last updated: Nov 12 '17