Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How to output a sine with usb 6008 on MATLAB

Good evening,

 

I am trying to output a sine using usb 6008 analog output. Could anyone help me on this please?

 

Thanks

0 Kudos
Message 1 of 10
(9,106 Views)

Hi Takou,

 

To use the 6008 with matlab, you need the daqmx driver,which must be consistent with your OS

I invite you to read the information found from our website on this subject: 


NI-DAQmx 9.2.2 - Windows 7 64 bit/7 x86/Server 2003 R2 (32-bit)/XP x86/Vista x64/Vista x86/Server 2008 R2 (64-bit)

http://joule.ni.com/nidu/cds/view/p/id/2214/lang/fr

 

An Introduction to NI-DAQmx Tools for Data Acquisition with The Mathworks, Inc. MATLAB® Software

http://zone.ni.com/devzone/cda/tut/p/id/3005

 

DAQmx (E/M/S-Series Cards) with MATLAB®

http://digital.ni.com/public.nsf/allkb/7076035AEA7835FE8625707300520720?OpenDocument

 


Best reagards,

 

 

 

Message 2 of 10
(9,096 Views)

Thanks you for your reply. Smiley Happy

 

In fact I have looked at the demos provided by Mathworks but any of them treats the case of the NIDAQmx usb 6008. In fact, for this card it is somehow particular because it does not contain an internal clock. So we can not use the functions putdata/start, we can only use "putsample". I have tried to output my signal like this:


First of all, I have configured my analog output:


    %Create an analog output object
    AO = analogoutput('nidaq',boardId);
    %Add hardware channels to analog output object
    chans = addchannel(AO,0);
    % Set the SampleRate
    set(AO,'SampleRate',100)
    % Obtain the actual rate set in case hardware limitations  
    % prevent using the requested rate
    ActualRate = get(AO,'SampleRate');

 

Then I tried to output my sine like this:

 

    %Number of samples
    duration=nb_period/frequency;
    nb_samples = ActualRate*duration;
    data = sin(linspace(0,2*pi*nb_period,nb_samples))+amplitude;
    

    for i=1:length(data)


        t = timer('TimerFcn', ' putsample(AO,data(i))',...
              'StartDelay',0);
        start(t)

    end

 

with frequency is the frequency of the signal to output and nb_period is the number of periods to generate. With this I got a sine as an output but its frequency is different from what I want (meaning different from the value frequency specified). Given that I want to generate sines in the range [0.01 Hz 0.5Hz], it seems to me that the sample rate is good. Could you help me on this please?

 

Thank you very much

 

Takoua

0 Kudos
Message 3 of 10
(9,093 Views)

Hi Takou,

 

Is the sine magnitude seems Ok?

With this configuration what frequency and "actualRate" value do you have (ActualRate = get(AO,'SampleRate');)? 

 

If you need plotting N period of the sine try this:

t = linspace(0, N*(1/Freq));

data = sin(2*pi*Freq*t)+amplitude;

plot(t,y);

 

Could you say me if the plotting seems OK in this case, and with your configuration?

 

In fact with the Usb 6008, you can perform a sine output only with software-timed updates of up to 150 samples per second.

 

USB-6008/6009 Continuous Analog Output

http://digital.ni.com/public.nsf/allkb/6F2C2B49A89D685C8625711D007BDD64?OpenDocument

 

Best regards,

Message 4 of 10
(9,080 Views)

Thank you for the fast reply again.

I don't need more than 150 samples per second given that the range frequency of my sine is [0.01Hz 0.5Hz]

Besides, I am not plotting the sine on matlab. I am visualizing the output with an oscilloscope. The sine magnitude is ok but not its frequency.

 

For example if I choose frequency=0.1 (frequency=0.1;) and set the sample rate to 100Hz (set(AO,'SampleRate',100); wich respect largely Shannon), I don't get the good frequency on my oscilloscope.

I think that in order to write the sine and output it, we need to use the sample rate of the card. That is why I use it in the calcul of my sine.

 

I do not see where is the problem. Smiley Sad

 

Best regards,

0 Kudos
Message 5 of 10
(9,077 Views)

Hi Takou,

 

For this application you need a software timed loop to output a sine with 6008 because this board is limited due to the digital analog converter inside the device.

- Frequency of 0.5Hz = 2S period.

- Linespace = 100 points

 

If you need to output one period with 100 point (within Linespace) at 0.5Hz, you need a delay inside the For loop to perform this generation.

So in the timer function, put the delay at 0.2S between two points.

 

t = linspace(0, (1/Freq));

data = sin(2*pi*Freq*t)+amplitude;

for i=1:length(data)


        t = timer('TimerFcn', ' putsample(AO,data(i))',...
              'StartDelay',0.2);
        start(t)

    end

 

Best Regards,

Message 6 of 10
(9,072 Views)

Thank you Remi.

It was helpfull. In fact, I  did not put the delay in the 'startdealy' but in the 'period' of the object timer and I didn't use a for loop but have incremented the variable i in my timer function and it has worked 🙂 .

 

Now the problem is that I have glitches on my signal. When looking in the datasheet of the card, I found this:

 

Minimizing Glitches on the Output Signal
When you use a DAC to generate a waveform, you may observe glitches in
the output signal. These glitches are normal; when a DAQ switches from
one voltage to another, it produces glitches due to released charges. The
largest glitches occur when the most significant bit of the DAC code
changes. You can build a lowpass deglitching filter to remove some of
these glitches, depending on the frequency and nature of the output signal.
Refer to ni.com/support for more information about minimizing
glitches.

 

Have you an idea how to implement thislowpass deglitching filter?  

 

Thank you again. All of your remarks were very helpfull 🙂

0 Kudos
Message 7 of 10
(9,055 Views)

Hi Takou,

 

 
Since the glitch is a transient event this leads designers to believe that a simple low pass filter can be used to eliminate or reduce the size of the glitch. Indeed, you can build a lowpass deglitching filter to remove some of these glitches, depending on the frequency and nature of the output signal. This circuit is external to the device.

 

 

 

Understanding Glitch in a High Speed D/A Converter

 

 

http://www.ingelec.uns.edu.ar/pds2803/Materiales/HDatos/Harris/tb325.pdf

 

 

Best regards,

 

Message 8 of 10
(9,049 Views)

i have the same problem with this code

%%get connected devices
 d = daq.getDevices
 %create session 
  s = daq.createSession('ni')
 s1= daq.createSession('ni')
  %add analog channel  s.addAnalogInputChannel('ID',channel num, 'measurement type')
 % s.addAnalogInputChannel('Dev1',0:3, 'Voltage')%irradiance
  s.addAnalogInputChannel('Dev1','ai0', 'Voltage')%irradiance
  s.addAnalogInputChannel('Dev1','ai1', 'Voltage')%Temperature
  s.addAnalogInputChannel('Dev1','ai2', 'Voltage')%current
  s.addAnalogInputChannel('Dev1','ai3', 'Voltage')%voltage
 % s.addAnalogInputChannel('Dev1','ai3', 'Voltage')%Temperature
 %set(s.Channels,'InputType','SingleEnded')
%set(s.Channels,'Range',[-10 10])
   %s.addAnalogInputChannel('Dev1','ai6', 'Voltage')%Temperature

AO=s1.addAnalogOutputChannel('Dev1', 'ao0', 'Voltage')
% set rate of scan  1000 scans/second ,  run for 30 seconds

 s.Rate=1000;
s.DurationInSeconds =10
v.TerminalConfig = ' Differential';
%v.TerminalConfig = ' SingleEnded';
%_____________________________
%%
%aquire data and process
lh = s.addlistener('DataAvailable', @plotData);
%store data
%h = s.addlistener('DataAvailable', @storedata);
% Analog Output 
 % duty cycle control signal x amplification 
 %for i=1:5
e= 18 - Vref
%end
IE=sum(e); %Integral of  Error

C = pid(20,10,0,'InputName','e','OutputName','u')  % calculate control signal of pid 
u=C*er 
for i=1:length(u)
        t = timer('TimerFcn', ' putsample(AO,u)',...
              'StartDelay',0.2);
        start(t)
  end
    
s.startBackground()
   s.wait()
 delete (lh)
 s.release()

 it gives me error message

Error while evaluating TimerFcn for timer 'timer-1840'

Undefined function 'putsample' for input arguments of type 'daq.ni.AnalogOutputVoltageChannel'.

0 Kudos
Message 9 of 10
(7,755 Views)

Can we change the sampling rate of the analog output by MatLab code for NI DAQ 6008?

0 Kudos
Message 10 of 10
(3,267 Views)