Anda di halaman 1dari 58

h4p://home.hit.no/~hansha/?

page=labview

LabVIEW Programming

Hans-Pe4er Halvorsen, M.Sc.

Prerequisites
It is assumed you are familiar with the
LabVIEW IDE such as Front Panels, Block
Diagrams, Controls and FuncOons Pale4e
You should be familiar with SubVIs
You should be familiar with dierent Loops
like While Loops, For Loops, Case Structures,
etc.
Now it is Ome to take the next step and learn
some real LabVIEW Programming

Do you miss the necessary


Basic LabVIEW
Knowledge?

PDF File
available for
download

LabVIEW Course
IntroducOon to
LabVIEW
h4p://home.hit.no/~hansha/?training=labview

Available as a PDF le from:


h4p://home.hit.no/~hansha/documents/labview/course/labview_introducOon.htm

Do you miss the necessary Basic


LabVIEW Knowledge?
LabVIEW Training
LabVIEW Training for Students (NaOonal Instruments):

h4p://ni.com/students/learnlabview
LabVIEW Course:
h4p://home.hit.no/~hansha/?training=labview

Learning by Doing!
It is recommended that you watch some of the videos before you read furter

LabVIEW InstallaOon
Download the so\ware here:

Note! You get the Serial Number from your Teacher,


but the so\ware can be used for 30 days before you
need to enter a valid Serial Number.

h4p://home.hit.no/~hansha/?page=labview
These are the main modules we use in the dierent
courses at Telemark University College:
LabVIEW (LabVIEW Professional Development System 32-Bit: English)
LabVIEW Control Design and Simula8on Module
LabVIEW MathScript RT Module
NI-DAQmx (Hardware Driver for NI USB-6008, NI TC-01, etc.)
Note! These packages are separate downloads! Make sure to
download and install all of them!
All LabVIEW So\ware can be downloaded here: www.ni.com/download

LabVIEW = Fun!

Graphical Programming:
Very dierent from tradiOonal programming like
VB, C#, Maple, MATLAB, MathScript, etc.
It is more like a drawing program than a
Programming Language
This makes it easy to use for those who are not
programmers (or dont like programming J)
Excellent tool when using Hardware, when you
need to take Measurements (DAQ), etc.
It is fun and makes you very creaOve!

LabVIEW Example

LabVIEW has the same things as other programming languages, but in a graphical way!
Sequence Structure Comment While Loop
Local Variable
Sub VI

Property Nodes

Constants

Stop Bu4on

Case Structure
(if-else)
CondiOon
Arrays
Note! To do something with an object Right-click on it (When shall the loop end?)

Hardware

Air Heater

USB-6008

Wi-Fi DAQ
Water Tank
Pt-100
TC-01

Arduino

ZigBee

Vision System

cRIO
NOx Sensor

LabVIEW
This is the core LabVIEW installaOon that installs
the LabVIEW Programming Environment.

LabVIEW MathScript RT Module


This module is a text-based tool that is very similar to MATLAB.
The syntax is similar to MATLAB, you can create and run so-called
m les, etc. The module is available from the Tools menu inside
LabVIEW.

LabVIEW Control Design and Simula8on Module


This module is used for creaOng Control and SimulaOon
applicaOons with LabVIEW. Here you will nd PID controllers,
etc. The module is available as a pale4e on your block diagram.

NI-DAQmx
DAQmx is the Hardware Driver needed in order to use hardware
devices like NI USB-6008, NI TC-01, etc. inside LabVIEW. The module is
available as a pale4e on your block diagram.

Spaghen Code Very Bad Example

Structure your code! Use SubVIs, Avoid Spaghen Code, Document your Code, etc.

Bad vs. Good Code


The Flow should go from leK to right

Avoid SpagheG Code!

Make your code readable for others!

This is a comment

Use SubVIs!!!
Use Labels and Comment your Code

Basic Loops & Structures


You cant do real LabVIEW Programming without these loops!

While Loop

For Loop

Sequence Structure

Case Structure (if...else...)

Code Modularity - Use SubVIs!

O\en, new LabVIEW users create re and forget applicaOons to accomplish simple tasks
and dont think about how they can use that code in the future. As users start to program
more o\en, they can nd themselves rewriOng the same piece of code over and over again.
You can save yourself a lot of development Ome by creaOng modular subVIs out of porOons
of your code that can be reused within other applicaOons.

h4p://www.ni.com/pdf/manuals/376039a.pdf

LabVIEW Programming

Hans-Pe4er Halvorsen, M.Sc.

Contents

Local/Global Variables
Arrays
Clusters
Property Nodes/Invoke Nodes
Debugging & Error Handling
Shi\ Registers
Event Structure
State Machine principle
Project Explorer
GUI, VI ProperOes
Create Executable Files & InstallaOon Packages

Local Variables
You can create Local Variables in 3 dierent ways; from the
Structures pale4e on the Block Diagram, right-clicking the
GUI object on the Front Panel, or right-clicking the items
on the Block Diagram.

Local Variables
This Local Variable is connected
to the Temperature Item

WriOng a Value to
the Local Variable

Local Variable

Sending Value from a Local


Variable to another Indicator

You may assign a Local Variable to the available Items


(Controls and Indicators)

Be careful when using Local Variables


A common mistake in LabVIEW programs is an overuse of local variables. Local variables
are a piece of shared memory used to pass data between dierent secOons of a
computer program. Commonly used in text-based languages, variables can be very
empowering, but can also lead to problems when a race condiOon is encountered.

Unlike other programming languages where passing data through variables is essenOal,
LabVIEW provides the dataow method of moving data from one part of a program to
another.
The parallelism inherent to LabVIEW makes overusing variables problemaOc because
shared memory is o\en accessed by dierent code locaOons at the same Ome. If this
happens, one read/write operaOon wins the race and the other loses. The losing data
operaOon is forgo4en, so overusing variables in LabVIEW can ulOmately lead to lost data

Students: ...

Global Variables

Arrays
Arrays are very powerful to use in LabVIEW.
In all your applicaOons you would probably use both
One-Dimensional Arrays and Two-Dimensional Arrays.

Auto-Indexing

Arrays

Students: Create these Example

Array FuncOons
You cannot work with Arrays without using one or more of the built-in Array FuncOons

Array FuncOons
Array Size
Index Array
Delete from Array
Search 1D Array
IniOalize Array
Build Array
Array Subset
Array Constant

All these funcOons are basic (but very useful) array funcOons you will
probably be using in all your applicaOons and VIs.

Clusters

Clusters

Students: ...

Debugging Tools
Step into/over/out debugging
Probes

Breakpoints

Highlight ExecuOon

Probes
Probe Tool
Probe

Breakpoints

Step into/over/out debugging

Step into

Step over

Step out

Debugging & Error Handling

Wiring the Error Cluster between the dierent VIs for Proper Error Handling

Property Nodes

Property Nodes

Students: ...

Invoke Node

Invoke Node

Students: ...

References

You can use References in order to


e.g., remotely control GUI components
in your Main VI from a subVI

You can use References in order to


e.g., remotely control GUI components
in your Main VI from a subVI

References

Students: Create this Example

Project Explorer

Similar to SoluOons Explorer in Visual Studio. It


Keeps all your Files for a spesic project in one place.

Students: Put all the Code you


created today into the Project
Explorer. Create Folders, etc.,
i.e. create a good Structure
It is recommended to use this
when you create larger
applicaOons that contains lots of
VIs and other les, etc.

Shi\ Registers

Shi\ Registers

Students: ...

Event Structure

Event Structure

Students: ...

State Machine

Students: Explore the Simple State


Machine Template

State Machine

State Machine

State Machine
Lets build our own State Machine from scratch
A State Machine consists of the following main
components:
While Loop
Shi\ Registers
Case Structure
Event Structure

State Machine

Students: ...

GUI/HMI Guidelines

VI ProperOes

Pop-up window

CreaOng Executables

CreaOng Executables

Students: ...

LabVIEW Course
LabVIEW
Programming

PDF File
available for
download

h4p://home.hit.no/~hansha/?training=labview
Students: Read the Course Manual and
do the Exercises (as many as possible)
in the Course Manual on your own.
Note! SoluOons with code for all the
exercises are located on the web page

Course Manual with Exercises


Available as a PDF le from:
h4p://home.hit.no/~hansha/documents/labview/course/labview_programming.htm

LabVIEW Training
Here you will nd lots of Videos, Tutorials and Exercises

InvesOgate LabVIEW further by yourself


LabVIEW Training for Students (NaOonal Instruments):

h4p://ni.com/students/learnlabview

LabVIEW Course:
h4p://home.hit.no/~hansha/?training=labview

Learning by Doing!

Select one of the following Challenges

Hans-Pe4er Halvorsen, M.Sc.

Program som pakker ut zip ler

Hans-PePer Halvorsen, M.Sc.


Telemark University College
Faculty of Technology
Department of Electrical Engineering, Informa8on Technology and Cyberne8cs


E-mail: hans.p.halvorsen@hit.no
Blog: hPp://home.hit.no/~hansha/

58

Anda mungkin juga menyukai