h6p://home.hit.no/~hansha/?
page=labview
Simula'on
in
LabVIEW
Hans-Pe6er
Halvorsen,
M.Sc.
LabVIEW
Installa'on
Download
the
soIware
here:
Note!
You
get
the
Serial
Number
from
your
Teacher,
but
the
soIware
can
be
used
for
30
days
before
you
need
to
enter
a
valid
Serial
Number.
h6p://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
SoIware
can
be
downloaded
here:
www.ni.com/download
LabVIEW
Training
Here
you
will
nd
lots
of
Videos,
Tutorials
and
Exercises
LabVIEW
Training
for
Students
(Na'onal
Instruments):
h6p://ni.com/students/learnlabview
LabVIEW
Course:
h6p://home.hit.no/~hansha/?training=labview
Learning
by
Doing!
It
is
recommended
that
you
watch
some
of
the
videos
before
you
read
furter
Contents
Introduc'on
to
LabVIEW
Installa'on
Block
Diagram
Simula8on
based
on
dieren8al
Equa8ons
Simula8on
Loop
PID
Control
with
built-in
PID
blocks/func'ons
Crea'ng
and
using
Simula8on
Subsystems
Simula'ons
using
a
While
Loop
with
Subsystems
inside
Discrete
Simula'on
Formula
Node
MathScript
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
installa'on
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
crea'ng
Control
and
Simula'on
applica'ons
with
LabVIEW,
here
you
will
nd
PID
controllers,
etc.
The
module
is
available
as
a
pale6e
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
pale6e
on
your
block
diagram.
h6p://www.ni.com/pdf/manuals/376039a.pdf
Control
and
Simula'on
in
LabVIEW
Control
Design
&
Simula'on
Pale6e
in
LabVIEW
PID
Pale6e
in
LabVIEW
Control
Design
Pale6e
in
LabVIEW
Simula'on
Loop
Simula'on
Pale6e
in
LabVIEW
Students:
Check
that
you
have
all
these
pale6es.
Open
the
dierent
subpale6es,
etc.
LabVIEW
Control
and
Simula'on
Example
Example
of
Simula'on
(and
control)
of
a
dynamic
system
based
on
the
blocks
in
the
Simula'on
Pale6e
We
are
going
to
learn
to
create
such
a
system
(and
more)!
Dynamic
Systems
Hans-Pe6er
Halvorsen,
M.Sc.
These
are
small-scale
processes
we
have
at
HiT
Dynamic
Systems
Examples
Water
Tank:
Mathema'cal
Models
(dieren'al
equa'ons):
Alt
1
(Integrator):
Alt
2
(Time
constant/1.order):
h
Level
in
the
tank
Air
Heater:
T
Temperature
in
the
tube
Alt
3
(Nonlinear):
These
are
small-scale
processes
we
have
at
HiT
Dynamic
Systems
Examples
2
Tank:
Mathema'cal
Models
(dieren'al
equa'ons):
4
Tank:
Dynamic
Systems
Dynamic
system
represented
as
a
dieren'al
equa'on
Note
We
can
easily
create
a
block
diagram
from
the
dieren'al
equa'on(s)
Dynamic
system
represented
as
a
block
diagram
Integrator
symbol
When
we
have
the
block
diagram
for
the
system,
we
can
easily
implment
it
in
LabVIEW
Block
Diagram
Examples
Example
1:
Example
2:
Students:
Find
the
block
diagrams
for
the
dieren'al
equa'ons
above
(pen
&
paper)
Block
Diagrams
-
Solu'ons
Example
1:
Example
2:
or:
Block
Diagram
Examples
Mass-Spring-Damper
Example:
Higher
order
dieren'al
equa'ons
Students:
Find
the
block
diagram
for
the
dieren'al
equa'on
above
(pen
&
paper)
Block
Diagram
-
Solu'ons
Mass-Spring-Damper
Example:
Higher
order
dieren'al
equa'ons
Simula'on
Tools
MATLAB
Text-based
Programming
Tool
www.mathworks.com
Simulink
Blockdiagram-based
Simula'on,
Integrated
with
MATLAB
LabVIEW
MathScript
Uses
MATLAB
syntax,
Integrated
with
LabVIEW
Modelica
h6ps://www.modelica.org
HYSYS
h6p://www.aspentech.com/products/aspen-hysys.aspx
...
Simula'on
in
LabVIEW
Hans-Pe6er
Halvorsen,
M.Sc.
Simula'on
in
LabVIEW
Simula8on
PaleLe
in
LabVIEW
Simula8on
Loop:
Similar
to
a
While
Loop,
but
cusomized
for
used
together
with
the
Simula'on
Blocks
available
in
LabVIEW
Dierent
Simula'on
Blocks
by
Category
Con'nuous
Systems
Discrete
Systems
Nonlinar
Systems
etc.
Simula8on
PaleLe
Overview
Video
(15
minutes)
h6p://home.hit.no/%7Ennh/videos/sim_module/
sim_pale6e_overview/sim_pale6e_overview.html
Students:
You
should
watch
this
Video
before
you
go
further!
Simula'on
Example
Dynamic
system
represented
as
a
dieren'al
equa'on
Students:
Implement
and
Simulate
this
system
in
LabVIEW
Integrator
symbol
Simula'on
Example
-
Congura'on
In
the
example
the
following
simula'on
parameters
could
be
used
(right-click
on
the
Simula'on
Loop
border
and
select
Congure
Simula'on
parameters):
Simula'on
Example
-
Solu'ons
Try
with
dierent
values
for
u
Step
Response
Correct
results?
Check
sta'c
response:
We
see
the
Step
Response
is
as
expected!
PID
Control
in
LabVIEW
Hans-Pe6er
Halvorsen,
M.Sc.
Example
of
Industrial
Controllers
PID
Controller
Control
System
DeltaV
Typical
Block
Diagram:
v
e
r
-
Kp Ti Td
PID
Process
Sensor
Students:
What
is
r,
e,
u,
v,
y,
Kp,
Ti,
Td?
Control
System
e
r
-
Kp Ti Td
PID
Process
Sensor
r
Reference
Value,
SP
(Setpoint),
SV
(Set
Value)
y
Measurement
Value
(MV),
Process
Value
(PV)
e
Error
between
the
reference
value
and
the
measurement
value
(e
=
r
y)
v
Disturbance,
makes
it
more
complicated
to
control
the
process
Kp,
Ti,
Td
PID
parameters
Control
System
implementa'on
with
Pen&Paper
r
-
Kp Ti Td
PID
Sensor
Process
The
transi'on
from
paper
to
LabVIEW
is
easy,
because
the
implementa'on
is
very
similar
to
the
paper
version
Control
System
implementa'on
in
LabVIEW
We
will
learn
to
create
this:
Control
System
implementa'on
with
Pen&Paper
Controller
r
-
Kp Ti Td
PID
All
this
is
normally
included
in
the
controller
Sensor
Process
The
transi'on
from
paper
to
LabVIEW
is
easy,
because
the
implementa'on
is
very
similar
to
the
paper
version
Control
System
implementa'on
in
LabVIEW
We
will
learn
to
create
this:
PID
Control
in
LabVIEW
Alterna8ve
1:
PID
Pale6e
in
LabVIEW
(PID
Toolkit)
Alterna8ve
2:
Note!
The
func'ons
PID.vi
and
PID
Advanced.vi
requires
that
Ti
and
Td
is
in
minutes,
while
its
normal
to
use
seconds
as
the
unit
for
these
parameters.
You
can
use
the
following
piece
of
code
in
order
to
transform
them:
This
means
we
enter
values
for
Ti
and
Td
in
secons
on
the
Front
Panel
and
the
values
are
converted
to
minutes
in
the
code.
This
alterna've
uses
seconds!
LabVIEW
PID
Example
Students:
Replace
u
in
the
previous
example
with
the
built-in
PID
Controller
(use
Alterna've
2)
PID
Example
-
Solu'ons
Front
Panel:
Students:
Find
proper
Kp,
Ti,
Td
Parameters
for
this
system.
Use
Trial
and
Error
or
one
of
the
methods
explained
in
the
Text
Book
PID
Example
-
Solu'ons
Block
Diagram:
Right-click
(Congura'on)
on
the
PID
block
to
select
Kp,
Ti,
Td
as
Inputs
(->Terminals)
Next
Step:
Con'nuous
Simula'on
Inf
=
Innite
Simula'on
in
Real
Time
Right-click
on
the
Simula'on
Loop
border
and
select
Congure
Simula'on
Parameters
Add
a
Stop
Bu6on
and
a
Halt
Simula'on
block
Students:
Change
your
Simula'on
Seeungs
and
Run
your
Simula'on
with
these
changes
PID
Example
Con'nuous
Simula'on
-
Solu'on
Students:
Extend
the
example
with
a
new
Chart
of
the
Control
Signal
(u)
and
include
the
reference
signal
(r)
in
the
exis'ng
Chart,
so
you
can
easily
see
the
changes
in
r
and
how
the
system
handle
it.
The
Simula'on
runs
un'l
you
press
the
stop
bu6on
Simula'on
Subsystem
A
Way
to
structure
your
code,
similar
to
SubVIs
File
->
New
This
is
the
recommended
way
to
do
it!
You
can
easly
reuse
your
Subsystems
in
dierent
VIs
and
your
code
becomes
more
structured!
Select
File
->
New
...,
Then
choose
Simula'on
Subsystem.
Copy
(or
move)
the
blocks
containing
your
process
into
the
New
VI
(Simula'on
Subsystem)
Create
a
Subsystem
of
this
part
(your
process
model)
Students:
Change
your
code
above
where
you
create
a
Simula'on
Sub
System
for
your
Process
Simula'on
Subsystem
-
Solu'ons
Simula'on
Subsystem
2
(PID
Controller)
Create
a
Subsystem
of
this
part
(the
PID
controller)
Students:
Change
your
code
above
where
you
create
a
Simula'on
Sub
System
for
the
PID
Controller
as
well.
Simula'on
Subsystem
Solu'ons2
Simula'on
Sub
Systems
Now
your
code
has
become
really
simple
to
understand!
Note!
You
may
select
dierent
icon
style
Simula'ons
using
a
While
Loop
The
Simula'on
Loop
has
some
drawbacks/is
more
complicated
to
use
than
an
ordinary
While
Loop.
If
we
use
simulata'on
Subsystems,
we
can
use
them
inside
a
While
Loop
instead!
-
which
becomes
very
handy!
Simula'on
Subsystems
So
for
real
applica'ons
that
involves
more
than
just
simula'ons
(such
as
DAQ,
File
Logging,
PID
control
of
the
real
process,
etc.),
I
recommend
to
use
a
While
Loop
instead
of
a
Simula'on
Loop.
Students:
Add
your
Controller
and
Process
Subsystems
inside
a
While
loop
as
shown
above.
Simulate
the
system.
Do
you
get
the
same
results?
Datalogging
with
LabVIEW
Hans-Pe6er
Halvorsen,
M.Sc.
Save
Data
to
File
(Datalogging)
Right-click-Proper'es
Recommended
Seungs
Datalogging
Example
Datalogging
Example
Block
Diagram
Students:
(1)
Log
Data
from
your
system,
i.e.,
the
Control
Signal
(u)
and
the
output
of
the
process
(y)
to
a
Measurement
File.
(2)
Then
Plot
the
Data
from
the
File
in
Excel.
Discrete
Systems
Hans-Pe6er
Halvorsen,
M.Sc.
Con8nuous
Signal
Discret
Signal
Note!
Dierent
books
use
dierent
nota'ons
A
computer
can
only
deal
with
discrete
signals
Ts
=
Sampling
Time
When
Ts
->
0,
we
have
a
con'nuous
signal,
but
in
a
computer
that
is
not
possible.
k
=
0,
1,
2,
3,
4,
....
Example
Discre'za'on
Given
the
following
dieren'al
equa'on:
In
order
to
simulate
this
system
in
LabVIEW
using
the
Formula
Node
we
need
to
nd
the
discrete
dieren'al
equa'on.
We
can
use
e.g.,
the
Euler
Approxima8on:
Ts
Sampling
Time
Then
we
get:
This
gives
the
following
discrete
dieren'al
equa'on:
Students:
Simulate
and
Plot
the
discrete
system
above
using
a
Formula
Node
and
a
For
Loop
in
LabVIEW
Ts
=
0.1
s
Formula
Node/MathScript
Hans-Pe6er
Halvorsen,
M.Sc.
Formula
Node
&
MathScript
Node
Formula
Node:
Create
and
use
C
code
within
LabVIEW
Example:
Very
useful
for
mathema'cal
expressions
and
simula'ons!
Right-click
on
the
border
to
Add
Input
or
Add
Output
MathScript
Node:
Create
and
use
MathScript/MATLAB
code
within
LabVIEW
Formula
Node/MathScript
Node
Students:
Create
this
Example
both
in
Formula
Node
and
in
MathScript
Node
Students:
Try
this
Formula
as
well
(both
in
Formula
Node
and
in
MathScript
Node):
Example
Simulate
Discrete
Systems
using
the
Formula
Node
in
LabVIEW
Given
the
following
dieren'al
equa'on:
In
order
to
simulate
this
system
in
LabVIEW
using
the
Formula
Node
we
need
to
nd
the
discrete
dieren'al
equa'on.
We
can
use
the
Euler
Approxima8on:
Ts
Sampling
Time
Then
we
get:
This
gives
the
following
discrete
dieren'al
equa'on:
Students:
Simulate
and
Plot
the
discrete
system
above
using
a
Formula
Node
and
a
For
Loop
in
LabVIEW
Ts
=
0.1
s
Formula
Node
in
LabVIEW
-
Solu'ons
Discrete
model:
xk1=(1-a*Ts)*xk + Ts*b*uk;
As
expected,
we
get
the
same
results
as
in
the
previous
example
using
a
block
diagram
simula'on
Students:
Improve
your
solu'on
by
crea'ng
a
SubVI
of
your
model
Formula
Node
in
LabVIEW
Solu'ons
2
SubVI
Main
VI
Advantages
using
SubVI:
Our
Main
program
has
become
simpler
and
easier
to
understand
and
maintain
We
can
reuse
our
model
in
other
applica'ons
If
we
need
to
do
changes
in
the
model,
we
only
do
it
once
and
in
one
place!
MathScript
LabVIEW
MathScript
RT
Module
Add-on
Module
for
LabVIEW
where
we
can
do
text-based
programming
and
simula'ons
very
powerful!
MathScript
Simula'on
Example
% Simulation of discrete model
clear, clc
Create
the
following
code
in
% Model Parameters
the
MathScript
a = 0.25;b = 2;
Script
Editor
% Simulation Parameters
Ts = 0.1; %s
Tstop = 20; %s
uk = 1; % Step Response
x(1) = 0;
% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;
end
Students:
Create
and
test
the
MathScript
code.
You
should
get
the
same
results
as
in
the
LabVIEW
Examples
% Plot the Simulation Results
k=0:Ts:Tstop;
plot(k,x)
grid on
MathScript
Node
Students:
Try
the
same
example
inside
LabVIEW
using
the
MathScript
Node
Just
copy
and
paste
the
code
from
the
previous
example
LabVIEW
Course
Simula'on
in
LabVIEW
PDF
File
available
for
download
h6p://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!
Solu'ons
with
cource
code
for
all
the
exercises
are
located
on
the
web
page
Course
Manual
with
Exercises
Available
as
a
PDF
le
from:
h6p://home.hit.no/~hansha/documents/labview/course/simula'on.htm
Select
one
of
the
following
Challenges
Hans-Pe6er
Halvorsen,
M.Sc.
Mass-Spring-Damper
System
h6p://www.techteach.no/simview/mass_spring_damper/index.php
Students:
Simulate
this
system
using
LabVIEW.
Plot
the
posi'on,
speed
and
the
accellera'on.
Test
with
dierent
values
on
m,
k
and
d.
Air
Heater
Overview
A
sketch
of
the
Air
Heater
System:
Air
ow
Control
Signal
Air
tube
Temperature
Measurement
Heater
TC
TT
Air
In
Air
Out
Real
Air
Heater
Air
Heater
Simula'on
In
this
assignment
we
are
going
to
simulate
and
control
a
mathema'cal
model
of
an
Air
Heater
system.
A
mathema'cal
model
of
the
system
could
be:
Where:
Use
the
following
values
in
the
simula'ons:
Note!
You
nd
an
example
of
how
to
do
it
in
LabVIEW
on
the
next
slides
but
try
to
solve
it
by
yourself
rst!
Air
Heater
Simula'on
Example
Implement
the
Heater
model
in
a
Simula'on
Sub
System,
as
shown
below:
See
next
slide
for
Front
Panel
example
Air
Heater
Simula'on
Front
Panel
Example:
Find
Proper
PI(D)
Parameters
Air
Heater
Simula'on
with
MathScript
MathScript
Simula'on
Example
% Simulation of discrete model
clear, clc
Are
you
able
to
simulate
the
Air
Heater
model
using
MathScript
or
MathScript
Node?
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.1; %s
Tstop = 20; %s
uk = 1;
x(1) = 0;
% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;
end
% Plot the Simulation Results
k=0:Ts:Tstop;
plot (k, x)
grid on
Hans-PeLer
Halvorsen,
M.Sc.
Telemark
University
College
Faculty
of
Technology
Department
of
Electrical
Engineering,
Informa8on
Technology
and
Cyberne8cs
E-mail:
[email protected]
Blog:
hLp://home.hit.no/~hansha/
65