Five Techniques for Better Labview Code

38
Five Techniques for Better LabVIEW Code Peter Blume President [email protected]

description

Labview

Transcript of Five Techniques for Better Labview Code

Page 1: Five Techniques for Better Labview Code

Five Techniques for Better LabVIEW Code

Peter BlumePresident

[email protected]

Page 2: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Overview

Introduction Five techniques with examples

Specifications Data flow State machines Error handling Documentation

Conclusion

Page 3: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Introduction

Most LabVIEW applications begin with an industrial measurement and/or control challenge

LabVIEW facilitates fast development cycles Easy to connect and control instrumentation Fast solutions to industrial challenges Instant gratification for developers

LabVIEW developers adopt fast habits Sloppy wiring No documentation Computer science fundamentals are ignored

Fast habits = bad habits

Page 4: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Justifying Bad Habits

LabVIEW is a graphical language Computer science fundamentals don’t apply Block diagram is self-documenting

We don’t have time to write good code Intense time pressures LabVIEW is a secondary responsibility Requirements continuously change Equipment availability constraints

I’m the only developer / end-user Nobody else needs to use or understand my programs

Page 5: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Issues Resulting from Bad Habits

Application requirements expand LabVIEW code becomes messy

Inefficient Buggy Difficult to troubleshoot, expand, and maintain Not reusable

Overall development time is increased

Page 6: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Bad Code Example

Page 7: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Good Code Example

Page 8: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Let’s Write Better LabVIEW Code!

Follow these five techniques More up-front time and effort Attention to detail Discipline

Dramatically save time in the long run

Page 9: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

1. Write a Functional Specification

Understand the application’s requirements Interview operators, developers, engineers, managers,

bean counters Document the requirements

Statement of high-level objectives Specific requirements for I/O, analysis, GUI Timetable and budget Design prototype screen shot Assign priorities to each requirement Test methodology

Page 10: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Functional Specification (Continued)

Avoid designing the system Don’t prolong specification phase by including too much

detail such as how to implement the systemMake the specification readily accessible

Have all contributors review and approve Revise when requirements change Keep it somewhere that you and others can quickly

reference it

Page 11: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Simple Specification Example

Page 12: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Bad Code Example

Page 13: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Good Code Example

Page 14: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

2. Use Proper Data Flow

Data flow definition:“A block diagram node executes when all its inputs are available.

When a node completes execution, it supplies data to its output terminals and passes the data to the next node in the dataflow path.”

Parallel paths are permitted and desirable Efficiency is optimized when LabVIEW determines the

execution order Readability is optimized when source terminals, wires,

and destination terminals are visible

Page 15: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Data Flow Impediments

Sequence structures Excessive nesting Local and global variables Coercions Sloppy wiring

Excessive bends Overlapping wires with other objects Multidirectional data flow

Page 16: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Simple Data Flow Example

Impeded Unnecessary

sequence Coercion Local variables

Better data flow

Page 17: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Bad Code Example

Page 18: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Bad Code – Zoom In on Wiring

Page 19: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Data Flow Enhancements

Artificial data dependency Use error cluster and/or task ID

Shift registers Reduce local and global variables

Clusters Reduce the overall number of wires

Neat wiring Left-to-right data flow Straight wires Consistent data types Comments with “>” indicating data flow direction

Page 20: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Good Code Example

Page 21: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

3. Use a State Machine Top-Level Architecture

Define application as a series of states Go to any state from any other state Easy to modify, maintain, and debug Self-documenting Scalable

Page 22: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Classic State Machine

Page 23: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

State Machine Enhancements

Use enumerated or string for case selector

Poll user interface events in “No Event, Default” frame or in separate event structure in parallel loop

Use intuitive state names Include “Initialize” and “Shutdown” states

Page 24: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Enumerated State Machine

Page 25: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Event-Driven State Machine

Page 26: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

4. Incorporate Proper Error Handling

Trap and report any I/O related errors in all VIs I/O functions include DAQ, file I/O, instrument I/O,

communication Trapping is facilitated by propagation of error cluster Reporting methods include dialog prompt or log to file

Page 27: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Error Trapping

Page 28: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

What’s Wrong With This Picture?

Page 29: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Better Error Handling

Page 30: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

5. Document Your Source Code

Assume every VI will be used and maintained by other developers

Best time to document your source code is while you develop it

Page 31: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

LabVIEW Documentation Techniques

Control labels Use succinct, intuitive labels Indicate units in parentheses or use free labels Enter descriptions or online help where further text is needed

Icons Intuitive text or graphic 10-point small fonts Color-coding for icons of related subVIs

Page 32: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Documentation Techniques (cont’d)

Diagram Leave the background color white Set all control labels visible Liberally document with free labels

Enter descriptions for every subVI

Page 33: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Bad Code Example

Page 34: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Good Code Example

Page 35: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Summary

Write a specification for all projects before you begin coding

Use proper data flow Use a state machine top-level architecture Incorporate proper error handling Document your source code while you code

Page 36: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Conclusion

Techniques improve: Readability Robustness Efficiency Maintainability Reusability

Techniques can be implemented quickly if you make them habits Apply to every project, starting now Apply consistently throughout each project

Dramatic reduction in overall time and effort

Page 37: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

About Bloomy Controls

Test, measurement, automation, and control specialists since 1991

Systems integration, software development, and training provider

NI Select Integrator and Certified Training Center 3 Certified LabVIEW Architects 8 Certified LabVIEW Developers 1 Certified TestStand Architect 2 Certified TestStand Developers 8 Certified Professional Instructors

Offices in Windsor, CT; Milford, MA; and Mahwah, NJ

Page 38: Five Techniques for Better Labview Code

www.bloomy.com© 2003 Bloomy Controls Inc.

Contact Bloomy Controls

Email [email protected] Write or visit

CT, Western MA, Eastern NY:

Eastern MA, RI, Northern New England:

Greater NYC, NJ:

839 Marshall Phelps Rd. 100 Medway Rd., Ste 202Windsor, CT 06095 Milford, MA 01757 Mahwah, NJ(860) 298-9925 (508) 902-0054 (201) 818-0117