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()
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')
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
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 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)
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
def run(self): mod = Dragonfly_Module(self.mid, 0) mod.ConnectToMMM(self.server) for sub in self.subs: print "subscribing to %s" % (sub) mod.Subscribe(sub) mod.SendModuleReady() while (self.status()): msg = CMessage() rcv = mod.ReadMessage(msg, 0) if rcv == 1: if msg.GetHeader().msg_type in self.subs: self.recv_msg(msg) sleep(.001)
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)
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()
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()
def run(self): while True: msg = CMessage() rcv = self.mod.ReadMessage(msg, 0.1) if rcv == 1: msg_type = msg.GetHeader().msg_type if msg_type == rc.MT_APP_START: try: mdf = rc.MDF_APP_START() copy_from_msg(mdf, msg) config = mdf.config print "Config: %s" % config # -- to do -- # get a list of all modules in appman.conf for this host # see if any of the modules above are already/still running # start non-running modules # -- to do -- print "Creating scripts" appman.create_script(config, self.host_name) print "Starting modules on host: %s" % self.host_name appman.run_script(self.host_name) self.mod.SendSignal(rc.MT_APP_START_COMPLETE) except Exception, e: print "ERROR: %s" % (e) elif msg_type == rc.MT_PING: print 'got ping' self.respond_to_ping(msg, 'AppStarter') # we use this msg to stop modules individually elif msg_type == MT_EXIT: print 'got exit' elif msg_type == MT_KILL: print 'got kill' appman.kill_modules()
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
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()
class Display(ColorLayer): is_event_handler = True def __init__(self): super(Display, self).__init__(180, 180, 180, 255) self.mod = Dragonfly_Module(0, 0) self.mod.ConnectToMMM("everest:7111") self.mod.Subscribe(rc.MT_TASK_STATE_CONFIG) self.mod.Subscribe(rc.MT_INPUT_DOF_DATA) self.mod.Subscribe(rc.MT_COMBO_WAIT) self.mod.Subscribe(rc.MT_TRIAL_CONFIG) self.mod.Subscribe(rc.MT_END_TASK_STATE) self.mod.Subscribe(rc.MT_PING) self.msg = CMessage() self.timer_sec = 0 self.timer_min = 0 self.blank_display = False self.position_bar = Polygon(v=[(20, 455), (20, 565), (1260, 565), (1260, 455)], color=(0.05, 0.05, 0.05, 1), stroke=0) self.tgt_window = Polygon(v=[(0, 0), (0, 0), (0, 0), (0, 0)], color=(0, 0.6, 0.2, 1), stroke=0) self.pos_fdbk = Polygon(v=[(20, 405), (20, 615), (28, 615), (28, 405)], color=(0, 0.2, 1.0, 1), stroke=0) self.pos_fdbk_txt = pyglet.text.Label('', font_name='times new roman', font_size=32, color=(0, 0, 0, 255), width=250, bold=True, x=1140, y=40) self.combo_wait_txt = pyglet.text.Label('', font_name='times new roman', font_size=32, color=(0, 0, 0, 255), width=250, bold=True, x=478, y=840) self.score_txt = pyglet.text.Label('', font_name='times new roman', font_size=36, color=(0, 0, 0, 255), width=250, bold=True, x=533, y=240) self.reward_txt = pyglet.text.Label('', font_name='times new roman', font_size=22, color=(0, 0, 0, 255), width=250, bold=True, x=475, y=120) self.reset_score() self.schedule_interval(self.update, 0.01) def timer_count_down(self, dt): self.timer_sec -= 1 if self.timer_sec < 0: if self.timer_min > 0: self.timer_min -= 1 self.timer_sec = 59 else: self.unschedule(self.timer_count_down) self.combo_wait_txt.text = '' return self.combo_wait_txt.text = 'Relax Time %d:%02d' % (self.timer_min, self.timer_sec) if self.timer_min == 0 and self.timer_sec <= 5 and self.timer_sec > 0: winsound.PlaySound( os.path.join(os.environ.get('ROBOT_CONFIG'), 'default', 'gocue.wav'), winsound.SND_FILENAME | winsound.SND_ASYNC) def reset_score(self): self.score = 0 self.score_force_level = 0 self.score_target_dist = 999 self.score_force_mult = 0 self.score_target_mult = 0 self.score_txt.text = "Score: %d" % self.score self.reward_txt.text = "" def update_reward(self): reward = self.score_force_mult * self.score_target_mult pts = "points" if reward == 1: pts = "point" self.reward_txt.text = "Current Reward: %d %s" % (reward, pts) def increment_score(self): self.score += self.score_force_mult * self.score_target_mult self.score_txt.text = "Score: %d" % self.score def update(self, dt): while True: rcv = self.mod.ReadMessage(self.msg, 0) if rcv == 1: hdr = self.msg.GetHeader() msg_type = hdr.msg_type if msg_type == rc.MT_PING: self.reset_score() elif msg_type == rc.MT_INPUT_DOF_DATA: mdf = rc.MDF_INPUT_DOF_DATA() copy_from_msg(mdf, self.msg) if mdf.tag == 'carduinoIO': fdbk = 5 - mdf.dof_vals[ 7] # invert to match phyiscal setup x_pos = int((fdbk * (MAX_WIDTH - 2 * OFFSET)) / 5.0) self.pos_fdbk_txt.text = "%.2f V" % fdbk self.pos_fdbk.v[0] = (x_pos, 405) self.pos_fdbk.v[1] = (x_pos, 615) self.pos_fdbk.v[2] = (x_pos + 8, 615) self.pos_fdbk.v[3] = (x_pos + 8, 405) # if msg_type == rc.MT_FORCE_SENSOR_DATA: # mdf = rc.MDF_FORCE_SENSOR_DATA() # copy_from_msg(mdf, self.msg) # # x_fdbk = mdf.data[0] # x_fdbk_width = int((x_fdbk / MAX_FDBK) * MAX_WIDTH) elif msg_type == rc.MT_COMBO_WAIT: mdf = rc.MDF_COMBO_WAIT() copy_from_msg(mdf, self.msg) print mdf.duration duration = mdf.duration / 1000 # convert to seconds self.timer_sec = duration % 60 self.timer_min = duration / 60 self.schedule_interval(self.timer_count_down, 1) elif msg_type == rc.MT_TRIAL_CONFIG: self.unschedule(self.timer_count_down) self.combo_wait_txt.text = '' # start displaying again self.blank_display = False self.color = (180, 180, 180) elif msg_type == rc.MT_END_TASK_STATE: mdf = rc.MDF_END_TASK_STATE() copy_from_msg(mdf, self.msg) if (mdf.id == REWARD_TS) and (mdf.outcome == 1): self.increment_score() if (mdf.id in [2, 3, 4]) and (mdf.outcome == 0): self.color = (0, 0, 0) self.blank_display = True elif msg_type == rc.MT_TASK_STATE_CONFIG: mdf = rc.MDF_TASK_STATE_CONFIG() copy_from_msg(mdf, self.msg) if mdf.background_color == 'gray': self.color = (180, 180, 180) elif mdf.background_color == 'red': self.color = (150, 12, 12) elif mdf.background_color == 'green': self.color = (0, 150, 50) if mdf.fdbk_display_color == 'gray': self.tgt_window.color = (0.3, 0.3, 0.3, 1) elif mdf.fdbk_display_color == 'yellow': self.tgt_window.color = (0.5, 0.5, 0.0, 1) elif mdf.fdbk_display_color == 'green': self.tgt_window.color = (0.0, 0.6, 0.2, 1) elif mdf.fdbk_display_color == 'red': self.tgt_window.color = (0.6, 0.05, 0.05, 1) if not (math.isnan(mdf.target[0])) and not (math.isnan( mdf.target[1])): x_tgt_lo = int( (mdf.target[0] * (MAX_WIDTH - 2 * OFFSET)) / 5.0) x_tgt_hi = int( (mdf.target[1] * (MAX_WIDTH - 2 * OFFSET)) / 5.0) self.tgt_window.v[0] = (x_tgt_lo, 430) self.tgt_window.v[1] = (x_tgt_lo, 590) self.tgt_window.v[2] = (x_tgt_hi, 590) self.tgt_window.v[3] = (x_tgt_hi, 430) # update multipliers during "ForceRamp" task state if mdf.id == FORCERAMP_TS: force_level = mdf.sep_threshold_f[1] if force_level > self.score_force_level: self.score_force_level = force_level self.score_force_mult += 1 # new combo, reset target multipliers self.score_target_mult = 0 self.score_target_dist = 999 target_level = mdf.target[1] - mdf.target[0] if target_level < self.score_target_dist: self.score_target_dist = target_level self.score_target_mult += 1 self.update_reward() #print mdf.id #print force_level, self.score_force_level, self.score_force_mult #print target_level, self.score_target_dist, self.score_target_mult #print "\n" else: break #def on_key_release( self, keys, mod ): def draw(self): super(Display, self).draw() if not self.blank_display: self.position_bar.render() self.tgt_window.render() self.pos_fdbk.render() #self.pos_fdbk_txt.draw() self.combo_wait_txt.draw() self.score_txt.draw() self.reward_txt.draw()
class Display(ColorLayer): is_event_handler = True def __init__(self): super(Display, self).__init__(180, 180, 180, 255) self.mod = Dragonfly_Module(0, 0) self.mod.ConnectToMMM() self.mod.Subscribe(rc.MT_TASK_STATE_CONFIG) self.mod.Subscribe(rc.MT_INPUT_DOF_DATA) self.mod.Subscribe(rc.MT_COMBO_WAIT) self.mod.Subscribe(rc.MT_TRIAL_CONFIG) self.mod.Subscribe(rc.MT_END_TASK_STATE) self.mod.Subscribe(rc.MT_PING) self.mod.Subscribe(rc.MT_RT_POSITION_FEEDBACK) self.msg = CMessage() self.timer_sec = 0 self.timer_min = 0 self.transformationType = 0 dims = director.get_window_size() self.width = dims[0] self.height = dims[1] self.oldWidth = 1280 self.oldHeight = 1024 self.blank_display = False self.position_bar = Polygon(v=[(20, 455), (20, 565), (1260, 565), (1260, 455)], color=(0.05, 0.05, 0.05, 1), stroke=0) self.resizePolygon(self.position_bar) self.tgt_window = Polygon(v=[(0, 0), (0, 0), (0, 0), (0, 0)], color=(0, 0.6, 0.2, 1), stroke=0) self.resizePolygon(self.tgt_window) self.pos_fdbk = Polygon(v=[(20, 405), (20, 615), (28, 615), (28, 405)], color=(0, 0.2, 1.0, 1), stroke=0) self.resizePolygon(self.pos_fdbk) self.pos_fdbk_txt = pyglet.text.Label('', font_name='times new roman', font_size=32, color=(0, 0, 0, 255), width=250, bold=True, x=1140, y=40) self.combo_wait_txt = pyglet.text.Label('', font_name='times new roman', font_size=32, color=(255, 255, 255, 255), width=250, bold=True, x=478, y=840) self.score_txt = pyglet.text.Label('', font_name='times new roman', font_size=36, color=(0, 0, 0, 255), width=250, bold=True, x=500, y=240) self.reward_txt = pyglet.text.Label('', font_name='times new roman', font_size=22, color=(0, 0, 0, 255), width=250, bold=True, x=475, y=120) self.reset_score() self.schedule_interval(self.update, 0.01) # def timer_count_down(self, dt): # self.timer_sec -= 1 # # if self.timer_sec < 0: # if self.timer_min > 0: # self.timer_min -= 1 # self.timer_sec = 59 # else: # self.unschedule(self.timer_count_down) # self.combo_wait_txt.text = '' # self.screen_on() # return # # self.combo_wait_txt.text = 'Relax Time %d:%02d' % (self.timer_min, self.timer_sec) # # if self.timer_min == 0 and self.timer_sec <= 5 and self.timer_sec > 0: # winsound.PlaySound(os.path.join(os.environ.get('ROBOT_CONFIG'), 'default', 'gocue.wav'), winsound.SND_FILENAME | winsound.SND_ASYNC) def resizePolygon(self, polygon): for i in xrange(4): polygon.v[i] = (polygon.v[i][0] * self.width / self.oldWidth, polygon.v[i][1] * self.height / self.oldHeight) def reset_score(self): self.score = 0 self.score_force_level = 0 self.score_target_dist = 999 self.score_force_mult = 0 self.score_target_mult = 0 self.score_txt.text = "Score: %d" % self.score self.reward_txt.text = "" def update_reward(self): reward = self.score_force_mult * self.score_target_mult pts = "points" if reward == 1: pts = "point" self.reward_txt.text = "Current Reward: %d %s" % (reward, pts) def increment_score(self): self.score += 1 #self.score_force_mult * self.score_target_mult self.score_txt.text = "Score: %d" % self.score def screen_off(self): self.color = (0, 0, 0) self.blank_display = True def screen_on(self): self.blank_display = False self.color = (180, 180, 180) def update(self, dt): while True: rcv = self.mod.ReadMessage(self.msg, 0) if rcv == 1: hdr = self.msg.GetHeader() msg_type = hdr.msg_type if msg_type == rc.MT_PING: self.reset_score() elif msg_type == rc.MT_INPUT_DOF_DATA: mdf = rc.MDF_INPUT_DOF_DATA() copy_from_msg(mdf, self.msg) if mdf.tag == 'carduinoIO': fdbk = 5 - mdf.dof_vals[ 7] # invert to match phyiscal setup x_pos = int((fdbk * (MAX_WIDTH - 2 * OFFSET)) / 5.0) x_pos += 20 self.pos_fdbk_txt.text = "%.2f V" % fdbk self.pos_fdbk.v[0] = (x_pos, 405) self.pos_fdbk.v[1] = (x_pos, 615) self.pos_fdbk.v[2] = (x_pos + 8, 615) self.pos_fdbk.v[3] = (x_pos + 8, 405) # if msg_type == rc.MT_FORCE_SENSOR_DATA: # mdf = rc.MDF_FORCE_SENSOR_DATA() # copy_from_msg(mdf, self.msg) # # x_fdbk = mdf.data[0] # x_fdbk_width = int((x_fdbk / MAX_FDBK) * MAX_WIDTH) elif msg_type == rc.MT_RT_POSITION_FEEDBACK: # updates real time position of handle on screen receives messages from cube_sphere while loop mdf = rc.MDF_RT_POSITION_FEEDBACK() copy_from_msg(mdf, self.msg) x_pos = mdf.distanceFromCenter x_pos += 20 self.pos_fdbk.v[0] = (x_pos, 405) self.pos_fdbk.v[1] = (x_pos, 615) self.pos_fdbk.v[2] = (x_pos + 8, 615) self.pos_fdbk.v[3] = (x_pos + 8, 405) self.resizePolygon(self.pos_fdbk) self.transformPolygon(self.pos_fdbk, self.transformationType) elif msg_type == rc.MT_COMBO_WAIT: mdf = rc.MDF_COMBO_WAIT() copy_from_msg(mdf, self.msg) print mdf.duration duration = mdf.duration / 1000 # convert to seconds self.timer_sec = duration % 60 self.timer_min = duration / 60 self.screen_off() self.schedule_interval(self.timer_count_down, 1) elif msg_type == rc.MT_TRIAL_CONFIG: self.unschedule(self.timer_count_down) self.combo_wait_txt.text = '' self.screen_on() elif msg_type == rc.MT_END_TASK_STATE: mdf = rc.MDF_END_TASK_STATE() #copy_from_msg(mdf, self.msg) read_msg_data(mdf, self.msg) print mdf.id, mdf.outcome if (mdf.id == REWARD_TS) and (mdf.outcome == 1): self.increment_score() if (mdf.id in [2, 3, 4]) and (mdf.outcome == 0): print "screen off" self.screen_off() elif msg_type == rc.MT_TASK_STATE_CONFIG: mdf = rc.MDF_TASK_STATE_CONFIG() copy_from_msg(mdf, self.msg) if mdf.background_color == 'gray': self.color = (180, 180, 180) elif mdf.background_color == 'red': self.color = (150, 12, 12) elif mdf.background_color == 'green': self.color = (0, 150, 50) if mdf.fdbk_display_color == 'gray': self.tgt_window.color = (0.3, 0.3, 0.3, 1) elif mdf.fdbk_display_color == 'yellow': self.increment_score() self.tgt_window.color = (0.5, 0.5, 0.0, 1) elif mdf.fdbk_display_color == 'green': self.tgt_window.color = (0.0, 0.6, 0.2, 1) elif mdf.fdbk_display_color == 'red': self.tgt_window.color = (0.6, 0.05, 0.05, 1) if not math.isnan( mdf.direction) and mdf.direction in range( -1, 3 ) and not mdf.direction == self.transformationType: self.position_bar.v = [(20, 455), (20, 565), (1260, 565), (1260, 455)] self.resizePolygon(self.position_bar) self.transformPolygon(self.position_bar, mdf.direction) self.transformPolygon(self.pos_fdbk, mdf.direction) self.transformationType = mdf.direction if not (math.isnan(mdf.target[0])) and not (math.isnan( mdf.target[1])): x_tgt_lo = mdf.target[0] + 20 x_tgt_hi = mdf.target[1] + 20 self.tgt_window.v[0] = (x_tgt_lo, 430) self.tgt_window.v[1] = (x_tgt_lo, 590) self.tgt_window.v[2] = (x_tgt_hi, 590) self.tgt_window.v[3] = (x_tgt_hi, 430) self.resizePolygon(self.tgt_window) self.transformPolygon(self.tgt_window, self.transformationType) else: break def transformPolygon(self, polygon, transformationType): for i in xrange(4): polygon.v[i] = self.transformPoint(polygon.v[i], transformationType) def transformPoint(self, input_point, transformationType): # tt values are determined by adding XorYorZ and UpOrDown if transformationType == 0: # goes from left to right XorYorZ=1 UpOrDown=-1 return input_point elif transformationType == 2: # goes from right to left XorYorZ=1 UpOrDown=1 return (self.width - input_point[0], input_point[1]) elif transformationType == -1: # backward XorYorZ=0 UpOrDown=-1 return (input_point[1], self.width - input_point[0]) elif transformationType == 1: # forwards XorYorZ=0 UpOrDown=1 return (input_point[1], input_point[0]) else: print "unknown transform type" #def on_key_release( self, keys, mod ): def draw(self): super(Display, self).draw() if not self.blank_display: self.position_bar.render() self.tgt_window.render() self.pos_fdbk.render() #self.pos_fdbk_txt.draw() self.score_txt.draw() self.reward_txt.draw() self.combo_wait_txt.draw()
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()
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