Different callback function for a topic with different namespaces?
Hi All, I have several move_base nodes launched each in a separate namespace. Now I have a list of robot objects as well. When I subscribe to a topic from namespace robot_1, I would like the callback function to be called that corresponds to robot_1 object in the list of robot objects. As of now I simply subscribe to the callbacks with different names. For ex: in the robot_1 instance I have a subscriber for /robot_1/topic_name while in the robot_2 instance I have /robot_2/topic_name These subscribers are merely functions within the robot class. Any idea how I could link the subscriber callback to the particular object instance? Really appreciate the help! Thank you
EDIT: Just an overview of my program. I have a derived class "robot", which is derived from a base class "agent". Now the process "trader" has a list of robots that are initialized. Each robot publishes an
odom to topic /robot_x/odom where x=robot_number.
Now in the "robot" constructor I initialize a NodeHandle in namespace robot_x
ros::NodeHandle nhr(robot_number);
And then I subscribe to the corresponding "odom" in a member function.
odom_sub = nhr.subscribe("odom", 100, &robot::odom_callback, this);
This works perfectly and I get the odometry for the n robots. However:
void robot::odom_callback(const nav_msgs::Odometry::ConstPtr& msg){
cout<<"robot_number"<<endl; /* Getting garbage values */
//robot_number is a variable in the base class agent
}
Please let me know if I should clarify more. Really appreciate the help!
Please do not re-type code but copy-paste it as stated in the support guidelines. The cout line above will definitely not print garbage values. It will just print the string
robot_number
.