ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Different callback function for a topic with different namespaces?

asked 2012-11-21 15:23:49 -0500

Ammar gravatar image

updated 2012-11-22 23:04:32 -0500

Lorenz gravatar image

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!

edit retag flag offensive close merge delete

Comments

2

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.

Lorenz gravatar image Lorenz  ( 2012-11-22 23:06:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2012-11-21 22:43:18 -0500

dornhege gravatar image

What do you mean with objects? Nodes?

In that case, just subscribe to topic_name in the node and when you launch the node put it in a namespace like robot_1/2... This will automatically remap the topics.

edit flag offensive delete link more

Comments

So they are not different nodes. They are merely a list of class instances. I have a class called "robot" and there are n robot objects. Each robot object must listen to the topic in its corresponding namespace. Does that clarify a bit?

Ammar gravatar image Ammar  ( 2012-11-22 00:51:00 -0500 )edit
1

That is also simple. Just pass in the name (e.g. robot_2) to your robot object and make it subscribe to the topic that you construct from that name.

dornhege gravatar image dornhege  ( 2012-11-22 02:54:59 -0500 )edit

Thats what I did and it does not quite work. During the callback it is unable to access values of other attributes encapsulated in that object. It gets garbage values for the other member variables. I am guessing the callback is happening on a new instance of the class rather than the member func.

Ammar gravatar image Ammar  ( 2012-11-22 05:41:19 -0500 )edit
1

That is not possible. Callbacks don't construct objects. You'll need to show a minimal example of your code to get some more precise help.

dornhege gravatar image dornhege  ( 2012-11-22 06:41:31 -0500 )edit

Question Tools

Stats

Asked: 2012-11-21 15:23:49 -0500

Seen: 534 times

Last updated: Nov 22 '12