Final project: The robot cannot move in a straight line and rotate exactly 90 degrees

Hi there,

I found that the robot cannot move in a straight line and rotate as we specified. Therefore, in the maze, when we wanted to move straight in the beginning, it would be slightly towards the right, which led to a crush after rotating 90 degrees in the first corner.
Could you please tell me why it is like this? Is that just because the initial position is not exactly facing the front? How can we adjust the initial position? Or it is also because the rotation precision cannot be guaranteed, even when I set the self.angular_tolerance = radians(0.1) instead of self.angular_tolerance = radians(2)?


Anyway, I tried to adjust the direction slightly for multiple times along the way, according to the laser reading when it was too close to the wall…
The final code works but it seems to be a bit stupid…:

from robot_control_class import RobotControl
import math
from math import pi

class RobotMaze():
    def __init__(self, motion = "forward", clockwise = "counter-clockwise", speed = 0.3, time = 1 ):
        self.motion = motion
        self.clockwise = clockwise
        self.speed = speed
        self.time = time
        self.rc = RobotControl()
        self.laser_list = self.rc.get_laser_full()
        self.status_out_maze = False

    def move(self):
        self.laser_list = self.rc.get_laser_full()

        print(f"Left Position Reading: {self.laser_list[719]}")
        print(f"Front Position Reading: {self.laser_list[359]}")
        print(f"Right Position Reading: {self.laser_list[0]}")
        
        if self.laser_list[0] == math.inf and self.laser_list[359] == math.inf and self.laser_list[719] == math.inf:
            self.rc.stop_robot()
            self.status_out_maze = True
            print ("Get out of the maze sucessfully")
        elif self.laser_list[359] == math.inf:
            self.rc.move_straight_time(self.motion,self.speed,self.time)
            print("detect the exit, move straight until go out of the maze")
        elif self.laser_list[359] > 1 and self.laser_list[0] != math.inf and self.laser_list[719] != math.inf:
            self.rc.move_straight_time(self.motion,self.speed,self.time)
            print("Silll in the maze, no obsticales on the front, can move straight")
        elif self.laser_list[0] > 2 :
            self.rc.rotate(degrees = - 90)
            print("detect corner on the right, turn right")
        elif self.laser_list[719] > 2 :
            self.rc.rotate(degrees = 90)
            print("detect corner on the left, turn left")
        else:
            print("too close to the wall, turn to adjust the direction!")
            if self.laser_list[0] < 0.5 :
                print("Too close to the right wall, slightly turn left")
                self.rc.rotate(degrees = 30)
            if self.laser_list[719] < 0.5 :
                print("Too close to the left wall,slight turn right")
                self.rc.rotate(degrees = -30)
            


       
robot = RobotMaze()
while robot.status_out_maze == False:
    robot.move()

Hello @min.wang.1997.01.13 ,

This happens because of an accumulated error, not only in the rotation but also when moving straight (the robot does not move exactly following a straight line). In robotics, this can be solved with proper controllers (you can learn this in the Robot Control course), but that is not the purpose of this course. So I would say your solution is correct for this course.

1 Like

Hi,

If you want to play a bit more with this, You can measure two distances on robot’s right (where the wall is) at equal angles from the 90 degree. Based on the difference, you can know If the robot is parallel with the wall or if it is toward or against the wall.

path1213

green: robot
Blue: wall
The longer purple line is the direction of the robot. The shorter purple line is perpendicular to it (90 degrees to the left).
The two red lines are laser rays at equal angles from the shorter purple line.
The difference of the length of the two red lines let you know the robot’s relative direction to the wall.

PĂ©ter

3 Likes

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