1 of 81

儀器電控

陳彥穆

July 11, 2008

2 of 81

Outline

  • Computer-based Measurement Automation
    • Design Flow
    • A Comprehensive Overview & some history
  • Labview
    • Introduction
    • Control Examples –
  • Matlab
    • Introduction
    • Control Examples – Graphic User Interface Design
  • Comparison
    • Merits and Defects
    • Hybrid

3 of 81

Instrument Control !?

The abilities to

  • Write

Write some bits or bytes to the instruments as commands

  • Read

Read information or measured data from the instruments

4 of 81

Instrument Control !?

In order to do that, we require

  • Interface

Physical connections between computers and instruments

  • Interface driver

It tells computers how to interpret the data on the interface ( Protocols )

5 of 81

Instrument Control !?

and we still need

  • Command book

The language of “the” instrument.

We call a complete and well-capsulated command set as “Instrument Driver.”

  • Control Program

combinations of commands to finish our job

6 of 81

Why?

  • Save time and man power
  • Your instrument has no front panel

(CAMAC VXI PXI)

7 of 81

Part-I: Design Flow

8 of 81

Measurement Automation

  • Design flow

Problem Define

Nature of Signal

Programming Language /

Environment

Programming

Testing

Instruments

The Control Flow Chart

Instrument Command

Interface Drivers

  • Type?

Magnitude?

Duration?

  • Resolution?

Dynamic Range?

Field of View?

  • Instrument Interface
  • VB, C/C++, VC.Net, BCB…
  • Matlab
  • Labview
  • Serial port driver
  • GPIB driver
  • USB driver

  • Instrument Command

User Interface

  • Control Procedure

9 of 81

Problem Define: The control flow chart

A sample control flow chart

10 of 81

Problem Define: Nature of signal

  • Type
    • Electro-

voltage/current

impedance

    • Photon

wavelength

  • Magnitude
  • Duration

11 of 81

Problem Define: Instruments

  • Resolution
    • Intensity
    • Time response of instrument
  • Dynamic Range
    • Intensity
    • Frequency
  • Field of View

  • Instrument Interface
    • Crate Instrument: CAMAC / VXI / PXI
    • GPIB / RS-232 / USB / IEEE1394
    • PCI / ISA

12 of 81

A real example: �measure laser contrast automatically

13 of 81

A real example-�measure laser contrast automatically�Control Flow Chart

14 of 81

A real example-�measure laser contrast automatically�The Instruments

15 of 81

A real example-�measure laser contrast automatically�The Instruments

Increase dynamic range by filters, which can be changed by

a stepping motor.

16 of 81

Programming : �Programming Language / environment

  • text-based languages
    • C/C++, VC.Net, BCB
    • Visual Basic
    • Matlab (Scientific environment)

  • graphical programming language
    • Labview

17 of 81

Programming : �Instrument Command / Interface Drivers

  • Interface Drivers
    • Serial port driver
    • GPIB driver
    • USB driver …
    • VISA (Virtual Instrument Software Architecture)

  • Instrument Command
    • SCPI (Standard Commands for Programmable Instrumentation)
    • VXI Plug&Play
    • IVI (Interchangeable Virtual Instrument )
    • Instrument specific command

18 of 81

Part-II: History

19 of 81

A Comprehensive Overview of Measurement Automation

  • CAMAC (Computer Automated Measurement And Control)

CAMAC is an IEEE-standard (583), modular, high-performance, real-time data acquisition and control system concept born in 1969.

A CAMAC “crate”

擷取自 http://www.kscorp.com

Register-based command

20 of 81

A Comprehensive Overview of Measurement Automation

  • GPIB (General Purpose Interface Bus / IEEE-488)

For monolithic instruments

HP designs HP-IB �for instrumentation systems

HP-IB becomes IEEE-488.1 �standard

IEEE 488.2 adopted

SCPI Specification introduced �for IEEE 488 instruments

IEEE 488.2 Standard revised

1965

1975

1987

1990

1993

Text-based command

21 of 81

A Comprehensive Overview of Measurement Automation

  • VXI (VME eXtensions for Instrumentation / IEEE-1155) 1987

Text-based / Register-based command

擷取自 �Matlab Instrument Control Toolbox Help documents

22 of 81

A Comprehensive Overview of Measurement Automation

  • Serial Port

RS-232 COM1

very common on personal computers

  • Parallel Port

printer port LPT

23 of 81

Difficulties in developing a measurement automation system

  • Differences in all Instruments
    • every company has its own interface
    • every instrument has its own command

  • Different software support in different environments
    • Interface driver for UNIX / DOS / Window/ …
    • Instrument driver written in C/ Fortran/

Non-Interchangeable

Time inefficient

24 of 81

A Comprehensive Overview of Measurement Automation

  • VISA (Virtual Instrument Software Architecture)

Is a superset of all interface driver

(from VXI plug&play)

Vendor B

Test

Executive

Application

Vendor C

Instrument

Programming

Tool

Computer Industry Standards

(DDE, DLL, OLE, TCP/IP, etc.)

Vendor A

Application

Software

Environment

VISA API

NI-488.2

API

NI-VXI

API

Other

API

Other

API

End Users

Application

Programs

25 of 81

A Comprehensive Overview of Measurement Automation

    • VISA provides us a unified communication “channel” from your program to the instrument with any physical interface.

VISA-GPIB, VISA-VXI, VISA-serial port, VISA-TCP/IP …

    • Same I/O function for open, send, read, query, close…, but not same command.

    • Now, The VISA specifications, formerly maintained by the VXI plug&play are now being maintained by the IVI.

26 of 81

A Comprehensive Overview of Measurement Automation

  • IVI (Interchangeable Virtual Instrument)

The IVI standards define an open driver architecture, a set of instrument classes, and shared software component.

27 of 81

A Comprehensive Overview of Measurement Automation

  • Utilizing VISA to achieve interface I/O (other interface drivers are also available)
  • IVI defines several Instruments class to achieve same “command” ( just like SCPI)
    • DC power supply
    • Digital multimeter (DMM)
    • Function generator & Arb
    • Oscilloscope
    • Power meter
    • RF signal generator
    • Spectrum analyzer
    • Switch
  • The IVI Config server – for instrument interchangeability
    • Two instruments with different vendors, different interfaces (e.g. GPIB and USB) can be interchanged just in a few minutes.

28 of 81

The Structure

Instrument Hardware

VISA others

Interface

GPIB/VXI/USB/serial… interface drivers

Or just VISA

IVI class driver

Application

Instrument

driver

Software

GPIB/CAMAC/VXI/USB interface

Hardware

Most of the time, we start here……

29 of 81

Part-III: LabVIEW

30 of 81

LabVIEW (Laboratory Virtual Instrument Engineering Workbench) is a program development application, much like C or BASIC.

However, other programming systems use text-based languages to create lines of code, while LabVIEW uses a graphical programming language, G, to create programs in block diagram form.

What is LabVIEW?

Writing programs in Labview is just like building real instruments.

31 of 81

Front Panel

VI : Virtual Instrument

The front panel is the graphical user interface of your LabVIEW VI. This interface collects user input and displays program output. The front panel can contain knobs, push buttons, graphs, and other controls and indicators.

The GUI of your program

32 of 81

Block Diagram

The block diagram can include functions and structures from the built-in LabVIEW VI libraries. It also can include terminals that are associated with controls and indicators created on the front panel.

The block diagram contains the graphical source code of your VI (Virtual Instrument). In the block diagram, you program your VI to control and perform functions on the inputs and outputs you created on the front panel.

The source code of your program

33 of 81

Front panel demo: a virtual instrument

…\examples\apps\demos.llb\Two Channel Oscilloscope.vi

34 of 81

Controls and indicators

35 of 81

LabVIEW palettes give you the options you need to create and edit your front panel and block diagram.

Tools Palette

Functions Palette

Controls Palette

Palettes

Front panel / Block diagram

36 of 81

LabVIEW VIs follow a dataflow model for program execution. The block diagram consists of nodes such as VIs, structures, and terminals from the front panel. These nodes are connected by wires, which define the flow of data through the program. The execution of a node occurs when all its inputs are available. When a node finishes executing, it releases all its outputs to the next node in the dataflow path.

Data Flow

37 of 81

Arithmetic

z = sqrt(abs(x)) + y;

One may use a

formula node:

38 of 81

Boolean algebra

D = A AND (B OR C)

diagram

panel

39 of 81

Comparison and CASE structure

if a > b then

todo list 1……

else

todo list 2……

end

40 of 81

FOR loop

C++ code:

a=1;

for (i=0, i<6, i++){

a=a+a*i;

}

LabVIEW diagram:

Output = 720

41 of 81

Sequence and While loop

sequence

while loop

42 of 81

Graph and chart

Example: plot a sine wave

43 of 81

Mechanical Action

Switch When Pressed

Switch When Released

Switch Until Released

Latch When Pressed

Latch When Released

Latch Until Released

44 of 81

PC serial

port

Output from computer

Input to computer

RS-232 pin assignments

45 of 81

46 of 81

Remote control using RS-232

Some parameters to know before using the serial port

1. Port number:

port number = 0 for COM1, 1 for COM2

2. Baud rate (or bits per sec, bps)

3. Data bits: number of bits in the incoming data

4. Number of stop bits

5. Parity bit? (None, Odd, Even)

6. Command characters used by the device

Find parameters 2~6 from the manual of the device!

47 of 81

RS-232 Serial character frame

48 of 81

Serial Port VIs in (Labview 6.1 or older version)

(Functions»Instrument I/O»Serial)

Bytes at Serial Port

Returns in byte count the number of bytes in the input buffer of the serial port indicated in port number.

Serial Port Init

Initializes the selected serial port to the specified settings.

49 of 81

Serial Port Read

Reads the number of characters specified by requested byte count from the serial port indicated in port number.

Serial Port Write

LabVIEW Function and VI Reference Manual 36-2 Writes the data in string to write to the serial port indicated in port number.

Serial Port VIs (Cont.)

50 of 81

VISA Serial Port VIs in (Labview 7.0 or later version)

(Functions»Instrument I/O»Serial)

VISA Open

Open the VISA channel according the VISA resource name

VISA Serial Port Init

Initializes the selected serial port to the specified settings.

VISA Close

Close the VISA channel according the VISA resource name

51 of 81

VISA Read

Reads the number of characters specified by requested byte count from the VISA interface indicated in VISA Resource name.

VISA Write

LabVIEW Function and VI Reference Manual 36-2 Writes the data in string to write to the VISA interface indicated in VISA Resource name.

Serial Port VIs (Cont.)

52 of 81

VISA Resource

  • The MAX (Measurement Automation eXplorer)

A GUI tool to manage and test your instruments

53 of 81

VISA driver

It usually comes along with Labview or you can simply download it from www.ni.com

54 of 81

Your Interface information

Some handy knowledge about this Interface

55 of 81

Part-IV: MATLAB

56 of 81

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.

What is Matlab?

57 of 81

MATLAB 7 R14 Instrument Control Toolbox Test&Measuement Tool

>> tmtool just like the MAX of Nation Instrument

58 of 81

How to use GPIB in Matlab

>>help gpib

  • GPIB Construct GPIB object.
  • OBJ = GPIB('VENDOR',BOARDINDEX,PRIMARYADDRESS) constructs a
  • GPIB object, OBJ, associated with board index, BOARDINDEX, and
  • instrument primary address, PRIMARYADDRESS. The primary address
  • can range between 0 and 30. The GPIB hardware is supplied by
  • VENDOR. Supported vendors include:
  • 'agilent' - Agilent Technologies hardware.
  • 'cec' - Capital Equipment Corporation hardware.
  • 'contec' - CONTEC hardware.
  • 'ics' - ICS Electronics hardware.
  • 'iotech' - IOTech hardware.
  • 'keithley' - Keithley hardware.
  • 'mcc' - Measurement Computing Corporation (ComputerBoards) hardware.
  • 'ni' - National Instruments hardware.
  • .
  • .
  • .

59 of 81

GPIB (IEEE-488) bus history

  • GPIB (General Purpose Interface Bus / IEEE-488)

For monolithic instruments

HP designs HP-IB �for instrumentation systems

HP-IB becomes IEEE-488.1 �standard

IEEE 488.2 adopted

SCPI Specification introduced �for IEEE 488 instruments

IEEE 488.2 Standard revised

1965

1975

1987

1990

1993

Text-based command

60 of 81

Pin assignment of GPIB

擷取從 MATLAB HELP

Data Line: 8

Management Line: 5

Handshake Line: 3

Signal Line: 16

Ground and Shield: 8

61 of 81

Listeners can only receive data and commands from the bus.

Talkers can transmit data to other devices.

The controller supervises the transfer of data along the bus.

This role is usually performed by a PC.

GPIB (IEEE-488) bus topology

62 of 81

Parallel buses possess a separate signal line for each bit,

enables a byte to be transmitted in one operation.

Serial buses transfer one bit at a time along a single

conductor.

Data-acquisition interface devices

63 of 81

IEEE-488 bus lines

8 bi-directional data lines, DIO1 – DIO8.

Data transfer handshake lines,

DAV: Data Valid

Asserted by talker to indicate bus holds valid data.

NRFD: Not Ready For Data

Asserted by listener to indicate that it cannot receive data.

NDAC: Not Data Accepted

Asserted by listener while reading data.

64 of 81

IEEE-488 handshaking sequence

All signal lines use active low, TTL logic levels.

Listener

Talker

Listener

Talker

Talker

Not Data Accepted

Not Ready For Data

Data Valid

Attention

65 of 81

IEEE-488 bus lines

Interface management lines,

IFC: Interface Clear

Asserted by controller to initialize all bus devices.

ATN: Attention

Asserted by controller to indicate that the data bus holds a

command rather than data.

REN: Remote enable

Asserted by controller to disable any front panel controls.

EOI: End or Identity

Asserted by talker to identify the last byte of data in a block.

SRQ: Service Request

Asserted by any device to request the attention of the controller.

66 of 81

GPIB

%Suppose We have a NI GPIB card with board index 0 Primary address 1

>>g = gpib(‘ni’, 0, 1);

%To connect the GPIB object to the instrument:

>>fopen(g)

% To query the instrument.

>>fprintf(g, '*IDN?'); %Identify yourself or else…

>>idn = fscanf(g);

% To disconnect the GPIB object from the instrument.

>>fclose(g);

It’s just like writing/reading to/from a file

>>instrhelp gpib %for further help, or

>>doc gpib %open the help

67 of 81

How about serial port

%Suppose we have a serial port ‘com1’, and we want to set its BaudRate to %1200

>>s1 = serial('COM1', 'BaudRate', 1200)

>>fopen(s1)

% To query the instrument.

>>fprintf(s1, '*IDN?'); %Identify yourself or else…

>>idn = fscanf(s1);

% To disconnect the serial port object from the instrument.

>>fclose(s1);

First, check it out in the command line

>>help serial

And you’ll find it almost the same with GPIB

68 of 81

Matlab vs. Labview

  • serial('COM1', 'BaudRate', 1200)

  • fopen(s1)

  • fprintf(g, '*IDN?');

  • fscanf(s1);

  • fclose(s1);

69 of 81

How to use VISA interface

  • MATLAB has it own GPIB and serial port driver, but you can still use the version in VISA library (vmtool)

% To construct a VISA-GPIB object connected to Board 4 with an

% instrument with primary address 1 and no secondary address.

>>g = visa('ni', 'GPIB4::1::0::INSTR');

% To construct a VISA-VXI object that communicates with a VXI

% instrument located at logical address 8 in the first VXI system.

>>v = visa('agilent', 'VXI0::8::INSTR');

% To construct a VISA-serial object that is connected to COM1 with %BaudRate 1200.

>>s=visa(‘ni’,’ASRL1::INSTR’,’BaudRate’,1200)

%still

>>fprintf(g,’*IDN?’)

>>fscanf(s)

>>inspect(s) %GUI tool for checking all the properties of this object

70 of 81

Basic building blocks of GUI �and Event-driven programming in Matlab

  • Figure – a front panel of your program
  • Uicontrol
    • Check boxes
    • Editable text fields
    • List boxes
    • Pop-up menus
    • Push buttons
    • Radio buttons
    • Sliders Static
    • text labels
    • Toggle
    • buttons

圖擷取從 MATLAB程式設計與應用-張智星

71 of 81

Basic building blocks of GUI �and Event-driven programming in Matlab

  • Callback functions

figure

h = uicontrol(gcf,'Style','Push','position',[50 20 80 30],'string',‘Push');

cmd = 'fprintf(''Somebody pushed me!\n'');';

set(h, 'Callback', cmd);

  • Figure and Object Handle

擷取從 MATLAB程式設計與應用-張智星

72 of 81

Some useful commands

  • findobj

Locate graphics objects with specific properties

  • gcf

Get current figure handle

  • gca

Get current axes handle

  • gcbo

Return handle of object whose callback is executing

  • set / get

set / get properties of object

73 of 81

Measurement Automation in Matlab

  • Retard.m

PC�MATLAB

GPIB

LPT

Pin2 and pin7

Oscilloscope

Serial Port

High Voltage Main Frame

FireBox

Pressure

Watcher

MCP

grid

Laser System

Laser Intensity

monitor

74 of 81

Part-V: Comparison

75 of 81

Labview > Matlab

  • Labview
    • G-Language (Graphic)
    • great GUI
    • fast response
    • Multi-threading-embedded

  • Matlab
    • M file (Text)
    • Limited GUI
    • Interpreter (Slow)
    • Single threading (M file)

擷取從 Labview Documentation

readasync

76 of 81

Matlab > Labview

  • Matlab
    • M file (Text), handle string well
    • Unlimited algorithms and it’s easy to add your own
    • It’s easy to modified your subroutine
    • Comment and Documentation

  • Labview
    • lots of algorithms but it takes a while to locate it.

“draw” another one?

    • Connector adding
    • It is messy to write documentation

Show dataprocess.vi

77 of 81

Matlab == Labview

  • Matlab
    • Instrument Control ability

Serial port, GPIB interface, VISA driver

Instrument Driver, tmtool

    • Instrument driver editor – write your own or load it from IVI driver

  • Labview
    • Instrument Control ability

Serial port, GPIB interface, VISA driver

Instrument Driver, IVI, MAX

78 of 81

Outsourcing

They are both capable of calling other program or being called

  • Matlab (External Interfaces)
    • Call other DLL ( MATLAB Interface to Generic DLLs )
    • To be a COM server or client
    • Calling C from Matlab ( MEX-File wrapper )

  • Labview
    • DLL Invoke node
    • ActiveX Invoke node
    • Matlab Script node
    • CIN Code Interface Node – to call C code

79 of 81

How to enhance the GUI in MATLAB

  • Adding ActiveX Controls

>>h = actxcontrolselect;

>>h.get;

>>h.invoke;

>>methods(h);

>>methodsview(h);

80 of 81

How to design a program?

81 of 81

How to design a program?