Duke-UNC Brain Imaging and Analysis Center
BIAC Forums | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password   Forgot your Password?
 All Forums
 Support Forums
 Experimental Control
 BIAC5 triggering: matlab
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

betsy.johnson
Starting Member

USA
7 Posts

Posted - Jun 23 2010 :  11:58:10 PM  Show Profile  Reply with Quote
I'd like to trigger an experiment start in matlab/psychtoolbox at the BIAC5 scanner. I found some code on some old posts and was hoping someone could advise me about what parameters need changing and what they should be changed to for the current setup. Thanks in advance for your help.

ainidaq = analoginput('nidaq',1);
chan = addchannel(ainidaq,2);
duration = 2; % two second acquisition
set(ainidaq,'SampleRate',44100);
ActualRate = get(ainidaq,'SampleRate');
set(ainidaq,'SamplesPerTrigger',ActualRate*duration);
set(ainidaq,'TriggerChannel',chan);
set(ainidaq,'TriggerType','Software');
set(ainidaq,'TriggerCondition','Rising');
set(ainidaq,'TriggerConditionValue',1);
set(ainidaq,'TriggerFcn','mycallback');
start(ainidaq);

joonkoo.park
New Member

11 Posts

Posted - Dec 06 2011 :  5:27:48 PM  Show Profile  Reply with Quote
Hi, I have the same question. I would like to trigger the scanner from Matlab. It looks like no one replied to this post so I was not sure if this is the right way to trigger the scanner. At the same time, this code seems quite complicated. Aren't there ways to trigger the scanner simply using the parallel or serial port? For example, the following code was how I used to trigger the scanner at another institute.

% state can be either 'on' or 'off'
try
s1 = serial('COM1');
fopen(s1);
set(s1,'DataTerminalReady',state);
fclose(s1);
catch ME
disp(ME);
end
Go to Top of Page

petty
BIAC Staff

USA
453 Posts

Posted - Dec 07 2011 :  09:58:50 AM  Show Profile  Reply with Quote
I don't think your task can trigger the scanner. Almost every task i'm familiar with is triggered BY the scanner.

There is a connection between the scanner pulse and the stimulus computers, so you'd need to read the pulse and start the task.
Go to Top of Page

jim.voyvodic
BIAC Faculty

138 Posts

Posted - Dec 07 2011 :  11:05:03 AM  Show Profile  Reply with Quote
Chris is correct. Both scanners at BIAC are set up so that the MR tech starts the scanner, which generates trigger pulses connected to the Windows PC's that your software can detect to simultaneously start your task. The trigger pulses are TTL signals (0-5v, 10 ms long) connected via serial port and via the Measurement Computing A/D device. Details are on the wiki at http://wiki.biac.duke.edu/biac:experimentalcontrol:inventory

Jim
Go to Top of Page

betsy.johnson
Starting Member

USA
7 Posts

Posted - Dec 07 2011 :  11:46:57 AM  Show Profile  Reply with Quote
Hi Joonkoo,
If you get this figured out and working, will you please let me know? I eventually gave up on the question after posting here and asking around. I found some people who had triggered their behavioral scripts using presentation (let me know if you need their contact info) but not matlab, although there was lots of interest for doing it for matlab. I'm sure it's just a few simple lines of code, but matlab programming involving interfacing with other devices is far from my areas of expertise.
Thanks,
Betsy
Go to Top of Page

joonkoo.park
New Member

11 Posts

Posted - Dec 29 2011 :  3:17:02 PM  Show Profile  Reply with Quote
For those interested,

So I was finally able have the scanner pulse trigger the PTB script (in Matlab), with some help from Phil Kragel and Chris Petty. The script below needs to be cleaned and there should be a better way to write this, but it at least works so I wanted to post it here.

You will need the scannerTrigger.m file below. The basic idea is to create and start an analog input object, make this object sample data point every couple of millisecond, and when there is a pulse it turns on the trigger "flag."

Here's an example for the experiment script:

% START

% Create an analog input object
ai = scannerTrigger;

% I would have some manual intervention here
% like [secs keyCode deltaSecs] = KbWait([],3);
% to have this experiment script ready to wait
% for the scanner pulse.

% Start the analog input object
start(ai);

% Wait for the pulse
trigger = false;
while ~trigger
trigger = ai.UserData;
pause(0.001);
fprintf('%g\n',ai.UserData);
end
if trigger
disp(' ## SCANNER TRIGGERED ##');
end

% Start of your run

% After the run, stop the object
stop(ai);

% After the entire experiment, delete the object
delete(ai);
clear ai;

% END


=========== scannerTrigger.m ===========
function ai = scannerTrigger

% This function will create an analog object and sample at 200 Hz
% indefinitely for the scanner trigger.
%
% Written by Phil Kragel 12/9/2011 for questions email pak5@duke.edu
% Modified by Joonkoo Park 12/29/2011 (joonkoo.park@duke.edu)

% configure analog input options
adaptor='mcc';
id=0;

% create object
ai = analoginput(adaptor,id);

% add channel to read in analog data
addchannel(ai,5);

% scanner trigger is input to analog channel 5 on biac5
% see http://wiki.biac.duke.edu/biac:experimentalcontrol:biac5hardware
% configure some properties
set(ai,'SampleRate',200); %200 hz sampling rate
set(ai,'UserData',false);
set(ai,'SamplesPerTrigger',Inf); %sample indefinitely until a stop function is issued or an error occurs
set(ai,'TriggerFcn',@waitforpulse); %call this function when the object starts
set(ai,'StopFcn',@(x,y)disp('AnalogInput Stopped.'));

% start(ai);

end

function waitforpulse(obj,event)

% this function will take the last five samples from analog channel 5 and if one is above
triggered=false;
counter=0;

while ~triggered

% take last five samples
x = max(peekdata(obj,5));
% x = max(getdata(obj,5));

if counter==0
fprintf('.....');
counter=counter+1;
% pause(.2);
fprintf('Ready to start\n');
end

% Debug
% disp(x);

if x > 3 %if there was a pulse recently (assuming the channel is at 0V baseline and 5V for trigger)
% set flag to 1
triggered=true;
obj.UserData=true;

fprintf('Triggered\n');
% stop acquiring data and delete object
% stop(obj;
% delete(obj);

else % otherwise wait for more data
pause(.001);
end

end

end
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
BIAC Forums © 2000-2010 Brain Imaging and Analysis Center Go To Top Of Page
This page was generated in 0.47 seconds. Snitz Forums 2000