gpt4 book ai didi

c++ - ROS:在 nodehandle.subscribe 中使用 lambda 作为回调

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:02 26 4
gpt4 key购买 nike

我希望能够通过 lambda 定义回调,但我无法获得任何匹配的函数签名。

std::function<void(const sensor_msgs::PointCloud2)> callback = 
[&mycapture](const sensor_msgs::PointCloud2 msg) {
// do something with msg and capture
};

auto sub = ros_node.subscribe("/sometopic", 1, callback)

我无法让它工作,因为订阅需要一个函数指针。我想用 ROS 做的事情是可能的吗?也就是说,我可以将带有捕获的 lambda 传递给订阅方法吗?

最佳答案

一行:

sensor_msgs::PointCloud2 pcl;
auto sub = nh.subscribe<sensor_msgs::PointCloud2>("scan", 1, [&](const sensor_msgs::PointCloud2ConstPtr& msg){pcl=*msg;});

或者使用 auto 类型的显式 multi-liner 可以让你省去很多回调的麻烦:

auto cb = [&](const sensor_msgs::PointCloud2ConstPtr& msg) {
pcl=*msg;
};
auto sub = nh.subscribe<sensor_msgs::PointCloud2>("scan", 1, cb);

关于c++ - ROS:在 nodehandle.subscribe 中使用 lambda 作为回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45340189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com