Variables

advertisement
Welcome!
Workshop
6 of 7
Today's Topics

Review of Workshop 5
o
Graphs & Charts
o
Error Handling
o
Debugging
o
VI Server
o
Variables
o
Race Conditions
o
Semaphores
Review Question 1
True or False?
You must create a descriptive icon for a subVI and
wire the connector pane
Review Question 1
True or False?
You must create a descriptive icon for a subVI and
wire the connector pane
FALSE
Review Question 2
On a subVI, which terminal setting causes an
error if the terminal is not wired?
a) Required
b) Recommended
c) Optional
Review Question 2
On a subVI, which terminal setting causes an
error if the terminal is not wired?
a) Required
b) Recommended
c) Optional
Review Question 3
True or False?
Descriptions appear in the Context Help window.
Review Question 3
True or False?
Descriptions appear in the Context Help window.
TRUE
Review Question 4
Which of the following forms of documentation are available
on the block diagram?
a) Free labels
b) Labels
c) Captions
d) Tip strips
Review Question 4
Which of the following forms of documentation are available
on the block diagram?
a) Free labels
b) Labels
c) Captions
d) Tip strips
Review Question 5
Which of the following statements are true about high-level
File I/O Vis?
a) High-level File I/O functions are more efficient than lowlevel I/O
b) High-level File I/O functions perform the open/create,
read/write, and close within the VI
c) High-level File I/O functions are specific to one file type
Review Question 5
Which of the following statements are true about high-level
File I/O Vis?
a) High-level File I/O functions are more efficient than lowlevel I/O
b) High-level File I/O functions perform the
open/create, read/write, and close within the VI
c) High-level File I/O functions are specific to one file type
Review Question 1
You are logging data from multiple sensors over
many hours that you will process later using VIs
written by a colleague. Which file type would
best to use?
a) Text (ASCII)
b) TDMS
c) Binary
Review Question 1
You are logging data from multiple sensors over
many hours that you will process later using VIs
written by a colleague. Which file type would
best to use?
a) Text (ASCII)
b) TDMS
c) Binary
Review Question 2

Fill in the blanks for each block
______
______
a) Close
b) Open/Create/Replace
c) Check for Errors
d) Read or Write
_____
______
Review Question 2

Fill in the blanks for each block
Open/
Create/
Replace File
Read
and/or
Write to File
a) Close
b) Open/Create/Replace
c) Check for Errors
d) Read or Write
Close
File
Check for
Errors
Review Question 3
True or False?
All instances of a polymorphic VI must have the
same connector pane.
Review Question 3
True or False?
All instances of a polymorphic VI must have the
same connector pane.
TRUE
Waveform Graphs & Charts
• Special type of
numeric indicator
that displays one or
more plots of data,
typically acquired
at a constant rate
• Displays single or
multiple plots
Graphs vs. Charts
• Chart:
• Remembers history – new point added to end of plot
• Good for inside a loop
• Graph:
• New plot of all new data
• Good for outside the loop
• Use the Context Help window
to determine how to wire multiplot data to Waveform Graphs
and XY Graphs
Graphs & Charts
Chart Update Modes
• Right-click the chart and select Advanced»Update
Mode from the shortcut menu
• Strip chart is the default update mode
• Scope chart and Sweep chart modes display plots
significantly faster than the strip chart mode
Demonstration:
Comparing Graphs and Charts
Error Handling
 Anticipation, detection, and resolution of
warnings and errors
Essential for:
• Debugging - helps to catch programming errors
• Stress testing - helps to test application boundary cases
• Robustness - helps detect errors introduced by system
and environment differences
Error Handling: Types
Automatic: if error out is left unwired, LabVIEW
will handle it automatically (dialog box)
Manual: wiring the error out terminal means
manual error handling is occuring- you decide
what to do with the error.
Remember... without a mechanism to check for errors,
you know only that the VI does not work properly
Errors vs. Warnings
Error
Status = TRUE
Warning
Status = FALSE
Code is non-zero
Error Handling: Functions
Simple Error Handler VI
• creates a pop-up dialog box to notify user of error
that has occurred
• Configurable to handle warnings, too
• Avoid using in subVIs
Merge Error VI
• combines multiple error wires into a single error wire
• error received on the top most input will be passed
out, others will be ignored
Error Handling: Detection and Reporting
• Use error wires to propagate errors along and
handle appropriately
• Merge errors from different wire paths
Error Handling: With Loops
• Use shift-registers to detect errors and warnings in
all loop iterations
• Use error status as a condition to stop a loop
• While Loops
• Conditional For Loops
Handling Errors with SubVIs
• Use a Case structure to handle errors passed into the
subVI
30
Error Code Ranges
• Refer to the LabVIEW Help for complete list of error
code ranges:
• Fundamentals » Running and Debugging Vis » How-To »
Error Codes and Messages » Ranges of LabVIEW Error
Codes
• Codes are subdivided into ranges
according to product and VI
grouping
• Most codes are unique, but some
error codes are used by more
than one product or VI grouping
Debugging
Your VI isn’t broken, but you are getting
unexpected data or behavior…
Any unwired or hidden subVIs?
Incorrect default data being used?
Undefined data being passed?
Numeric representation correct?
Node execution order correct?
32
Debugging Tools
- use highlight execution
- use probes to see the current value in
the wire
- set breakpoints
to pause execution
at a specific point in the code
- single-step through the code
Debugging Tools: Highlight Execution
• Use execution highlighting to watch the data flow
through the block diagram
• If the VI runs more slowly than expected, confirm that
you turned off execution highlighting in subVIs
34
Debugging Tools: Probes
• Use the Probe tool to
• observe intermediate data values
• check the error output of VIs and functions, especially
those performing I/O
• Values can be retained in the wires so that you can
probe wires for data after execution has finished
35
Debugging Tools: Breakpoints
• When you reach a breakpoint during execution, the
VI pauses and the Pause button appears red
• You can take the following actions at a breakpoint:
• Single-step through execution using the single-stepping
buttons
• Probe wires to check intermediate values
• Change values of front panel controls
• Click the Pause button to continue running to the next
breakpoint or until the VI finishes running
36
Debugging Tools: Single Stepping
• Single-step through the VI to view each action of
the VI on the block diagram
• Suspend the execution of a subVI to
• edit values of controls and indicators
• control the number of times it runs
• go back to the beginning of the execution of the subVI
• Open subVI and select Operate»Suspend When
Called from the shortcut menu
37
VI Server
LabVIEW organizes and operates on its parts (front panel, button,
arrays, etc.) within an architecture called VI Server, which is
object oriented.
Terminology:
A class defines what an object is able to do, what operations it
can perform (methods), and what properties it has.
An object is a member of a class.
Methods perform an operation on an object.
Properties are the attributes of an object.
VI Server: Organization
Generic
Object
GObject
Pets
Control
Control
Dogs
Labrador
Cats
Poodle
Boolean
Stop
Array
LED
Boolean
Property Nodes
• Access the properties of an object
• Enable you to modify the appearance of front panel
objects programmatically in response to certain inputs.
• For example,
If a user enters an invalid password, you might want a red LED
to start blinking
If a data point is above a certain value, you might want to show a
red trace instead of a green one
• Execute from top to bottom
By default, if an error occurs on 3rd of 5 terminals, last two do not
execute and error is output
40
Invoke Nodes
• Use the Invoke Node to perform actions, or methods, on
referenced items (VI, Control)
• Most methods have parameters
• Examples of VI Methods:
• Front Panel: Center
• Default Values: Reinitialize all to Default
• Print: VI to HTML
Method Name
Parameters
Demonstration:
Creating Property and Invoke Nodes
Implicitly and Explicitly Linking
Invoke/Property Nodes
Explicitly Linked
(requires reference)
Implicitly Linked
(color/ label for data type/label)
Property
Node
Invoke
Node
43
Why Implicitly Link?
Implicitly Linked Property Node
Explicitly Linked Property Node
Main VI
VI
Main VI
VI
SubVI
SubVIs, References and Invoke/Property Nodes
Edit»
Create
SubVI
1. Select the portion of
the block diagram that
will be in the subVI.
SubVI Front Panel
SubVI Block Diagram
2. Select Edit»Create
SubVI; LabVIEW
automatically creates
the control references
needed for the subVI.
45
Demonstration:
Creating References for Property and
Invoke Nodes; Selecting a Class
Variables
• Cannot pass data between parallel loops with a wire
• Variables allow you to circumvent normal dataflow by
passing data from one place to another without
connecting the two places with a wire
47
Variables
Variables - Block diagram elements that allow you to access
or store data in another location
Types:
• Local - Stores data in front panel controls and indicators
• Global - Stores data in special repositories that can be
accessed from multiple VIs
• Functional Global - Stores data in While Loop shift
registers
• Shared - Transfers data between various distributed targets
connected together over a network
48
Variables: within a single VI
Use local variables to pass data within a single VI
NOTE: Boolean controls with associated local variables must use switch
mechanical action; boolean latch action is incompatible with local variables
49
Variables: between multiple VIs
Use a global variable to share data among VIs on the
same computer, especially if you do not use a project
file
Use a single process shared variable if you may need
to share the variable information among VIs on
multiple computers in the future
50
Single Process Shared Variables
Shared Variables: creating and configuring
• You must have a
project open to create
a shared variable
• Variable configuration
data is stored in
Project Libraries
• LabVIEW will
automatically create a
library if the variable
was not created from
an existing library
Demonstration:
Creating Variables
Using Variables: tips
• Use wires if possible
• Variables go against
LabVIEW’s data flow
paradigm, so only use when
necessary
• Variables create opportunity
for race conditions
(discussed later)
Which block diagram is easier
to determine function? This
one or….
54
Using Variables: tips
…this one?
55
Using Variables: Initialization
• Verify that variables
contain known data
values before the VI runs
• If you do not initialize
the variable before the
VI reads it for the
first time, it contains the
default value of the
associated front
panel object
56
Functional Global Variables
Special subVI with purpose of storing data by using uninitialized shift
register
General form includes
- an uninitialized shift register
- a single iteration For or While Loop
uninitialized shift
register
True constant permits while loop to iterate only on
57
Functional Global Variables
• A functional global variable usually has an action
input parameter that specifies which task the VI
performs
• The VI uses an uninitialized shift register in a
While Loop to hold the result of the operation
58
Functional Global Variables: Timing
Very useful for performing customized elapsed
time measurements
59
Variables: Recap
Variable Type
Scope
Notes
Local variable
A single VI
• Tied to a front panel control/indicator
Global variable
Multiple VIs on • Tied to a special global VI that has a front
same
panel but no block diagram
computer
Functional
global
Multiple VIs on • Implemented using a While Loop with an
same
uninitialized shift register to store global
computer
data
Single-process
shared variable
Multiple VIs on • Implemented using a project library in a
same
project
computer
• Can easily convert into a networkpublished shared variable
Networkpublished
shared variable
Across an
Ethernet
network
• Implemented using a project library in a
project
• Often used to communicate with RealTime targets
Race Condition
A situation where the timing of events or the scheduling of
tasks may unintentionally affect an output or data value
•
Common problem for programs that execute multiple tasks in parallel and
share data between the tasks
•
Race conditions are very difficult to identify and debug
•
Often, code with a race condition can return the same result thousands of
times in testing, but still be capable of returning a different result
•
Avoid race conditions by:
• Controlling shared resources
• Properly sequencing instructions
• Identifying and protecting critical sections within your code
• Reducing use of variables
61
Race Conditions: Shared Resources
62
Race Conditions: Sequencing
What is the final value?
Four possible outcomes:
• Value = (Value * 5) +2
• Value = (Value + 2) * 5
• Value = Value * 5
• Value = Value +2
63
Race Condition: Critical Code
• A critical section of code is code that may behave
inconsistently if some shared resource is altered
while it is running
• If one loop interrupts another loop while it is
executing critical code, then a race condition can
occur
• Eliminate race conditions by identifying and
protecting critical code with:
Functional Global Variables
Semaphores
64
Race Condition: Critical Code
Protecting critical code with Functional Global
Inside the Functional Global
65
Semaphores
Semaphores behave like a
talking stick …
only the one with the
semaphore/stick can
talk/operator
66
Homework
LAST Homework Assignment
Turn in ALL HOMEWORK soon!!
No Class Next Week!
Wednesday ,March 21 Toomey 199, prepare for a
quiz
67
CLAD
Review: Wednesday, April 4 at 4:00
Register
STUDY
• Go through the study guides
• Old Exams
• Webcasts
68
Download
Study collections