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()