示例#1
0
    def __init__(self, proto):
        self._proto = proto
        self._paused = False

        self._headset = emotiv.Emotiv()

        # Start the emotiv data grabbing in a separate thread
        gevent.spawn(self._headset.setup)
        gevent.sleep(1)
def readEEG(Fs,time,ch):
	"""Return EEG matrix nxm (n:time,m:channels) from Emotiv
		Parameters: Fs: -int- sampling frequency
					time: -int- time recording
					ch: -int- number of channels
		Returns: EEG: -ndarray- EEG signal
				 headset: -object- emotiv headset -> emotiv.Emotiv()"""
	headset = emotiv.Emotiv()
	gevent.spawn(headset.setup)
	gevent.sleep(1)

	samples = int(Fs*time)
	EEG = np.zeros(shape=(samples, ch))
	for i in xrange(samples):
		packet = headset.dequeue()
		
		EEG[i,0]=value = int(''.join(map(str,list(packet.F3)))) #Tuple to int
		EEG[i,1]=value = int(''.join(map(str,list(packet.F4)))) #Tuple to int
		EEG[i,2]=value = int(''.join(map(str,list(packet.P7)))) #Tuple to int
		EEG[i,3]=value = int(''.join(map(str,list(packet.FC6)))) #Tuple to int
		EEG[i,4]=value = int(''.join(map(str,list(packet.F7)))) #Tuple to int
		EEG[i,5]=value = int(''.join(map(str,list(packet.F8)))) #Tuple to int
		EEG[i,6]=value = int(''.join(map(str,list(packet.T7)))) #Tuple to int
		EEG[i,7]=value = int(''.join(map(str,list(packet.P8)))) #Tuple to int
		EEG[i,8]=value = int(''.join(map(str,list(packet.FC5)))) #Tuple to int
		EEG[i,9]=value = int(''.join(map(str,list(packet.AF4)))) #Tuple to int
		EEG[i,10]=value = int(''.join(map(str,list(packet.T8)))) #Tuple to int
		EEG[i,11]=value = int(''.join(map(str,list(packet.O2)))) #Tuple to int
		EEG[i,12]=value = int(''.join(map(str,list(packet.O1)))) #Tuple to int
		EEG[i,13]=value = int(''.join(map(str,list(packet.AF3)))) #Tuple to int
		
		#gevent.sleep(0)

	#headset.close()
	
	return(EEG, headset)
示例#3
0
def read_from_headset(polling, logfile):
    headset = emotiv.Emotiv()
    gevent.spawn(headset.setup)
    gevent.sleep(1)

    # Packets are received at 128Hz (see emotive_protocol.asciidoc in
    # the openyou repo), i.e. 128 packets per second.
    # If the polling time is 0.2s, we are reading at 5Hz, so we store
    # in a queue 128Hz / 5Hz = ~25 packets and write on stdout the
    # average values of the 25 packets before resetting the queue.
    size = int(128 / (1 / polling))
    packets = deque(maxlen=size)

    print('-----')
    sys.stderr.write('START\n')

    # if a logfile is provide, open it
    if logfile is not None:
        f = open(logfile, 'w')

    # Read the packets in loop and add them to the queue.
    # Once enough packets are collected, calculate the average of the values,
    # serialize the result with json, print it on stdout, and reset the queue
    try:
        while True:
            packet = headset.dequeue()
            # We only need the sensors data + the battery.
            # The rawdata, counter, and sync are not needed;
            # the other values are redundant
            data = dict(packet.sensors)
            del data['Unknown']  # we don't need this either
            data['battery'] = packet.battery
            packets.append(data)
            packets_num = len(packets)
            if packets_num >= size:
                avgdata = {}
                for key in data:
                    if key == 'battery':
                        # return the battery level for the last packet
                        avgdata[key] = packets[-1][key]
                    else:
                        # calculate the average of sensors qualities and values
                        # for all the packets
                        qsum, vsum = 0, 0
                        for p in packets:
                            sensor_data = p[key]
                            qsum += sensor_data['quality']
                            vsum += sensor_data['value']
                        avgdata[key] = dict(quality=qsum / packets_num,
                                            value=vsum / packets_num)
                json_data = json.dumps(avgdata)
                print(json_data)
                if logfile is not None:
                    f.write(json_data + '\n')
                sys.stdout.flush()
                packets.clear()
    except KeyboardInterrupt:
        headset.close()
    finally:
        headset.close()
        if logfile is not None:
            f.close()
示例#4
0
def read_from_headset(polling, logfile):
    headset = emotiv.Emotiv()
    gevent.spawn(headset.setup)
    gevent.sleep(1)
    # don't move the headset while reading this
    #packet = headset.dequeue()
    #x = packet.gyroX
    #y = packet.gyroY
    x = 1692.5231211892856
    y = 1700.0293896006028

    x = 1693.33246285
    y = 1700.17013241

    x = 1693.83995781
    y = 1699.888864

    x = 1691
    y = 1700



    #print("Calibrating (don't move the headset)...")
    #PNUM = 500
    #xs, ys = 0, 0
    #print('-'*50)
    #for k in range(PNUM):
        #if k%1000 == 0:
            #print('|', end='')
            #sys.stdout.flush()
        #packet = headset.dequeue()
        #xs += packet.gyroX
        #ys += packet.gyroY
    #x0 = xs / PNUM
    #y0 = ys / PNUM
    #print('[done]')
    #print('x0:y0 = %4d:%4d' % (x0, y0))
    #x, y = x0, y0

    # Packets are received at 128Hz (see emotive_protocol.asciidoc in
    # the openyou repo), i.e. 128 packets per second.
    # If the polling time is 0.2s, we are reading at 5Hz, so we store
    # in a queue 128Hz / 5Hz = ~25 packets and write on stdout the
    # average values of the 25 packets before resetting the queue.
    size = int(128 / (1/polling))
    positions = deque(maxlen=size)

    print('-----')
    sys.stderr.write('START\n')

    # if a logfile is provide, open it
    if logfile is not None:
        f = open(logfile, 'w')

    # this controls how much you have to move your head before changing
    # direction
    THRESHOLD = 300

    xpos, ypos = 0, 0
    xspeed, yspeed = 0, 0
    #last_direction = 'STOP'
    try:
        while True:
            packet = headset.dequeue()
            #print(packet.gyroX, packet.gyroY)
            xdiff = x - packet.gyroX
            ydiff = y - packet.gyroY
            if abs(xdiff) > 0:
                xspeed += xdiff
                xpos += int(round(xspeed/10))
            if abs(ydiff) > 0:
                yspeed -= ydiff
                ypos += int(round(yspeed/10))
            positions.append((xpos, ypos))
            if len(positions) >= size:
                xavg = sum(pos[0] for pos in positions) / len(positions)
                yavg = sum(pos[1] for pos in positions) / len(positions)
                #print('  xp:yp = %5d %5d' % (xavg, yavg))
                positions.clear()
                if abs(xavg) > THRESHOLD or abs(yavg) > THRESHOLD:
                    if abs(xavg) > abs(yavg):
                        direction = 'RIGHT' if xavg > 0 else 'LEFT'
                    else:
                        direction = 'FORWARD' if yavg > 0 else 'BACKWARDS'
                else:
                    direction = 'STOP'
                #if direction != last_direction:
                    #last_direction = direction
                print(direction)
                sys.stdout.flush()

                #print '%4d %4d' % (xspeed, yspeed)
                print('  x:y = %4d %4d' % (xpos, ypos))
            x, y = packet.gyroX, packet.gyroY
            if logfile is not None:
                f.write('%s %s\n' % (packet.gyroX, packet.gyroY))
            #headset.packets.empty()
    except KeyboardInterrupt:
        headset.close()
    finally:
        headset.close()
        if logfile is not None:
            f.close()
示例#5
0
from emokit import emotiv
import platform
if platform.system() == "Windows":
    import socket
import gevent

if __name__ == "__main__":
    headset = emotiv.Emotiv(display_output=True)
    gevent.spawn(headset.setup)
    gevent.sleep(0)
    try:
        while True:
            packet = headset.dequeue()
            print packet.gyro_x, packet.gyro_y
            print packet.raw_data
            gevent.sleep(0)
    except KeyboardInterrupt:
        headset.close()
    finally:
        headset.close()
示例#6
0
    beep(HIGH_SOUND, STANDART_TIME)


def end_experiment_section():
    beep(LOW_SOUND, STANDART_TIME)


def say(text):
    reader.say(text)
    reader.runAndWait()


#Main
if __name__ == "__main__":
    #Headset setup
    headset = emotiv.Emotiv(display_output=False)
    gevent.spawn(headset.setup)
    gevent.sleep(0)

    intentions = {0: '.', 1: 'w', 2: 's', 3: 'q', 4: 'e'}
    readings_array = []
    readings = 0
    number_of_readings = 150
    experiment = 0
    iteration = 0
    intention = 0
    Class_index = 0

    iters = 1000
    k = PyKeyboard()
    init_experiment_sound()
示例#7
0
from emokit import emotiv
import gevent

if __name__ == "__main__":
    headset = emotiv.Emotiv()
    gevent.spawn(headset.setup)
    gevent.sleep(1)
    try:
        while True:
            packet = headset.dequeue()
            print packet.gyroX, packet.gyroY
            gevent.sleep(0)
    except KeyboardInterrupt:
        headset.close()
    finally:
        headset.close()