Giter VIP home page Giter VIP logo

matlab_daqmx_examples's Introduction

DAQmx Examples

These examples show you how to interact with National Instruments devices using MATLAB. The focus is Vidrio's free DAQmx wrapper, dabs.ni.daqmx. Also supplied are some contrasting examples using The Mathworks Data Acquisition Toolbox. All examples require a Windows machine.

The dabs.ni.daqmx wrapper and .NET

The MATLAB Data Acquisition Toolbox is the most common way of handling data acquisition in MATLAB. However, with NI hardware you can also use the free dabs.ni.daqmx wrapper that is part of ScanImage. This is a thin, object-oriented wrapper that provides access to almost the full DAQmx API. This may be particularly helpful to those who are used to the ScanImage API and it works pretty well. Alternatively, you can call the DAQmx library more directly using the .NET interface. Example code for this is in the nidaqmx module (i.e. the +nidaqmx directory). The Vidrio wrapper is a little more similar to the call structure you will find in Python, but it requires a ScanImage install (free). The .NET interface requires you to have installed .NET support when you installed DAQmx.

In addition to dabs.ni.daqmx, ScanImage also supplies an FPGA interface wrapper (dabs.ni.rio) which can be used to run a bitfile compiled using LabVIEW FPGA on any NI RIO FPGA target. The NI VISA wrapper (dabs.ni.visa) connects to and communicates with devices that support NI's VISA interface (such as oscilloscopes). This currently supports only a small subset of the NI VISA API.

What is provided here

This repository contains a bunch of NI DAQmx examples using both the MATLAB DAQ toolbox and the dabs.ni.daqmx wrapper. The examples provided here overlap with those provided by Vidrio in dabs.ni.daqmx.demos, but are more up to date and more extensively commented. The DAQmx_ANSI_C_examples directory is for convenience and contains copies of some of the examples installed along with DAQmx. The basicConcepts directory illustrates some of the more advanced programming concepts which crop up.

Installation

  • Download ScanImage and add its root directory to your MATLAB path.
  • Install the supported version of DAQmx. For example, ScanImage 5.2 requires v15.5.
  • Add the examples in this repository to your path or cd to the code directory to run the examples.
  • You may create simulated devices in NI MAX to run the examples on a machine with no NI hardware connected. Triggers do not work in simulated mode: they fire immediately. Simulated mode also works in a virtual machine.

Running examples

By default all examples will run on NI DAQ device Dev1. In each example this device ID is defined by a variable called devName near the start of the function. You will therefore either need a DAQ device called Dev1 or you will need to edit the code accordingly. You can see which devices are connected in NI MAX or by running: vidrio.listDeviceIDs at the MATLAB command line. For each example, first look at the help text (e.g. help vidrio.AO.softwareTimedVoltage) then run at the MATLAB command-line. e.g.

>> vidrio.AO.softwareTimedVoltage

There are further comments in-line so open the example in an editor to learn more. Examples have been tested with an NI PCI-6229 and PXIe-6341. Many examples have also been verified to work with a simulated NI PCIe-6341. Not all examples work on all boards (e.g. the re-triggerable example requires an X-Series board). You may simply get errors with certain combinations of cards and examples. e.g. With a PCI-6115, certain buffered examples will complain that the sample rate is too low so you will need to modify this.

Key Contents

  • vidrio.listDeviceIDs - shows how to list available device and query them to obtain detailed information

Basic analog input and output examples

  • vidrio.AO.softwareTimedVoltage - software-timed ("on demand") analog output
  • vidrio.AO.hardwareFiniteVoltage - hardware-timed analog output (using the on-board clock) of a fixed number of points
  • vidrio.AO.hardwareContinuousVoltage - basic continuous analog output with the on-board clock
  • vidrio.AO.hardwareContinuousVoltageNoRegen - basic continuous analog output with the on-board clock that recycles the output buffer
  • vidrio.AO.hardwareContinuousVoltageNoRegen_DigTrig - Continuous analog output that is hardware triggered by a digital (TTL) rising edge
  • vidrio.AI.softwareTimedVoltage - software-timed ("on demand") analog input
  • vidrio.AI.hardwareFiniteVoltage - hardware-timed analog input (using the on-board clock) of a fixed number of points
  • vidrio.AI.hardwareContinuousVoltage - A simple example showing hardware-timed continuous analog input with no callback functions
  • vidrio.AI.hardwareContinuousVoltageWithCallBack - Hardware-timed continuous analog input with a callback function. Acq ends when figure is closed.
  • vidrio.mixed.AOandAI - continuous AI and AO that run simultaneously and in sync.

Basic digital IO and counters

  • vidrio.DO.softwareBasic - simple on-demand digital output
  • vidrio.CO.singlePulse - create a single digital pulse using a counter task

More advanced techniques

  • vidrio.mixed.AOandAI_OO - Interactive continuous AI and AO using object-oriented programming.
  • vidrio.mixed.AOandAI_OO_sharedClock - Interactive continuous AI and AO with shared clock between AO and AI.
  • For retriggerable tasks see vidrio.mixed.DO_retriggerable and vidrio.mixed.AO_retriggerable
  • vidrio.sync is a demo package showing how to synchronise two DAQ devices and why you need to do so.

Hints

The use of try/catch blocks should ensure the DAQmx tasks always shut down cleanly. If they do not:

  • If the DAQ device claims to be in use, you can reset it using MAX.
  • If you end up with orphan tasks, you will have to close and re-open MATLAB.

Further information

DAQmx in other languages

Projects using the Vidrio DAQmx wrapper

Projects using NI's DAQmx .NET assembly

matlab_daqmx_examples's People

Contributors

raacampbell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matlab_daqmx_examples's Issues

helper function for clearing existing task of a defined name

Because of the annoying errors where an existing task of the same name exists following an error:



    % Check if free

    taskMap = dabs.ni.daqmx.Task.getTaskMap;

    if any(strcmp(taskMap.keys, obj.taskName))

        oldTask = taskMap(obj.taskName);

        disp(['Deleting existing task "' obj.taskName '"']);

        delete(oldTask);

    end

Check that fixes our problem. If so, write a function that wraps task creation so all functions use it and we avoid this annoying problem.

Identify why the device does not disconnect in certain cases where there is an error

I have more than once found that on some errors the device is not disconnected, even though the cleanup function ought to do this. So check:

  1. Is it indeed the case that the cleanup function isn't running in these cases?
  2. Really do we need the try/catch statements? Maybe there is a less nasty way of achieving the same effect.
  3. Is there a way of liberating a blocked device? Perhaps a MAX reset? If so, document it.

Sync demo plotter

Make a class the pulls in data from two vidrio.sync..sine_AO_AI classes and plots them: both the raw traces and one against the other to show the phase change in a nice way.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.