Skip to content

Commit

Permalink
Add trial start and end timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed May 7, 2018
1 parent 2aa2710 commit 057d128
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pybpodapi/bpod/bpod_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, serial_port=None, sync_channel=None, sync_mode=None, net_port
self.net_port = net_port if net_port is not None else settings.NET_PORT
self._hardware = Hardware() # type: Hardware
self.bpod_modules = None # type: BpodModules
self.bpod_start_timestamp = None

self._new_sma_sent = False # type: bool

Expand Down Expand Up @@ -288,6 +289,10 @@ def run_state_machine(self, sma):

self.trial_start_timestamp = self._bpodcom_get_trial_timestamp_start()

if self.bpod_start_timestamp is None:
self.bpod_start_timestamp = self.trial_start_timestamp




#####################################################
Expand Down Expand Up @@ -566,10 +571,10 @@ def __update_timestamps(self, sma, state_change_indexes):

current_trial = self.session.current_trial
current_trial.trial_start_timestamp = self.trial_start_timestamp # start timestamp of first trial
current_trial.bpod_start_timestamp = self.trial_start_timestamp


current_trial.bpod_start_timestamp = self.bpod_start_timestamp

trial_end_timestamp, discrepancy = self._bpodcom_read_timestamps()
current_trial.trial_end_timestamp = trial_end_timestamp

if discrepancy>1:
self.session += WarningMessage( "Bpod missed hardware update deadline(s) on the past trial by ~{milliseconds}ms".format(milliseconds=discrepancy) )
Expand Down
4 changes: 2 additions & 2 deletions pybpodapi/bpod/bpod_com_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def _bpodcom_run_state_machine(self):

def _bpodcom_get_trial_timestamp_start(self):
data = self._arcom.read_bytes_array(8)
self.trial_start_micros = ArduinoTypes.cvt_float64(b''.join(data))
self.trial_start_micros = ArduinoTypes.cvt_uint64(b''.join(data))
return self.trial_start_micros / float(self.hardware.DEFAULT_FREQUENCY_DIVIDER)

def _bpodcom_read_trial_start_timestamp_seconds(self):
Expand All @@ -360,7 +360,7 @@ def _bpodcom_read_timestamps(self):
data = self._arcom.read_bytes_array(12)

n_hw_timer_cyles = ArduinoTypes.cvt_float32(b''.join(data[:4]))
trial_end_micros = ArduinoTypes.cvt_float64(b''.join(data[4:12]))# / float(self.hardware.DEFAULT_FREQUENCY_DIVIDER)
trial_end_micros = ArduinoTypes.cvt_uint64(b''.join(data[4:12]))# / float(self.hardware.DEFAULT_FREQUENCY_DIVIDER)
trial_time_from_micros = trial_end_micros - self.trial_start_micros
trial_time_from_cycles = n_hw_timer_cyles/self.hardware.cycle_frequency
discrepancy = abs(trial_time_from_micros - trial_time_from_cycles)*1000
Expand Down
5 changes: 5 additions & 0 deletions pybpodapi/com/arcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def cvt_float64(message_bytes):
def cvt_int64(message_bytes):
return int.from_bytes(message_bytes, byteorder='little')

@staticmethod
def cvt_uint64(message_bytes):
return struct.unpack('<Q',message_bytes)[0]



class ArCOM(object):
"""
Expand Down
6 changes: 4 additions & 2 deletions pybpodapi/com/messaging/trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class Trial(BaseMessage):
"""
:ivar float bpod_start_timestamp: None
:ivar float trial_start_timestamp: None
:ivar StateMachine sma: sma
:ivar list(StateOccurrence) states_occurrences: list of state occurrences
:ivar list(EventOccurrence) events_occurrences: list of event occurrences
Expand All @@ -25,7 +25,7 @@ class Trial(BaseMessage):

def __init__(self, sma=None):
super(Trial,self).__init__('New trial')
self.bpod_start_timestamp = None
self.trial_start_timestamp = None
self.sma = sma # type: StateMachine
self.states_occurrences = [] # type: list(StateOccurrence)
self.events_occurrences = [] # type: list(EventOccurrence)
Expand Down Expand Up @@ -109,6 +109,8 @@ def get_all_timestamps_by_event(self):

def export(self):
return {'Bpod start timestamp': self.bpod_start_timestamp,
'Trial start timestamp': self.trial_start_timestamp,
'Trial end timestamp': self.trial_end_timestamp,
'States timestamps': self.states_durations,
'Events timestamps': self.get_all_timestamps_by_event()}

Expand Down

0 comments on commit 057d128

Please sign in to comment.