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

PJ's profile - activity

2018-06-12 08:51:07 -0500 marked best answer subscriber and if-else/switch-case statement

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.

2018-06-12 08:51:01 -0500 received badge  Student (source)
2017-09-25 09:09:22 -0500 received badge  Famous Question (source)
2015-12-03 10:29:54 -0500 received badge  Taxonomist
2014-12-21 00:11:12 -0500 received badge  Famous Question (source)
2014-08-16 12:53:31 -0500 received badge  Enthusiast
2014-08-15 17:01:44 -0500 commented question [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

It was my own mistake that caused the problem, and sorry about that! Anyway, thanks a lot for your time!

2014-08-15 16:59:58 -0500 received badge  Commentator (source)
2014-08-15 16:59:58 -0500 commented answer [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

Thanks a lot for your time, t.pimentel!!! I have found where I did wrong, and it was a careless mistake. The comparison I made between squad_leader_no and integer value was in different format. Sorry about wasting your time. :/

2014-08-15 12:27:03 -0500 commented answer [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

well, it does not help, either. The weird thing is, it could unsubscribe using shutdown() within callbacks and removing all those if statements in while(). But once I wanted it to resubscribe with if statements in while(), it seemed no unsubscribing and resubscribing happened, it just stayed on initial subscription.

2014-08-14 20:17:26 -0500 received badge  Famous Question (source)
2014-08-14 20:06:28 -0500 commented answer [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

I am wondering if the frequency the loop spinning will be a potential reason causing this problem, like it needs time to proceed received data before subscribing again.

2014-08-14 20:03:20 -0500 commented answer [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

hi t.pimentel, I have tried boost::bind and defined squad_leader_no as local, but still got the same results, topics were not chosen and resubscribed then. But when I put 'uav1/split_cmd' as initial condition before while(ros::ok()) and remove all the if statements in while(), it could successfully be shut down.

2014-08-14 17:00:51 -0500 commented question [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

I have tried those, but it did not work

2014-08-14 16:59:41 -0500 commented answer [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

yes, the squad_leader_no is a global variable so far and its value changes. I will try your code first and let know if the problem has been solved. Thank you.

2014-08-14 16:13:35 -0500 received badge  Notable Question (source)
2014-08-14 15:40:03 -0500 received badge  Popular Question (source)
2014-08-14 14:41:07 -0500 commented question [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

to be more clear, the publisher split_cmd has been defined and publishing in the same .cpp file

2014-08-14 14:26:53 -0500 asked a question [roscpp] unsubscribe within a callback and resubscribe in while(ros::ok)

Hi there, as new to ros and c++, I have a question to ask about unsubscribe and resubscribe. I am working on a multi-quadrotor path planning project and need to define squad leader everytime the team split into new squads. In that case, the squad member will subscribe to its leader for further commanding. But I have trouble on how to unsubscribe old leader's command and resubscribe to a new leader's. Below is the part of code that doing this little function:

ros::Subscriber splitcmd_sub;
// get split command from the leader and determine leader(s) for squad(s)
void getsplitcmd(const geometry_msgs::Point::ConstPtr& msg)

{

//...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); 

while(ros::ok()){

 // ...some other code here

  if(squad_leader_no == 1){splitcmd_sub = n.subscribe("/uav1/split_cmd", 1000, getsplitcmd);}

  if(squad_leader_no == 2){splitcmd_sub = n.subscribe("/uav2/split_cmd", 1000, getsplitcmd);}

  if(squad_leader_no == 3){splitcmd_sub = n.subscribe("/uav3/split_cmd", 1000, getsplitcmd);}

  if(squad_leader_no == 4){splitcmd_sub = n.subscribe("/uav4/split_cmd", 1000, getsplitcmd);}

// ...some code here

  ros::spinOnce();

    cycle.sleep();

  }
  return 0;
}

After a numerous tests, only once I have the subscriber resubscribed. At other times, it just seem not to work at all(no disconnection and reconnection). Also, I have tried splitcmd_sub::shutdown() within the callback, but it just did not work.

I have searched and read a lot of tutorials and questions by other people online, it seems that I will have to use boost::bind if I want to pass parameters from callback to while() in main everytime it receives new messages. Could anybody tell me what to do if I want to unsubscribe a topic and resubscribe to another one at correct times, please? Thanks a lot!

2014-08-14 14:04:13 -0500 commented answer subscriber inside if-else/switch-case statements

Thanks a lot, Murilo, but I have closed this question due to duplication. :)

2014-08-14 09:50:43 -0500 received badge  Notable Question (source)
2014-08-11 20:25:25 -0500 received badge  Popular Question (source)
2014-08-11 15:49:51 -0500 received badge  Notable Question (source)
2014-08-11 15:49:08 -0500 received badge  Scholar (source)
2014-08-11 15:48:54 -0500 received badge  Supporter (source)
2014-08-11 15:48:52 -0500 commented answer subscriber and if-else/switch-case statement

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

2014-08-11 15:46:48 -0500 commented answer subscriber and if-else/switch-case statement

I have seen what happens and thanks a lot!

2014-08-11 11:42:29 -0500 received badge  Popular Question (source)