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

using same callback function with more than one subs node

asked 2018-04-20 03:44:58 -0500

Nebula gravatar image

updated 2018-04-20 03:54:48 -0500

I want to use information received from the different nodes in one callback function. I am not sure. For example my callback function

void follow(const geometry_msgs::PoseStamped::ConstPtr& fmsg, const geometry_msgs::PoseStamped::ConstPtr& gmsg ){ #and do something with than info}

Subscribers

ros::Subscriber pos_sub_uav1 = nh_uav1.subscribe<geometry_mgs::PoseStamped>
        ("uav1/mavros/local_position/pose", 10, follow);

ros::Subscriber pos_sub_uav2 = nh_uav2.subscribe<geometry_mgs::PoseStamped>
        ("uav2/mavros/local_position/pose", 10, follow);

One way I think of is to make separate callback functions for each subscriber node and then receive messages from corresponding nodes, store info in some global variable and the use other function for manipulation of those variables or there is a better way to do this.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-04-20 04:52:04 -0500

Tom Moore gravatar image

updated 2018-04-20 04:52:26 -0500

You could do something like this:

ros::Subscriber pos_sub_uav1 = nh.subscribe<geometry_mgs::PoseStamped>("uav1/mavros/local_position/pose", 10, boost::bind(&follow, _1, "uav1"));
ros::Subscriber pos_sub_uav2 = nh.subscribe<geometry_mgs::PoseStamped>("uav2/mavros/local_position/pose", 10, boost::bind(&follow, _1, "uav2"));
...
void follow(const geometry_msgs::PoseStamped::ConstPtr& msg, const std::string &uav) { }

You might also want to just use a time sychronizer.

edit flag offensive delete link more

Comments

can I get messages in callback function from both(or many) subscribers simultaneously?

Nebula gravatar image Nebula  ( 2018-04-21 01:23:48 -0500 )edit

If you use the time synchronizer, yes.

Tom Moore gravatar image Tom Moore  ( 2018-04-21 02:51:56 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-20 03:44:58 -0500

Seen: 457 times

Last updated: Apr 20 '18