Example #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))
Example #2
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))
Example #3
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)
Example #4
0
pairing mode first.

"""
if __name__ == '__main__':
    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])

    if args.measure== 'attention':
        measure_name = 'Attention'
    else:
        measure_name = 'Meditation'

    bar = ProgressBar(widgets=[measure_name, Percentage(), Bar()]).start()

    while 1:
        time.sleep(0.25)
        data = socket.recv(20000)
        parser.feed(data)
        v = 0
        if args.measure == 'attention':
            if len(recorder.attention)>0:
Example #5
0
# 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
attention_duration = 0
Example #6
0
        dict(name='measure',
             type=str,
             nargs='?',
             const="attention",
             default="attention",
             help="""Measure you want feedback on. Either "meditation"
            or "attention\"""")
    ]
    mw_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])

    if args.measure == 'attention':
        measure_name = 'Attention'
    else:
        measure_name = 'Meditation'

    #bar = ProgressBar(widgets=[measure_name, Percentage(), Bar()]).start()

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('192.168.43.119', 2222)
    print >> sys.stderr, 'connecting to %s port %s' % server_address
    sock.connect(server_address)

    while 1:
        time.sleep(0.25)
Example #7
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