我希望能够通过 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/