UserPreferences

NNCollectDataProgram


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 

# brain to collect data for offline learning
# 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 *

def saveListToFile(ls, file):
   for i in range(len(ls)):
      file.write(str(ls[i]) + " ")
   file.write("\n")

class CollectDataBrain(Brain):

   def setup(self):
      self.counter = 0
      self.countstopping = 0
      self.datafile1 = open("frontsensors.dat", "w")
      self.datafile2 = open("translatetargets.dat", "w")
      self.maxvalue = self.robot.range.getMaxvalue()
      print "max sensor value is ", self.maxvalue

   def determineMove(self, front):
      if front[0] < 0.5 or front[1] < 0.5:
         print "collision imminent"
         self.countstopping = self.countstopping+1
         return(0)
      elif front[0] < 0.8 or front[1] < 0.8:
         print "object detected"
         return(0.08)
      else:
         print "clear"
         return(0.15)
   def scale(self, val):
      x = val / self.maxvalue
      if x > 1:
         print "scaled > 1"
         return 1
      else:
         return x
   def step(self):
      front = [s.distance() for s in self.robot.range["front"]]
      translation = self.determineMove(front)
      print "front sensors", front[0], front[1]
      if self.counter > 1000:
         self.datafile1.close()
         self.datafile2.close()
         print "done collecting data"
         self.stop()
      elif self.countstopping > 5:
         self.move(-0.3, 0.2)
         sleep(0.5)
         self.countstopping = 0
      else:
         print "move", self.counter, translation
         saveListToFile([self.scale(front[0]), self.scale(front[1])],
                        self.datafile1)
         saveListToFile([translation] , self.datafile2)
         self.move(translation, 0.0)
         self.counter += 1

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