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::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?

edit retag close merge delete

Sort by » oldest newest most voted

However, your correlate function is called only once and before you received any data, so it's not processing anything. I'm not sure, what "skips some data and leaves some parts of it" is supposed to mean, but this program essentially should do nothing.

You need to call you processing function from somewhere, e.g., the callback.

more

correlate function is supposed to delete 2 entries from 3600 of them, and then get some of those which have less value than 50. However, when I call it from the callback, this operation is done for only 1850 beams, and the rest is left. I guess callback function is running too fast and skipping it.

( 2016-02-22 09:31:13 -0500 )edit

You are not calling correlate on anything right now. So it can't process any data. If you call it from the callback it will always be called until everything is processed - at least as far as the code looks. This only changes if you introduce threaded spinning, which you don't.

( 2016-02-23 07:56:18 -0500 )edit