def METHOD_NAME(self):
    SPEED = 4
    state = 'SEARCHING'
    save_image = True
    width = self.range_finder.getWidth()
    height = self.range_finder.getHeight()
    while self.step(self.timeStep) != -1:
        image = self.range_finder.getRangeImage()
        average_distance = 0
        minimal_distance = 100
        # Here we analyse the image we've got from the range-finder. Our robot has two
        # states: SEARCHING and MOVING. When it is in SEARCHING, it looks around
        # for a direction in which it has enough distance to move. So in this
        # case we compute the weighted average distance of the range-finder. In the
        # MOVING state, it is moving straight until it comes too close from an
        # obsacle. In this case we compute the minimal distance of the range-finder.
        for i in range(width):
            # The weight is very simple, it is only an interval in the center of
            # the image to be shure to have enough place to move our robot.
            centering_weight = 1 if i < width / 4 or i > 3 * width / 4 else 3
            for j in range(height):
                distance = RangeFinder.rangeImageGetDepth(image, width, i, j)
                if distance > 100:  # distance may be infinity
                    distance = 100
                if state == 'SEARCHING':
                    average_distance += distance * centering_weight
                elif distance < minimal_distance:
                    minimal_distance = distance
        average_distance /= width * height
        if average_distance >= 1 and state == 'SEARCHING':
            state = 'MOVING'
        elif minimal_distance < 0.1 and state == 'MOVING':
            state = 'SEARCHING'
        if state == 'MOVING':
            left_speed = SPEED
            right_speed = SPEED
        else:
            left_speed = -SPEED
            right_speed = SPEED
        if save_image:
            # Save range-finder's current view as HDR image in home directory
            # compute the file path in the user directory
            if os.name == 'nt':
                user_directory = os.environ['USERPROFILE']
                # we need a DOS 8.3 path to support non-ascii characters in user name
                BUFFER_SIZE = 1024
                buffer = create_unicode_buffer(BUFFER_SIZE)
                windll.kernel32.GetShortPathNameW(user_directory, buffer, BUFFER_SIZE)
                user_directory = buffer.value
            else:
                user_directory = os.environ['HOME']
            filename = os.path.join(user_directory, 'test.hdr')
            self.range_finder.saveImage(filename, 100)
            save_image = False
        # Set the motor speeds.
        self.left_motor.setVelocity(left_speed)
        self.right_motor.setVelocity(right_speed)