# robot goes forward and then slows to a stop when it detects something
from pyrobot.brain import Brain
from pyrobot.brain.conx import *
from time import *
from random import random
class NNBrain(Brain):
# Designed for a Khepera
# Give the front two sensors, decide the next move
def determineMove(self, front):
if front[0] < 0.5 or front[1] < 0.5: # about to hit soon, STOP
print "collision imminent, stopped"
return(0)
elif front[0] < 0.8 or front[1] < 0.8: # detecting something, SLOWDOWN
print "object detected"
return(0.1)
else:
print "clear" # all clear, FORWARD
return(0.3)
def determineTurn(self, front, left, right):
if left[0] < .5:
return -.5
elif right[0] < .5:
return .5
elif front[0] < .5 or front[1] < .5:
return random() * 2 - 1
else:
return 0.0
def step(self):
front = [s.value for s in self.robot.range["front"]]
left = [s.value for s in self.robot.range["left"]]
right = [s.value for s in self.robot.range["right"]]
translation = self.determineMove(front)
rotate = self.determineTurn(front, left, right)
print "front sensors", front[0], front[1]
self.move(translation, rotate)
def INIT(engine):
return NNBrain('NNBrain', engine)