Robotics StackExchange | Archived questions

Getting multiple values of Scan from Callback Function

I have made a subscriber to pioneer/laser/scan for the scan values of laser on P3AT.

When I run the subscriber, it just doesn't stop. It keeps on running. I want only a single value of the scan i.e. the ranges[] array even if the robot is standing.

And also the value of any index in ranges[] array fluctuates ranges[i] - 0.2 to ranges[i] + 0.2

How to get a single valued array ranges[] when the robot is not moving? I need only one value for one index so that I can process it further.

P.S. I am using these lines of code and obstaclesCallBack is my function for printing the scan values. I am attaching an image where I am printing the ranges[180] value. Notice that it is fluctuating even if the robot is standing.

ros::NodeHandle laser_n;
ros::Subscriber sub = laser_n.subscribe<sensor_msgs::LaserScan>("pioneer/laser/scan", 1000, obstaclesCallback);

void obstaclesCallback(const sensor_msgs::LaserScan::ConstPtr& msg) {
std::cout<<msg->ranges[180]<<"\n";
}

image description

Asked by sumant on 2016-10-16 01:58:45 UTC

Comments

Answers

If you only want a single message, you could use waitformessage. This is a bit slower as a new subscriber has to be created, but it gives you a single message without having to define a callback.

Asked by NEngelhard on 2016-10-16 05:37:30 UTC

Comments

Another option is only using the callback message once when the robot is not moving, then wait until you move again to use the callback message. This can be accomplished with flags in the callback message that update when you move or stop moving.

Asked by JoshMarino on 2016-10-16 07:44:54 UTC