
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