You are on page 1of 80

LabVIEW

DAQ addendum

ME 2e semestre
rev. 1

Christophe Salzmann
Photo Martin Klimas
Module 2
Data acquisition & real-time control
–  Intro
–  Acquisition chain - from sensor to CPU
–  Multi-functions DAQ board
–  DAQ on Windows
–  DAQ on OSX
–  Selected examples
–  Simple control solution

Aims: being able to program a simple data


acquisition/control VI
Intro - Getting measurements
•  Via your machine (PCIe, PCMCIA)

•  Via a bus (USB, GPIB, Ethernet, Fire wire)

•  Via a remote controller/instrument

•  Wirelessly
Data AcQuisition (DAQ) @ LA

• M-Serie PCIe 6259


32 x 16-bits analog inputs, 1.25MS/s
4 x 16-bits analog ouputs, 833 kS/s
48 x DIO
2 x counters

•  Same pinout as PCI-6229 (0.25MS/s) in old Mac G5


•  Previously we used PCI-1200, only available in MacOS9
From Sensor to CPU

Physical Signal Signal CPU


phenomenon Sensor Signal conditioning conversion memory

0x6544

Perturbations
Sensors
device that measures a physical quantity and converts it into a signal
which can be read by an observer or by an instrument

Position -> optical encoder, camera, potentiometer, laser


Speed -> tachometer
Force -> strain gauges
Light -> phototransistor
Temperature -> thermocouple, RTD
pH -> pH electrode
Sound -> microphone

Sensor fundamental http://zone.ni.com/devzone/cda/tut/p/id/4045


How-To Guide for Most Common Measurements http://zone.ni.com/devzone/cda/tut/p/id/7136
Sensors output - signals
Analogical
–  Shape
–  Level
–  Frequency

Digital
–  State
–  Rate
digital signals can be treated as analog signals
Signals conditioning
Make your signal easier to measure by the DAQ device
Reduce the effects of perturbations

What you have What you expect


Signals conditioning
•  Amplification/attenuation
–  use the full range of the DAQ A/D converter, increase accuracy

•  Isolation
–  protect your hardware

•  Excitation (power) to the sensor


–  for strain gauges, potentiometer, microphone

•  Linearization, Cold-Junction Compensation


–  RTD, thermocouples

•  Filtering
•  Multiplexing
Signals conversion – DAQ
clk, sync., trig.

RTSI connector

I/O connector

PCIe connector
0x12F884B1 0x12346D31
0x67C12893 0x856A7727
Signals conversion – to CPU

Interrupt
-  every sample
-  ½ buff.
-  full buff. CPUs

FIFO buffers

PCI/PCI-express Bus

DMA access

RAMs
Data acquisition board

Understand the board operations to


understand the board programming

Focus on
–  NI PCI 6229 (G5)
–  NI PCIe 6259 (MacPro)
Multi-functions DAQ board

PCI 6229
2 x 16-bits AD, 250kS/s
Analog IN 4 x 16-bits DA, 833 kS/s
Mux G AD FIFO 48 x DIO
2 x counters
Analog OUT
DA FIFO

Digital I/O
DIO Buff

Cntr Counter
STC
PFI

0x12F884B1 0x12346D31
0x67C12893 0x856A7727
Analog IN
Select input line Amplify signal Convert to
binary
Select input mode
Filter Bufferize
Analog IN - multiplexer

•  The analog multiplexer select the


input to be converted (out of 32)
•  The PCIe 6229 has only 1 AD
converter

•  Only 1 conversion can occur at a


given time ADC

•  Other boards have multiple ADC


and thus perform the AD ADC

conversions concurrently …

ADC
Analog IN – GND reference

•  Select the type of input grounding


•  Proper grounding is required to get
correct measurements
•  You need 2 wires for a measurement:
Ain & reference

AI+
+ NRSE
Differential
+ –  input signal low level > 1v
–  RSE
require 2 inputs
AI- – connection length < 3m
– – 
–  –  low
avoid level signal (<1v)
grounds
input signal potential
low level > 1v
–  difference
noisy environment
–  connection length < 3m
–  long connection (>3m)
–  subject to common
–  require separate GND ref
AI SENSE ground

Referenced –  no bias resistor needed

Non-Referenced
Single-Ended
Differential (RSE) AI GND
Single-Ended (NRSE)
Analog IN - Amplifier

•  Select a gain multiplier


•  Allow to use the full ADC range
•  Augment precision

+ 10 V + 10 V

+
G

- 10 V - 10 V
Analog IN - Filter

•  Reduce aliasing of signals beyond the


Nyquist frequency
•  On the PCI-6229, the frequency is fixed
at 750kHz

•  Nyquist Theorem
You must sample at greater than 2 times the maximum
frequency component of your signal to accurately
represent the FREQUENCY of your signal
You must sample between 5 - 10 times greater than the
maximum frequency component of your signal to
accurately represent the SHAPE of your signal

http://zone.ni.com/devzone/cda/tut/p/id/4869
http://zone.ni.com/devzone/cda/tut/p/id/3016
http://www.ni.com/swf/presentation/us/sampling/
Analog IN – A/D converter

Convert analog signal to digital


representation

[v]
+ 10 1111
11111111
11111110
1110
11
1101
1100
1011
ADC
10 "10
("10)
10b bb10 ""("10)
("10)
1010
10
1001
1010
10101001 2
"10"10
= ==
"10
1000 48
0 0111.
.
10
1010
10101001 2 22
0110.
01
0101
0100
(3*
(169 *(10 5) "10"10
*1.25)
0.078125) 5[v]
="10 ==2.5[v]
3.20[v]
0011
0010
00
0001
00000001

- 10 0000
00000000

! !
2 bits ->
4
8 2842 = 256
4 levels
16 levels
levels
Analog IN – A/D converter

Convert analog signal to digital


representation

[v]
+ 10 1111111111111111
10 " ("10)
= 308 [µV]
216
200 mV" ("200 mV)
= 6.10 [µV]
216
65536
0 ADC
!
-5% of codes for calibration
!
± 10 V -> 320 [µV]
- 10 0000000000000000
± 200 mV -> 6.4 [µV]

16 bits -> 216 = 65536 levels


Analog IN - buffer

•  Store data during acquisition to ensure


that no data are lost
•  4096 points can be store
•  Not used for control since we want
the most recent value
•  Used for multi-channels acquisition

channel 1 Conversion period


channel 2 6229= 250kS/s, 6259= 1MS/s

∆ settling time (influenced by gain,


conversion period channels order, impedance)

sampling period h

http://zone.ni.com/devzone/cda/tut/p/id/2825
http://digital.ni.com/public.nsf/allkb/73CB0FB296814E2286256FFD00028DDF?OpenDocument
Analog IN – recap

•  Multiplexed inputs
•  Multi-channels acquisition
•  Ground reference mode
•  Sampling frequency
•  Quantification

channel 1
channel 2

conversion period

sampling period h

http://zone.ni.com/devzone/cda/tut/p/id/2825
http://digital.ni.com/public.nsf/allkb/73CB0FB296814E2286256FFD00028DDF?OpenDocument
Analog OUT
Convert to Analog signal

Buffer
Analog OUT – FIFO buffer
•  The FIFO buffer can store waveforms to
be written to the DA converters thus
without CPU interaction
•  Hardware timed waveforms generations
-  The time between samples can be much shorter
-  The timing between samples can be
deterministic
-  Hardware-timed acquisitions can use hardware
triggering
-  Allow regeneration (replay)

•  Software timed generations


-  used for control
Analog OUT – D/A converters
•  4 concurrent outputs, 16 bits, 833kS/s
•  Range PCI 6229: ± 10 [v]
•  Range PCI 6259: ± 10, ±5 [v], ext. ref.
•  Output current max ~ 25 mA
•  Triggering modes
-  Software ASAP
-  PFI <0..15> Synch with AD, k+1
-  RTSI <0..7> n/a on OSX,
but clock routed on RTSI
Digital IN-OUT
•  32 bidirectional digital input/output lines
•  16 Programmable Function Interface (PFI)
signals that can function as DIO
•  Can acquire or generate digital waveforms
using DI/DO FIFO buffer
•  Various signals/clocks (AI, A0, etc) can be
used to trigger DIO actions

No fly zone

•  TTL valid levels, referenced to D GND


0.0 – 0.8 v -> 0 +5.0 V

2.0 – 5.0 v -> 1 high

0.8 – 2.0 v -> undetermined +2.0 V


indeterminate
+0.8 V
low
0V
Counters
•  2 multi-purposes 32-bits counters/timers
•  Counter input applications
-  Counting edges
-  Pulse-width measurement
-  Period measurement
-  Frequency measurement
-  Position measurement – quadrature encoder

•  Counter output applications


-  Frequency generation
-  Pulse train generation

90°

A
B

counter value
PFI and RTSI
•  16 Programmable Function Interface (PFI)
-  can act as DIO
-  used to route internal/external signals to clocks, triggers, gate, etc.

•  Real-Time System Integration (RTSI) bus


-  8 RTSI programmable lines
-  share common clocks, triggers signals among NI-boards
-  restrictions apply when programmed from DAQmx base
Triggers
•  Signals that generate actions such as
starting or stopping data acquisition
•  Trigger sources can be external or
based on specific events related to:
-  Analog input acquisition
-  Analog output generation
-  Counter behavior
-  Digital waveform acquisition and
generation

start acq. stop acq.


DAQ software model

LabVIEW C, Matlab, …
User space

DAQmx DAQmx base - LV DAQmx base - Lib

Chip object (board abstraction)

osiBus (generic bus)


Kernel space

Board registers

Board Hwd

http://digital.ni.com/public.nsf/allkb/1C3988D7D33B925C86256C59007FB4B4
Acquiring data
•  At the lowest level the DAQ board is configured/
programmed via its registers (RLP). It s tedious!
Write32 (0x1234 @ 0x0019B8F)

Write32 (0x1234 @ 0x0019B8F)

•  At the highest level the DAQ board is programmed via


Tasks. It s a lot easier!
Speed measurement
Acquiring data
•  Identical hardware on Mac and PC

•  Software (may) differ


•  On Windows
MAX, DAQmx, DAQmx base

•  On Mac
DAQmx base

•  DAQmx base is a subset of DAQmx written in plain LabVIEW

Similar numbers of developers for DAQ drivers and LabVIEW!


BUT ~3 developers to develop DAQmx base
Acquiring data on Windows - MAX

•  MAX stands for Measurement & Automation


Explorer

•  Standalone application

•  MAX configures and organizes all your National


Instruments DAQ, PCI/PXI instruments, GPIB, IMAQ,
IVI, Motion, VISA, and VXI devices

•  Used for configuring and testing devices on


Windows
Windows - DAQ assistant
•  Express VI
•  Configure task as in MAX
•  Can also generate equivalent
LabVIEW code
Acquiring data on Windows
DAQmx

Timed loop
Acquiring data on MacOS 9
Traditional DAQ

LA’s Real-Time Kernel

RT DLL
Measurement Hardware DDK
•  Provides register level access to DAQ boards
•  Abstract board components into Chip Objects

•  Can be interfaced from C or LabVIEW


•  DAQ acquisition using Register Level
Programming is definitely not trivial!

•  National Instruments provides a higher level


on the top of the MHDDK, called DAQmx
base
DAQmx base
•  Current version 3.4.5 (Aug. 2011)
•  Is a lightweight subset of DAQmx
•  Many NI boards are supported, more to come, hopefully!
•  New low cost USB hardware from NI is supported
•  DAQmx base is improving, features are regularly added
DAQmx base
•  Is mainly written in LabVIEW (expose the source code)
•  Is therefore easily portable to other platforms/OS
•  Is therefore modifiable by the user


10 x

4x
on a 30 screen
DAQmx base
Task logic

Board logic

Register logic
DAQmx base – Static Tasks
Similar to MAX
Configured outside of LV with mxbaseconfig

Called and run within LabVIEW


Not all boards supported
DAQmx base – Dynamic Tasks
Configured and run inside LabVIEW

LabVIEW
Hardware
Create
Task

Create
Channel
Access
Hwd FIFO
Configure Stop

Clear
Start
DAQmx base – Create task
Create an empty task
The task name is optional
Other parameters are currently
ignored

Error handling is omitted for clarity


DAQmx base – Create channel
Create a channel

Specify the operation from the


popup menu
(AI, AO, DI,DO, CTRI, CTRO)

Specify the physical channel Analog In


location (not test at edition time)

Specify the channel specificities,


depends on the selected
operation

Error handling is omitted for clarity


Analog Out
DAQmx base – Configure
Define timing & sample mode

Specify the sample mode


–  Continuous
–  Finite number of samples
–  Hardware Timed Single Point

Specify the acquisition rate

Specify the clock source

Error handling is omitted for clarity


DAQmx base – Configure
Define triggers

Specify the trigger line type


–  Digital
–  Analog

Specify the trigger mode


–  Rising edge
–  Falling edge

Specify the trigger line

Specify the trigger level

Error handling is omitted for clarity


DAQmx base – Start
Start the defined task

Error handling is omitted for clarity


DAQmx base – Read/Write
Read from hwd buffer
Specify operation type
(Analog, Digital, Counter, Raw)

Specify the # of samples to acquire


Specify a timeout
Read

Write to hwd buffer


Idem above
Specify the value to be written

Error handling is omitted for clarity


Write
DAQmx base – End
Task completed ?

Stop task

The task can be started again

Clear task

The task resources are cleared

Error handling is omitted for clarity


DAQmx base – Wait
Wait for Next Sample Clock VI
Use this VI to ensure I/O cycles completion
within Sample Clock periods => We ll
use it for control
If an extra Sample Clock pulse occurs
between calls to this VI, the second call
returns a warning and waits for the next
Sample Clock pulse.
Only supports analog input M-Series tasks
Warm up iterations are not supported
DAQmx base – More
Reset a device
Abort and release all tasks
associated with this device

Resize In/Out buffers


0 to perform a hardware-timed
operation without using a buffer

Export signal
Route a given signal to the specified
terminal

Error handling is omitted for clarity


Timing tasks
Means to time a task (call it repetitively)
–  LabVIEW primitives
10ms
Wait ms Wait Until Next ms Multiple

–  The OS primitive (threading, interrupt, timers)


0.1 ms
The OS can influence the execution of such task
Old MacOS9 real-time kernel
Timed loop (Windows only)

0.001 ms
–  The DAQ card (on board clock)
Very accurate, not disturbed by the OS
LabVIEW RT & FPGA

–  A mix of the 3 ~ 0.1 ms on current MacPro


DAQmx base – From C
C functions similar to the LabVIEW VI, but you don t have the power
of LabVIEW (multicores, mulithreading, etc)


#include "NIDAQmxBase.h



Function Read_one_Sample_one_Chanel() {
err=DAQmxBaseStartTask(taskHandle);
int32 err = 0;
err=DAQmxBaseReadAnalogF64(taskHandle,
TaskHandle taskHandle = 0;
1, 10.0, DAQmx_Val_GroupByChannel,
float64 data;
&data, 1, &pointsRead,NULL));
int32 pointsRead;


printf ("Acquired reading: %f\n", data);
err=DAQmxBaseCreateTask("",&taskHandle);

err=DAQmxBaseCreateAIVoltageChan(taskHandle,
If (err != noError):
"Dev2/ai0 ,"",
DAQmx_Val_Cfg_Default, // process the error,
-10.0, 10.0, // code removed for clarity
DAQmx_Val_Volts,NULL);
return 0;
}
Samples and channels

Most of DAQmx base functions


handle one or more channels
and one or more samples
channel 1

DAQmx base task always read channel 2


or write a group of channels
(vector) even if the group

Samples k+1
Samples k
only contain 1 channel

In the coming examples we


won t make any difference
between group sizes
Task configuration steps

Define the device & channel (Dev1:ai2)

Define the type of operation R&W (read)

Define the clock reference

Define triggering

Task config. are validated & stored in an


enormous global variable
Read ONE sample
LabVIEW

Read
AD
Hwd

convert
return
time
Read n samples, software timed
LabVIEW
n

Read
AD
Hwd

return
convert
≠ time
Read n samples, hardware timed
LabVIEW

Config. Read =
clock n AD
Hwd

buffer

return
convert
time
Read continuously, hwd timed
LabVIEW

Config. Read =
clock n AD
Hwd

return
convert
buffer

return
time
Read cont. 1 sample, hwd timed
LabVIEW

Config. Read
clock AD
Hwd

return
convert
= time
Write ONE sample
LabVIEW

Write
DA
Hwd

convert
return
time

∆ no auto start in DAQmx base, the AO task has to be started first


Write n samples, software timed
LabVIEW
n

Write
DA
Hwd

return
convert
≠ time
Write n samples, hardware timed
LabVIEW

Config. Set Write


clock Buff. n DA
Hwd

convert

done
buffer

convert
= time
Write n samples cont., hwd timed
LabVIEW
Regenerate AO buff.
Config. Set Write
clock Buff. n DA

Hwd

convert

done
buffer

convert
time
Write n samples cont., hwd timed
LabVIEW
new
Config. Set Write AO buff.
clock Buff. n DA

Hwd

convert

done
buffer

convert
time
Lines and Ports

Digital Lines are grouped into Ports


generally 8 or 32 bits
Port0:line1
:line2
It may not be possible to access


:line32
lines directly, need to mask the
unwanted lines Port1:line1
:line2


:line16

Samples k+1
Samples k
Single Digital In - Out
LabVIEW

DIO
Hwd

time

DIO
Digital in Digital out
Digital In, cont., hwd. timed
LabVIEW

Config. Read =
clock D-In
Hwd

read

read
time
# Read!
Incremental Encoder, cont.
LabVIEW

Read
Enc.
Hwd

read
≠ time
Ask for More

More examples can be found in

/LabVIEW/examples/daqmxbase

Especially trigger and external clocks examples


We re almost done

Take a deep breath


Real-time Tasks
No built-in Real-Time Task support on
g LabVIEW
MacOSX
On OSX LabVIEW code execution can be PID
interrupted

software
Tasks running on the DAQ board are
executed in hardware clocked

To implement Real-time tasks we need to

hardware
- Synchronize board tasks
- Synchronize board tasks and LabVIEW
t
- Ensure that LabVIEW RT code (g) runs faster
than DAQ period h ADAD h
DADA
DIO
DIO
Synchronize DAQ and LabVIEW
•  DAQmx base can propagate the DAQ board sample clock to
LabVIEW
•  LabVIEW can actively wait until the next clock pulse

•  Real-time control can be guaranteed as long as the LabVIEW RT task


is completed BEFORE the next clock pulse
•  The LabVIEW RT task (PID) can be floating between 2 sample clock
pulses

PID PID PID PID

✔ ✔ ✔ ✗
Synchronize Multiple Tasks
Share the Sample Clock in the terminal RTSI0
Set the source of the other sample clocks to RTSI0


Synchronize DAQ and LabVIEW
DAQmx base can propagate the DAQ board sample clock to LabVIEW
LabVIEW can actively wait until the next clock pulse

Read task


Write task
Data exchange, RT -> GUI
The RT task runs at ~1 kHz
1 ms

RT task
The RT task should not be interrupted PID
nor delayed (waiting for resource)
The RT task data display should be
performed in another thread (loop)
Queue
The LabVIEW front panel updates at ~
50 Hz
The RT task data should be stored at 100 ms

GUI task
each interruption and transferred to
a secondary thread for display
This synchronous buffer is implemented
using Queue, others mechanisms
are possible
Data exchange, GUI -> RT

In the GUI parameters for the RT task 1 ms KP

RT task
(e.g. Kp) are updated PID
asynchronously, ~10 s

The RT task should not be interrupted


nor delayed (waiting for resource) Params Queue

The GUI parameters for the RT task are


stored in a LabVIEW 2 style global
(remember ex. 1.6 ?). This 100 ms

GUI task
mechanism cannot delay RT task
KP
Class 2 - recap
•  Correctly wire your sensors (gnd)
•  Configure your DAQ operation accordingly
•  DAQ is task based
•  Create/Configure/Start – Exchange data – Stop/clear
•  RT is possible at ~1ms with plain LabVIEW

You might also like