Unexpected behavior real project part 1

Hi everyone,
I’m trying to complete the part of the real project but I have some doubts regarding the instructions and robot behavior.
I’ve check that the length of ranges array for /scan topic is 360 in the 180-degree range.
So, right now, I’m paying attention to ranges[180] for a 90 degree obstacle’s detection, ranges[0] for a 0 degree and ranges[359] for a 180 degree. Is it correct?
Furthermore, mi robot get stuck in obstacles when they are not in a perfect 90º angle. I’ve tried to pay attention to the left (ranges[0]) and right (ranges[359]) laser scan values but the behavior is not correct. Can anyone give me any clue? Thanks in advance.

# Distance to an obstacle bigger than 0.5 meter --> move forward
        if self.laser_forward > 0.5:
            self.cmd.linear.x = 0.2
            self.cmd.angular.z = 0.0
        # Distance to an obstacle smaller than 0.5 meter --> turn left
        if self.laser_forward <= 0.5:
            self.cmd.linear.x = 0.0
            self.cmd.angular.z = 0.2
        # Distance to an obstacle left side smaller than 0.3 meter --> turn right
        if self.laser_left < 0.3:
            self.cmd.linear.x = 0.0
            self.cmd.angular.z = -0.2
        # Distance to an obstacle left side smaller than 0.3 meter --> turn right
        if self.laser_right < 0.3:
            self.cmd.linear.x = 0.0
            self.cmd.angular.z = 0.2

Hi @lfernandez, are you sure that the lidar range is 180 degrees? Check opening rviz2 and adding the scan topic. That way, you’ll see half of the map for 180, and the whole map for 360

Thanks @roalgoal!! I will check it!

Hi everyone again
I’ve checked that the range is 360. So the right laser must be at 90º, the front laser at 180 and the left one at 270.
I don’t understand the exercise instructions:

** If the ray distance is bigger than 0.3m, you need to make the robot approach the wall a little, by adding some rotational speed to the robot*
** If the ray distance is smaller than 0.2m, you need to move the robot away from the wall, by adding rotational speed in the opposite direction*
** If the ray distance is between 0.2m and 0.3m, just keep the robot moving forward*

I guess these instructions refer to the right laser? The algorithm is not clear to me.
Thanks again.

yes, you got the laser directions right.

The instructions refer to the right side of the robot. Although it can work on the left too, just change the angular velocity sent to the robot to negative in that case.

If the side of the robot detect that the laser value is getting too high, then it should move closer to it using an angular velocity. The opposite is true. That way, the robot will stay between 0.2 and 0.3 m from the wall as it moves forward.

so, attending to the instructions the algorithm should be something like this?

# Distance to an obstacle bigger than 0.5 meter --> move forward
        if self.laser_forward > 0.5:
            self.cmd.linear.x = 0.2
            if self.obsAtRight > 0.3:
                self.cmd.angular.z = 0.2
            else:
                if self.obsAtRight < 0.2:
                    self.cmd.angular.z = -0.2
                else:
                    self.cmd.angular.z = 0.0
        else:
            self.cmd.linear.x = 0.0
            self.cmd.angular.z = 0.2


where

self.laser_forward = msg.ranges[180]

and self.obsAtRight = min(msg.ranges[130:165])

my robot gets stuck when finding an obstacle by following these instructions…