C++ 您将如何在结构向量的 ROS 中发布消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15992990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:58:31  来源:igfitidea点击:

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

c++ros

提问by joshualan

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?

我想发布一个包含两个整数和两个字符串的结构体长度未知的向量。ROS 中是否有发布者和订阅者可以做到这一点?

If not, I've been looking at the tutorial of how to create custom messagesand I figure I can make one .msgfile containing:

如果没有,我一直在查看如何创建自定义消息教程,我想我可以制作一个.msg包含以下内容的文件:

int32 upperLeft
int32 lowerRight
string color
string cameraID

and another .msgfile 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 .msgfile. Furthermore, I am not sure how to even use this custom message in a C++ program.

和另一个.msg包含先前消息数组的文件。但是教程并没有给出如何使用数组的例子,所以我不知道在第二个.msg文件中放什么。此外,我什至不确定如何在 C++ 程序中使用此自定义消息。

Any tips on how to do this would be great!

关于如何做到这一点的任何提示都会很棒!

回答by Avio

Just to expand a little bit what @Sterlingalready explained...

只是为了扩展一点@Sterling已经解释过的内容......

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

如果您有一个名为“test_messages”的项目(以及目录),并且您有以下两种类型的消息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:

然后,您可以编写此 C++ 代码:

#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);

回答by Sterling

Let's say your first msg is called MyStruct. To have a msg that is an array of MyStructs, you would have a .msg with the field:

假设您的第一个 msg 名为 MyStruct。要拥有一个 MyStructs 数组的 msg,您将拥有一个带有该字段的 .msg:

MyStruct[] array

Then in the code you make a MyStruct and set all the values:

然后在代码中创建一个 MyStruct 并设置所有值:

MyStruct temp;
temp.upperLeft = 3
temp.lowerRight = 4
temp.color = some_color
temp.cameraID = some_id

Then to add MyStructs to an array your array in the second .msg type, you can use push_back (just like with std::vector):

然后要将 MyStructs 添加到第二个 .msg 类型的数组中,您可以使用 push_back (就像使用 std::vector 一样):

MySecondMsg m;
m.push_back(temp);
my_publisher.publish(m);