Skip to content

Commit

Permalink
Fix bug: wrong states timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed Jul 20, 2018
1 parent 0acbf7e commit 7fae43d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
29 changes: 18 additions & 11 deletions pybpodapi/bpod/bpod_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def __init__(self, serial_port=None, sync_channel=None, sync_mode=None, net_port

self._new_sma_sent = False # type: bool

self.session_timestamps = [] #Store the session timestamps in case bpod is using live_timestamps

self._hardware.sync_channel = self.sync_channel # 255 = no sync, otherwise set to a hardware channel number
self._hardware.sync_mode = self.sync_mode # 0 = flip logic every trial, 1 = every state

Expand Down Expand Up @@ -166,8 +164,6 @@ def open(self):

self._hardware.setup(self.bpod_modules)

self.session_timestamps = []

# initialise the server to handle commands
if self.net_port is not None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand Down Expand Up @@ -279,6 +275,7 @@ def run_state_machine(self, sma):

logger.info("Running state machine, trial %s", len(self.session.trials) )

self.trial_timestamps = [] #Store the trial timestamps in case bpod is using live_timestamps

self._bpodcom_run_state_machine()
if self._new_sma_sent:
Expand Down Expand Up @@ -323,11 +320,18 @@ def run_state_machine(self, sma):
elif inline.startswith('SoftCode'):
softcode = int(inline[-1])-1
self.trigger_softcode(softcode)
elif inline.startswith('trigger:'):
tdata = inline.split(':')
evt_name = tdata[1]
evt_data = int(tdata[2])
event_index = sma.hardware.channels.event_names.index(evt_name)
self.trigger_event(event_index, event_data)
#####################################################

# read commands from a net socket ###################
if self.socketin is not None:
inline = self.socketin.readline()

if inline is not None:
inline = inline.decode().strip()
if inline.startswith('pause-trial'):
Expand All @@ -343,6 +347,12 @@ def run_state_machine(self, sma):
elif inline.startswith('SoftCode'):
softcode = int(inline[-1])-1
self.trigger_softcode(softcode)
elif inline.startswith('trigger:'):
tdata = inline.split(':')
evt_name = tdata[1]
evt_data = int(tdata[2])
event_index = sma.hardware.channels.event_names.index(evt_name)
self.trigger_event(event_index, evt_data)
#####################################################


Expand Down Expand Up @@ -440,8 +450,8 @@ def softcode_handler_function(self, data):
def echo_softcode(self, softcode):
return self._bpodcom_echo_softcode(softcode)

def trigger_event(self, state_index, event):
return self._bpodcom_manual_override_exec_event(state_index, event)
def trigger_event(self, event_index, event_data):
return self._bpodcom_manual_override_exec_event(event_index, event_data)

def trigger_softcode(self, softcode):
return self._bpodcom_send_softcode(softcode)
Expand Down Expand Up @@ -475,7 +485,7 @@ def __process_opcode(self, sma, opcode, data, state_change_indexes):

if self.hardware.live_timestamps:
event_timestamp = self._bpodcom_read_event_timestamp()
self.session_timestamps.append(event_timestamp)
self.trial_timestamps.append(event_timestamp)
else:
event_timestamp = None

Expand Down Expand Up @@ -583,13 +593,11 @@ def __update_timestamps(self, sma, state_change_indexes):
end_time =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) )


if self.hardware.live_timestamps:
timestamps = self.session_timestamps
timestamps = self.trial_timestamps
else:
timestamps = self._bpodcom_read_alltimestamps()
timestamps = [float(t)*self._hardware.times_scale_factor for t in timestamps]
Expand All @@ -603,7 +611,6 @@ def __update_timestamps(self, sma, state_change_indexes):
self.session += e
###################################################################################################

#update the states timestamps
current_trial.state_timestamps += [timestamps[i] for i in state_change_indexes]
current_trial.state_timestamps += timestamps[-1:]
#########################################
Expand Down
5 changes: 3 additions & 2 deletions pybpodapi/bpod/bpod_com_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,12 @@ def _bpodcom_echo_softcode(self, softcode):
bytes2send = ArduinoTypes.get_uint8_array([ord(SendMessageHeader.ECHO_SOFTCODE), softcode])
self._arcom.write_array(bytes2send)

def _bpodcom_manual_override_exec_event(self, state_index, event):
def _bpodcom_manual_override_exec_event(self, event_index, event_data):
"""
Send soft code
"""
logger.debug("Manual override execute virtual event")
bytes2send = ArduinoTypes.get_uint8_array([ord(SendMessageHeader.MANUAL_OVERRIDE_EXEC_EVENT), ord(state_index), ord(event)])
bytes2send = ArduinoTypes.get_uint8_array([ord(SendMessageHeader.MANUAL_OVERRIDE_EXEC_EVENT), event_index, event_data])
self._arcom.write_array(bytes2send)

def _bpodcom_send_softcode(self, softcode):
Expand Down Expand Up @@ -333,6 +333,7 @@ def _bpodcom_run_state_machine(self):
logger.debug("Requesting state machine run (%s)", SendMessageHeader.RUN_STATE_MACHINE)

self._arcom.write_char(SendMessageHeader.RUN_STATE_MACHINE)
print('run--')

def _bpodcom_get_trial_timestamp_start(self):
data = self._arcom.read_bytes_array(8)
Expand Down
4 changes: 3 additions & 1 deletion pybpodapi/bpod/non_blockingsocketreceive.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def run(self):
if not data: self.event.set()
except socket.timeout:
pass
if data: self.queue.put(data)
if data:
self.queue.put(data)
data = None

self.event.wait(0.01)

Expand Down
5 changes: 3 additions & 2 deletions pybpodapi/bpod/non_blockingstreamreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def run(self):
while True:
if self.event.is_set(): break
line = self.stream.readline()
if line: self.queue.put(line)

if line:
self.queue.put(line)
lines = None
self.event.wait(0.01)


Expand Down

0 comments on commit 7fae43d

Please sign in to comment.