ROS Basics in 5 days Topics quiz

Hi all, I hope you are in the best of health…
I have written the following code for the quiz but it just doesn’t seem to work… Having viewed the solution, may i please reaccess the quiz to submit it?

#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

class MoveRobot:

def __init__(self, speedRobot):

    rospy.init_node('topics_quiz_node', anonymous=True)

    self.speed = speedRobot

    self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

    self.sub = rospy.Subscriber(

        'kobuki/laser/scan', LaserScan, self.callback)

    self.rate = rospy.Rate(2)

    self.cmd = Twist()

    self.Lasermsg = LaserScan()

def callback(self, msg):

    self.Lasermsg = msg

def get_front_laser_reading(self):

    print(self.Lasermsg)

    return self.Lasermsg.ranges[360]

def avoid_wall_now(self):

    self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

    self.sub = rospy.Subscriber(

        'kobuki/laser/scan', LaserScan, self.callback)

    while not rospy.is_shutdown():

        if self.get_front_laser_reading() > 1.0:

            self.cmd.linear.x = self.speed

        else:

            self.cmd.linear.x = 0.0

            self.cmd.angular.z = self.speed


        self.pub.publish(self.cmd)

        self.rate.sleep()

mr1 = MoveRobot(0.3)

mr1.avoid_wall_now()

The output keeps telling me the laser index is out of range, as in:

Traceback (most recent call last):
File “/home/user/catkin_ws/src/topics_quiz/src/my_script2.py”, line 46, in
mr1.avoid_wall()
File “/home/user/catkin_ws/src/topics_quiz/src/my_script2.py”, line 33, in avoid_wall
if self.get_front_laser() > 1.0:
File “/home/user/catkin_ws/src/topics_quiz/src/my_script2.py”, line 25, in get_front_laser
return self.Lasermsg.ranges[360]
IndexError: list index out of range

Hi, you can’t resubmit a quiz after viewing the solution, is your code failing even after seeing it?

It seems like your line of code

return self.Lasermsg.ranges[360]

Is returning an error because the index you are trying to access does not exist.