How to send a trigger to BNC via USB?

1,501 views
Skip to first unread message

Tats

unread,
Jan 28, 2014, 9:46:05 AM1/28/14
to psychop...@googlegroups.com
Hi all,

I am trying to do some experiments using Psychopy and would like to send a trigger to BNC at the begging of each routine, via USB.
However, I cannnot figure out how to do so from the Code Properties window. (I only found out the one via the parallel port.)

Does anyone know how to do it?

Thank you so much for your attention.

Tats

Jonathan Peirce

unread,
Jan 28, 2014, 1:49:11 PM1/28/14
to psychop...@googlegroups.com
You would need a piece of hardware to do this digital to analog
converter of some sort. I use a labjack U3 for this but there are many
and maybe your lab already has something. The instructions on how to do
it from psychopy would depend on the hardware device you use.

best wishes,
Jon
> --
> You received this message because you are subscribed to the Google
> Groups "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/psychopy-users/106b82e7-d5f8-47db-8455-1fc05b18ebab%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.




Tats

unread,
Jan 29, 2014, 10:09:11 AM1/29/14
to psychop...@googlegroups.com
Hi Jon,

Thank you so much for your reply.
We have a National Instruments USB-6218 BNC and a force plate with a BNC trigger input.
The force plate is controlled by its original software, and thus we can get the csv data using it.
We would like to send a trigger to the BNC trigger input so we can identify the onset of stimuli
that we created with Psychopy.

Can I  send a trigger using NI USB-6218 BNC?
If so, could you give me the instructions?

Thank you in advance.

Yours sincerely,

Tats

Jonathan Peirce

unread,
Jan 30, 2014, 5:00:03 AM1/30/14
to psychop...@googlegroups.com
OK, I don't have a National Instruments interface and no experience using one from python/psychopy.

According to google you'll need to use something like PyDAQmx (and I'm assuming you've already installed the hardware drivers from NI)
    http://packages.python.org/PyDAQmx/

If you're using psychopy standalone the installation is going to be rather different - you'll need to fetch the zip file from this page:
    https://github.com/clade/PyDAQmx
open it and either copy the PyDAQmx folder into your site-packages or you'll need to add its location into the psychopy prefs like this:
    Preferences>General>paths = ['/users/myStuff/code/PyDAQmx']

But that's as far as I can help - I can't test things for you and I know nothing about the commands in that package. You'll need someone local to you that's tech-savvy enough to work it out from here.

best wishes,
Jon

PS I could add PyDAQmx to standalone for future releases, so that the install step is at least painless!

For more options, visit https://groups.google.com/groups/opt_out.

Richard Höchenberger

unread,
Jan 30, 2014, 5:27:18 AM1/30/14
to psychop...@googlegroups.com
Dear Tats,

I am successfully using an NI USB-6212 with a BNC-2110 terminal block in Python and PsychoPy.
There are different ways to get NI devices to work. I am using PyLibNIDAQmx, which basically makes the NI-DAQmx driver functions available to Python.

To get started, you first need to install NI-DAQmx; then download PyLibNIDAQmx, and run

python setup.py install

from the directory you downloaded PyLibNIDAQmx to. This will install the package into your current Python environment. If you are using the stand-alone version of PsychoPy, installation will be a bit trickier (I cannot try this right now, so cannot give you any assistance regarding that point ;))

The bus powered USB devices (unlike the more expensive X-Series) unfortunately do not feature hardware timing capabilities for DIO; all digital acquisitions and generations have to be controlled via software, i.e. the computer connected to those devices. That is why you should rely on analog output for sending triggers if possible.

For analog generation, you can do the following. It will generate a 5 ms long +5 V spike on your first analog output port. You could easily hook up a scope to that port and watch the voltage change.

from __future__ import print_function
import nidaqmx
import numpy as np

NIDEVICE = 'Dev1'
AOPORT = NIDEVICE + '/ao0'

SAMPLE_RATE = 100000 # Hz
TRIGGER_LENGTH = 500 # samples, i.e. 5 ms

device = nidaqmx.libnidaqmx.Device(NIDEVICE)
device.reset()

analogOutputTask = nidaqmx.AnalogOutputTask(name='MyAOTask')
analogOutputTask.create_voltage_channel(AOPORT, channel_name='ao0',
                                        min_val=0, max_val=5, units='volts')

# The idea is to write 500 +5 V samples (5 ms trigger), followed by a single 0 V sample (trigger off)
#
triggerPulse = 5 * np.ones(TRIGGER_LENGTH + 1)
triggerPulse[-1] = 0

analogOutputTask.configure_timing_sample_clock(rate=SAMPLE_RATE,
                                               sample_mode='finite',
                                               samples_per_channel=TRIGGER_LENGTH + 1)

# We prepare the buffer, but don't start the generation yet.
samplesWritten = analogOutputTask.write(triggerPulse, auto_start=False)
print('Wrote', samplesWritten, 'samples to buffer.')

# We can do other fancy things just here
# ...
#

# Now, let's send the trigger!
analogOutputTask.start()

# This line should be taken out in a real experiment,
# as it would block until the card has finished
#
analogOutputTask.wait_until_done()

del analogOutputTask

You can find out the device and port names via the Measurement and Automation Explorer (MAX) shipping with NI-DAQmx for Windows.

If you want to generate a digital output signal (like I have to, to control certain devices in my lab which expect certain bit patterns on their input ports), you can take a look at the following example. This code will generate a series of HIGH and LOW (+5 V and 0 V, respectively) output voltages on DIO port 0.

import nidaqmx
from psychopy.core import wait

NIDEVICE = 'Dev1'
DOPORT = NIDEVICE + '/port0/line0'

device = nidaqmx.libnidaqmx.Device(NIDEVICE)
device.reset()

digitalOutputTask = nidaqmx.DigitalOutputTask(name='MyDOTask')
digitalOutputTask.create_channel(DOPORT, name='line0')

digitalOutputTask.start()

for i in range(10):
    # Set DO port to HIGH
    digitalOutputTask.write(1)
    wait(1, hogCPUperiod=1)
    # Set DO port to LOW
    digitalOutputTask.write(0)
    wait(1, hogCPUperiod=1)

# Stop DO signal generation
digitalOutputTask.stop()
del digitalOutputTask

As you can see, I have to employ PsychoPy's wait() function here to get the desired timing. This could also be threded, but I think this would lead too far at this moment.

This would not have been necessary when using AO, as we could have created a NumPy array containing all the samples we needed, whose generation would then be scheduled by the internal sample clock of the NI board.
In a real PsychoPy experiment, for sending an analog trigger you would probably do something like:

analogOutputTask.write(triggerPulse, auto_start=False)

# Prepare stimuli here
# ...

# Send the trigger as the visual stimulus is presented
win.callOnFlip(analogOutputTask.start)
win.flip()

# Do random stuff here
# ...

# Stop the task, so we can at a later point write() and start() it again if desired.
analogOutputTask.stop()

del analogOutputTask


And for a digital pulse, something like:
digitalOutputTask.start()

# Prepare stimuli here
# ...

# Set DO port to HIGH as soon as the visual stimulus gets presented
win
.callOnFlip(digitalOutputTask.write, 1)
win
.flip()

# Now, set DO port to LOW again
digitalOutputTask
.write(0)

# Do random stuff here
# ...

# Stop DO signal generation
digitalOutputTask
.stop()

del digitalOutputTask

Please let me know if you have any further questions regarding the signal generation.

All the best,

    Richard

Tats

unread,
Jan 30, 2014, 10:47:33 AM1/30/14
to psychop...@googlegroups.com
Dear Jon and Richard,

Thank you so much for your help.
I really appreciate it.

I will follow the instructions and may ask for further help if needed.

Thanks again,

Tats
Reply all
Reply to author
Forward
0 new messages