I have been trying to get the minimum distance from laser scan data. My code is as follows: Here, "msg" is the "const sensor_msgs::LaserScan::ConstPtr &msg" from the callback.
int minIndex = 540;
int maxIndex = 1080;
for (int i = minIndex + 1; i < maxIndex; i++)
{
if (((msg->ranges[i] <= msg->ranges[minIndex]))&& msg->ranges[i] > 0)
{
minIndex = i;
}
}
ROS_INFO_STREAM(msg->ranges[800]);
ROS_INFO_STREAM((double)msg->ranges[minIndex]);
Now, I checked the stream for these values, and got these:
[ INFO] [1531360195.851970761, 15.721000000]: 3.42491
[ INFO] [1531360195.895046530, 15.754000000]: 3.42411
[ INFO] [1531360195.922127253, 15.774000000]: 3.41909
[ INFO] [1531360195.958390861, 15.802000000]: 3.42099
[ INFO] [1531360195.982909709, 15.822000000]: 3.42179
[ INFO] [1531360196.011697987, 15.846000000]: 3.42434
If we look at the third and fourth value, the third value is lower than the minimum I found. I randomly chose index 800, but I guess it is enough to prove that my code has some problem. Also, i keep getting the index 540, which is my first value as the minimum value.
My guess is that the laser scan data coming in is float32, but the check is somehow truncating that value to a fixed digit rounded off value. or is it due to the subscription rate, my values may be overwritten?
-Adi
Originally posted by fabritya on ROS Answers with karma: 13 on 2018-07-11
Post score: 0
Original comments
Comment by Delb on 2018-07-12:
First, you should add text in the ROS_INFO it's easier to understand the outputs.
Secondly, where are you executing this code ? Is it inside the callback ? If not then it's possible that the msg->ranges[]
is updated during the for loop
leading to those kind of errors.
Comment by Delb on 2018-07-12:
You should save in another variable ranges_temp = msg->ranges
and then execute the for loop
so that you are sure the variable ranges_temp
won't be updated during the loop.