"""
Braitenberg Vehicle2b
The more light sensed on the left side the faster the right motor moves.
The more light sensed on the right side the faster the left motor moves.
This causes the robot to turn towards a light source.
"""

from pyrobot.brain import Brain, avg
from time import sleep

class Vehicle(Brain):
   def setup(self):
      self.robot.light[0].units = "SCALED"
   def step(self):
      if self.robot.stall:
         self.robot.move(-1, 0)
         sleep(0.5)
         self.robot.move(0, -1)
         sleep(1.5)
      else:
         leftSpeed  = max([s.value for s in self.robot.light[0]["right"]])
         rightSpeed = max([s.value for s in self.robot.light[0]["left"]])
         self.motors(leftSpeed,  rightSpeed) 

def INIT(engine):
   if engine.robot.type not in ['K-Team', 'Pyrobot']:
      raise "Robot should have light sensors!"
   return Vehicle('Braitenberg2b', engine)
      
