subscriber and if-else/switch-case statement [closed]
Hi there, I am new to ROS and having a bit of problems when using subscriber. I am writing a leader following function and trying to subscribe to a rostopic under a conditional statement, switch statement in this case, but it doesn't work. Key lines of the problem are shown below:
main(int argc, char **argv)
{
ros::init(argc, argv, "mycontrol_1");
ros::NodeHandle n;
ros::Subscriber h_sub = n.subscribe("/uav1/sonar_height",1000,hcontrol);
switch (squad_leader_no){
case 1:
{ros::Subscriber splitcmd_sub = n.subscribe("/uav1/split_cmd", 1000, getsplitcmd);
break;}
case 2:
{ros::Subscriber splitcmd_sub = n.subscribe("/uav2/split_cmd", 1000, getsplitcmd);
break;}
case 3:
{ros::Subscriber splitcmd_sub = n.subscribe("/uav3/split_cmd", 1000, getsplitcmd);
break;}
case 4:
{ros::Subscriber splitcmd_sub = n.subscribe("/uav4/split_cmd", 1000, getsplitcmd);
break;}
}
...
// loop rate of 35Hz
ros::Rate cycle(35);
...
In above case, subscription fails. But when I pull the subscriber out of the conditional statement, it subscribe successfully. Could anybody be helpful on this problem if know why, please? Thanks in advance.
what do you mean by "subscription fails" , you are using same callback function for all subcriptions, so for all message it will get called. I mean once you get case 1 , for next your callback will be called for all messages from uav1. I missed the scope part