Ask Your Question
1

subscriber and if-else/switch-case statement [closed]

asked 2014-08-11 09:25:02 -0600

PJ gravatar image

updated 2014-08-11 10:14:38 -0600

BennyRe gravatar image

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.

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by PJ
close date 2014-08-11 15:49:45.935044

Comments

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

bvbdort gravatar imagebvbdort ( 2014-08-11 10:16:38 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
6

answered 2014-08-11 10:19:58 -0600

BennyRe gravatar image

Read the Publisher Subsriber Tutorial carefully:

When all copies of the Subscriber object go out of scope, this callback will automatically be unsubscribed from this topic.

And this is exactly what happens to you. The subscriber goes out of scope and unsubscribes.

Oh I just have seen even your variable splitcmd_sub goes out of scope after each case.

Have a look at C++ scoping.

edit flag offensive delete link more

Comments

I have seen what happens and thanks a lot!

PJ gravatar imagePJ ( 2014-08-11 15:46:48 -0600 )edit
3

answered 2014-08-11 10:50:00 -0600

l0g1x gravatar image

updated 2014-08-11 10:58:13 -0600

Create the variable outside of the switch statement and initialize it within the switch statements. Use one subscriber variable for you cmd_vel, and now this way you wont go out of scope.

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);
 ros::Subscriber splitcmd_sub;

switch (squad_leader_no)
{

case 1:
       splitcmd_sub = n.subscribe("/uav1/split_cmd", 1000, getsplitcmd);
       break;

case 2:
       splitcmd_sub = n.subscribe("/uav2/split_cmd", 1000, getsplitcmd);
       break;

case 3:
       splitcmd_sub = n.subscribe("/uav3/split_cmd", 1000, getsplitcmd);
       break;

.....
}
edit flag offensive delete link more

Comments

I have seen what the problem is and thanks for your comment!

PJ gravatar imagePJ ( 2014-08-11 15:48:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-08-11 09:25:02 -0600

Seen: 975 times

Last updated: Aug 11 '14