Programming Designs

background image

TOPICS

ni.com/training

0

background image

TOPICS

ni.com/training

Programming Designs

A. Event-Driven Programming

B. Sequential Programming

C. State Machine Design Pattern

D. Multiple Loop Design Patterns

1

background image

ni.com/training

A. Event-Driven Programming

Events and Event Structure

Parts of an Event Structure

Configuring the Event Structure

Recommendations

background image

ni.com/training

Events

Events originate from the user interface, external

I/O, or other parts of the program.

Examples of events:

changing value of a control, pressing a key, clicking the
mouse, resizing the window, closing the window.

Event

An asynchronous notification that

something has occurred

background image

ni.com/training

Using Event Structures for Event-Driven
Programming

Event Structure

— LabVIEW’s programmatic tool for

handling events.
By using it, execution of code can be made dependent
on whether or not an event has occurred.
Use an Event structure
to handle user-interface
events such as:

• Pressing a button

on the mouse.

• Pressing a key on

the keyboard.

• Changing the value

of a numeric control.

background image

ni.com/training

Parts of an Event Structure

Event Selector Label

—Identifies the event case viewed.

Timeout

—Specifies time in ms to wait for events.

Default value is

1 (indefinite).

Timeou
t

Event Selector Label

background image

ni.com/training

Parts of an Event Structure
(continued)

• Event Data Node—Identifies the data LabVIEW provides

when the event occurs; similar to Unbundle By Name
function.

• Event Filter Node—Identifies the subset of data available in

the Event Data node that the event case can modify.

Event

Data

Node

Event
Filter
Node

background image

ni.com/training

Using an Event Structure

In general, place Event structures inside While
Loops.

Event structures
handle exactly
one event per

iteration of

the While Loop.

Event structures
sleep when no
events occur.

background image

ni.com/training

Configuring the Event Structure

Use a dialog box to configure
each event by right-clicking the
Event structure border and
selecting Edit Events
Handled by This Case
from
the shortcut menu.

background image

ni.com/training

Edit Events Dialog Box

Events

Event
Sources

Configured
Events

background image

ni.com/training

Notify and Filter Events

Notify Events (

green

arrow)

• User action has already occurred

and LabVIEW has processed the
event.

Filter Events (

red

arrow)

• User action has already occurred

and LabVIEW has NOT
processed the event.

• Filter events allow you to override

default behavior for event.

background image

DEMONSTRATION

Configure and Use Events

Demonstrate configuring and using an Event
structure.

background image

ni.com/training

Recommendations

•If no timeout is configured and an event never
occurs

– event loop is stuck waiting indefinitely.

Therefore, wire the timeout terminal with a value
other than -1 (at least while developing).

•“Heavy” code within the Event Structure causes
the front panel to lock up, often confusing the user
– try to keep code in an event case to a minimum.

background image

ni.com/training

Recommendations (Continued)

•If your event-driven application has a STOP
button, add a Value change event for the button

otherwise stopping the VI might be delayed.

•Event structure may be used in a state machine
design pattern. One state is waiting for events and
depending on which of them occurs, the next state
to execute is chosen.

background image

ni.com/training

B. Sequential Programming

Using Sequential Programming

Flow-Through Parameters

Sequence Structures

Flat Sequence Structure

Stacked Sequence Structure

Error Case Structures

background image

ni.com/training

Using Sequential Programming

Many of the VIs you write accomplish sequential tasks.

By default, LabVIEW does not force sequential
programming.

Example: Nothing forces the execution order of these tasks.
Any one of these tasks could happen first

.

background image

ni.com/training

Flow-Through Parameters Force
Execution Order

Use error clusters and refnums to force order of
execution.

background image

ni.com/training

Sequence Structures Force
Execution Order

Sequence structures are a structure with frames,
where each frame executes in order.

The second frame cannot begin execution until
everything in the first frame completes execution.

background image

ni.com/training

Flat Sequence Structure

Executes each frame beginning with the left-most
frame and ending with the right-most frame.

The previous frame must complete before the next
frame executes.

Data can be passed out of or between frames using
tunnels.

Once the sequence
begins, it cannot be
stopped.

1st

2nd

3rd

background image

ni.com/training

Stacked Sequence Structure

Stacks each frame so you see only one frame at a time.

Executes frame 0, then frame 1, etc. until the last frame
executes.

Returns data only after the last frame executes.

To transfer data from frame to frame, a Sequence Local

must be created (right-

click » Add

Sequence Local).

Once the sequence begins,
it cannot be stopped.

Sequence Local

background image

DEMONSTRATION

Flat Sequence and Stacked
Sequence Structures

• passing data between frames
• tunnel outputs
• shifting between Flat and Stacked Sequence

background image

ni.com/training

Avoid Overuse of Sequence
Structures

… because you cannot stop the execution in the
middle of a sequence.

Single frame sequence structures are better than
multi-sequence frames.

background image

ni.com/training

Better to Use Error Case Structures

Instead, write this VI to enclose the dialog boxes in
Case structures, wiring the error cluster to the case
selectors.

background image

ni.com/training

C. State Machine Design
Pattern

State Programming

State Transition Diagrams

State Machines

Infrastructure

Transitions

Event-Based State Machine

background image

ni.com/training

State Programming

State programming helps you solve the following
issues that sequential programming or flow-
through parameters do not:

What if you need to change the order of the sequence?

What if you need to repeat one item in the sequence more
often than other items?

What if some items in the sequence execute only when
certain conditions are met?

What if you need to stop the program immediately, rather
than waiting until the end of the sequence?

background image

ni.com/training

State Transition Diagram

A state transition diagram is a type of flowchart
that indicates the states of a program and
transitions between states.

25

Transition

— Condition, action, or event that

causes the program to move to the next state

State

— Part of a program that satisfies

a condition, performs an action or waits for
an event

background image

ni.com/training

State Transition Diagram Example:
Coffee Vending Machine

initialize:

money=0

wait for

event

money=money+coin

dispense money;

money=0

dispense coffee;

money=money

–0.50€

coin

inserted

money

≥0.50€

money<0.50

money>0

money=0

cancel button

pressed

background image

ni.com/training

State Machines

The state machine design pattern implements
a state diagram or flow chart.

It can programmatically determine which state to
execute next.

background image

ni.com/training

State Machines

– Infrastructure

State machine is a set of states and transitions between them.

Each state can lead to one or multiple states or end the process
flow.

State Functionality Code = which action would you like to
perform in this state?

Transition Code = which state is next?

While Loop

Case Structure

Shift Register

Type-Defined

Enum

background image

ni.com/training

State Machines

– Transition Code

background image

ni.com/training

Event-Based State Machine

Combines event programming with a State
Machine design.

Includes a “Wait on Event” case to processes user-
interface events.

background image

ni.com/training

D. Multiple Loop Design
Patterns

Parallelism

Master/Slave Design Pattern

Notifiers

Producer/Consumer Design Pattern

Queues

Notifiers vs. Queues Summary

background image

ni.com/training

Parallelism

• Some tasks require multiple actions to be run in

parallel on several processor cores.

• LabVIEW has built-in methods for communicating

between parallel loops that execute these actions.

32

Parallelism

executing multiple

tasks at the same time

background image

ni.com/training

Multiple Loop Design Pattern

Parallel Loops

33

background image

ni.com/training

Multiple Loop Design Pattern

Producer/Consumer

34

background image

ni.com/training

Master/Slave Framework

Master/Slave: a type of Producer/Consumer

35

• Master loop
tells the slave
loop(s) when
to execute.

• Notifiers
allow loops to
run at different
rates.

background image

DEMONSTRATION

Notifiers in Action

Build a simple VI following the Master/Slave
design pattern.

background image

ni.com/training

Notifiers

Pros:

• Both loops are synchronized to the master loop—the slave loop

only executes when the master loop sends a notification.

• You can use notifiers to create globally available data, since it is

possible to send data with a notification.

• Using notifiers creates efficient code—there is no need to poll to

determine when data is available from the master loop.

Cons:

• Notifiers do not buffer data.
• If the master loop sends another piece of data before the first piece

of data has been read by the slave loops, that data is overwritten
and lost.

37

background image

ni.com/training

Queues

• Queues are similar to notifiers, except that a queue can

store multiple pieces of data.

• By default, queues work in a FIFO (first in, first out)

manner.

• Use a queue

when you want
to process all data
placed in the queue.

• Use a notifier if you only want to process current data.
• Use the Queue Operations functions to create a queue

for communicating data between sections of a block
diagram or between VIs.

38

background image

ni.com/training

Producer/Consumer Framework (Data)

Data type here
determines the
data type that
can be placed
in the queue

background image

DEMONSTRATION

Queues in Action

Build a simple VI following the Producer/Consumer
design pattern.

background image

ni.com/training

41

Producer/Consumer Framework
(Events)

• Efficiently responds
asynchronously to the
user interface

background image

ni.com/training

Notifiers vs. Queues

– Summary

Tool

Function

Use when

Notifier

sends alert to helps
control timing of parallel
loops

can transfer data
between loops

data NOT buffered
(lossy)

you have parallel loops that
are running at different
rates and 1+ loop is
dependent on another

you want to transfer data
from one loop to another

Queue

helps control timing of
parallel loops

transfers data between
loops

buffers data to be
transferred (FIFO)

you have parallel loops that
are running at different
rates and 1+ loop is
dependent on another

you want to transfer ALL
data from one loop to
another

background image

ni.com/trainin

g

Create a VI using events. Its front panel should
contain:

• Numeric

Control (I32)

• Numeric

Indicator (I32)

• 2 x String

Indicator

• Stop Button

Homework: Events

background image

ni.com/trainin

g

One string indicator should display the coordinates
of each mouse click.

You could use two numeric indicators as well,
but I recommend playing with string functions (Concatenate,
Number to String) for practice.

Homework: Events

background image

ni.com/trainin

g

When you enter a value in the numeric control, the
remaining indicators should display the difference between
old and new value (positive, negative or 0) and tell if the
number has grown, decreased or remained unchanged.

Homework: Events

background image

ni.com/trainin

g

If nothing happens for 5 seconds, display
a message to the user.

Hint: Functions

» Programming » Dialog & User Interface

Homework: Events

background image

ni.com/trainin

g

Ask for confirmation when the user attempts to
close the window.

Homework: Events

background image

ni.com/trainin

g

Pressing the STOP button ends the program.

Try executing the VI without an event for STOP button
configured. Then add the Value Change event for the button and
observe the difference.

Homework: Events

background image

ni.com/trainin

g

• Write a program using a simple state machine

to familiarize yourself with this design pattern.

• You may use your own ideas or follow the

instructions on the website and create a simple
traffic light.

Homework: Simple State Machine

background image

ni.com/trainin

g

• Create an event-driven state machine.
• The instructions for writing an alarm clock

application are provided on the website. You
might also implement the coffee vending
machine from slide 26 or use your own ideas.

Homework:
Event-Based State Machine

background image

ni.com/trainin

g

Create a VI using the Producer/Consumer
Template.

Homework: Producer/Consumer


Wyszukiwarka

Podobne podstrony:
Uruchom program Alligator Flash Designer
How to Design Programs An Introduction to Computing and Programming Matthias Felleisen
Polski opis programu BUSINESS CARD DESIGNER PRO
Nowy Prezentacja programu Microsoft PowerPoint 5
Charakterystyka programu
1 treści programoweid 8801 ppt
Programowanie rehabilitacji 2
Rola rynku i instytucji finansowych INowy Prezentacja programu Microsoft PowerPoint
Nowy Prezentacja programu Microsoft PowerPoint ppt
Szkoła i jej program
wykluczenie społ program przeciwdział
ProgrammingJavaLecture9
Nowa podstawa programowa WF (1)

więcej podobnych podstron