from pyrobot.brain import Brain

class Avoid(Brain):

    def step(self):
        self.minSide = 0.5
        (speed, rotate) = self.wander()
        self.robot.move(speed, rotate)

    def wander(self):
        left = min([s.distance() for s in self.robot.range["front-left"]])
        right = min([s.distance() for s in self.robot.range["front-right"]])
        # if approaching an obstacle to the left side, turn right
        if left < self.minSide:
            return (0, -0.3)
        # if approaching an obstacle to the right side, turn left
        elif right < self.minSide:
            return (0, 0.3)
        else: # go forward
            return (0.5, 0)


def INIT(engine):
   return Avoid('Avoid', engine)
