【发布时间】:2021-02-05 10:34:50
【问题描述】:
我目前正在编写一个 ROS 2 节点来将值从 PLC 通过 ROS 传递到可视化:
PLC 系统 --> ROS --> 可视化
由于 ROS 应该只传递数据,所以我希望能够尽可能少地配置接口。这个想法可以用 ROS 最好地实现,它是一个配置文件(.msg 文件),其中输入了变量的名称及其类型。其他一切都由此衍生。 我不可避免地遇到了这个问题:在 ROS 中,数据是通过所谓的消息传递的。这些消息是通过结构定义的,并且是从我的配置文件中自动生成的。为了给结构体中的变量赋值,我不想处理程序中硬编码的每一个变量,而是使用已知名称遍历结构体。
TLNR:变量可以用变量名来寻址吗?
我知道整个事情听起来有点混乱。我希望下面的例子能阐明我的意思:
#include <vector>
#include <string>
struct MsgFile
{
int someVariable;
int someOtherVariable;
};
using namespace std;
class Example
{
public:
vector<string> variableNames{"someVariable", "someOtherVariable"};
MsgFile message;
void WriteVariables()
{
for (auto const &varName : variableNames)
{
message."varName" = 0; //<-- pseudo code of what I'm thinking of
}
}
};
问候 蒂尔曼
【问题讨论】:
-
您是否考虑在您的机器人中使用Lua?还是GNU guile?或者写你的GCC plugin?还是生成一些 C++ 代码?另见this draft report 和DECODER