你将如何在结构向量的ROS中发布消息? [英] How would you publish a message in ROS of a vector of structs?

查看:667
本文介绍了你将如何在结构向量的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?

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

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

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

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!

推荐答案

只是展开一些 @Sterling 已解释的内容...

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

如果你有一个名为test_messages的项目(和目录),并且在 test_messages / msg 中有这两种类型的消息:

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

然后你可以写这个C ++代码:

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

这篇关于你将如何在结构向量的ROS中发布消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆