690 likes | 888 Views
Creating and Managing Reusable Code in LabVIEW. Wes Willis Field Sales Engineer- NM. What do we mean by “Reusable?”. Scalable —Easy to add functionality to a VI without completely redesigning the VI
E N D
Creating and Managing Reusable Code in LabVIEW Wes Willis Field Sales Engineer- NM
What do we mean by “Reusable?” Scalable—Easy to add functionality to a VI without completely redesigning the VI Readable—Easy to visually inspect the design of a VI and understand its purpose and functionality Maintainable—Easy to change a VI without affecting the intent of the original VI
Scalable VIs • Scalable design easily allows modifications to original design Data Acquisition VI that acquires data from one thermocouple Data Acquisition VI that acquires from any number of thermocouples Data Acquisition VI that acquires thermocouple data Data Acquisition VI that acquires any type of data
Maintainable VIs • Allow you to add new features without completely rewriting the application • Use good program design and architecture • Are designed with the understanding that other programmers may need to use and modify your code
Business Benefits of Reusable Code • Faster time to market • Consistent quality • Consistent implementation across developers • Transfer ease • Reduction of software defects - Less support $$
Agenda • Scalable Architectures • Strategy for Designing an API • Icons & Documentation • Maintaining Reusable Code • Distributing Reusable Code Scalable Readable Maintainable
Creating Reusable Code Scalable architectures
Basic Architectures State Machine > Producer Consumer > < Event-Driven
Queued Message Handler • Producer-consumer, events and state machine combined • Receive and process messages generated by events • Events can be triggered from UI, or other communication interface • Each message will perform an action
Queued Message Handler • Event Structure Handle UI and trigger messages • State Machine Process messages • Queue Send messages to State Machine • User Events Feedback to Event Structure
Queued Message Handler • IMAGE!!!! (And Demo) Demo: Queued Message Handler
Functional Global Variable • Also known as: • FGV • LV2 Style Global • Action Engine • Etc. • Stores data in an uninitialized Shift Register • Performs multiple tasks using the same stored data
Basic Functional Global Variable Demo: FGV - Basic
FGV in a Large Application • Performs all module functions • Store references, shared data, etc. • Encapsulates low level functionality from user
FGV in a Large Application Demo: FGV – File IO
Subpanel Plugins • Run different Front Panels within main VI • Scalable: easily add another VI • Flexible: UI Can be anything that fits within subpanel • Modular: VIs independently coded and tested • Simple: may not need communication to main VI
Subpanel Plugins Demo: Subpanel
Asynchronous Processes • Run VIs in parallel • Calling VI does not need to wait • Can dynamically spawn any number of the same process • Useful for Background tasks • Examples • Auto-save • Archive data • Garbage collection
Asynchronous Processes Demo: Asynchronous Processes
Object Oriented • Use dynamic dispatch in the Command Pattern to avoid predicting data types • Use class hierarchy in the Factory Pattern to automatically execute correct functionality • Many more…
State Machine vs. Command PatternThese diagrams represent functionally equivalent code
The Basics of an Object Factory A B Objects Loaded Into Memory C Generic Plugin Parent Location on Disk Where Plugins are Stored A B C Children
Creating Reusable Code Strategy for designing an API
What is an API? • Application Programming Interface • Public functions used to programmatically call code that you or someone else wrote. • Group of SubVIs that all access the same set of underlying code
API Considerations • Simplicity • Consistency • Adaptability • Usability
API Simplicity • Make it simple • But offer optional advanced functionality Most Common Functions Advanced functions
API Consistency • Icons • Connector Panes • Data Types • Naming
API Adaptability • Plan for future expansion and change to the API • Use typedefs so changes to enums and clusters update throughout • Leave empty connector pane terminal for future possible controls/indicators • Ensure future additions/changes won’t break existing code • i.e. renaming VIs, rearranging connector panes, etc.
API Usability • Logical organization of palettes • Intuitive functions • Helpful names of functions and inputs • Well-Documented
Creating Reusable Code icons
Use Color Wisely vischeck.com
verb object Be Consistent Add Network Device
Icon Tools / Resources LabVIEW Icon editor Axialis Icon Workshop Corel Paint Shop Pro ni.com/iconlibrary LabVIEWPartnerProgram@ni.com eTrainings at ni.com/addondevcenter
Creating Reusable Code Documentation
Why Is Documentation Important? • Documentation is a code feature • Supplements the software • Adds credibility to your code • Reduces number of support calls • Meets Compatible with LabVIEW requirements
What Kind of Documentation is Important? Goal: Answer “pre-use” questions Useractivities • Goal: Provide in-product support for features
In-Product Support—Context Help Answer for users,“Does this fit my use case?”
In-Product Support—Context Help VI descriptions NOT “Acquires data.”
In-Product Support—Context Help “Returnstwo arrays that describe the magnitudes and angles of obstacles the device detects within a given range.” VerbOutput(s)Input(s)”
In-Product Support—Context Help Input/output descriptions • Provide units in which values are expressed, default values, error information • NOT “timeout is the time to wait for a response.” • INSTEAD “timeout (ms) sets the amount of time to wait for a response from the host. If timeout (ms) elapses, this VI returns error code 5. If you set timeout (ms) to -1 (default), this VI waits indefinitely.
In-Product Support—Detailed Help • Cross references to relevant examples • Error codes VIs can return • Screenshots of workflows
In-Product Support Solution: Context help + HTML Help • Use tools from NI Partner team to populatecontext help and generate detailed HTML help • ni.com/addondevcenter
Where to Go from Here • ni.com/addondevcenter • Readme HTML file template • VI Properties Editor tool • VI to XML tool for creating HTML Help • Notepad++, KompoZer—HTML editors (free) • FAR—Compiled HTML Help (CHM) editor (purchase) • http://helpware.net/FAR/ • Requires Microsoft HTML Help Workshop (free)
Managing Reusable Code Source code control