# File: Alive.py
# Implementation of Braitenberg Vehicle#1
# See Chapter 6 of learning Computing with Robots by Deepak Kumar

from myro import *

# Read ambient value from sensor
Ambient = getLight("center")
print "Ambient value = ", Ambient

def normalize(v):
    # normalize v which is in 0..5000 to a value in 0.0...1.0
    # First we squash it to a maximum of Ambient value
    if v > Ambient:
        v = Ambient

    # Could replace the avove with: v = min(v, Ambient)
    # next, transform it to 0..1.0
    return 1.0 - v/Ambient

    # We subracted from 1.0 so that the values are proportional
    # to the amount of light
    
def main():
    # Vehicle#1 has one sensor (photo) and one motor
    # The more light isnsed, the faster the motor moves
    # We wire the Scribbler as Vehicle#1 by using the center light sensor
    # and using both motors at the same speed

    while timeRemaining(30):
        # get reading from sensor
        C = getLight("center")
        speed = normalize(C)
        motors(speed, speed)    # alternately, use forward(speed)
    stop()
    
