How to make a robot navigate a maze?

1.4k views Asked by At

I'm using the Myro library with the Python language. I've had some weird results.

My idea was to call the getObstacle sensors.

left = getObstacle(0)
center = getObstacle(1)
right = getObstacle(2)

I want the robot to move forward as long as the center obstacle sensor is less than or equal to 4500.

If the right obstacle sensor on the robot has a higher reading than the left obstacle sensor, I want it to turn left.

Otherwise turn right.

Here are my attempts on youtube

Attempt 1

Attempt 2

I'm going to submit 3 different variations of my code

def main():
    setIRPower(135)
    while True:
        left = getObstacle(0)
        center = getObstacle(1)
        right = getObstacle(2)
        # 2 feet per 1 second at 1 speed
        if (center <= 4500):
            forward(0.5, 0.2)   
            wait(0.4)   
        elif (right > left):
            turnLeft(1, .45) 
        else:
            turnRight(1, .45)



def main():
    setIRPower(135)
    while True:
        left = getObstacle(0)
        center = getObstacle(1)
        right = getObstacle(2)
        # 2 feet per 1 second at 1 speed
        if (center <= 4500):
            forward(0.5, 0.2)   
            wait(0.3)   

        elif(right > center and left):
            turnLeft(1, .45) 
        elif(left > center and right):
            turnRight(1, .45)

The latest one I'm working with

def main():
    setForwardness(1)
    setIRPower(135)
    while True: 
        left = getObstacle(0)
        center = getObstacle(1)
        right = getObstacle(2)
        if (center <= 5000 and left <= 5000 and right <= 5000):
            forward(0.5, 0.2)
            wait(.3)
        elif(right>left):
            turnLeft(1, 0.45)
        else:
            turnRight(1, 0.45)

Is there any way I can improve my code? I want it to turn left and right at the correct times.

Should I be using different logic altogether? Any help would be appreciated.

0

There are 0 answers