Subscribing to multiple topics using one (meta)callback
Hello Guys,
I have 13 different topics of the same class (control_msgs::JointControllerState) that I would like to subscribe to using ROSCPP. If I follow the standard procedure, I would need to define 13 different callbacks, which will be pretty similar to each other. This would make my code to be more extensive and harder to maintain. Is it possible to create one common callback (like a meta callback) for all these topics? I do not need any sort of sync.
I have been thinking of creating an array of subscribers and callbacks. Using typedef I created a array of pointers to functions (the callbacks), but the problem comes when I need to assign a specific topic to each of the callbacks.
Any suggestions? Thanks in advance,
Charlie
You can achieve exactly what you want using lambda functions. I'm a bit rusty on the syntax, but you need to write a normal callback function but with an additional parameter, say string topic_name or int id. Then you use a pass a lambda to each subscribe call which passes the appropriate additional parameter.
That's the gist of it.
This answer here gives you some examples : https://answers.ros.org/question/3083...
@PeteBlackerThe3rd: that Q&A only applies to ROS 2.
That's true, I'll put together a ROS 1 example quickly
@PeteBlackerThe3rd, is there some thing similar to that for python ROS1?