Callback function and Threading
Hello everyone,
I am having a callback function which is supposed to receive the data and to assign it to a local variable. Until here, everything is fine.
However, after the assignment, when I want to "process" the data (that is, taking out some entries or such primitive operations), the callback skips some of the data and leaves some parts of it. I suspect the problem is related to threading issue. I know, I am not supposed to "process" the data in the callback, but even when I create a separate function for that, when I call the function after the call back, it doesn't get invoked, so nothing happens. I either place it in the wrong place, or something else is going on.
Here is my callback:
void multibeamCallback(const sensor_msgs::LaserScan::ConstPtr& data)
{
// get the beam ranges from the callback msg into the local vector
ranges = data->ranges;
minimum_beam_range = data->range_min;
}
and the function to process the received data:
void correlate()
{
for (int i=0; i < ranges.size(); i++)
{
// the beams 3599 and 3600 are errornous, they always have the range of 0, therefore they need to be taken out from the array
if(ranges[i] == minimum_beam_range)
ranges.erase(ranges.begin() + (i-1));
// from the ranges, filter in the beams which do not travel the max. range, which means they get blocked by some obstacle
// if the range of a beam is not the maximum value, it means that the beam was blocked by an obstacle, hence the detection
if(ranges[i] < maximum_beam_range)
{
blockedBeams.push_back(make_pair(i, ranges[i])); // store the beam info in another vector for further use, i.e. beam id: 190, range:34
}
}
}
And finally, my main:
int main(int argc, char **argv)
{
ros::init(argc, argv, "RadarListener");
ros::NodeHandle n("~");
ros::Subscriber sub = n.subscribe("/multibeam", 1000, multibeamCallback);
correlate();
ros::spin();
return 0;
}
Maybe the error is with me using spin()
in a wrong way or some threading issue which I skip.
Any thoughts?