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

Revision history [back]

click to hide/show revision 1
initial version

Ok i found a solution ros subscribe cannot be in the loop too a created global variable sub3

case 2:{
      *sub3 = n.subscribe("D3toMain", 100, arrayCallback);  
      while(ros::ok() && prikaz){
      ros::spinOnce();
      }
  break;
  }

and callback function looks like this after one reading I set prikaz = 0 it stops my loop and send me back to menu and then I just for sure use sub3->shutdown(); and I shutdown subscribing. Maybe it could help somenone

void arrayCallback(const std_msgs::Float64MultiArray::ConstPtr& array)
{

    int i = 0;

    for(std::vector<double>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)

    {
        angle[i] = *it;
        ROS_INFO("Uhol klbu %d je: %lf \n ",i+1,angle[i]);
        i++;
    }
    prikaz=0;
    sub3->shutdown(); 
    return;
}

Ok i found a solution ros subscribe cannot be in the loop too too. I created a created global variable sub3

ros::Subscriber* sub3;

initialize it in main

sub3 = new ros::Subscriber;

and case looks like this

case 2:{ *sub3 = n.subscribe("D3toMain", 100, arrayCallback);
while(ros::ok() && prikaz){ ros::spinOnce(); } break; } }

and callback function looks like this after one reading I set prikaz = 0 it stops my loop and send me back to menu and then I just for sure use sub3->shutdown(); and I shutdown subscribing. Maybe it could help somenone

void arrayCallback(const std_msgs::Float64MultiArray::ConstPtr& array)
{

    int i = 0;

    for(std::vector<double>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)

    {
        angle[i] = *it;
        ROS_INFO("Uhol klbu %d je: %lf \n ",i+1,angle[i]);
        i++;
    }
    prikaz=0;
    sub3->shutdown(); 
    return;
}

Ok i found a solution ros subscribe cannot be in the loop too. I created a global variable sub3

ros::Subscriber* sub3;

initialize it in main

sub3 = new ros::Subscriber;

and case looks like this

 case 2:{
          *sub3 = n.subscribe("D3toMain", 100, arrayCallback); 
while(ros::ok() && prikaz){ ros::spinOnce(); } break; }

}

and callback function looks like this after one reading I set prikaz = 0 it stops my loop and send me back to menu and then I just for sure use sub3->shutdown(); and I shutdown subscribing. Maybe it could help somenone

void arrayCallback(const std_msgs::Float64MultiArray::ConstPtr& array)
{

    int i = 0;

    for(std::vector<double>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)

    {
        angle[i] = *it;
        ROS_INFO("Uhol klbu %d je: %lf \n ",i+1,angle[i]);
        i++;
    }
    prikaz=0;
    sub3->shutdown(); 
    return;
}