Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you publish a message in ROS of a vector of structs?

Tags:

c++

ros

I want to publish a vector of unknown length of structs that contain two integers and two strings. Is there a publisher and subscriber in ROS that can do this?

If not, I've been looking at the tutorial of how to create custom messages and I figure I can make one .msg file containing:

int32 upperLeft
int32 lowerRight
string color
string cameraID

and another .msg file containing an array of the previous messages. But the tutorial does not give an example of how to use arrays, so I do not know what to put in the second .msg file. Furthermore, I am not sure how to even use this custom message in a C++ program.

Any tips on how to do this would be great!

like image 239
joshualan Avatar asked Apr 13 '13 21:04

joshualan


People also ask

How do I add messages to ROS?

Creating a msg Let's define a new msg in the package that was created in the previous tutorial. Note that at build time, we need "message_generation", while at runtime, we only need "message_runtime". Open CMakeLists. txt in your favorite text editor (rosed from the previous tutorial is a good option).

What is a message in ROS?

A message is a simple data structure, comprising typed fields. Standard primitive types (integer, floating point, boolean, etc.) are supported, as are arrays of primitive types. Messages can include arbitrarily nested structures and arrays (much like C structs).


1 Answers

Just to expand a little bit what @Sterling already explained...

If you have a project (and thus directory) called "test_messages", and you have these two types of message in test_messages/msg:

#> cat test.msg 
string first_name
string last_name
uint8  age
uint32 score

#> cat test_vector.msg 
string vector_name
uint32 vector_len         # not really necessary, just for testing
test[] vector_test

You can then write this C++ code:

#include "test_messages/test.h"
#include "test_messages/test_vector.h"

...

  ::test_messages::test test_msg;

  test_msg.age          = 29;
  test_msg.first_name   = "Firstname";
  test_msg.last_name    = "Lastname";
  test_msg.score        = 79;

  test_pub.publish(test_msg);


  ::test_messages::test_vector test_vec;

  test_vec.vector_len    = 5;
  test_vec.vector_name   = std::string("test vector name");

  for (int idx = 0; idx < test_vec.vector_len; idx++)
  {
      test_msg.age          = 50;
      test_msg.score        = 100;
      test_msg.first_name   = std::string("aaaa");
      test_msg.last_name    = std::string("bbbb");

      test_vec.vector_test.push_back(test_msg);
  }

  test_vector_pub.publish(test_vec);
like image 94
Avio Avatar answered Oct 05 '22 00:10

Avio