Same Callback with Multiple Topics
I'm working with a package that publishes the same message type on multiple different topics, one topic for each of a set of objects it relates to. e.g. say we have 5 cars, car1 through car5, this package will publish a "velocity" custom msg on the topic /cars/CARNAME. So, I will have "velocity" msgs on /cars/car1, /cars/car2, etc. topics.
In the package I am building to subscribe to these messages (in C++), I would like to use the same callback function for each of the above topics (one topic for each car). However, since the message that is sent on each topic only contains a velocity and no information about which car it's for, then when the callback runs with a received message I have no idea what car that message is for.
My question is, is there some way with the NodeHandle.subscribe() function to pass a static argument to the callback every time it is called? Something like:
nh.subscribe("cars/car1", 1000, getVelocityCallback, "car1")
nh.subscribe("cars/car2", 1000, getVelocityCallback, "car2")
nh.subscribe("cars/car3", 1000, getVelocityCallback, "car3")
where the last argument is a string that would be available in the callback function, like so:
void getVelocityCallback(const carspack::Velocity::ConstPtr& msg, std::string carname)
{
// process this message, knowing that it relates to "carname"
}
Thanks!