Introduction to Programming Concepts Using National Instruments Lab
Introduction to Programming Concepts Using National Instruments Lab. VIEW, part II Greg Bucks EPICS Skill Session 10 -March-2010 1
Acknowledgements o Portions of this presentation were adapted from the NI Lab. VIEW 3 hour introductory course n Available at http: //www. ni. com/academic 2
Goals o o o Introduce you to the main concepts you will use when programming Introduce you to the Lab. VIEW programming environment Equip you with basic skills necessary for programming in Lab. VIEW 3
Outline o o o Review of last week Arrays Looping Structures User defined functions Conclusion and Questions Final assessment 4
Overview of Use o Lab. VIEW environment consists of two items: n n Front Panel Block Diagram 5
Overview of Use o All programming instructions and user interface objects are accessible through the “Functions Palette” and “Controls Palette” 6
Flowcharts o o Graphical representation of the flow of information or control Usually the first step in creating a program n n o Good way to organize and brainstorm about program Used as reference/documentation for program Used for more then programming 7
Flowcharts o Symbols: n n n Start/Stop: Process instruction: Conditional test (decision or loop): Input/Output: Connector (to another place): 8
Dataflow Programming o Block diagram execution: n n o o Dependent on the flow of data Block diagram does not execute left to right Node executes when data available for all inputs Nodes supply data to all output terminals when done 9
Flowchart vs. Lab. VIEW: Structure Start Converter Get Temperature Value Convert Temp from C to F or F to C Convert Temp from F to C Output Converted Temperature Value End Converter 10
Mathematical Operations o o Most basic and fundamental programming structure Flowchart: process statements Commonly overlooked All mathematical functions accessed through functions palette 11
Mathematical Operations o For basic math functions, top input always first: n o 10 – 5 10 + 5 Can use Formula Express VI to write more complicated formulas 12
Math. Script Node o o Implement equations and algorithms textually Input and Output variables created at the border Generally compatible with popular m-file script language Terminate statements with a semicolon to disable immediate output 13
Logic o Allows you to create programs that do more than crunch numbers n o You can now make decisions Two types n n Boolean Logic (AND, OR, XOR, NOT) Conditional Logic (>, <, =) 14
Boolean Logic in Lab. VIEW o Lab. VIEW allows you to do all of the basic boolean operations: n Express/Arithmetic and Comparison/Express Boolean 15
Conditional Logic o Lab. VIEW allows you to do all conditional logic operations: n Express/Arithmetic and Comparison/Express Comparison 16
If-Then-Else/Case Structures o If-Then-Else/Case Structures are all handled using the same VI in Lab. VIEW: n Express/Execution Control/ Case Structure 17
Outline o o o Review of last week Arrays Looping Structures User defined functions Conclusion and Questions Final assessment 18
Arrays are 1 or more dimensional tables of data Access information stored in an array using indexes o o 0 0 1 2 3 4 0 A B C D E 0 1 2 3 4 1 F G H I J A B C D E 2 K L M N O 3 P Q R S T 4 U V W X Y 19
Arrays o o Array functions are in ProgrammingArray area of Functions Palette Can do all normal array manipulations 20
Creating Arrays Block Diagram: Right click on array: add dimension Front Panel: Right click on array: add dimension
Arrays: Important Functions Lab. VIEW Function Name Function Description Array Constant Creates an array constant based on the type of constant placed inside (i. e. numeric, boolean, etc) Array Size Returns the size of the array Index Array Pulls out the value specified by index input Array Subset Pulls out a subset of a given array, based on the index and length inputs Insert Array Inserts a value or array into an existing array. Initialize Array Creates an array of size indicated by dimension size with all values equal to element Build Array 22 Builds an array with values equal to inputs
Mathematical Operations o Mathematical operations performed using arrays proceed in element by element fashion o Linear algebra operations can be performed using linear algebra section in mathematics area of functions palette 23
Activity The purpose of this activity is to get you familiar with the Lab. VIEW programming environment and create a simple programs utilizing some of the array functionality discussed. 24
Outline o o o Review of last week Arrays Looping Structures User defined functions Conclusion and Questions Final assessment 25
Loops o Loops allow for repetition within a program n n n Repeat operations Commonly used in majority of programs Can set up to run: o o For a predetermined number of times Until some condition is met (using logic) 26
Shift Register o o Allows values to be passed from one iteration of a loop to the next To place: right click on loop and select “Add shift register” 27
Shift Register X 3610 34210 Y Iteration X Y S 0 0 1 0 1 1 2 3 3 6 4 10 S 610310 10 Result = 10 28
For Loops: Flowchart 29
For Loops: Lab. VIEW o For Loops: n n Run a predetermined number of times Commonly used when operating on arrays Set number of iterations Access loop iteration 30
Example - Flowchart 31
Example - Program Find array size and set up number of iterations Add to our previous value Output result Input Array Initialize sum to zero 32 Read in value from array Divide by the number of elements summed
While Loops: Flowchart 33
While Loops: Lab. VIEW o While Loops: n n Will continue to run until some condition is met Commonly used to determine program flow o Enclose entire program Exit condition gets connected here Right click Access current loop iteration 34
Example - Flowchart 35
Example - Program Initialize sum to zero Output result Input Array Read in value from array Divide by number of summed elements Stop Add to Continue Check for exit Looping previous values looping condition 36
Combination Loops: Lab. VIEW You can add a conditional terminal to a For loop to create a loop that works as both a For loop and a While loop o Set number of iterations Exit condition gets connected here Access current loop iteration 37 Right click
Activity The purpose of this activity is to get you familiar with the Lab. VIEW programming environment and create a simple programs utilizing some of the repetition functionality discussed. http: //web. ics. purdue. edu/~gbucks/EPICS. html 38
Outline o o o Review of last week Arrays Looping Structures User defined functions Conclusion and Questions Final assessment 39
Subroutines o o Method for easily reusing commonly used pieces of code Miniature programs that can be used inside other programs Help to make code easier to read and understand Called Sub. VI’s in Lab. VIEW 40
Sub. VI’s o Think of Sub. VI’s like a webpage: 41
Subroutines o Most of the functions you use in Matlab and Lab. VIEW are subroutines 42
Subroutines o Most of the functions you use in Matlab and Lab. VIEW are subroutines 43
Subroutines o o Any program you write in Lab. VIEW can be turned into a VI to be used in any other program Two ways to create your own VI: n Option 1: o o n Option 2: o o Define inputs and outputs on front panel Create icon to be used when imported Select code on block diagram and select “EditCreate Sub. VI” Insert into new programs using “Select a VI…” on functions palette 44
Option 1 45
Option 2 EditCreate Sub. VI 46
Activity The purpose of this activity is to get you familiar with the Lab. VIEW programming environment and create a simple program utilizing some of the Sub. VI functionality discussed. 47
Upcoming: o Session 3: n NI hardware Example projects Resources n Wednesday, March 24 th n n 48
Questions? 49
- Slides: 49