Products & Services Solutions Academia Support User Community Company

Learn more about Data Acquisition Toolbox   

Understanding the Data Acquisition Session

Overview

The data acquisition session consists of all the steps you are likely to take when acquiring or outputting data. These steps are

  1. Create a device object — You create a device object using the analoginput, analogoutput, or digitalio creation function. Device objects are the basic toolbox elements you use to access your hardware device.

  2. Add channels or lines — After a device object is created, you must add channels or lines to it. Channels are added to analog input and analog output objects, while lines are added to digital I/O objects. Channels and lines are the basic hardware device elements with which you acquire or output data.

  3. Configure properties — To establish the device object behavior, you assign values to properties using the set function or dot notation.

    You can configure many of the properties at any time. However, some properties are configurable only when the device object is not running. Conversely, depending on your hardware settings and the requirements of your application, you might be able to accept the default property values and skip this step.

  4. Queue data (analog output only) — Before you can output analog data, you must queue it in the engine with the putdata function.

  5. Start acquisition or output of data — To acquire or output data, you must execute the device object with the start function. Acquisition and output occurs in the background, while MATLAB continues executing. You can execute other MATLAB commands while the acquisition is occurring, and then wait for the acquisition or output to complete.

  6. Wait for the acquisition or output to complete — You can continue working in the MATLAB workspace while the toolbox is acquiring or outputting data. (For more information, see Doing More with Analog Input.) However, in many cases, you simply want to wait for the acquisition or output to complete before continuing. Use the wait function to pause MATLAB until the acquisition is complete.

  7. Extract your acquired data (analog input only) — After data is acquired, you must extract it from the engine with the getdata function.

  8. Clean up — When you no longer need the device object, you should remove it from memory using the delete function, and remove it from the MATLAB workspace using the clear command.

The data acquisition session is used in many of the documentation examples included in this guide. Note that the fourth step is treated differently for digital I/O objects because they do not store data in the engine. Therefore, only analog input and analog output objects are discussed in this section.

Real-Time Data Acquisition

Because it is operating on a consumer operating system, the Data Acquisition Toolbox cannot ensure response to an event within a specified maximum time limit. In order to ensure a high throughput of the acquisition, the toolbox manages acquired data in blocks, which increases the latency associated with any given acquired data point. In addition, it must share system resources with other applications and drivers on the system.

If you want to create a control loop with the least latency, and do not require a deterministic response time, you can perform single point operations using getsample and putsample. In this case, the data is acquired and processed as follows:

  1. Data is acquired through your hardware vendor's software.

  2. The data is then handed off to the Data Acquisition Toolbox engine.

  3. The toolbox makes the data available in MATLAB or Simulink.

  4. The data is run through the control algorithm that you develop in MATLAB or Simulink.

  5. The data is then routed back to the engine, through the hardware vendor's software, and onto the board.

This still does not guarantee the response time of a control loop. A higher priority thread can take precedence over the control loop.

Most PC based data acquisition cards provide an internal, high accuracy clock that is used to pace data acquisition. The cards store the data they collect in local memory, and then transfer the samples to main computer memory (using interrupts or DMA). The timing of samples acquired this way is extremely accurate, and these cards can guarantee that the acquired data was obtained at the requested sample rate, and that no samples were dropped. The maximum sampling rate is governed by the data acquisition card, not the PC.

For true real-time closed loop control with MATLAB, consider some of these other MathWorks products:

Example: The Data Acquisition Session

This example illustrates the basic steps you take during a data acquisition session using an analog input object. You can run this example by typing daqdoc3_1 at the MATLAB Command Window.

  1. Create a device object — Create the analog input object AI for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo.

    AI = analoginput('winsound');
    %AI = analoginput('nidaq','Dev1');
    %AI = analoginput('mcc',1);
  2. Add channels — Add two channels to AI.

    addchannel(AI,1:2); 
    %addchannel(AI,0:1); % For NI and MCC
  3. Configure property values — Configure the sampling rate to 11.025 kHz and define a 2 second acquisition.

    set(AI,'SampleRate',11025)
    set(AI,'SamplesPerTrigger',22050)
  4. Start acquisition — Before the start function is issued, you might want to begin inputting data from a microphone or a CD player.

    start(AI)
    
  5. Wait for the acquisition or output to complete — Pause MATLAB until either the acquisition completes or 3 seconds have elapsed (whichever comes first). If 3 seconds elapse, an error occurs.

    wait(AI,3);
  6. Extract the acquired data from the engine and plot results

    data = getdata(AI);
    

    Plot the data and label the figure axes.

    plot(data)
    xlabel('Samples') 
    ylabel('Signal (Volts)')
  7. Clean up — When you no longer need AI, you should remove it from memory and from the MATLAB workspace.

    delete(AI)
    clear AI

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS