ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
subscribe() returns a Subscriber object that you must hold on to until you want to unsubscribe. When all copies of the Subscriber object go out of scope, this callback will automatically be unsubscribed from this topic.
So it should work to just resubscribe to another topic, is squad_leader_no a global variable? Is its value being changed? If it's not working to just resubscribe, you can try to unsubscribe by doing:
splitcmd_sub.shutdown();
Your code would be something like (it's usually better to have local variables, so I changed squad_leader_no to local):
ros::Subscriber splitcmd_sub;
void getsplitcmd(const geometry_msgs::Point::ConstPtr& msg, int *squad_leader_no)
{
splitcmd_sub.shutdown();
//...some code to find out whom to follow
*squad_leader_no = ...;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "mycontrol_4");
ros::NodeHandle n;
ros::Rate cycle(8);
int squad_leader_no = INITIAL_LEADER;
while(ros::ok()){
// ...some other code here
if(squad_leader_no == 1){
splitcmd_sub = n.subscribe<geometry_msgs::Point>("/uav1/split_cmd", 1000, boost::bind(getsplitcmd, &squad_leader_no, _1) );
}
if(squad_leader_no == 2){ repeat }
if(squad_leader_no == 3){ repeat }
if(squad_leader_no == 4){ repeat }
// ...some code here
ros::spinOnce();
cycle.sleep();
}
return 0;
}