예제 #1
0
    def get_frequency(self):
        # loop over receiving messages until we get a POLARIS_POSITION message
        while True:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0.001)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type
                dest_mod_id = msg.GetHeader().dest_mod_id
                if msg_type == MT_EXIT:
                    if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                        print "Received MT_EXIT, disconnecting..."
                        self.mod.SendSignal(rc.MT_EXIT_ACK)
                        self.mod.DisconnectFromMMM()
                        break
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, "PolarisDragonfly")
                else:
                    msg_type = msg.GetHeader().msg_type
                    if msg_type == rc.MT_POLARIS_POSITION:
                        # handling input message
                        mdf = rc.MDF_POLARIS_POSITION()
                        copy_from_msg(mdf, msg)
                        self.fsamp = 1 / mdf.sample_header.DeltaTime
                        if self.fsamp != 0:
                            break

        self.user_start_calibrate()
예제 #2
0
    def timer_event(self):
        done = False
        while not done:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type

                if msg_type == rc.MT_TASK_STATE_CONFIG:
                    mdf = rc.MDF_TASK_STATE_CONFIG()
                    copy_from_msg(mdf, msg)
                    x = mdf.target[0]
                    y = mdf.target[1]
                    self.setTargetPos(x, y)

                    #print "x: ", x, "|", self.tgt2pix(x) , " y: ", y, "|", self.tgt2pix(y)

                elif msg_type == rc.MT_ROBOT_CONTROL_SPACE_ACTUAL_STATE:
                    mdf = rc.MDF_ROBOT_CONTROL_SPACE_ACTUAL_STATE()
                    copy_from_msg(mdf, msg)
                    x = mdf.pos[0]
                    y = mdf.pos[1]
                    self.setCursorPos(x, y)

                    #print "x: ", mdf.pos[0], "y: ", mdf.pos[1]

                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'CursorDisplay')

                elif msg_type == MT_EXIT:
                    self.exit()
                    done = True

            else:
                done = True
예제 #3
0
    def timer_event(self):
        done = False
        while not done:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type
            
                if msg_type == rc.MT_TASK_STATE_CONFIG:
                    mdf = rc.MDF_TASK_STATE_CONFIG()
                    copy_from_msg(mdf, msg)
                    x = mdf.target[0]
                    y = mdf.target[1]
                    self.setTargetPos(x, y)

                    #print "x: ", x, "|", self.tgt2pix(x) , " y: ", y, "|", self.tgt2pix(y)
                
                elif msg_type == rc.MT_ROBOT_CONTROL_SPACE_ACTUAL_STATE:
                    mdf = rc.MDF_ROBOT_CONTROL_SPACE_ACTUAL_STATE()
                    copy_from_msg(mdf, msg)
                    x = mdf.pos[0]
                    y = mdf.pos[1]
                    self.setCursorPos(x, y)

                    #print "x: ", mdf.pos[0], "y: ", mdf.pos[1]
                
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'CursorDisplay')

                elif msg_type == MT_EXIT:
                    self.exit()
                    done = True
                
            else:
                done = True                
예제 #4
0
    def on_daq_callback(self, data):
        mdf = rc.MDF_PLOT_POSITION()
        self.serial_no += 1
        mdf.tool_id = 0
        mdf.missing = 0
        self.variable += 1
        mdf.xyz[:] = np.array([self.variable] * 3)
        mdf.ori[:] = np.array(
            [self.variable] * 4
        )  # will work but need!!! reading modules to know the format of buffer
        #mdf.buffer[data.size:] = -1
        msg = CMessage(rc.MT_PLOT_POSITION)
        copy_to_msg(mdf, msg)
        self.mod.SendMessage(msg)
        print self.variable
        sys.stdout.write('|')
        sys.stdout.flush()

        # now check for exit message
        in_msg = CMessage()
        rcv = self.mod.ReadMessage(msg, 0)
        if rcv == 1:
            hdr = msg.GetHeader()
            msg_type = hdr.msg_type
            dest_mod_id = hdr.dest_mod_id
            if msg_type == MT_EXIT:
                if (dest_mod_id == 0) or (dest_mod_id
                                          == self.mod.GetModuleID()):
                    print "Received MT_EXIT, disconnecting..."
                    self.daq_task.StopTask()
                    self.mod.SendSignal(rc.MT_EXIT_ACK)
                    self.mod.DisconnectFromMMM()
                    self.stop()
            elif msg_type == rc.MT_PING:
                respond_to_ping(self.mod, msg, 'RandomGen')
예제 #5
0
 def process_message(self, msg):
     # read a Dragonfly message
     msg_type = msg.GetHeader().msg_type
     dest_mod_id = msg.GetHeader().dest_mod_id
     if msg_type == MT_EXIT:
         if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
             print 'Received MT_EXIT, disconnecting...'
             self.mod.SendSignal(rc.MT_EXIT_ACK)
             self.mod.DisconnectFromMMM()
             return
     elif msg_type == rc.MT_PING:
         respond_to_ping(self.mod, msg, 'PlotHead')
     elif msg_type == rc.MT_POLARIS_POSITION:
         in_mdf = rc.MDF_POLARIS_POSITION()
         copy_from_msg(in_mdf, msg)
         positions = np.asarray(in_mdf.xyz[:])
         orientations = self.shuffle_q(np.asarray(in_mdf.ori[:]))
         if in_mdf.tool_id == (self.pointer + 1):
             Qf = qa.norm(orientations)
             Qr = qa.mult(Qf, qa.inv(self.pointer_Qi)).flatten()
             #find_nans(self.store_head, Qr, 'Qr')
             Tk = positions
             #find_nans(self.store_head, Tk, 'Tk')
             tip_pos = (qa.rotate(Qr, self.pointer_Xi) + Tk).flatten()
             self.pointer_position = np.append(self.pointer_position,
                                               (tip_pos[np.newaxis, :]),
                                               axis=0)
             #self.pl.reset(x=self.pointer_position[:,0], y=self.pointer_position[:,1], z=self.pointer_position[:,2])
             print("old=", tip_pos)
             print("new=", self.tp.get_pos(orientations, positions)[0])
예제 #6
0
    def run(self):
        while True:
            msg = PyDragonfly.CMessage()
            self.mod.ReadMessage(msg)  # blocking read
            print "Received message ", msg.GetHeader().msg_type

            if msg.GetHeader().msg_type == rc.MT_UR5_MOVEMENT_COMMAND:
                msg_data = rc.MDF_UR5_MOVEMENT_COMMAND()
                copy_from_msg(msg_data, msg)
                #position = np.frombuffer(msg_data.position)
                #print "  Data = [X: %d, Y: %d, Z: %f]" % \
                #    (msg_data.position[0], msg_data.position[1], msg_data.position[2])
                movement_complete = self.ur5.send_movement_command(
                    msg_data.position, msg_data.max_velocity,
                    msg_data.acceleration)
                if movement_complete:
                    # send movement complete message
                    self.mod.SendSignal(rc.MT_UR5_MOVEMENT_COMPLETE)
                else:
                    # print ur5 error message
                    print "No movement complete received."

            elif msg.GetHeader().msg_type == rc.MT_UR5_REQUEST_CONNECTED:
                if self.ur5.connected:
                    self.mod.SendSignal(rc.MT_UR5_CONNECTED)
                # ideally we'd capture not connected, but not today...

            elif msg.GetHeader().msg_type == rc.MT_PING:
                respond_to_ping(self.mod, msg, 'UR5Control')
예제 #7
0
    def timer_event(self):
        done = False
        while not done:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type

                if msg_type == rc.MT_HOTSPOT_POSITION: #rc.MT_RAW_SPIKECOUNT: #rc.MT_SPM_SPIKECOUNT:
                    mdf = rc.MDF_HOTSPOT_POSITION()
                    copy_from_msg(mdf, msg)
                    self.coil_tail = np.array(in_mdf.xyz[:])
                    self.coil_head = np.array(in_mdf.ori[:3])
                    self.update_judging_data()
                
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'PolarisDragonfly')

                #elif msg_type == MT_EXIT:
                #    self.exit()
                #    done = True

            else:
                done = True

        self.update_plot()
예제 #8
0
 def get_frequency(self):
     # loop over receiving messages until we get a POLARIS_POSITION message
     # get a POLARIS_POSITION message, read sample_header.DeltaTime to get
     # message frequency
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.001)
         if rcv == 1:
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if  msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break;
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'CoilPlotter')
             else:
                 msg_type = msg.GetHeader().msg_type
                 if msg_type == rc.MT_POLARIS_POSITION:
                     # handling input message
                     mdf = rc.MDF_POLARIS_POSITION()
                     copy_from_msg(mdf, msg)
                     self.fsamp = 1/mdf.sample_header.DeltaTime
                     if self.fsamp != 0:
                         break
                     
     self.user_start_calibrate()               
예제 #9
0
 def run(self):
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.1)
         if rcv == 1:
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print "Received MT_EXIT, disconnecting..."
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, "SimpleArbitrator")
             else:
                 self.process_message(msg)
예제 #10
0
 def run(self):
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.1)
         if rcv == 1:
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if  msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break;
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'ButtonDetector')
             else:
                 self.process_message(msg)
    def timer_event(self):
        done = False
        while not done:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type

                if msg_type == rc.MT_TASK_STATE_CONFIG:
                    self.tsc_mdf = rc.MDF_TASK_STATE_CONFIG()
                    copy_from_msg(self.tsc_mdf, msg)

                elif msg_type == rc.MT_FORCE_FEEDBACK:
                    mdf = rc.MDF_FORCE_FEEDBACK()
                    copy_from_msg(mdf, msg)

                    #self.fdbk_actual_pos = []
                    self.fdbk_actual_pos = [mdf.x, mdf.y, mdf.z, 0.0, 0.0, 0.0]

                    self.update_judging_data()

                elif msg_type == rc.MT_FORCE_SENSOR_DATA:
                    mdf = rc.MDF_FORCE_SENSOR_DATA()
                    copy_from_msg(mdf, msg)

                    self.fdbk_actual_pos = []
                    self.fdbk_actual_pos.extend(mdf.data)

                    self.update_judging_data()

                elif msg_type == rc.MT_END_TASK_STATE:
                    self.ets_mdf = rc.MDF_END_TASK_STATE()
                    copy_from_msg(self.ets_mdf, msg)

                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'SimpleDisplay')

                elif msg_type == MT_EXIT:
                    self.exit()
                    done = True

            else:
                done = True

        self.update_plot()
예제 #12
0
 def run(self):
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0)
         if rcv == 1:
             # read a Dragonfly message
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if  msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     return
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'fast_display_rms')
             else:
                 self.process_message(msg)
예제 #13
0
 def chk_msg(self):
     while True:
         in_msg = CMessage()
         rcv = self.mod.ReadMessage(in_msg, 0.1)
         if rcv == 1:
             msg_type = in_msg.GetHeader().msg_type
             if msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id
                                           == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, in_msg, 'Metronome')
             elif msg_type == self.in_msg_num:
                 in_mdf = eval('rc.MDF_%s()' % (self.in_msg_type.upper()))
                 copy_from_msg(in_mdf, in_msg)
                 return in_mdf.sample_header.DeltaTime
예제 #14
0
 def initial_ping(self):
     t = time.time()
     while time.time() < t + 10:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.001)
         if rcv == 1:
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id
                                           == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'TMS_Mapping_Collection')
                 break
             else:
                 pass
예제 #15
0
 def process_message(self, msg):
     # read a Dragonfly message
     msg_type = msg.GetHeader().msg_type
     dest_mod_id = msg.GetHeader().dest_mod_id
     if  msg_type == MT_EXIT:
         if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
             print 'Received MT_EXIT, disconnecting...'
             self.mod.SendSignal(rc.MT_EXIT_ACK)
             self.mod.DisconnectFromMMM()
             return
     elif msg_type == rc.MT_PING:
         respond_to_ping(self.mod, msg, 'FastDisplay')
     else:
         # if it is a NiDAQ message from channels 0-7, plot the data
         #self.counter += 1
         if msg_type == rc.MT_TRIGNO_DATA:
             sys.stdout.write("*")
             #sys.stdout.flush()
             mdf = rc.MDF_TRIGNO_DATA()
             copy_from_msg(mdf, msg)
             # add data to data buffers (necessary, or just use graphics buffers?)
             # update plots to new data buffers
             buf = mdf.T
         elif msg_type == rc.MT_SAMPLE_GENERATED:
             sys.stdout.write("#")
             sys.stdout.flush()
             buf = np.random.normal(1, 1, size=(432,))
         else:
             return False
             
         self.new_data[:,:-self.config.perchan] = self.old_data[:,self.config.perchan:]
         print (buf[0:2])
         for i in xrange(self.config.nchan):
             #if i == 0:
             #    print mdf.buffer[perchan * i:perchan * (i + 1)].size
             self.new_data[i, -self.config.perchan:] = buf[i:self.config.nchan * self.config.perchan:self.config.nchan]
             self.axes.flat[i].setData(self.new_data[i])
             #sys.stdout.write("*")
             #sys.stdout.flush()
         self.old_data[:] = self.new_data[:]
예제 #16
0
 def run(self):
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.001)
         if rcv == 1:
             msg_type = msg.GetHeader().msg_type
             dest_mod_id = msg.GetHeader().dest_mod_id
             if  msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'CoilPlotter')
             elif (msg_type == rc.MT_POLARIS_POSITION) & (self.calibrated == False):
                 self.calibrate_head(msg)
             elif msg_type == rc.MT_TMS_TRIGGER:
                 self.set_collecting = True
                 self.got_coil = False
                 self.got_head = False
             else:
                 self.process_message(msg)
예제 #17
0
    def run(self):
        while True:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0.1)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type
                dest_mod_id = msg.GetHeader().dest_mod_id
                if msg_type == MT_EXIT:
                    if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                        print "Received MT_EXIT, disconnecting..."
                        self.mod.SendSignal(rc.MT_EXIT_ACK)
                        self.mod.DisconnectFromMMM()
                        break
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, "TrialStatus")
                else:
                    self.process_message(msg)

            this_time = time()
            self.diff_time = this_time - self.last_time
            if self.diff_time > 1.0:
                self.last_time = this_time
                self.write()
예제 #18
0
 def run(self):
     self.delta_time_calc = self.default_timer()  #time.time()
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.001)
         if rcv == 1:
             hdr = msg.GetHeader()
             msg_type = hdr.msg_type
             dest_mod_id = hdr.dest_mod_id
             if msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id
                                           == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'SampleGenerator')
             elif (msg_type == rc.MT_SPM_SPIKECOUNT):
                 msg_src_mod_id = hdr.src_mod_id
                 if msg_src_mod_id == rc.MID_SPM_MOD:
                     print "\n\n ** Detected SPM_SPIKECOUNT messages coming from SPM_MOD! Quitting..\n\n"
                     sys.exit(0)
             else:
                 if len(self.triggers) > 0:
                     self.process_msg(msg)
         else:
             # if no triggers...
             if len(self.triggers) == 0:
                 period = (1. / self.freq)
                 time_now = self.default_timer()
                 delta_time = period - (time_now - self.delta_time_calc)
                 #print "%f %f %f\n\n" % (time_now, self.delta_time_calc, delta_time)
                 if delta_time > 0:
                     time.sleep(delta_time)
                 self.delta_time_calc = self.delta_time_calc + period
                 self.send_sample_generated()
예제 #19
0
 def process_message(self, msg):
     # read a Dragonfly message
     msg_type = msg.GetHeader().msg_type
     dest_mod_id = msg.GetHeader().dest_mod_id
     if  msg_type == MT_EXIT:
         if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
             print 'Received MT_EXIT, disconnecting...'
             self.mod.SendSignal(rc.MT_EXIT_ACK)
             self.mod.DisconnectFromMMM()
             return
     elif msg_type == rc.MT_PING:
         respond_to_ping(self.mod, msg, 'PlotHead')
     elif msg_type == rc.MT_PLOT_POSITION:
         in_mdf = rc.MDF_PLOT_POSITION()
         copy_from_msg(in_mdf, msg)
         tail = np.array(in_mdf.xyz[:])*0.127 + (self.plot_vertex_vec)#Hotspot position
         head = np.array(in_mdf.ori[:3])/4 #Vector head of coil, used to find ori
              
         if np.any(np.isnan(tail)) == True:
             pass
         elif np.any(np.isnan(head)) == True:
              pass
         elif np.any(np.isinf(tail)) == True:
             pass
         elif np.any(np.isinf(head)) == True:
             pass
         else:
             queue.put(np.vstack((head, tail)))
             self.count=+1
             print 'sent message'
     elif msg_type == rc.MT_MNOME_STATE:
         in_mdf = rc.MDF_MNOME_STATE()
         copy_from_msg(in_mdf, msg)
         if in_mdf.state == 0:
             print 'got clear'
             self.parent.reset = True
예제 #20
0
    def run(self):
        while True:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0.1)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type
                dest_mod_id = msg.GetHeader().dest_mod_id
                if msg_type == MT_EXIT:
                    if (dest_mod_id == 0) or (dest_mod_id
                                              == self.mod.GetModuleID()):
                        print 'Received MT_EXIT, disconnecting...'
                        self.mod.SendSignal(rc.MT_EXIT_ACK)
                        self.mod.DisconnectFromMMM()
                        break
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'TrialStatus')
                else:
                    self.process_message(msg)

            this_time = time()
            self.diff_time = this_time - self.last_time
            if self.diff_time > 1.:
                self.last_time = this_time
                self.write()
예제 #21
0
 def run(self):
     self.delta_time_calc = self.default_timer() #time.time()
     while True:
         msg = CMessage()
         rcv = self.mod.ReadMessage(msg, 0.001)
         if rcv == 1:
             hdr = msg.GetHeader()
             msg_type = hdr.msg_type
             dest_mod_id = hdr.dest_mod_id
             if  msg_type == MT_EXIT:
                 if (dest_mod_id == 0) or (dest_mod_id == self.mod.GetModuleID()):
                     print 'Received MT_EXIT, disconnecting...'
                     self.mod.SendSignal(rc.MT_EXIT_ACK)
                     self.mod.DisconnectFromMMM()
                     break;            
             elif msg_type == rc.MT_PING:
                 respond_to_ping(self.mod, msg, 'SampleGenerator')
             elif (msg_type == rc.MT_SPM_SPIKECOUNT):
                 msg_src_mod_id = hdr.src_mod_id
                 if msg_src_mod_id == rc.MID_SPM_MOD:
                     print "\n\n ** Detected SPM_SPIKECOUNT messages coming from SPM_MOD! Quitting..\n\n";
                     sys.exit(0);
             else:
                 if len(self.triggers) > 0:
                     self.process_msg(msg)
         else:
             # if no triggers...
             if len(self.triggers) == 0:
                 period = (1. / self.freq)
                 time_now = self.default_timer()
                 delta_time = period - (time_now - self.delta_time_calc)
                 #print "%f %f %f\n\n" % (time_now, self.delta_time_calc, delta_time)
                 if delta_time > 0:
                     time.sleep(delta_time)
                 self.delta_time_calc = self.delta_time_calc + period
                 self.send_sample_generated()
예제 #22
0
    def run(self):
        while True:
            in_msg = CMessage()
            rcv = self.mod.ReadMessage(in_msg, 0.1)
            if rcv == 1:
                msg_type = in_msg.GetHeader().msg_type
                if msg_type == MT_EXIT:
                    if (dest_mod_id == 0) or (dest_mod_id
                                              == self.mod.GetModuleID()):
                        print 'Received MT_EXIT, disconnecting...'
                        self.mod.SendSignal(rc.MT_EXIT_ACK)
                        self.mod.DisconnectFromMMM()
                        break
                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, in_msg, 'Metronome')
                elif msg_type == rc.MT_MNOME_STATE:
                    print 'got message'
                    in_mdf = rc.MDF_MNOME_STATE()
                    copy_from_msg(in_mdf, in_msg)
                    if in_mdf.state == 0:
                        print 'got stop'
                        self.pause_state = True
                        self.count = 0
                    elif in_mdf.state == 1:
                        print 'got start'
                        self.pause_state = False
                        self.count = 0
                    elif in_mdf.state == 2:
                        print 'got pause'
                        self.pause_state = True
                        self.count = 0
                elif msg_type == self.in_msg_num:
                    if self.pause_state:
                        pass
                    else:
                        self.count += 1
                        if self.pretrigger_time > 0:
                            if self.count == self.metronome_count:
                                in_mdf = eval('rc.MDF_%s()' %
                                              (self.in_msg_type.upper()))
                                copy_from_msg(in_mdf, in_msg)
                                out_mdf = rc.MDF_TMS_TRIGGER()
                                out_mdf.sample_header = in_mdf.sample_header
                                out_msg = CMessage(rc.MT_TMS_TRIGGER)
                                copy_to_msg(out_mdf, out_msg)
                                self.mod.SendMessage(out_msg)
                                self.count = 0 - int(
                                    np.random.uniform(0, 1.5, 1)[0] *
                                    self.in_msg_freq)

                            if self.count == self.trigger_out_count:
                                sound_thread = threading.Thread(
                                    target=self.play_sound)
                                sound_thread.start()

                        else:
                            if self.count == self.trigger_out_count:
                                in_mdf = eval('rc.MDF_%s()' %
                                              (self.in_msg_type.upper()))
                                copy_from_msg(in_mdf, in_msg)
                                out_mdf = rc.MDF_TMS_TRIGGER()
                                out_mdf.sample_header = in_mdf.sample_header
                                out_msg = CMessage(rc.MT_TMS_TRIGGER)
                                copy_to_msg(out_mdf, out_msg)
                                self.mod.SendMessage(out_msg)

                            if self.count == self.metronome_count:
                                self.count = 0 - int(
                                    np.random.uniform(0, 1.5, 1)[0] *
                                    self.in_msg_freq)
                                sound_thread = threading.Thread(
                                    target=self.play_sound)
                                sound_thread.start()
예제 #23
0
    def timer_event(self):
        done = False
        while not done:
            msg = CMessage()
            rcv = self.mod.ReadMessage(msg, 0)
            if rcv == 1:
                msg_type = msg.GetHeader().msg_type

                # SESSION_CONFIG => start of session
                if msg_type == rc.MT_SESSION_CONFIG:
                    #self.msg_cnt += 1
                    self.num_trials = 0
                    self.reset_counters()
                    self.update_gui_label_data()


                # EM_DECODER_CONFIGURATION => end of an adaptation round
                elif msg_type == rc.MT_EM_DECODER_CONFIGURATION:
                    #self.msg_cnt += 1
                    self.reset_counters()
                    self.update_gui_label_data()

                # END_TASK_STATE => end of a task
                elif msg_type == rc.MT_END_TASK_STATE:
                    #self.msg_cnt += 1
                    mdf = rc.MDF_END_TASK_STATE()
                    copy_from_msg(mdf, msg)

                    # need to know:
                    #    begin task state code
                    #    final task state code
                    #    intertrial state code

                    if (mdf.id == 1):
                        self.trial_sync = 1
                        self.shadow_started_window.append(0)

                    if (mdf.id == self.task_state_codes['begin']) & (mdf.outcome == 1):
                        if self.trial_sync:
                            #print "*** trial started ***"
                            #self.rewards_given += 1
                            self.shadow_num_trial_started_postcalib += 1
                            self.shadow_success_window.append(0)
                            self.shadow_givenup_window.append(0)
                            self.shadow_started_window[-1] = 1

                    if mdf.reason == "JV_IDLE_TIMEOUT":
                        if self.trial_sync:
                            self.shadow_num_trial_givenup_postcalib += 1
                            self.shadow_givenup_window[-1] = 1

                    if (mdf.id == self.task_state_codes['final']) & (mdf.outcome == 1):
                        if self.trial_sync:
                            #print "*** trial complete and successful"
                            self.shadow_num_trial_successful_postcalib += 1
                            self.shadow_success_window[-1] = 1

                    if (mdf.id == self.task_state_codes['intertrial']):
                        if self.trial_sync:
                            # do end-of-trial stuff here
                            self.num_trials += 1
                            self.num_trials_postcalib += 1
                            self.num_trial_started_postcalib = self.shadow_num_trial_started_postcalib
                            self.num_trial_successful_postcalib = self.shadow_num_trial_successful_postcalib
                            self.num_trial_givenup_postcalib = self.shadow_num_trial_givenup_postcalib

                            if len(self.shadow_success_window) > self.window_wide: #self.window_narrow:
                                self.shadow_success_window.pop(0)

                            if len(self.shadow_givenup_window) > self.window_wide: #self.window_narrow:
                                self.shadow_givenup_window.pop(0)

                            if len(self.shadow_started_window) > self.window_wide: #self.window_narrow:
                                self.shadow_started_window.pop(0)

                            self.success_window = copy.deepcopy(self.shadow_success_window)
                            self.started_window = copy.deepcopy(self.shadow_started_window)
                            self.givenup_window = copy.deepcopy(self.shadow_givenup_window)

                            if self.num_trials_postcalib > 0:
                                self.percent_start = 100 * self.num_trial_started_postcalib / self.num_trials_postcalib
                                self.percent_givenup = 100 * self.num_trial_givenup_postcalib / self.num_trials_postcalib
                                self.percent_success = 100 * self.num_trial_successful_postcalib / self.num_trials_postcalib


                            percent_success_wide_window = np.NAN
                            if len(self.success_window) >= self.window_wide:
                                num_success_window = np.sum(self.success_window)
                                percent_success_wide_window = 100 * num_success_window / len(self.success_window)

                            percent_givenup_wide_window = np.NAN
                            if len(self.givenup_window) >= self.window_wide:
                                num_givenup_window = np.sum(self.givenup_window)
                                percent_givenup_wide_window = 100 * num_givenup_window / len(self.givenup_window)

                            percent_started_wide_window = np.NAN
                            if len(self.started_window) >= self.window_wide:
                                num_started_window = np.sum(self.started_window)
                                percent_started_wide_window = 100 * num_started_window / len(self.started_window)

                            percent_success_narrow_window = np.NAN
                            if len(self.success_window) >= self.window_narrow:
                                success_window_narrow = self.success_window[len(self.success_window)-self.window_narrow:]
                                num_success_window = np.sum(success_window_narrow)
                                percent_success_narrow_window = 100 * num_success_window / len(success_window_narrow)

                            percent_givenup_narrow_window = np.NAN
                            if len(self.givenup_window) >= self.window_narrow:
                                givenup_window_narrow = self.givenup_window[len(self.givenup_window)-self.window_narrow:]
                                num_givenup_window = np.sum(givenup_window_narrow)
                                percent_givenup_narrow_window = 100 * num_givenup_window / len(givenup_window_narrow)

                            if len(self.started_window) >= self.window_narrow:
                                started_window_narrow = self.started_window[len(self.started_window)-self.window_narrow:]
                                num_started_window = np.sum(started_window_narrow)
                                percent_started_narrow_window = 100 * num_started_window / len(started_window_narrow)
                                self.hist_narrow_STR.append(percent_started_narrow_window)
                                self.hist_narrow_SUR.append(percent_success_narrow_window)
                                self.hist_narrow_GUR.append(percent_givenup_narrow_window)

                                self.hist_wide_STR.append(percent_started_wide_window)
                                self.hist_wide_SUR.append(percent_success_wide_window)
                                self.hist_wide_GUR.append(percent_givenup_wide_window)

                            self.update_gui_label_data()


                elif msg_type == rc.MT_PING:
                    respond_to_ping(self.mod, msg, 'TrialStatusDisplay')

                elif msg_type == MT_EXIT:
                    self.exit()
                    done = True

            else:
                done = True

                self.console_disp_cnt += 1
                if self.console_disp_cnt == 50:
                    self.update_plot()
                    self.console_disp_cnt = 0