Delayed parallel port trigger for key response in logfile

I am using Psychopy version 1.90.1, standalone, running on Windows 10.

I am using a parallel port to send event triggers to Brain Products Recorder.

AIM: My aim is to send an event trigger every time the space bar is pressed.

What I have done: My code (below) is written in a code component in builder. I have included win.logOnFlip() so I can record when the trigger was sent and compare this to when the spacebar was pressed in the logfile. I am not receiving any coding errors and this code is functioning enough for the triggers to be recorded by the EEG Recorder software. I have tried including the code under “Each Frame” in the “Begin Routine” section however this does not generate the trigger.

***Begin Experiment***

from psychopy import parallel
target_space = parallel.ParallelPort(address=0xD050)

***Begin Routine***

# keep track if the trigger has started
if hasattr(target_space, 'status'):
    target_space.status = NOT_STARTED

***Each Frame***
if (target_response.status == STARTED) and target_space.status == NOT_STARTED:
    if len(target_response.keys)>0:
        if 'space' in target_response.keys:
            # keep track of start time/frame for later
            target_space.tStart = t
            target_space.frameNStart = frameN  # exact frame index
            target_space.status = STARTED
            win.callOnFlip(target_space.setData, int(target_space_trigger))
            win.logOnFlip(level=logging.EXP, msg = 'target_space_start')
        if target_space.status == STARTED and frameN >= (target_space.frameNStart + 2):
            target_space.status = STOPPED
            win.callOnFlip(target_space.setData, int(0))

What specifically went wrong when you tried that?:

When considering the logfile output (table below) I can see that my trigger event (i.e. target_space_start) is 50-60ms after the key response was made (i.e. keypress: space).

I would be grateful if someone could review the logic of my code above and let me know if this could be causing the delay. I am v new to Psychopy and have written this code based on the code automatically created by the p_port component in builder.

Also - would any delay be expected or should these two events occur simultaneously?

Timing Event
18.9707 Keypress: space
19.0328 target_space_start
30.1031 Keypress: space
30.1527 target_space_start

Many thanks in advance for your help
From
Mica K

The two events couldn’t be happening simultaneously. The time of up to one frame could be between the two (which should still be considerably less then 60ms).

What is your refresh rate? Do you get warnings about dropped frames?

Hi Lukas,

Thank you for your reply. I have investigated this further running the experiment a few times and completing the timebyframes test. You are correct I am dropping frames and the frame rate is double what I would expect for my 60hz monitor. ~31ms rather than 16.67ms (see output below).

Another point to note: I have been back through the output excel files and the majority of times the experiment is running at 30hz but a couple of times it did complete at 60hz.

As per the feedback given with the error I am not running any other processes on my computer. With regards to the graphics card I do not have the advanced settings available which would allow me to check whether the card is set to syn vertical blank.

As the lag between the two is greater than the time of one frame, could this still be a coding issue rather than a hardware issue?

I am happy to mark this as “solved” until the hardware errors are resolved and retest after if you recommend.

Regards
Mica K

image

1 Like

It is definately a good idea to first rule out that your timing issues have anything to do with an inconstant refresh rate or dropped frames.

Regarding the dropped frames: have you tried everything listed on this help page?

Hi

I have upgraded my graphics card and processing power - this has resolved the problem of dropped frames! I am no longer receiving any errors when I run my task.

Unfortunately my problem of delayed parallel port triggers persists when I use the code component in builder.

I believe this must be a coding issue because when I used the p_port component there is not delay between stimuli and when the triggers are sent (see logfile extract below)

Is anyone able to review the code extract below and suggest what error may be causing the delay between when the key is pressed and the trigger is sent? This is consistently 2 frames.
I think I must be missing a line of code which accurately times when the key response is pressed.

Many thanks in advance

**Begin Experiment***

from psychopy import parallel
target_space = parallel.ParallelPort(address=0xD050)

***Begin Routine***

# keep track if the trigger has started
if hasattr(target_space, 'status'):
    target_space.status = NOT_STARTED

***Each Frame***
if (target_response.status == STARTED) and target_space.status == NOT_STARTED:
    if len(target_response.keys)>0:
        if 'space' in target_response.keys:
            # keep track of start time/frame for later
            target_space.tStart = t
            target_space.frameNStart = frameN  # exact frame index
            target_space.status = STARTED
            win.callOnFlip(target_space.setData, int(target_space_trigger))
            win.logOnFlip(level=logging.EXP, msg = 'target_space_start')
        if target_space.status == STARTED and frameN >= (target_space.frameNStart + 2):
            target_space.status = STOPPED
            win.callOnFlip(target_space.setData, int(0))```

You’re intentionally delaying the parallel port call until the next window flip. Just immediately send the parallel port trigger and log the event. Don’t use callOnFlip() or logOnFlip() here: they are designed to sync the pulse to the next window drawing, but you’re wanting to send the pulse as soon as you can after detecting the key press.

1 Like

Hi Michael,

Thank you for solving my problem - I have removed these items from my code and the timing of the trigger in the logfile is now correct.

  • When coding the start and stop triggers for visual stimulus (e.g. target, feedback) should I still use the callOnFlip() or logOnFlip() ?

  • I have also come across another solution of yours (Word by word sentence presentation with parallel port output) in this method you demonstrate the coding of triggers using pulse_start _time and pulse_terminated would you recommend this approach or are both methods OK?

Many thanks
Mica

Yes. Otherwise you end up with the opposite problem: triggers being sent early (i.e. at the time that line in the code runs, rather than at the time at which the screen updates and the stimulus is actually shown).

This all depends on what the receiving system (the EEG) expects. If it needs pulses to be of a certain duration, then yes, add that extra layer of timing complexity. If it just detects when the value has changed, and doesn’t care about the pulse duration, don’t worry.

Just test and see what works with your system.