如何将函数 return 值获取到要发布的 ROS 主题消息中
How to get function return values into ROS topic msg to be published
我是 ROS 的初学者。
目前在 catkin 工作区之外的 Melodic 工作。即:不使用柔荑花序
我的目标是创建一个包含 x 和 y 值的主题,然后根据我的路径预测函数的 return 值更新这些值,return 是 std::vector<Eigen::Vector2f>
包含 x 和 y 值。
这是我的代码的样子:
ros::NodeHangle nh;
ros::Publisher topic_pub = nh.advertise<geometry_msgs::Point>("mytopic/path", 1000);
int counter=0;
ros::Rate rate(10);
while (ros::ok()) {
geometry_msgs::Point msg;
msg.data = engine.getPathFunction();
topic_pub.publish(msg);
rate.sleep();
}
我设置的行 msg.data = engine.getPathFunction(); , return当然是个错误。
return 类型的 getPathFunction() 是 std::vectorEigen::Vector2f ...路径的 x 和 y 值。
处理此转换并将这些 x 和 y 值放入 msg 的最佳方法是什么?
这是 ROS 可以处理的事情吗?如果没有,是否有另一个对转换有用的库?
我应该只写自己的函数吗?如果是这样,该函数在这个例子中应该是什么样的?
处理这种转换的最好也是唯一的方法是手动进行。 Point
消息包含 (x,y,z)
字段,因此您只需自己分配这些值即可。另一件需要注意的事情是 Point
消息没有像 std_msgs
那样的 .data
字段。
geometry_msgs::Point msg;
std::vector<Eigen::Vector2f> tmp_vec = engine.getPathFunction();
msg.x = tmp_vec[0]; //Assumes x here
msg.y = tmp_vec[1]; //Assumes y here
topic_pub.publish(msg);
我是 ROS 的初学者。
目前在 catkin 工作区之外的 Melodic 工作。即:不使用柔荑花序
我的目标是创建一个包含 x 和 y 值的主题,然后根据我的路径预测函数的 return 值更新这些值,return 是 std::vector<Eigen::Vector2f>
包含 x 和 y 值。
这是我的代码的样子:
ros::NodeHangle nh;
ros::Publisher topic_pub = nh.advertise<geometry_msgs::Point>("mytopic/path", 1000);
int counter=0;
ros::Rate rate(10);
while (ros::ok()) {
geometry_msgs::Point msg;
msg.data = engine.getPathFunction();
topic_pub.publish(msg);
rate.sleep();
}
我设置的行 msg.data = engine.getPathFunction(); , return当然是个错误。
return 类型的 getPathFunction() 是 std::vectorEigen::Vector2f ...路径的 x 和 y 值。
处理此转换并将这些 x 和 y 值放入 msg 的最佳方法是什么?
这是 ROS 可以处理的事情吗?如果没有,是否有另一个对转换有用的库?
我应该只写自己的函数吗?如果是这样,该函数在这个例子中应该是什么样的?
处理这种转换的最好也是唯一的方法是手动进行。 Point
消息包含 (x,y,z)
字段,因此您只需自己分配这些值即可。另一件需要注意的事情是 Point
消息没有像 std_msgs
那样的 .data
字段。
geometry_msgs::Point msg;
std::vector<Eigen::Vector2f> tmp_vec = engine.getPathFunction();
msg.x = tmp_vec[0]; //Assumes x here
msg.y = tmp_vec[1]; //Assumes y here
topic_pub.publish(msg);