ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Publish and subscribe array of vector as message

asked 2017-10-19 05:00:58 -0600

rosusernli gravatar image

Hi all, I am trying to send an array of vector<points> from one node to another node as a message. The message contains this array and other message fields as well.

My cpp file has the following array vector:

 Vector<Point> distancePoints[11];

The above array of vector is the one I need to publish and subscribe.

For example, distancePoints[0] contains vector< Point > from distance 1m and so on. Point contains x and y coordinates. Point is an opencv 2D point class.

Is it possible to publish and receive such array of vector using ROS messages? Does anyone have an idea about how it can be done? What will be the structure of message header?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-10-19 11:02:40 -0600

updated 2017-10-20 14:31:29 -0600


I've created a new message to do that, including an array and another field, just to show how you can do that.

The command to create the package: catkin_create_pkg my_pkg roscpp std_msgs geometry_msgs

The message definition (my_pkg/msg/my_msg.msg)

geometry_msgs/Point[] points
uint8 another_field

In the my_pkg/package.xml file, don't forget to add the dependencies for generating the messages:


my_pkg/CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS

## Generate messages in the 'msg' folder
#   Message2.msg

## Generate added messages and services with any dependencies listed here
  geometry_msgs#   std_msgs

#  INCLUDE_DIRS include
#  LIBRARIES my_pkg
   CATKIN_DEPENDS geometry_msgs roscpp std_msgs message_runtime
#  DEPENDS system_lib


add_executable(my_publisher src/my_publisher.cpp)
add_executable(my_subscriber src/my_subscriber.cpp)

add_dependencies(my_publisher ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(my_subscriber ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

target_link_libraries(my_publisher ${catkin_LIBRARIES})
target_link_libraries(my_subscriber ${catkin_LIBRARIES})

Finally, the publisher and subscriber files:


#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#include "my_pkg/my_msg.h"

#include <vector>

struct Point {
    float x;
    float y;

int main(int argc, char **argv)
  // ROS objects
  ros::init(argc, argv, "my_publisher");
  ros::NodeHandle n;
  ros::Publisher pub = n.advertise<my_pkg::my_msg>("my_topic", 1);
  ros::Rate loop_rate(0.5);

  // the message to be published
  my_pkg::my_msg msg;
  msg.another_field = 0;

  // creating the vector
  Point my_array[11];
  Point point;
  for (int i=0; i < 11; i++) {
    point.x = i;
    point.y = i;
    my_array[i] = point;
  std::vector<Point> my_vector (my_array, my_array + sizeof(my_array) / sizeof(Point));

  // loop control
  int count = 0;
  while (ros::ok())

    msg.another_field = count;
    int i = 0;
    for (std::vector<Point>::iterator it = my_vector.begin(); it != my_vector.end(); ++it) {
        geometry_msgs::Point point;
        point.x = (*it).x;
        point.y = (*it).y;
        point.z = 0;

    ROS_INFO("%d", msg.another_field);




  return 0;


#include "ros/ros.h"
#include "my_pkg/my_msg.h"

void clbk(const my_pkg::my_msg::ConstPtr& msg) {
    ROS_INFO("%d", msg->another_field);
    ROS_INFO("first point: x=%.2f, y=%.2f", msg->points[0].x, msg->points[0].y);

int main(int argc, char **argv)
  // ROS objects
  ros::init(argc, argv, "my_subscriber");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("my_topic", 1, clbk);



If you want to see step-by-step, I've created a video ( ).

edit flag offensive delete link more



If you have problems to compile, try to compile the message before the nodes

your CMakeLists.txt does not have any add_dependencies(..) lines. That is why build ordering may not be correct.

See the catkin docs.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-19 11:07:07 -0600 )edit

Perfect, thanks for the reminder! I totally forgot about this instruction since the messages were there when I tried to compile the nodes. I'm updating the answer.

marcoarruda gravatar image marcoarruda  ( 2017-10-20 14:23:40 -0600 )edit

Thank you for your detailed answer. It was really helpful for me. The code compiled and worked perfectly. But I was not able to reach the video link. But anyway the example code helped me to do my stuff.

rosusernli gravatar image rosusernli  ( 2017-10-22 23:27:57 -0600 )edit

Is it possible to do same thing with service & client relation ?

Developer gravatar image Developer  ( 2018-06-06 14:34:42 -0600 )edit

answered 2020-11-03 03:23:59 -0600

jimmycanuck gravatar image

This tutorial solved this exact situation for me:

edit flag offensive delete link more

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-10-19 05:00:58 -0600

Seen: 25,681 times

Last updated: Oct 20 '17