LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers...

58
LabVIEW Programming Guidelines Hans-Petter Halvorsen https://www.halvorsen.blog

Transcript of LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers...

Page 1: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEWProgramming Guidelines

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 2: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

ContentsIn this tutorial I assume you are alreadyfamiliar with LabVIEW• Blog:

https://www.halvorsen.blog/documents/programming/labview/

• LabVIEW Basics YouTube Playlist: https://www.youtube.com/playlist?list=PLdb-TcK6Aqj2_aDQVCQgMu9Hz77pLhVqa

Page 3: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Programming Guidelines

• Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't have the proper structure and best practices.

• The solution to this problem is organizing your code and data in a way that enables modularity, readability, and reuse.

3

Page 4: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Topics – some examples• Intuitive GUI (Front Panels)

– Add Units– Use Controls and Indicators from the same Palette– Resizing the Window size– Don’t use “strong” Colors

• Structured Code (Block Diagram)– SubVIs– Avoid Spaghetti Code– Resizing the Window size– Use Labels– Always use Project Explorer– State Machine

Page 5: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

5

What is Wrong with this GUI?

Page 6: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

6

Page 7: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Structure your Code

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 8: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

8

Page 9: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

9

Page 10: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

10

Page 11: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Spaghetti Code

Since LabVIEW is a graphical programming language with lots of wires, etc., it is extremely important to have a good and clear structure in your program!

Page 12: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Structure your Code

12

Page 13: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Alignment Tools

13

Page 14: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Alignment Tools

14

Page 15: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Alignment Tools

15

Page 16: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Use Comments and Labels

16

Use Labels for SubVIsand built-in Functions

Use Comments and Labels

Page 17: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Customizing LabVIEW IDE

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 18: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Customizing LabVIEW IDE

18

Page 19: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Change Visible Palettes

19

Page 20: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEW Palettes

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 21: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEW Palettes

21

Page 22: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEW Palettes

• In LabVIEW we have different palettes for creating user interface objects, like Modern, System, Silver, Classic, etc.

• You decide which of those you want to use, but please don’t mix controls from different palettes. Stick to one of them.

• Personally, I think Silver has a modern and fresh look

22

Page 23: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Don’t mix Controls from

different Palettes

23

Page 24: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Make Current Value Default

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 25: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Make Current Value(s) Default

25

Page 26: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Resizing the Window Size

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 27: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Resizing the Window Size

27

• You don’t need to show lots of “empty” space

• Resize the window size so it fits your GUI (Front Panel) and your code (Block Diagram)

• The Save it (Ctrl +S). It will be like this next time you open your application!

Page 28: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

28

Page 29: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

29

Page 30: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Don’t use “strong” Colors

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 31: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Colors

31

• Don’t use more colors than you need

• It should look like an ordinary Windows Application

Page 32: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Labels and Captions

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 33: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Labels and Captions

• Hide Labels

• Use Captions in your GUI (Front Panel)

33

Page 34: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Hide Labels when not needed

34

Page 35: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Labels vs Captions

• Labels -> Code (Block Diagram)

• Captions -> GUI (Front Panel)

35

Page 36: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Labels -> Code (Block Diagram)

Captions -> GUI (Front Panel)

36

Labels vs Captions

Page 37: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Project Explorer

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 38: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Project Explorer

38

Page 39: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Project Explorer

39

Page 40: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Project Explorer

• You should always use the Project Explorer even for simple applications

• Like the “Solutions Explorer” in Visual Studio it Keeps all your Files for a specific project in one place

40

Page 41: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Units

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 42: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Add Units

• A value without a Unit is useless

• Make sure to always add a Unit in Controls,Indicators or in Plots/Charts

42

Page 43: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Add Units

43

Page 44: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Add Units in Charts (both x axis and y axis)

44

Page 45: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Number of Decimals

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 46: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Number of Decimals

46

Make sure to read the Datasheet and use

common sense

Page 47: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

SubVIs

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 48: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

SubVIs

• A SubVI is the same as a function or a method used in other languages

• Hundreds or thousands of SubVIs are included with LabVIEW

• But even better: You can create and use your own SubVIs

Page 49: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Why create and use SubVIs?

• You need to use the same operation many times in your code

• Reuse your code

• Hide complicated code

• Easier to maintain your code

• Easier to find bugs and repair them

• ...

Page 50: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

SubVIsInput Output

(Control) (Indicator)

Icon that makes it easier to understand what the SubVI is doing

A SubVI is the same as a function or a method used in other languages

Select Inputs and Outputs

Create a nice icon as well(Icon Editor)

𝑇𝐹 =9

5𝑇𝐶 + 32

Page 51: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Icon Editor Use the Icon Editor in order to create a descriptive icon for your SubVI.

Page 52: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Using SubVIs

SubVI created in previous slide(just drag it on the Block Diagram with your mouse)

Page 53: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEW SubVIs Video

• For details, see the Video “LabVIEW SubVIs”: https://youtu.be/zTbQxkDVSic

53

Page 54: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

LabVIEW State Machine

Hans-Petter Halvorsen

https://www.halvorsen.blog

Page 55: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

State Machine

55

Page 56: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

State Machine• Typically engineers often create simple LabVIEW VIs that

eventually grow out of control, because they don't have the proper structure and best practices. The solution to this problem is organizing your code and data in a way that enables modularity, readability, and reuse. Using a state machine approach is a good way to make it right from the early beginning.

• The state machine is one of the fundamental architectures LabVIEW developers frequently use to build applications.

• In LabVIEW software, you can create a basic state machine with a While loop, a Shift Register, a Case Structure, and some form of case selector.

56

Page 57: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

State Machine• I will show a State Machine that I have already

made.

• For details, see the Video “LabVIEW Applications using State Machine”: https://youtu.be/-b9St8wNhpQ

• Here I will through how to create and use a State Machine in detail.

57

Page 58: LabVIEW Programming Guidelines · 2020-04-14 · Programming Guidelines •Typically engineers often create simple LabVIEW VIs that eventually grow out of control, because they don't

Hans-Petter Halvorsen

University of South-Eastern Norway

www.usn.no

E-mail: [email protected]

Web: https://www.halvorsen.blog

YouTube: https://www.youtube.com/IndustrialITandAutomation