Robotics StackExchange | Archived questions

Reading data from Laser error

I have made a script in which i am subscribing to topic /scan from my laser and i use the different ranges (angles) to detect obstacles . When i print the total of different ranges print (len(msg.ranges)) i am getting a total of 460 .

For me it sound weird cause i expected 360 in total (as its a 360 Laser scanner) .Nevermind i check at 459th (cause its not letting me use 460th )(with that piece of code : front = msg.ranges[459] *100) and i can see that it measures the distance in this range (angle) .

The problem is that when i launch the launch file i get this error :

front = msg.ranges[459] * 100  # The 100 is to convert it in cm
IndexError: tuple index out of range

I reduce the number lets say 445 . The problem solved for at least 2-3 times of testing .Then the error comes back , this time with 445 in it . I don't know why is this happening

Asked by Kostas Tzias on 2022-10-05 05:58:10 UTC

Comments

Please do not use an index directly. Before that just add a conditional statement to avoid out of range error.

Asked by ravijoshi on 2022-10-05 06:49:47 UTC

You can try to check it with iteration over tuple elements, that's it: for element in front: (do stuff)

Asked by ljaniec on 2022-10-05 11:07:10 UTC

On a side note, you reported that the length of your array is 460, and you are trying to access the 459th element, i.e., the last element of the list. In such cases, please use negative indexing in the list. For example, please use msg.ranges[−1] to select the last element. However, this discussion is out of the scope of this forum.

Asked by ravijoshi on 2022-10-05 20:32:56 UTC

Answers