Esempio n. 1
0
    def run(self):  #子线程接受信号做处理
        sigf = False
        sig = None
        trig = None
        clk = sysclock()
        tt = {}
        for key in self.Trigger:
            tt[key] = []

        while True:  #确保大约0.1ms的执行interval,因此,可能存在没有数据返回
            if sysclock() - clk > 0:
                clk += 0.1
                if not self.Q_s2c.empty():
                    tts = deepcopy(tt)
                    ss = []
                    while not self.Q_s2c.empty():
                        s, t = self.Q_s2c.get()
                        ss.append(s)
                        for key in tts:
                            tts[key].append(t[key])

                    sig = np.hstack(ss)
                    for key in tts:
                        tts[key] = np.hstack(tts[key])
                    self.Process(1, (sig, tts))
                else:
                    self.Process(0, (0, 0))
            time.sleep(0.01)

            # s,t = self.Q_s2c.get()
            # self.Process(1,(s,t))
        print 'main subthre'
Esempio n. 2
0
def sock_process():
    f = open('_tem4mmap_.dat', 'w')
    f.write('\x00' * 25612)
    f.close()
    f = open('_tem4mmap_.dat', 'r+')
    mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)
    mm.seek(0)
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind(('192.168.43.162', 34217))
        # s.connect(('192.168.43.225', 6666 ))
    except socket.error as msg:
        print msg
        sys.exit(1)
    # s.sendall('hello')
    clk = sysclock()
    # ind = 0
    while True:
        # ind+=1

        cl = sysclock()
        print(cl - clk)
        clk = sysclock()
        data = s.recv(25612)

        datanew = struct.unpack('{0}f'.format(len(data) / 4), data)
        print(datanew[2])
        # if ind == 2:
        #     ind = 0
        #     mm.seek(0)
        mm.seek(0)
        # # print('writepo',mm.tell())
        mm.write(data)
Esempio n. 3
0
def phase_process(ph, p2c, c2p, g2p):
    PHASES = register_phase(ph)
    time.sleep(1)
    current_phase = 'start'  #phase必须从start开始
    p2c.put(current_phase)
    _clk = sysclock()

    while True:
        clk = sysclock()

        if clk - _clk > PHASES[current_phase]['duration']:
            current_phase = PHASES[current_phase]['next']
            p2c.put(current_phase)
            _clk = clk

        if not c2p.empty():
            typ, p = c2p.get()
            if typ == 'change' and PHASES.has_key(p):
                current_phase = p
                p2c.put(current_phase)
            else:
                write_log(self_name,
                          '[warning] someting error with <%s %s>' % (typ, p))

        if g2p.is_set():
            p2c.put('stop')
            break

        if current_phase == 'stop': break
        time.sleep(0.005)

    write_log('[info] process killed!')
Esempio n. 4
0
    def StartRun(self):
        self.Initialize()  #初始化变量,更新配置等

        self.p_once = self.configs['SamplingRate'] / 20

        ampname = self.configs['Amplifier']
        if ampname == 'simulator':
            self.amp = AmpSimulator(self.configs)
        elif ampname == 'actichamp':
            self.amp = AmpActichamp(self.configs)
        elif ampname == 'mindwave':
            self.amp = AmpMindwave(self.configs)
        elif ampname == 'xintuo':
            self.amp = AmpXintuo(self.configs)
        else:
            raise IOError, 'unsupported amplifier'

        if self.configs['SaveData']:
            st = Store(self.configs, self.q)
            p = multiprocessing.Process(target=st.run)
            p.daemon = True
            p.start()

        self.__trigger_ary = {}
        self.init_trigger_ary()

        self.sysclk = sysclock()
        self.start()
        print('bm_sigpropid', os.getpid())

        while True:
            if self.kil.is_set():
                break
            clk = sysclock()
            if clk - self.sysclk > 0.05:
                # print(clk-self.sysclk)
                signal = self.amp.read()  #read data from amplifier
                # print('miao')
                # while head-self.indx<1:
                # print('miao')
                # print(head,self.indx)
                # [head, signal] = self.amp.read()
                # self.indx = head
                # print(head)
                self.sysclk += 0.05
                tri = copy(self.__trigger_ary)  #get the trigger array
                if self.configs['SaveData']:
                    self.q.put([signal, tri])
                self.init_trigger_ary()  #reset trigger array
                self.s2c.put(self.Process(signal, tri))
            time.sleep(0.005)

        self.write_log('[info] process killed!')
Esempio n. 5
0
    def StartRun(self):
        self.p_once = self.configs['SamplingRate'] / 20

        ampname = self.configs['Amplifier']
        if ampname == 'simulator':
            self.amp = AmpSimulator(self.configs)
        elif ampname == 'actichamp':
            if not self.configs.has_key('addr'):
                raise IOError, 'actichamp config error! address is not given!'
            self.amp = AmpActichamp(self.configs)
        elif ampname == 'mindwave':
            self.amp = AmpMindwave(self.configs)
        elif ampname == 'xintuo':
            self.amp = AmpXintuo(self.configs)
        else:
            raise IOError, 'unsupported amplifier'

        self.sysclk = sysclock()
        self.start()

        while True:
            if self.E_c2s.is_set():
                break
            sclks, signals = self.amp.read(
            )  #等待信号,sclk是已经经过同步的,信号中带的时间戳是一次信号采集完成时记录的

            # 如果存在网络拥堵,那么在某一帧中可能受到多个信号包,和多个trigger
            sclks = np.array(sclks) - 0.05  #信号采集起始时刻
            trigger_array = self.init_trigger_ary(len(sclks))
            signal_array = np.hstack(signals)

            while len(self.gtris) > 0:  #遍历triggers
                gclk, trigger = item = self.gtris.pop(0)
                ind = np.where(sclks <= gclk)[0]
                if ind.size == 0:
                    self.write_log('[warning] late trigger')
                    indx = 0  #说明trigger延迟了
                else:
                    indx = ind[-1]
                er, p = self.definep(sclks[indx], gclk)
                if not er:  #正常写入
                    for key in trigger:
                        trigger_array[key][self.p_once * indx +
                                           p:] = trigger[key]
                        self.Trigger[key] = trigger[key]
                else:  #异常,trigger超出了信号包时间域范围,应当打入下一个信号包
                    self.gtris.insert(0, item)  #将其放回去
                    self.write_log(
                        "[warning] package late! Don't worry, we can handle it"
                    )
                    break

            if self.configs['SaveData']:
                self.storageQ.put([signal_array, trigger_array, 0])
            self.Q_s2c.put((signal_array, trigger_array))

        self.storageQ.put([0, 0, 1])
        print 'sigproend=================='
Esempio n. 6
0
def phase_process(phase_list,Q_p2c,Q_c2p,E_g2p):
    #phase_list:
    #接受一个列表,每一个元素是一个字典,用来描述这个phase
    #e.g.
    # ph = [  {'name':'start','next':'prompt','duration':1},
             # {'name':'prompt','next':'on','duration':1},
         # ]
    #Q_p2c: multiprocessing.Queue  phase -> core
    #Q_c2p: multiprocessing.Queue  core -> phase
    #E_g2p: multiprocessing.Event  gui -> phase  user cease signal

    PHASES = register_phase(phase_list)
    time.sleep(2)
    current_phase = 'start' #phase必须从start开始
    Q_p2c.put(current_phase)
    _clk = sysclock()

    while True:
        clk = sysclock()

        if clk - _clk > PHASES[current_phase]['duration']:
            current_phase = PHASES[current_phase]['next']
            Q_p2c.put(current_phase)
            _clk = clk
        
        if not Q_c2p.empty():
            typ,p = Q_c2p.get()
            if typ == 'change':
                if PHASES.has_key(p):
                    current_phase = p
                    Q_p2c.put(current_phase)
                else:
                    write_log(self_name,'[warning] change phase ?? <%s %s>'%(typ,p))
            elif typ == 'ask_cur_phase'
                Q_c2p.put(current_phase)
            else:
                pass

        if E_g2p.is_set():
            Q_p2c.put('stop')
            break

        if current_phase == 'stop': break
        time.sleep(0.005)
Esempio n. 7
0
    def run(self):
        while True:
            tri = self.c2s.get()
            p = int(self.p_once * (sysclock() - self.sysclk) /
                    0.05)  #计算当前时刻对应的采样点
            if p > self.p_once:
                p = -1
            if p < 0:
                p = 0

            self._lock.acquire()
            for n in tri:
                if self.Trigger.has_key(n):
                    self.Trigger[n] = tri[n]
                    self.__trigger_ary[n][p:] = tri[n]

                else:
                    self.write_log('[warning] trigger error')
            self._lock.release()
Esempio n. 8
0
                print '[actichamp] old data returned!'
            elif buf[2] - self.indx > 1:
                print '[actichamp] late package!'
            self.indx = buf[2]
            # print(self.indx)
        # print('indx',self.indx)
        return [self.indx, eeg]


if __name__ == '__main__':

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind(('192.168.43.162', 34217))
        # s.connect(('192.168.43.225', 6666 ))
    except socket.error as msg:
        print msg
        sys.exit(1)
    # s.sendall('hello')
    clk = sysclock()
    while True:
        cl = sysclock()
        if cl - clk > 0.06:
            print(cl - clk)
        clk = sysclock()

        data = s.recv(20000)
        # print(len(data))
        # datanew = struct.unpack('{0}f'.format(len(data)/4),data)
        # print(datanew[2])
Esempio n. 9
0
def write_log(m):
    print '[%s][%3.4f]%s' % (self_name, sysclock(), m)
Esempio n. 10
0
 def write_log(self, m):
     print '[Core][%.4f]%s' % (sysclock(), m)
Esempio n. 11
0
 def write_log(self, m):
     print '[Sigpro][%.4f]%s' % (sysclock(), m)