2017-07-27 25 views
1

我希望能够通过lambda来定义回调,但是我无法获得任何匹配的函数签名。ROS:使用lambda作为nodehandle中的回调。订阅

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与捕获?

回答

1

管理让lambda运作。虽然找不到捕捉方法。相反,我把它作为参数传入。

auto *callback = static_cast<void (*)(const sensor_msgs::PointCloud2::ConstPtr &, CustomObject&)>([](const sensor_msgs::PointCloud2::ConstPtr &msg, CustomObject &o) { 
                ROS_INFO_STREAM("Received: \n"<<msg->header); 
               }); 

ros_node.subscribe<sensor_msgs::PointCloud2>(topic, 1, boost::bind(callback, _1, custom_object)) 
1

问题中的部分解决方案看起来没有静态转换就显得更清晰一些。

此代码工作正常(也与定时器,服务等):

boost::function<void (const sensor_msgs::PointCloud2&, CustomObject&)> callback = 
    [] (const sensor_msgs::PointCloud2& msg, CustomObject& obj) { 
    // do something with msg and obj 
    } 

ros::Subscriber = ros_nh.subscribe(topic, 1, boost::bind(callback, _1, obj)); 
0

我能得到与捕获这方面的工作。

T obj; 
boost::function<void (const sensor_msgs::PointCloud2&) callback = 
    [obj] (const sensor_msgs::PointCloud2& msg) { 
    // Do something here 
    } 
auto sub = ros_nh.subscribe(topic, 1, callback); 

ROS版本:靛蓝
C++ 14