我正在尝试使用相同的回调来订阅 ROS 中的不同主题(每辆弹出的车辆对应一个主题)。这个想法是boost::bind
将主题名称作为附加参数传递,以便我知道我应该在回调中访问哪个工具。
问题是,尽管我已经回答了关于该主题的多个问题,但似乎没有一个解决方案有效。
基本上,我有以下类(class) VOBase
包含 std::map<std::string, VOAgent*> agents_
成员函数如下:
void VOBase::callback_agentState(const custom_msgs::VState::ConstPtr& vStateMsg,
std::string topic) {
// [...] regex to find agent name from topic string
std::string agent_name = match.str();
// [...] Check if agent name exists, else throw exception
// Process message
agents_[agent_name]->pos_ = vStateMsg->pose.position; // etc.
}
我正在通过此订阅调用:
void VOBase::callback_agentList(const custom_msgs::VehicleList& vehListMsg) {
// [...] New agent/vehicle found: process vehListMsg and get agent_name string
// Subscribe to VState
topic_name = agent_name + "/state_estimate";
subscribers_[topic_name] = nodeHandlePtr->subscribe<custom_msgs::VState>(topic_name, 1,
std::bind(&VOBase::callback_agentState, this, _1, topic_name));
}
但是,我得到一个 template argument deduction/substitution failed
所有候选人和这个错误:
mismatched types ‘std::reference_wrapper<_Tp>’ and ‘VO::VOBase*’
typename add_cv<_Functor>::type&>::type>()(
我已经测试了很多解决方案,例如使用 std::ref(this)
得到 std::reference_wrapper<_Tp>
而不是 VO::VOBase*
(尽管引用文献不存在: use of deleted function
),使用 boost::bind
而不是 std::bind
(但从 C++11 开始它应该完全相同),有和没有 ...::ConstPtr
对于回调函数参数(以及 subscribe<acl_msgs::ViconState::ConstPtr>
等)中的 ROS 消息,所以我只是在处理部分解决方案及其排列……
有什么线索吗?
最佳答案
我没有查看您显示的代码的细节(很难找出未显示的信息并推断出需要什么)。
但是,上次我帮助某人进行 ROS 订阅和类型推导时,很明显处理程序应该对消息采用 shared_ptr。这可能会帮助您开始寻找解决方案:
关于c++ - ROS 订阅回调 - 使用 boost::bind 和成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899300/