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

Revision history [back]

You've got a good start. The right way to do this is to create custom ROS messages for both the "struct" and the "array of structs". In the ROS .msg format, an undefined-length array is defined using the int[] intArray syntax (with no size in the array-brackets).

The specifics of how arrays are handled in ROS messages are described on this wiki page. In particular, the "array handling" section shows that variable-length arrays are interpreted as std::vector in roscpp (C++).

As an example, see the Joint Trajectory Action tutorial. In the armExtensionTrajectory function, the example builds up the goal.trajectory message, which is of type JointTrajectory and contains a variable-length array of JointTrajectoryPoints. As you can see, the variable-length array is built using the usual C++ std::vector methods (push_back, resize, etc.).

Here's a more specific example to address your case above (it's untested, so forgive any typos):

---------------------------------------
imgData.msg
  int32 upperLeft
  int32 lowerRight
  string color
  string cameraID

---------------------------------------
imgDataArray.msg
  imgData[] images

---------------------------------------
demo_pub.cpp
  ros::Publisher pub = n.advertise<my_pkg::imgDataArray>("test", 1000);

  my_pkg::imgData data;
  my_pkg::imgDataArray msg;

  data.upperleft=0; data.lowerRight=100; data.color="red"; data.cameraID="one";
  msg.images.push_back(data);
  data.upperleft=1; data.lowerRight=101; data.color="blue"; data.cameraID="two";
  msg.images.push_back(data);

  pub.publish(msg);

---------------------------------------
demo_sub.cpp
  void subCB(const my_pkg::imgDataArray::ConstPtr& msg)
  {
    for (int i=0; i<msg->images.size; ++i)
    {
      const my_pkg::imgData &data = msg->images[i];
      ROS_INFO_STREAM("UL: " << data.upperleft << "UR: " << data.upperright <<
                      "color: " << data.color << "ID: " << data.cameraID);
    }
  }

  int main(int argc, char **argv)
  {
    ...
    ros::Subscriber sub = n.subscribe("test", 1000, subCB);
    ros::spin();
  }