Rosject Part II Service Attribute Error


I am currently on the Rosject from ROS in 5 Days Python part 2.

When I run my code, I sometimes get this error:

Here is the part of the code which causes the error:

class Find_Nearest_Wall():
def init(self):
# start publisher, subscriber and server = rospy.Publisher(‘/cmd_vel’, Twist, queue_size=1)
self.move = Twist()
self.sub = rospy.Subscriber(‘/scan’, LaserScan, self.sub_callback)
self.rate = rospy.Rate(1)
self.my_service = rospy.Service(“/find_wall”, FindWall, self.server_callback)
rospy.loginfo(“The service: /find_wall is ready”)

def sub_callback(self, data):
    # get laser data
    self.laser = data.ranges

def server_callback(self, request):
    print("server_callback started with {",request,"}")
    # init values
    min_laser = float('inf')
    min_index = 0
    # get robot standing still
    self.move.linear.x = 0
    self.move.angular.z = 0
    # loop through all laserbeams to find the smallest
    for i in range(0,len(self.laser)):
        if self.laser[i] < min_laser:
            min_laser = self.laser[i]
            min_index = i

So I mean my self.laser gets initiated in the sub_callback(self, data). So therefore I should be able to use it in the server_callback() function.

What is weird, is that if I launch my code multiple times then after the 3th or 4th attempt it then works without error.
Meaning I do the following in the simulation:

  1. Launch the code → Error message → Ctrl C to stop the code
  2. Launch again the code → Error message → Ctrl C to stop the code
  3. Launch again (3th or 4th attempt) → No Error Message → Code runs like it is supposed to do.

So I don’t really know why I am getting this error messages. Sometimes it does also work on the 1st attempt without any error.

So I don’t know why I get this error message and why then suddently not any more after a few attempts, without having moved the robot.

So I would be thankful for any help.

Hi @Dkae ,

What you experience is called an initialization error. You must have known about this when you learnt C/C++ for programming in your School/College when you took programming for the first time.
In case you have never taken a formal programming course, then here is an explanation.

This error occurs due to the variable not declared and initialized in the run time. So when you are actually querying for self.laser, the variable self.laser as this instant was not initialized in your program during the runtime.

The simplest fix for this is the following:

def __init__(self):
    // other initialization codes
    self.laser = [0.0] * 720  # since laser ranges is a list of 720 values
    // other initialization codes
    return None

By addling that line to your init function, you are (pre)allocating a memory block for the laser ranges variable to store the ranges data, whenever the data changes in the callback(s).

Now you will not have the error coming at the 1st or 2nd try. The error will never occur hereafter.

Let me know if this does not fix your problem.


Hi @girishkumar.kannan

Thanks for your response. This solved my problem.

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