How to exclude "inf(inite)" values when checking laser values?

I am an IT admin learning python/ROS and when i hit this script I came up with the following:

from robot_control_class import RobotControl

rc = RobotControl()

laserlist = rc.get_laser_full()
biggest = float(0)
spud = laserlist[0]

for laser in laserlist:
    if laser != spud:
        distance = float(laser)
        if biggest < distance:
            biggest = distance
            print("new longer distance is: " + str(biggest))
        else:
            print(str(laser) + "Was less than existing distance: " + str(biggest))
    else:
        print("no distance found")

print("The biggest distance found was: "+ str(biggest))

I couldn’t figure out how to test for ‘INF’ without finding a potato or ‘spud’ laser with a bad value and using that as a !=.

When I looked further on an saw the MUCH simpler result I ran the example code feeling very silly… and it came back as “('The higher value in the list is: ', inf)” which, while mechanically true is not useful.
I reset my environment to default and got"The biggest distance found was: 2.92194676399" on my own test and got an inf from the provided code.

Did i miss something about putting the bot in a box of walls?

Hi @dave.m.mckay,

You’re right, you need to consider the inf value, which represents points where the laser sensor is not seeing anything beyond 30 meters (the range of the sensor). Actually the biggest technically.

However, the biggest “useful” concrete distance is actually the biggest number that is not an inf, like you said.


By the way, welcome to the community!

Thanks Bayodesegun. I am considering subscribing to the Robot Ignite materials and so far so good!

I figured out thats where the value was coming from,'that is why I made the spud value. The problem I had was how do exclude this as a valid value without finding a known version first? float(laser) = ínf didnt seem to do anything.

Even the example code has the same problem of returning the biggest inf result, which as described above is correct, but not the desired information for this part of the course.

Hi @dave.m.mckay, you can try the following to replace all infs with zero.

from numpy import array, inf
#...
laserlist = array(laserlist)
laserlist[laserlist == inf] = 0

Hi @dave.m.mckay,

A good way of ignoring laser values with infinite is checking whether the laser value is inf.
In your code below:

distance = float(laser)
if biggest < distance:
    biggest = distance

this could be achieved with (note and distance != inf):

inf = float('inf')

distance = float(laser)
if biggest < distance and distance != inf:
    biggest = distance

This should solve the problem.

Please let us know in case you have more questions.

A post was split to a new topic: Python tests are graded as incorrect - please help!

HI, going further on that, I want just to get the distance value which is not infinite…

from robot_control_class import RobotControl 

ob1 = RobotControl()

distances = ob1.get_laser_full()

max_value = 0

for ite in distances:

    if (ite > max_value) and (ite != inf) :

        max_value = ite

print (max_value)

I got an error :slight_smile:
Traceback (most recent call last):
  File "test_for.py", line 10, in <module>
    if (ite > max_value) and (ite != inf) :
NameError: name 'inf' is not defined

What’s wrong?

HI, I understood that inf is a string, so I just changed by:

from robot_control_class import RobotControl 

ob1 = RobotControl()

distances = ob1.get_laser_full()

max_value = 0

for ite in distances:

    

    if (ite > max_value) and (str(ite) != "inf") :

        max_value = ite

print (max_value)
1 Like