ROS subscribe callback - using boost::bind with member functions

8k Views Asked by At

I'm trying to subscribe to different topics in ROS (one for every vehicle that pops up) using the same callback for all of them. The idea is that boost::bind will pass the topic name as an additional argument so I know which vehicle I should access in the callback.

The problem is that, even though I've gone through multiple questions on the topic, none of the solutions seem to work.

Basically, I have the following class VOBase containing a std::map<std::string, VOAgent*> agents_ with a member function as follows:

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.
}

Which I'm calling through this subscription:

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));
}

However, I'm getting a template argument deduction/substitution failed with all the candidates and this error:

mismatched types ‘std::reference_wrapper<_Tp>’ and ‘VO::VOBase*’
                    typename add_cv<_Functor>::type&>::type>()(

I've tested a number of the solutions out there, e.g. using std::ref(this) to get a std::reference_wrapper<_Tp> instead of a VO::VOBase* (the reference doesn't survive though: use of deleted function), using boost::bind instead of std::bind (but it should be all the same since C++11), with and without the ...::ConstPtr for the ROS message in the callback function arguments (and in the subscribe<acl_msgs::ViconState::ConstPtr>, etc. So I'm just juggling with partial solutions and their permutations here...

Any clues?

1

There are 1 best solutions below

2
On BEST ANSWER

I haven't looked into the specifics of the code you show (it's hard to figure out the information not shown and deduce what's required).

However, last time I helped someone with ROS subscriptions and the type-deduction, it was apparent that the handler should take a shared_ptr to the message. This might help you get started seeing a solution: