Discussion:
Simultaneous digital and analog acquisition and control using NI hardware
(too old to reply)
Srinivas
2013-06-03 15:12:09 UTC
Permalink
Hi,

This is what I want to do:

1) read N samples from some analogue input channels
2) write N samples to some analogue output channels
3) write N samples to some digital output channels

simultaneously.

so far, i can do 1) and 2) simultaneously using the session based interface, but I can't seem to get writing to digital channels too. first, when i add a digital channel, i get this warning:

Warning: A channel that does not support clocked sampling was added to the session. Clocked operations
using startForeground and startBackground will be disabled. Only on-demand operations using
inputSingleScan and outputSingleScan can be done.

and so i'm forced to used outoutSingleScan to write to my digital channel, which is annoying. how do i configure my digital channel so that i can use s.startBackground to also write a vector of N samples to my digital output channel, like i use my analogue output channel?

thanks in advance!

S
Kristian Nielsen
2013-07-08 12:00:14 UTC
Permalink
Hi Srinivas

I'm having similar problems. So I'm eager to know - did you find a solution ?

regards,
Kristian
Srinivas
2013-07-08 14:10:15 UTC
Permalink
Post by Kristian Nielsen
Hi Srinivas
I'm having similar problems. So I'm eager to know - did you find a solution ?
regards,
Kristian
Hi Kristian,

I solved this problem; I think the issue we're seeing is because of a bug in MATLAB. I upgraded to R2013a and everything is OK.

Here is some sample code to do what you want:

% configure the session
d = daq.getDevices;
s = daq.createSession('ni');
s.addAnalogInputChannel('Dev1',0:1, 'Voltage');
s.addAnalogOutputChannel('Dev1',0,'Voltage');
s.addDigitalChannel('Dev1','Port0/Line11','OutputOnly');
s.Rate = 1000;

% configure the control
control = [zeros(100,2);ones(100,2);zeros(1000,2); ones(1000,2); zeros(1000,2)];

% set up data logging in background
fid1 = fopen('log.bin','w');
lh = s.addlistener('DataAvailable',@(src, event)logData(src, event, fid1));
s.IsContinuous = false;
s.queueOutputData(control);

% run in background
s.startBackground;

pause(10)
delete(lh);
fclose(fid1);

% get data
fid2 = fopen('log.bin','r');
[data,count]= fread(fid2,[3,inf],'double');
fclose(fid2);

% plot
t = data(1,:);
ch = data(2:3,:);
plot(t, ch);

I'm also writing a larger programme to control and acquire generic signals using NI boards and the DAQ:

http://sourceforge.net/projects/daqkontroller/
Douglas Freire
2016-09-23 11:59:03 UTC
Permalink
Post by Srinivas
I solved this problem; I think the issue we're seeing is because of a bug in MATLAB. I upgraded to R2013a and everything is OK.
I'm using version R2016a and still having the same error.

Running your example code I am getting the error:
"Warning: A channel that does not support clocked sampling was added to the session. Clocked operations using startForeground and startBackground will be disabled. Only on-demand operations using inputSingleScan and outputSingleScan can be done.
Error using srinivasgsCode (line 18) queueOutputData is disabled because the session contains channels that do not support clocked operations."

There is any configuration to do in NI DAQ USB6008 to accept clocked operations?
Rob Campbell
2016-09-23 13:09:04 UTC
Permalink
Post by Douglas Freire
I'm using version R2016a and still having the same error.
It's not really an error: it's telling you that th NI USB 6008 does not have any hardware clocked outputs. It's all software clocked: http://sine.ni.com/nips/cds/view/p/lang/en/nid/201986
Douglas Freire
2016-09-24 19:51:03 UTC
Permalink
Post by Douglas Freire
I'm using version R2016a and still having the same error.
It's not really an error: it's telling you that the NI USB 6008 does not have any hardware clocked outputs. It's all software clocked: http://sine.ni.com/nips/cds/view/p/lang/en/nid/201986
By software clocked you mean that I need to use single scan I/O and get/set signal successively, by script commands, using scalar values, right?

I need to get/set signals in background, while another functions are executed.
How could I do this using DAQ NI USB-6008?
Rob Campbell
2016-09-24 21:31:04 UTC
Permalink
Post by Douglas Freire
software clocked you mean that I need to use single scan I/O and get/set signal
successively, by script commands, using scalar values, right?
Yes. On the more expensive boards you can queue samples to a buffer and play them out at a fixed rate using the clock. The cheaper USB DAQs don't have this ability.
Post by Douglas Freire
I need to get/set signals in background, while another functions are executed.
How could I do this using DAQ NI USB-6008?
Depends on what exactly you're trying to do. If the timing is not very critical you could maybe "fake" it with a timer object in MATLAB. Otherwise you might need a different DAQ board. You might also be able to use an Arduino.
dpb
2016-09-24 21:32:42 UTC
Permalink
On 09/24/2016 2:51 PM, Douglas Freire wrote:
...
Post by Douglas Freire
I need to get/set signals in background, while another functions are executed.
How could I do this using DAQ NI USB-6008?
That's more function of the NI gear than Matlab...
Rob Campbell
2016-09-27 21:11:03 UTC
Permalink
...
Post by Douglas Freire
I need to get/set signals in background, while another functions are executed.
How could I do this using DAQ NI USB-6008?
The above, said. There is a counter on the USB-6008. Likely this is reserved for the analog outputs but you *might* get lucky and find you can use it for the digital too. For docs on the general procedure you can look here:

https://www.mathworks.com/help/daq/create-a-clock-using-counter-channel-for-digital-acquisition.html
https://www.mathworks.com/help/daq/share-an-analog-input-subsystems-internal-clock-to-acquire-clocked-digital-data.html

I don't know if it'll work with the USB-6008, though. That's supposed to be a simple, low-cost, device.
Douglas Freire
2016-10-13 17:39:03 UTC
Permalink
NI DAQ USB 6008 only allows background execution for acquisition of analog data (analog input).
NI DAQ USB 6008 input counter counts and accumulate all pulses received without need of parallel (background) execution.
NI DAQ USB 6008 output signlas, digital or analog, are only allowed using SingleScan commands.

I did my code using this NI DAQ USB 6008 features, but I'm unable to reset counter using resetCounter(s) command.

There is another way to reset counter in NI DAQ USB 6008?


%%douglasfcx
%% DAQ Rectified DC Motor (DAQ NI USB 6008)
%warning off; close all; clear all; clc;
SimStartTime=tic;

global Ts dataBuffer
Ts=0.1; %Sample Time;
dataBuffer=[];

%% Create Sessions
d=daq.getDevices;
%Analog Input (Background)
s_anlgin=daq.createSession(d.Vendor.ID);
s_anlgin_nc=6; %Number of Channels;
s_anlgin.addAnalogInputChannel(d.ID,0:s_anlgin_nc-1,'Voltage');
for i=1:s_anlgin_nc;
s_anlgin.Channels(1,i).TerminalConfig='SingleEnded';
end
s_anlgin.Rate=1000;
% s_anlgin.Rate=s_anlgin.RateLimit(2);
%Time of background DAQ:
%s_anlgin.DurationInSeconds = 30;
s_anlgin.IsContinuous=true;
%Digital Counter Input
s_ctr=daq.createSession(d.Vendor.ID);
s_ctr.addCounterInputChannel(d.ID,0,'EdgeCount');
%Analog Output
s_anlgout=daq.createSession(d.Vendor.ID);
s_anlgout.addAnalogOutputChannel(d.ID,0,'Voltage');
s_anlgout.Channels.TerminalConfig='SingleEnded';

%% DC Motor Essay
%Create Variables
VcSignal=struct('HoldTime',5,'StepValue',0.05,'Min',2.3,'Max',3.3,'Value',5.0,'Inc',0,'Dec',0);%VoltageControlSignalStruct
NumUpDown=2; %Number of Up and Down of Signal;
NsUpDown=2*((VcSignal.Max-VcSignal.Min)/VcSignal.StepValue); %Number of samples for ONE Up Down Signal;
Ns=NumUpDown*NsUpDown*(fix(VcSignal.HoldTime/Ts)); %Number of samples;
VcChangePoints=(0:VcSignal.HoldTime:Ns); %Change Points of VcSignal;
VcChangePosition=1;
Td=0; %Discrete Time
DTG=3; %Data To Get;
Data=zeros(Ns,DTG);

Tt=NumUpDown*NsUpDown*VcSignal.HoldTime; %Simulation Total Time;
disp(['Simulation Total Time: ',num2str(Tt),' seconds.'])

%Initialize Variables
%Analog Input Data (Background)
s_anlgin_listener=addlistener(s_anlgin,'DataAvailable',@storeData);
% function storeData(src,event)
% temp = evalin('base','dataBuffer');
% temp = [temp;event.TimeStamps,event.Data];
% assignin('base','dataBuffer',temp);
% end
startBackground(s_anlgin)
%Digital Counter Input Data
ctr0_value=s_ctr.inputSingleScan; %Get current counter value;
%Analog Output Data/VoltageControlSignalValue
s_anlgout.outputSingleScan(VcSignal.Max);%Set value;

for i=1:Ns
t_start=tic; %Get loop start time;

%Digital Counter Input Data
%s_ctr.resetCounters %NOT WORKING!
ctr0_past=ctr0_value; %Save last counter value;
ctr0_value=s_ctr.inputSingleScan; %Get current counter value;
ctr0_loop=ctr0_value-ctr0_past; %Set current loop counter value;
%Analog Output Data/VoltageControlSignalValue
Td=Td+Ts;
p=(VcChangePoints(VcChangePosition)+Ts);
if (p-Td)<(1e-10)
if VcSignal.Value>=VcSignal.Max;VcSignal.Value=VcSignal.Max;VcSignal.Inc=0;VcSignal.Dec=1;end
if VcSignal.Value<=VcSignal.Min;VcSignal.Value=VcSignal.Min;VcSignal.Inc=1;VcSignal.Dec=0;end
if VcSignal.Inc==1;VcSignal.Value=VcSignal.Value+VcSignal.StepValue;end
if VcSignal.Dec==1;VcSignal.Value=VcSignal.Value-VcSignal.StepValue;end
%disp(['Td: ',num2str(Td)])
%disp(['VcSignal.Value: ',num2str(VcSignal.Value)])
VcChangePosition=VcChangePosition+1;
end
s_anlgout.outputSingleScan(VcSignal.Value);%Set value;

%Hold time till t_loop
%t_elapsed=toc(t_start);
disp(['Index = ',num2str(i),'/',num2str(Ns),';'])
%disp(['Elapsed Time = ',num2str(t_elapsed),'.'])
%disp('----------------------------------------')
t_elapsed=toc(t_start);
t_missing=Ts-t_elapsed;
pause(t_missing)

%Save Data
Data(i,:)=[i*Ts VcSignal.Value ctr0_loop];
end

stop(s_anlgin);
delete(s_anlgin_listener);

figure
plot(Data(:,1),Data(:,2),'b')
figure
plot(Data(:,1),Data(:,3),'r')

figure
plot(dataBuffer(:,1),dataBuffer(:,2),'b')
hold on
plot(dataBuffer(:,1),dataBuffer(:,3),'b')
hold off
figure
plot(dataBuffer(:,1),dataBuffer(:,4),'b')
hold on
plot(dataBuffer(:,1),dataBuffer(:,5),'b')
hold off
figure
plot(dataBuffer(:,1),dataBuffer(:,6),'b')
hold on
plot(dataBuffer(:,1),dataBuffer(:,7),'b')
hold off

SimTotalTime=toc(SimStartTime);
disp('')
disp(['Simmulation Total Time: ',num2str(SimTotalTime)]);
disp('END')
%%douglasfcx
Ashley
2020-01-17 17:08:06 UTC
Permalink
I am using Matlab 2019b with a NI DAQ6363 and also cannot queue output data using both analog and digital outputs simultaneously. I am passing the daq a file called "trial_sequence" with a matrix called flicker_info of 3 columns vectors of 0's and 1's, one column per channel.

clear; clc; %clear everything
timeStarted = cputime;
daqreset; %reset DAQ
session_info = daq.createSession('ni');
session_info.IsContinuous = true; %for continuous background
sample_rate = 100000; %sample rate is 100k
session_info.Rate = sample_rate; %pass the sample rate to the DAQ
addAnalogOutputChannel(session_info,'Dev1', [2 3], 'Voltage'); %AO2sound, AO3 for LEDs

% add rewardpump channel where input 0 = off, 1 = 1
addDigitalChannel(session_info,'Dev1','Port0/Line1', 'OutputOnly');


filename = 'trial_sequence.mat';
load(filename);
queueOutputData(session_info, flicker_info);
session_info.startBackground();

Loading...