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

Callback function and Threading

asked 2016-02-22 08:56:34 -0500

Jägermeister gravatar image

updated 2016-02-22 09:29:12 -0500

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

  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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-02-22 09:26:27 -0500

dornhege gravatar image

updated 2016-02-22 09:26:44 -0500

There is no threading issues going on in your code as you don't create a thread.

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.

edit flag offensive delete link 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.

Jägermeister gravatar image Jägermeister  ( 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.

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

Question Tools

1 follower


Asked: 2016-02-22 08:56:34 -0500

Seen: 565 times

Last updated: Feb 22 '16