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.
Originally posted by PJ on ROS Answers with karma: 30 on 2014-08-11
Post score: 0
Original comments
Comment by ahendrix on 2014-08-11:
This is a duplicate of http://answers.ros.org/question/189706/subscriber-and-if-elseswitch-case-statement/ . Please do not ask duplicate questions.