Getting data from subscribers

Hi, im trying to get the data from a subscriber, and the use it to determinate the movement of the robot. But, when the gets to the while, it tells me that d_laser.ranges is size 0 and i exceed the index. I dont know where the problem is.

#! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

d_laser=LaserScan()
def callback(datos_laser):
global d_laser
d_laser.ranges=datos_laser.ranges

rospy.init_node(‘topics_quiz_node’)
salida=rospy.Publisher(‘/cmd_vel’,Twist,queue_size=1)
entrada=rospy.Subscriber(‘/kobuki/laser/scan’,LaserScan,callback)
velocidad=Twist()
velocidad.linear.x=0
velocidad.angular.z=0

fm=rospy.Rate(2)
while not rospy.is_shutdown():

if d_laser.ranges[len(d_laser.ranges)//2]>1: 
    velocidad.angular.z=0   
    velocidad.linear.x=1
    salida.publish(velocidad)  
else:
    velocidad.linear.x=0
    velocidad.angular.z=0.5
    salida.publish(velocidad)
    if d_laser.ranges[len(d_laser.ranges)-1]<1:
        velocidad.linear.x=0
        velocidad.angular.z=0.5
        salida.publish(velocidad)
    elif d_laser.ranges[1]<1:
        velocidad.linear.x=0
        velocidad.angular.z=-0.5
        salida.publish(velocidad)
fm.sleep()

Welcome to the world of programming, where nothing happens as/when expected, and you have to check if/when!

You need to put some print/logging statements within your code to inspect critical variables. For instance, you should inspect the datos_laser variable to see if/when something actually gets there.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.