2

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.

user3577397
  • 453
  • 3
  • 12
  • 27
  • lol.. I just realized I said "Are you kidding me?!" In the first vid. I'm a tad frustrated lol :P This type of programming is like pulling teeth for me, and I'm fairly new to both programming and python in general. – user3577397 Dec 11 '14 at 21:40
  • you can use a stack or a queue and perform a depth first or breadth first search (preferring to either go horizontal or vertical depending on available moves). Only store onto the stack or queue the available moves and once your goal has been reached you may pop/dequeue the moves. – Mike McMahon Dec 11 '14 at 21:43
  • Well, I vaguely remember stacking and queuing. I remember learning it briefly last year. I understood it conceptually, but I never actually coded that before. Not sure how to do that. – user3577397 Dec 11 '14 at 21:48

0 Answers0