from myro import *
init("/dev/tty.scribbler")
cruiseSpeed = 0.3
turnSpeed = 0.3
lightThresh = 80

def cruise():
    #is always ON, just move forward
    return[True, -cruiseSpeed, 0]

def avoid():
    #see if there are any obstacles
    L,R = getIR()
    L = 1 - L
    R = 1 - R

    if L:
        return [True, 0, -turnSpeed]
    elif R:
        return [True, 0, turnSpeed]
    else:
        return [False, 0, 0]

def seekLight():
    L, C, R = getLight()

    if L < lightThresh:
        return [True, -cruiseSpeed/2, turnSpeed]
    elif R < lightThresh:
        return [True, -cruiseSpeed/2, -turnSpeed]
    else:
        return [False, 0, 0]

def arbitrate():
    for behavior in behaviors:
        output, T, R = behavior()
        if output:
            return [T, R]

def main():
    while True:
        T, R = arbitrate()
        move(T, R)

#list of behaviors, ordered by priority (left is the highest)
senses()
behaviors = [seekLight, avoid, cruise]
main()