def __init__(self): self.us = Ultrasound() try: p = subprocess.Popen(['pidof','raspberry_pi_camera_streamer'], stdout=subprocess.PIPE) out,err = p.communicate() pid = int(out.strip('\n')) os.kill(pid,signal.SIGKILL) try: self.camera = PiCamera(resolution = (512,320)) self.camera.hflip = True self.camera.vflip = True except: pass print "Camera killed" except: print "Camera already dead" #need to fix this try: self.ser = serial.Serial(port = '/dev/ttyUSB0',baudrate = 9600,timeout = 1) except Exception as e: print e try: self.ser = serial.Serial(port = '/dev/ttyUSB1',baudrate = 9600,timeout = 1) except: self.ser = None self.angle = 0 self.top = 0 self.bottom = 0 self.right = 0 self.left = 0
class Sensor: def __init__(self): self.us = Ultrasound() try: p = subprocess.Popen(['pidof','raspberry_pi_camera_streamer'], stdout=subprocess.PIPE) out,err = p.communicate() pid = int(out.strip('\n')) os.kill(pid,signal.SIGKILL) try: self.camera = PiCamera(resolution = (512,320)) self.camera.hflip = True self.camera.vflip = True except: pass print "Camera killed" except: print "Camera already dead" #need to fix this try: self.ser = serial.Serial(port = '/dev/ttyUSB0',baudrate = 9600,timeout = 1) except Exception as e: print e try: self.ser = serial.Serial(port = '/dev/ttyUSB1',baudrate = 9600,timeout = 1) except: self.ser = None self.angle = 0 self.top = 0 self.bottom = 0 self.right = 0 self.left = 0 def takePicture(self, name = "testPic", extention = "jpeg"): self.camera.capture(name, extention) print self.camera.resolution def removeOutLiers(self, listS): low = 100 high = 0 for i in listS: if i < low: low = i elif i > high: high = i listS.remove(low) listS.remove(high) return listS #reads from either the right or left sensor. #'r' == right #'l' == left # def getSensor(self, side = 'r'): self.ser.flushInput() self.ser.write(side) dist = self.ser.readline() d = 0 try: d = float(dist.strip('\r\n')) except: d = None return d def getDistance(self): listSense = [] for i in range(50): listSense.append(self.us.read_normalized()) #print listSense[i] listSense = self.removeOutLiers(listSense) dist = float(sum(listSense))/48.0 return dist def isWall(self, colours={'white':1,'black' :1}): ''' colours dictionary of colours in a picture. returns odds of the picture being a Wall ''' black = float(colours['black']) if black == 0.0: return 0 others = float(sum(v for k,v in colours.iteritems() if k != 'black')) if others == 0.0: return 100 #only black in the picture print black,others return black/(others + black) def makeBox(self): length = int(self.top + self.bottom) width = int(self.left + self.right) room = [] for i in range(length): line = [] for j in range(width): if (i == 0 or j == 0) or (i == length - 1) or (j == width - 1): line.append(1) else: line.append(0) room.append(line) room[int(self.top)][int(self.left)] = 8 return room
camera.vflip = True #calibrate servos c_left = Servo(pins.SERVO_LEFT_MOTOR) c_right = Servo(pins.SERVO_RIGHT_MOTOR) c_left.set_normalized(0.5) c_right.set_normalized(0.5) movement = Movement() servo = Servo() #initialize the Buzzer buzzer = Buzzer() #initialize the ultrasound usound = Ultrasound() #initialize the physical button press event b = Button() #Clean up on exiting so that the motors are off and the image is destroyed. @atexit.register def goodbye(): print "You are killing the PyNet" #os.system("rm *.jpg") movement.stop() def takePicture(name=0): buzzer.on()