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