Ejemplo n.º 1
0
def reader():
    while True:
        try:
            packet = headset.read()
            queue.append(packet)
            time.sleep(1)
        except:
            headset = emotiv.Emotiv(simulation='sample_data')
Ejemplo n.º 2
0
 def init_worker(self, com_emotiv='COM4', com_rotary='COM6'):
     """
     ワーカーの初期化
     ロータリーエンコーダは別スレッドで動かす
     :return: None
     """
     self.__emo = emotiv.Emotiv(port=com_emotiv)
     self.__rotary = rotaryencoder.RotaryEncoder(port=com_rotary)
     self.__rotary.started.connect(self.start_worker)
     self.__rotary.signal.connect(self.event_rotary)
     self.start_worker()
     self.bitalino_measurement.start()
     self.countdown(sec=10)
Ejemplo n.º 3
0
    def __init__(self, loc):
        QWidget.__init__(self)
        self.resize(500, 800)
        self.strengths = {}
        self._src = emotiv.Emotiv()

        def read_func():
            results = []
            for packet in self._src.read(1):
                results.append(getattr(packet, loc)[0])
            return numpy.array(results)

        self.read = read_func

        self.widgets = dict(
            zip(rhythms.keys(), map(RhythmWidget, rhythms.keys())))

        layout = QVBoxLayout()
        for _, widget in self.widgets.items():
            layout.addWidget(widget)

        self.setLayout(layout)
Ejemplo n.º 4
0
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error as msg:
        s = None

    try:
        s.bind((HOST, PORT))
        s.listen(1)
    except socket.error as msg:
        s.close()
        s = None

    if s is None:
        print 'Could not open socket'
        return

    conn, addr = s.accept()
    print 'Listening to', addr


try:
    headset = emotiv.Emotiv(True, False)
    #gevent.spawn(headset.setup)
    main()

except Exception, e:
    print e
    print "Device Time Out or Disconnect . . .    Reconnect to Server."
    conn.close()
    main()
Ejemplo n.º 5
0
# Original code here: https://gist.github.com/512987

import time
import struct
import socket
import hashlib
import sys
import re
import logging
import threading
import emotiv

from select import select

try:
    headset = emotiv.Emotiv()
except:
    headset = emotiv.Emotiv(simulation='sample_data')

callbacks = []
queue = []


def reader():
    while True:
        try:
            packet = headset.read()
            queue.append(packet)
            time.sleep(1)
        except:
            headset = emotiv.Emotiv(simulation='sample_data')
Ejemplo n.º 6
0
import emotiv
headset = emotiv.Emotiv()
'''
string_to_num(string)
Converts a string, 'string' into a number that represents its binary notation.
'a' becomes '61' and 'aa' becomes '61 61'
'''


def string_to_num(string):
    binary = ""  #Binary is an empty string
    for char in string:  #For each character in the provided 'string'
        binary += char_to_bin(
            char
        )  #Append the binary representation string of the character to binary
    return int(binary, 2)  #Convert the binary into an integer and return.


'''
char_to_bin(char)
Converts a character into its corresponding binary number
'''


def char_to_bin(char):
    ascii = ord(
        char
    )  #Convert char into an integer that represents it. 'a' becomes '61'
    bin = []  #an empty array to store the binary representation of char

    while (
Ejemplo n.º 7
0
def main():
    drone = libardrone.ARDrone()
    headset = emotiv.Emotiv()
    gevent.spawn(headset.setup)
    gevent.sleep(1)
    pygame.init()
    W, H = 320, 240
    screen = pygame.display.set_mode((W, H))

    direction = {}
    direction['Yaw'] = 0
    direction['Pitch'] = 0
    yawCounter = 0
    pitchCounter = 0

    drone.speed = 0.2
    drone.takeoff()

    isRunning = True

    while isRunning:
        packet = headset.dequeue()

        if (yawCounter > 0) and (packet.gyroX < -5):
            yawCounter = 0
        elif (yawCounter < 0) and (packet.gyroX > 5):
            yawCounter = 0
        elif (packet.gyroX > 5):
            yawCounter += 1
        elif (packet.gyroX < -5):
            yawCounter -= 1
        elif (packet.gyroX == 0):
            yawCounter = 0

        if (packet.gyroX > 50):
            direction['Yaw'] = 0
            yawCounter = 0

        elif (packet.gyroX < -50):
            direction['Yaw'] = 0
            yawCounter = 0

        elif (yawCounter > 20) and (direction['Yaw'] == 1):
            direction['Yaw'] = 0
            yawCounter = 0

        elif (yawCounter > 40):

            if (direction['Yaw'] == 1):
                direction['Yaw'] = 0
            else:
                direction['Yaw'] = -1
                yawCounter = 0

        elif (yawCounter < -20) and (direction['Yaw'] == -1):
            direction['Yaw'] = 0
            yawCounter = 0

        elif (yawCounter < -40):
            if (direction['Yaw'] == -1):
                direction['Yaw'] = 0
            else:
                direction['Yaw'] = 1
                yawCounter = 0

        if (pitchCounter > 0) and (packet.gyroY < -3):
            pitchCounter = 0
        elif (pitchCounter < 0) and (packet.gyroY > 2):
            pitchCounter = 0
        elif (packet.gyroY > 2):
            pitchCounter += 1
        elif (packet.gyroY < -3):
            pitchCounter -= 1
        elif (packet.gyroY == 0):
            pitchCounter = 0

        if (packet.gyroY > 40):
            direction['Pitch'] = 0
            pitchCounter = 0

        elif (packet.gyroY < -40):
            direction['Pitch'] = 0
            pitchCounter = 0

        elif (pitchCounter > 20) and (direction['Pitch'] == 1):
            direction['Pitch'] = 0
            pitchCounter = 0

        elif (pitchCounter > 30):
            if (direction['Pitch'] == 1):
                direction['Pitch'] = 0
            else:
                direction['Pitch'] = -1
                pitchCounter = 0

        elif (pitchCounter < -20) and (direction['Pitch'] == -1):
            direction['Pitch'] = 0
            pitchCounter = 0

        elif (pitchCounter < -40):
            if (direction['Pitch'] == -1):
                direction['Pitch'] = 0
            else:
                direction['Pitch'] = 1
                pitchCounter = 0

        if (direction['Yaw'] == 0) and (direction['Pitch'] == 0):
            drone.hover()

        else:
            if (direction['Pitch'] == 1):
                drone.move_forward()
            elif (direction['Pitch'] == -1):
                drone.move_backward()
            elif (direction['Yaw'] == 1):
                drone.turn_right()
            elif (direction['Yaw'] == -1):
                drone.turn_left()

        print "Gyro X: %d Gyro Y: %d" % (packet.gyroX, packet.gyroY)
        print "Yaw: %d Pitch: %d" % (direction['Yaw'], direction['Pitch'])

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    drone.land()
                    isRunning = False

    headset.close()
    print "Shutting down...",
    drone.halt()
    print "Success"
Ejemplo n.º 8
0
import sys
sys.path.insert(0, './Python/CyKit/')
import emotiv
import gevent
import socket
del socket
del sys

if __name__ == "__main__":
    headset = emotiv.Emotiv(False, False)
    gevent.spawn(headset.setup)
    gevent.sleep(1)
    try:
        while True:
            packet = headset.dequeue()
            print packet.gyro_x, packet.gyro_y
            gevent.sleep(0)
    except KeyboardInterrupt:
        headset.close()
    finally:
        headset.close()
Ejemplo n.º 9
0
def flightMode(normal):
  drone = libardrone.ARDrone()
  headset = emotiv.Emotiv()
  controller = Leap.Controller()
  
  gevent.spawn(headset.setup)
  gevent.sleep(1)
  pygame.init()
  W, H = 320, 240
  screen = pygame.display.set_mode((W, H))
  
  direction = {}
  direction['Yaw'] = 0
  direction['Pitch'] = 0
  yawCounter = 0
  pitchCounter = 0
  
  isRunning = True
  
  drone.takeoff()
  
  xoffset = -4
    
  while isRunning:
      packet = headset.dequeue()
      
      if (yawCounter > 0) and (packet.gyroX < -5+xoffset):
	yawCounter = 0
      elif(yawCounter < 0) and (packet.gyroX > 5+xoffset):
	yawCounter=0
      elif(packet.gyroX > 5+xoffset):
	yawCounter += 1
      elif(packet.gyroX < -5+xoffset):
	yawCounter -= 1
      elif(packet.gyroX==0+xoffset):
	yawCounter=0

      if(packet.gyroX > 50+xoffset):
	direction['Yaw']=0
	yawCounter=0
			
      elif(packet.gyroX < -50+xoffset):
	direction['Yaw']=0
	yawCounter=0
		
      elif(yawCounter > 20) and (direction['Yaw']==1):
	direction['Yaw']=0
	yawCounter=0
			
      elif(yawCounter > 40):
	
	if(direction['Yaw']==1): 
	  direction['Yaw']=0
	else:
	  direction['Yaw']=-1
	  yawCounter=0
			
      elif(yawCounter < -20) and (direction['Yaw']==-1):
	direction['Yaw']=0
	yawCounter=0
			
      elif(yawCounter < -40):
	if(direction['Yaw']==-1): 
	  direction['Yaw']=0
	else: 
	  direction['Yaw']=1
	  yawCounter=0
			
		

      if(pitchCounter > 0) and (packet.gyroY < -3):
	pitchCounter=0
      elif(pitchCounter < 0) and (packet.gyroY > 2):
	pitchCounter=0
      elif(packet.gyroY > 2):
	pitchCounter+=1
      elif(packet.gyroY < -3):
	pitchCounter-=1
      elif(packet.gyroY==0):
	pitchCounter=0

      if(packet.gyroY > 40):
	direction['Pitch']=0
	pitchCounter=0
			
      elif(packet.gyroY < -40):
	direction['Pitch']=0
	pitchCounter=0
			
      elif(pitchCounter > 20) and (direction['Pitch']==1):
	direction['Pitch']=0
	pitchCounter=0
			
      elif(pitchCounter > 30):
	if(direction['Pitch']==1):
	  direction['Pitch']=0
	else:
	  direction['Pitch']=-1
	  pitchCounter=0
			
      elif(pitchCounter < -20) and (direction['Pitch']==-1):
	direction['Pitch']=0
	pitchCounter=0
			
      elif(pitchCounter < -40):
	if(direction['Pitch']==-1):
	  direction['Pitch']=0
	else:
	  direction['Pitch']=1
	  pitchCounter=0
	  
      if(direction['Yaw'] == 0) and (direction['Pitch'] == 0):
	print "here"
	
      else:
	if(direction['Pitch'] == 1):
	  drone.move_forward()
	elif(direction['Pitch'] == -1):
	  drone.move_backward()
	elif(direction['Yaw'] == 1):
	  drone.turn_right()
	elif(direction['Yaw'] == -1):
	  drone.turn_left()
      
      print "Gyro X: %d Gyro Y: %d" % (packet.gyroX+xoffset, packet.gyroY)
      print "Yaw: %d Pitch: %d" % (direction['Yaw'], direction['Pitch'])
      
      frame = controller.frame()
      
      if not frame.hands.is_empty:
	hand = frame.hands[0]
      
	fingers = hand.fingers
	normal = hand.palm_normal
	handdirection = hand.direction
      
	print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
                handdirection.pitch * Leap.RAD_TO_DEG,
                normal.roll * Leap.RAD_TO_DEG,
                handdirection.yaw * Leap.RAD_TO_DEG+normal.yaw)
      
	if handdirection.yaw * Leap.RAD_TO_DEG > 10+normal.yaw:
	  drone.move_right()
	  print "Move right"
	
	elif handdirection.yaw * Leap.RAD_TO_DEG < -10+normal.yaw:
	  drone.move_left()
	  print "Move left"
	  
	else:
	  drone.hover()
	  print "Hover"
	  
      elif (direction['Yaw'] == 0) and (direction['Pitch'] == 0) and frame.hands.is_empty:
	drone.hover()
	print "Hover"
	  
      for event in pygame.event.get():
	 if event.type == pygame.KEYDOWN:
	  if event.key == pygame.K_SPACE:
	      drone.land()
	      isRunning = False
      
   
  headset.close()
  print "Shutting down...",
  drone.halt()
  print "Success"