Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

subscriber inside if-else/switch-case statements

Hi there, I am new to ros and having a bit of problem when using subscribers. Here is some lines of my code and it is for leader following:

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. However, when I pull the subscriber out of the switch-case statement, it subscribes successfully. Could anybody explain this if know why, please? Thank you in advance.