How to make a robot navigate a maze?

青春壹個敷衍的年華 提交于 2020-01-15 08:29:25

问题


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.

来源:https://stackoverflow.com/questions/27432813/how-to-make-a-robot-navigate-a-maze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!