コード例 #1
0
    def transition(self, phase):
        write_log(phase)
        if phase == 'test':
            self.gui.update(
                {
                    'ssvep1': {
                        'start': True,
                        'bordercolor': (0, 0, 0)
                    },
                    'ssvep2': {
                        'start': True,
                        'bordercolor': (0, 0, 0)
                    },
                    'ssvep3': {
                        'start': True,
                        'bordercolor': (0, 0, 0)
                    },
                    'ssvep4': {
                        'start': True,
                        'bordercolor': (0, 0, 0)
                    }
                }, {})

            t = global_clock()
            print(t)
            self.marker.send_marker(
                {'process': {
                    'value': [1],
                    'timestamp': [t]
                }})  #开始测试

        elif phase == 'relax':
            self.gui.update(
                {
                    'ssvep1': {
                        'start': False
                    },
                    'ssvep2': {
                        'start': False
                    },
                    'ssvep3': {
                        'start': False
                    },
                    'ssvep4': {
                        'start': False
                    }
                }, {})

            self.marker.send_marker(
                {'process': {
                    'value': [2],
                    'timestamp': [global_clock()]
                }})  #测试完成

            self.test_count += 1
            if self.test_count > self.TEST_NUM:
                self.change_phase('rrelax')
コード例 #2
0
    def stop_run(self):

        self.marker.send_marker({
            'endsigpro': {
                'value': [1],
                'timestamp': [global_clock()]
            },
            'end-record': {
                'value': [1],
                'timestamp': [global_clock()]
            }
        })  #测试完成
        self.gui.quit()
        self.ROBOT.close()
コード例 #3
0
ファイル: sync_sock.py プロジェクト: yuejw1112/BCIpf
    def syncclock(self):
        record = np.zeros((20,4))
        for i in range(20):
            clkA = global_clock()
            clk_buf = struct.pack('d', clkA)
            self.sock.send(clk_buf)
            reply = self.sock.recv(128)
            clkB = global_clock()
            clk_server = struct.unpack('d',reply)[0]
            t = 0.5*(clkB - clkA)
            record[i,:] = np.array([t,clkA,clkB,clk_server])
            time.sleep(0.2)

        # 取通信时间最短的一组计算参数
        ind = np.argsort(record[:,0])[0]
        t,clkA,clkB,clk_server = record[ind,:]
        er = clk_server - t - clkA
        print(er)
        return er
コード例 #4
0
ファイル: sync_sock.py プロジェクト: yuejw1112/BCIpf
    def syncclock(self):
        while True:
            buf = self.client.recv(128)
            try:
                request = bytes.decode(buf, encoding = 'utf-8')
                if request[:7] == 'sync-ok':
                    return
            except:
                pass

            clock_buf = struct.pack('d',global_clock())
            self.client.send(clock_buf)
コード例 #5
0
    def run(self):  # 开启后始终将数据缓存到self.DATA中
        lastBlock = -1
        while not self.finish:
            # Get message header as raw array of chars
            rawhdr = RecvData(self.con, 24)
            # Split array into usefull information id1 to id4 are self.constants
            (id1, id2, id3, id4, msgsize, msgtype) = unpack('<llllLL', rawhdr)
            # Get data part of message, which is of variable size
            rawdata = RecvData(self.con, msgsize - 24)
            # Perform action dependend on the message type
            if msgtype == 1:
                # Start message, extract eeg properties and display them
                (channelCount, samplingInterval, resolutions,
                 channelNames) = GetProperties(rawdata)
                # reset block counter
                lastBlock = -1

                if len(self.eegchs) != channelCount:
                    raise SystemError(
                        '[bp amp] eeg channels is not match between configuration and device setting. configs: %d  device: %d'
                        % (len(self.eegchs), channelCount))

            elif msgtype == 4:
                # Data message, extract data and markers
                clk = global_clock(
                ) - self.interval  # 数据是50ms范围内的数据,因此受到数据的前0.05s才是数据起始时刻
                (block, points, markerCount, data,
                 markers) = GetData(rawdata, channelCount)
                if not self.deviceOK:
                    fs = points / self.interval
                    if fs != self.fs:
                        raise SystemError(
                            '[bp amp] samplingrate is not match between configuration and device setting. configs: %d  device: %d'
                            % (self.fs, fs))
                    self.deviceOK = True

                # Check for overflow
                if lastBlock != -1 and block > lastBlock + 1:
                    print('[bp amp] warning: Overflow with' +
                          str(block - lastBlock) + " datablocks")
                lastBlock = block

                self._lock.acquire()
                self.DATA.append(np.array(data).astype(np.float32))
                self.CLKS.append(clk)
                self._lock.release()

            elif msgtype == 3:
                self.finish = True

        self.con.close()
コード例 #6
0
    def StartRun(self):
        print('[guiengine] process started')
        self.E_g2c.set()
        clock = pygame.time.Clock()
        # END = 0
        while True:
            self.screen.fill(self.screen_color)
            #需要帧刷新的对象进行相应更新
            [sti.update_per_frame() for sti in self.__update_per_frame_list]

            if self.ask_4_update_gui:  #子线程请求刷新
                self.update_in_this_frame = True  #将在这一帧刷新
                self.ask_4_update_gui = False

            #.items()方法将stimuli字典转换为列表,元素为元祖,k[0]对应ID, k[1]为具体的刺激对象实例
            # 意思是按照layer属性排序
            stis = sorted(self.stimuli.items(), key=lambda k: k[1].layer)
            [s[1].show() for s in stis]  #按照图层书序顺序绘图,layer越大,越顶层
            pygame.display.flip()  #该帧刷新完毕

            if not SCREEN_SYNC: clock.tick(self.Fps)

            # 有刷新请求,且在该帧完成了刷新
            if self.update_in_this_frame:
                _clk = global_clock()
                if len(self.marker_event) > 0:  #确实接受到了marker
                    for ky in self.marker_event:  # 将时间戳记录下来
                        self.marker_event[ky]['timestamp'] = _clk
                    self.marker_sender.send_marker(self.marker_event)
                self.update_in_this_frame = False

            pygame.event.get()
            if self.stp: break  #只能通过主控结束

        pygame.quit()
        for ID in self.__release_ID_list:
            del self.stimuli[ID]
        print('[guiengine] process exit')
コード例 #7
0
ファイル: signal_generator.py プロジェクト: yuejw1112/BCIpf
 def read(self):
     data = self.y[:, :self.readpoint]
     self.y = np.hstack((self.y[:, self.readpoint:], data))
     return data, global_clock() - 0.1