Ejemplo n.º 1
0
 def testParser(self):
     s = file("baseline.bytes").read()
     ts_recorder = TimeSeriesRecorder()
     parser = ThinkGearParser(recorders=[ts_recorder])
     parser.feed(s)
     
     self.assertTrue(len(ts_recorder.attention) == len(ts_recorder.meditation) == len(ts_recorder.blink) == len(ts_recorder.poor_signal))
Ejemplo n.º 2
0
def main():
    """Main loop capture."""
    recorder = TimeSeriesRecorder()
    parser = ThinkGearParser(recorders=[recorder])

    view = View()
    controller = Controller(view)
    socket, args = mindwave_startup(view=view, description="TEEG")

    mock = True
    spectra = []
    spectrum_mean = False
    prediction = True
    iteration = 0
    last_action = None

    while view.screen.quit is False:
        try:
            if socket is not None:
                data = socket.recv(10000)
                parser.feed(data)
        except BluetoothError:
            view.print_message("Bluetooth Error", "status")
            pass
        if mock:
            for _ in range(10):
                mock_data(parser)
        if has_recorded(recorder):
            flen = 50
            if len(recorder.raw) >= 500:
                spectrum, relative_spectrum = bin_power(
                    recorder.raw[-512 * 2:], range(flen), 512)

                # Mostrar a média spectral
                if spectrum_mean:
                    spectra.append(array(relative_spectrum))
                    if len(spectra) > 30:
                        spectra.pop(0)
                    spectrum = mean(array(spectra), axis=0)
                else:
                    spectrum = relative_spectrum

                view.update_spectrum(spectrum)
            else:
                pass

            if prediction and len(recorder.raw) >= 1024 and iteration % 5 == 0:
                if ai.kind == "raw":
                    action, proba = ai.predict(
                        array([array(recorder.raw[-512:], dtype=float32)]))
                else:
                    dt, data_spec = bin_power(recorder.raw[-512:], range(flen),
                                              512)
                    action, proba = ai.predict(array([data_spec]))
                view.print_action(action, proba)

                if proba > 0.6 and (action != last_action
                                    or action == "tooth"):
                    controller.send_action(action)
                    last_action = action
                elif proba > 0.5 and action != last_action and action == "punchleft":
                    controller.send_action(action)
                    last_action = action

            view.print_waves(recorder)
            """if len(parser.current_vector)>7:
                m = max(p.current_vector)
                for i in range(7):
                    if m == 0:
                        value = 0
                    else:
                        value = p.current_vector[i] *100.0/m
                    pygame.draw.rect(window, redColor,
                    (600+i*30,450-value, 6,value))"""
        elif socket is None:
            view.print_message("Mindwave not detected...", "status")
            pass
        else:
            view.print_message("Not receiving any data from mindwave...",
                               "status")
            pass
        iteration += 1
        time.sleep(0.01)
Ejemplo n.º 3
0
    def run(self):
        extra_args = [
            dict(name='measure',
                 type=str,
                 nargs='?',
                 const="attention",
                 default="attention",
                 help="""Measure you want feedback on. Either "meditation"
            or "attention\"""")
        ]
        socket, args = mindwave_startup(description=description,
                                        extra_args=extra_args)

        if args.measure not in ["attention", "meditation"]:
            print "Unknown measure %s" % repr(args.measure)
            sys.exit(-1)
        recorder = TimeSeriesRecorder()
        parser = ThinkGearParser(recorders=[recorder])

        spectra = []
        while not self.quite:
            time.sleep(0.05)
            idelta = 0
            itheta = 0
            ialpha = 0
            ibeta = 0
            igamma = 0
            try:
                data = socket.recv(10000)
                parser.feed(data)
            except BluetoothError:
                pass
            if len(recorder.attention) > 0:
                flen = 50
                if len(recorder.raw) >= 512:
                    self.data_ready = True
                    spectrum, relative_spectrum = bin_power(
                        recorder.raw[-512 * 3:], range(flen), 512)
                    spectra.append(array(relative_spectrum))
                    if len(spectra) > 30:
                        spectra.pop(0)
                    spectrum = mean(array(spectra), axis=0)
                    for i in range(flen - 1):
                        value = float(spectrum[i] * 1000)
                        if i < 3:
                            idelta += value
                        elif i < 8:
                            itheta += value
                        elif i < 13:
                            ialpha += value
                        elif i < 30:
                            ibeta += value
                        else:
                            igamma += value
                    if False:
                        print "Delta: {}".format(self.delta)
                        print "Theta: {}".format(self.theta)
                        print "Alpha: {}".format(self.alpha)
                        print "Beta: {}".format(self.beta)
                        print "Gamma: {}".format(self.gamma)
                    self.delta = idelta
                    self.theta = itheta
                    self.alpha = ialpha
                    self.beta = ibeta
                    self.gamma = igamma
Ejemplo n.º 4
0
# from numpy.random import randn
# from mindwave.pyeeg import bin_power
'''
import pygame, random, sys, numpy
from numpy import *
from pygame import *
from mindwave.parser import ThinkGearParser, TimeSeriesRecorder
from startup_sub import *
from tello import *
import paho.mqtt.client as mqtt

description = """Tello Neurosky
"""

socket, args = mindwave_startup(description=description)
recorder = TimeSeriesRecorder()
parser = ThinkGearParser(recorders=[recorder])

# For MQTT
MQTT_name = "localhost"
Topic_name = "tello"
mqtt_event = 0
mqtt_connected = False

# Set mindwave mobile
control_mode = False
raw_eeg = True
gain = 1.0
attention_value = 0
Th_attention = 50
Th2_attention = 90
Ejemplo n.º 5
0
        dict(name='frequency',
             type=int,
             nargs='?',
             const=10,
             default=10,
             help="""Frequency of recording, in iterations per second.
            This doesn't affect the sampling accuracy, but rather how
            often the parser is translating the data from the device
            into Timeseries data.
            """)
    ]

    socket, args = mindwave_startup(description=description,
                                    extra_args=extra_args)

    recorder = TimeSeriesRecorder(args.filename[0])
    parser = ThinkGearParser(recorders=[recorder])
    loop_time = 1.0 / float(args.frequency)
    last_message = time.time()
    start = last_message
    while 1:
        t = time.time()
        try:
            data = socket.recv(20000)
        except BluetoothError, e:
            print e
            time.sleep(0.5)
            continue
        parser.feed(data)
        elapsed = time.time() - t
        time.sleep(max(0.01, loop_time - elapsed))