0
$\begingroup$

Rosanswers logo

Hello everyone,

I have multiple subscribers that share the same callback function. Since they subscribe to different previously unknown topics, I am using the ShapeShifter class.

This is how one of the subscribers would be instantiated:

sub_ = nh.subscribe(topic, 10, &MyClass::topicCallback, this);

And here is my Callback:

void MyClass::topicCallback(const topic_tools::ShapeShifter::ConstPtr& input)
{      // do stuff    }

Now I'd like to pass an additional argument to that function. I found out that this is possible using boost::bind(), so the callback would look like this:

void MyClass::topicCallback(const topic_tools::ShapeShifter::ConstPtr& input, const std::string& arg)
{      // do stuff    }

and the subscriber:

sub_ = nh.subscribe(topic, 10, boost::bind(&MyClass::topicCb, this, _1, arg));

Initializing the subscriber this way doesn't work, however. This answer suggested to template the subscriber to the correct data type, e.g.

sub_ = nh.subscribe<my_data_type>(topic, 10, boost::bind(&MyClass::topicCb, this, _1, arg));

The point is that I don't know the message type, that's why I am using the ShapeShifter in the first place. Does anyone have an idea how I can circumvent this? Like subscribing without templating or maybe getting the message type before initializing the subscriber?

Thanks a lot!


Originally posted by labude on ROS Answers with karma: 98 on 2022-10-10

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Problem solved, don't know why I didn't think of this earlier:

sub_ = nh.subscribe<topic_tools::ShapeShifter>(topic, 10, boost::bind(&MyClass::topicCallback, this, _1, topic));

does the job :)


Originally posted by labude with karma: 98 on 2022-10-10

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.