4

I'd like to simultaneously get data from a camera (i.e. an image) and an analog voltage using matlab. For the camera I use the imaq toolbox, for reading the voltage I use the daq toolbox (reading NI-USB device), with a following code:

clear all
% Prepare camera
vid = videoinput('gentl', 1, 'Mono8');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
vid.TriggerRepeat = Inf;
triggerconfig(vid, 'hardware', 'DeviceSpecific', 'DeviceSpecific');
src.FrameStartTriggerMode = 'On';
src.FrameStartTriggerActivation = 'RisingEdge';

% prepare DAQ
s=daq.createSession('ni');
s.addAnalogInputChannel('Dev1','ai1','Voltage');
fid = fopen('log.txt','w');
lh = s.addlistener('DataAvailable',@(src,event)SaveData(fid,event));
s.IsContinuous = true;

% Take data
s.startBackground();
start(vid)
N=10;
for ii=1:N
    im(:,:,ii)=getsnapshot(vid);
end


% end code
delete(lh );
fclose('all');
stop(vid)
delete(vid)

where the function SaveData is:

function SaveData(fid,event)
     time = event.TimeStamps;
     data = event.Data;
     fprintf(fid, '%f,%f\n ', [time data]);
end

I do get images and a log.txt file with the daq trace (time and data), but how can I use the external triggering (that trigger the camera) or some other clock to synchronize the two? For this example, the daq reads the camera triggering TTL signal (@ 50 Hz), so I want to assign each TTL pulse to an image.

Addendum: I've been searching and have found a few discussions (like this one) on the subject, and read the examples that are found in the Mathworks website, but haven't found an answer. The documentation shows how to Start a Multi-Trigger Acquisition on an External Event, but the acquisition discussed is only relevant for the DAQ based input, not a camera based input (it is also working in the foreground).

  • Nothing straightforward exists in the MATLAB; you might want to use a Mex based external C++ threaded library or a Simulink based solution. – Arcturus Feb 23 '13 at 05:03
  • Which device do you use to record the voltage? – H.Muster Feb 23 '13 at 13:13
  • I don't see why it should matter, but you can see in the code it is an NI device (an analogue voltage USB reader such as the USB6009) –  Feb 26 '13 at 01:22
  • Why don't you record the TTL signal that triggers the camera with the NI via an additional analog input channel? Then you can assign the images to the voltage data in the first analog channel afterwards by referencing the data to the pulses in the second analog input channel. – H.Muster Mar 06 '13 at 10:49
  • I tried that, but there's always some lag between the daq and the camera, so while the trigger signal is recorded, only after some time the camera kicks in. In other words, it takes `s.startBackground();` about `0.15` sec to process, in that time I have several of these triggers recorded but the camera's 'getsnapshot' hasn't started yet... –  Mar 10 '13 at 06:15

3 Answers3

1

This will not entirely solve your problem, but it might be good enough. Since the synchronization signal you are after in at 50 Hz, you can use clock in order to create time stamps for both types of your data (camera image and analog voltage). Since the function clock takes practically no time (i.e. below 1e-7 sec), you can try edit to your SaveData function accordingly:

fprintf(fid, '%f,%f\n ', [clock time data]);

And in the for loop add:

timestamp(i,:)=clock;
bla
  • 25,846
  • 10
  • 70
  • 101
  • 1
    Might be better to use tic and toc, which Mathworks recommends for time measuring, since they don't rely on calls to system time. – reverse_engineer Mar 07 '13 at 08:38
1

Can you use the sync to trigger the AD board? From the USB-6009 manual...

Using PFI 0 as a Digital Trigger-- When an analog input task is defined, you can configure PFI 0 as a digital trigger input. When the digital trigger is enabled, the AI task waits for a rising or falling edge on PFI 0 before starting the acquisition. To use AI Start Trigger (ai/StartTrigger) with a digital source, specify PFI 0 as the source and select a rising or falling edge.

My experience suggests that delay between trigger and AQ is very short

I'm sorry I use Python or C for this, so I can't give you MatLab code, but you want to look at functions like.

/* Select trigger source */
Select_Signal(deviceNumber, ND_IN_START_TRIGGER, ND_PFI_0, ND_HIGH_TO_LOW);

/* specify that a start trigger is to be used */
DAQ_Config(deviceNumber, startTrig, extConv); // set startTrig = 1

/* start the acquisition */ 
DAQ_Start(deviceNumber, …)

If you want to take this route you could get more ideas from: http://www.ni.com/white-paper/4326/en

Hope this helps, Carl

Carl Houtman
  • 155
  • 1
  • 10
0

This is yet no complete solution, but some thoughts that might be useful.

I do get images and a log.txt file with the daq trace (time and data), but how can I use the external triggering (that trigger the camera) or some other clock to synchronize the two?

Can you think of a way to calibrate your setup? I.e. modify your experiment and create a distinct event in both your image stream and voltage measurements, which can be used for synchronization? Just like this ...

s-m-e
  • 3,433
  • 2
  • 34
  • 71