Bad reading from /scan ?

asked 2022-01-12 14:12:20 -0500

Trahery gravatar image

Hello, guys I am making basic run and stop program and I found a problem. I don't know why when the call back function runs the self.l_scan = scan_data updates like it should. But when the is_obstacle member function runs. The self.l_scan is empty. I have probably some spell mistake, but I tried to look for it like for hour and can find the mistake. The topic /scan is from Turtlebot and it runs like it should. I tried via echo.

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
import math

class Robot(object):
    def __init__(self):
        # Params
        self.l_scan = LaserScan()
        self.loop_rate = rospy.Rate(10)
        self.velocity = Twist()

        self.pub = rospy.Publisher("/cmd_vel", Twist, queue_size=10)
        self.sub = rospy.Subscriber("/scan", LaserScan, self.callback)

    def callback(self,scan_data):
        self.l_scan = scan_data
        print("readed")

    def is_obstacle(self,distance):
        print(self.l_scan)
        slice_of_array = self.l_scan.ranges[174: 184+1]
        if min(slice_of_array) < distance:
            return True
        else:
            return False

    def stop(self):
        print("stop")    
        self.velocity = Twist()
        self.pub.publish(self.velocity)

    ## move until find obstacle closer than 0.6, laser [-5,+5] degrees
    def move(self):
        self.velocity.linear.x = 1

        while not self.is_obstacle(0.6):
            print("moving")
            self.pub.publish(velocity)
            self.loop_rate.sleep()
        self.stop()

## rotate until find obstacle greater then 3
##def rotate():
##    global l_scan
##    laser_sub = rospy.Subscriber("/scan", LaserScan, scan_callback)
##    vel_pub = rospy.Publisher("/cmd_vel", Twist)

if __name__ == '__main__':
    try:
        rospy.init_node('Turtle_robot_control', anonymous=True)
        my_robot = Robot()
        my_robot.move()

    except rospy.ROSInterruptException:
        rospy.loginfo("node terminated.")

Output

header: 
  seq: 0
  stamp: 
    secs: 0
    nsecs:         0
  frame_id: ''
angle_min: 0.0
angle_max: 0.0
angle_increment: 0.0
time_increment: 0.0
scan_time: 0.0
range_min: 0.0
range_max: 0.0
ranges: []
intensities: []
readed
Traceback (most recent call last):
  File "/home/trahery/catkin_ws/src/ros_essentials_cpp/src/topic04_perception02_laser/robot_control.py", line 54, in <module>
    print(my_robot.is_obstacle(0.2))
  File "/home/trahery/catkin_ws/src/ros_essentials_cpp/src/topic04_perception02_laser/robot_control.py", line 24, in is_obstacle
    if min(slice_of_array) < distance:
ValueError: min() arg is an empty sequence
edit retag flag offensive close merge delete

Comments

Can you print just the value of

slice_of_array = self.l_scan.ranges[174: 184+1]

I believe you are comparing a list with distance that is a single value, thus min() fails

osilva gravatar image osilva  ( 2022-01-12 18:13:45 -0500 )edit

This makes empty list []

Trahery gravatar image Trahery  ( 2022-01-13 06:18:33 -0500 )edit

That’s what I thought. You are comparing a list with distance value so you min() function will always fail.

osilva gravatar image osilva  ( 2022-01-13 06:27:23 -0500 )edit

When I print min(slice_of_array) it goes again with.

Traceback (most recent call last): File "/home/trahery/catkin_ws/src/ros_essentials_cpp/src/topic04_perception02_laser/robot_control.py", line 53, in <module> my_robot.move() File "/home/trahery/catkin_ws/src/ros_essentials_cpp/src/topic04_perception02_laser/robot_control.py", line 37, in move while not self.is_obstacle(0.6): File "/home/trahery/catkin_ws/src/ros_essentials_cpp/src/topic04_perception02_laser/robot_control.py", line 23, in is_obstacle print(min(slice_of_array)) ValueError: min() arg is an empty sequence

Trahery gravatar image Trahery  ( 2022-01-13 06:32:14 -0500 )edit

I do not understand how is possible, that when I call print(self.l_scan) in callback function it has data. And when I printed in move it is emtpy.

Trahery gravatar image Trahery  ( 2022-01-13 06:33:05 -0500 )edit

It’s because list is empty. https://stackoverflow.com/questions/2...

osilva gravatar image osilva  ( 2022-01-13 06:35:41 -0500 )edit

So this means that I have to wait for valid data ? This is the problem ?

Trahery gravatar image Trahery  ( 2022-01-13 06:39:14 -0500 )edit

I think now you understand what is causing the error and now you need to work on the logic. Once you do please share the answer so others benefit. Thank you

osilva gravatar image osilva  ( 2022-01-13 06:57:56 -0500 )edit