Tuesday, August 2, 2011

How to Customize Edit > Create SubVI in LabVIEW 2011

LabVIEW 2011 was announced at the NI Week keynote today, and there are plenty of places you can learn about all the great new features. In this blog post, I'll describe how to customize one of the best new features...the improvements to the Edit > Create SubVI gesture in the LabVIEW IDE.

You may recall that last year, Stephen and I solicited the LabVIEW Community to vote up (or 'kudo' up, I guess) the Create a proper connector pane when doing Edit -> Create subVI idea posted by Yair on the Idea Exchange. You all responded in an impressive way, and we were able to add the feature in LabVIEW 2011.  Specifically, we now create a subVI with the proper connector pane, properly-named error terminals in the bottom corners, properly-named refnum/class terminals in the top corners, and an organized front panel.

But that's not all!  Thanks to Stephen, the code that modifies the created subVI is all VI-based.  So if you want to customize the created subVI in some way with your own scripting VIs, there are two ways to do it. Here's how:

Providing Additional Create SubVI Functionality
Do you like the way we create the subVI, but you want to perform additional actions on it?  If so, then follow these steps:
  1. Open this VI: [LabVIEW]\resource\plugins\CreateSubVI\Additional Actions Template.vi
  2. Save a copy of the VI here: [LabVIEW]\resource\plugins\CreateSubVI\CreateSubVI_AdditionalActions.vi
  3. Add scripting code to this copy of the VI to do whatever you want to the created subVI.  Some ideas I've had are:
    • Perform a Diagram Cleanup on the created subVI's diagram
    • Wrap the created subVI's diagram in an error case structure
    • Programmatically launch the Icon Editor on the created subVI
Completely Replacing Our Create SubVI Functionality
Would you rather define your own behavior for how the subVI is created?  If so, then follow these steps.
  1. Make a copy of  [LabVIEW]\resource\plugins\lv_modifyNewSubVI.vi and name it lv_modifyNewSubVI.bak.
  2. In the original lv_modifyNewSubVI.vi, you can remove the code on its diagram, and write your own scripting code to modify the new subVI.  Here is a description of the inputs/outputs of the VI:
    • VI Refnum - A VI reference to the newly created subVI.
    • Fail? - If true, LabVIEW assumes an error occurred during your scripting, does not commit any of your scripting changes, and instead creates the subVI in the same way it did in LabVIEW 2010 and previous...full conpane, weirdly-named controls, etc.
    • Caller Connections in/out - This is the most complicated part.  The input Caller Connections array defines numeric indices for the controls/indicators on the (full) subVI conpane as it is originally created.  For example, if you create a subVI with one input and one output, then the array might have the values [0,1].  You can then programmatically inspect the conpane to see which items are connected to which indices...let's say for this example, the control is index 0 and the indicator is index 1.  For the output Caller Connections array, you must specify which indices on the new conpane those same objects are now wired to.  So for example, if you change to a 4x2x2x4 conpane, and you place the indicator at index 0 and the control at index 11 (those are the top corners of 4x2x2x4), you would output [11,0] as the output Caller Connections.  If you want to learn more about how this works, check out [LabVIEW]\resource\plugins\CreateSubVI\Calculate New Conpane Array.vi, which is the VI that figures this out for the current shipping Create SubVI technique.
  3. If you ever want to revert to the shipping functionality, restore the .bak copy of the VI.
I should also point out that these VIs are not kept in memory after a Create SubVI operation...so it's very easy to debug (via breakpoints, etc.) any plugins you write, as you can easily modify your plugin code and test it by performing the Edit > Create SubVI  gesture again.

I would like to eventually post some plugins of both types to the NI Community.  In the meantime, please let me know if you've written some plugins to augment (or replace) the Create SubVI functionality...I'd love to see what y'all can come up with!

Wednesday, March 16, 2011

My Interview on VI Shots

Last week, Michael posted his first interview with me on VI Shots. So far, I've been impressed with the content on this blog. Additionally, with the twitter account @vishots, he retweets relevant LabVIEW-related topics posted by other twitter users.

In our interview, we talk a bit about my background in LabVIEW, along with some of my tips for programming in LabVIEW at breakneck speed.  Check out our interview here.  And from what I understand, Michael has lots more great LabVIEW content in the works for future podcasts and articles.

Wednesday, February 16, 2011

Code Madness: The LabVIEW Example Program Challenge 2011

(What a funny coincidence that my last two LabVIEW blog posts had a college basketball-themed title. And I'm not even into college basketball. Although I guess as good as the Longhorns are doing this season, I probably should be...)

The latest LabVIEW coding contest on the NI Community has begun. Enter the LabVIEW Example Program Challenge 2011 today for your chance to win an XBox, Amazon gift cards, and other prizes. In this contest, example submissions will initially compete with one another in a qualifying round (taking place now until March 6th), with downloads, "likes", and ratings all contributing to each submissions's ranking. The top 16 submissions will then move on to a tournament-style coding competition (taking place from March 8th to April 4th), with a bracket system being used to match up winners for the rounds of 16, 8, 4, and 2, until the winner is decided in the final round.

To learn more about the Code Madness Program Challenge, visit http://www.ni.com/codechallenge. Begin submitting code today!

Thursday, October 7, 2010

The Sweet 16: Idea Exchange Entries That Need More Support

I've really been enjoying all the fantastic LabVIEW 2010 features that originated in the Idea Exchange, and I'm also excited about the ones we have planned for LabVIEW 2011.  Last night, I went through the Idea Exchange and read through all the ideas that have more than 10, but less than 100 kudos.  I found 16 that I think would really benefit the LabVIEW environment and increase my productivity, but aren't currently on our roadmap of features because they haven't bubbled up high enough in the rankings.  So if you haven't already, please consider kudoing some or all of the following ideas, so we can push to get them included in LabVIEW 2012:
By the way, I got the idea for this blog post from Aristos Queue, who has already posted his own list of Idea Exchange entries that he'd like to see implemented in a future LabVIEW version.

Tuesday, July 20, 2010

Changes to the CLA Exam

The Preparing for the CLA post is one of the most highly visited links on my blog, so I figured I should post some updates that I just learned about (thanks to JG's post on LAVA) to the way the CLA exam is now done. Much of the advice I gave in the aforementioned post is no longer valid, so I'd like to highlight some of the new features of the CLA Exam as described on the NI CLA prep website:
  • No more written portion - When I took the CLA exam, it contained a written portion that was 40% of my score. This part of the exam has been removed, which now makes the CLA a 100% practical exam.
  • Customize LabVIEW settings prior to exam start - When I took the exam, I had to spend the first few minutes after the clock started customizing LabVIEW (adding Quick Drop shortcuts, turning off auto wire routing, etc. etc.) before I even read the first question. But now, it seems you can ask your proctor to allow you to customize LabVIEW *before* starting the exam. The following is a direct quote from the new CLA Exam Preparation Guide (page 2) on ni.com: "Please note that you will not receive extra exam time to compensate for non-familiarity with the LabVIEW environment. If you need time to customize the environment, please make arrangements with your proctor to hold off on giving you the exam packet until you are ready to start the exam."
  • Sample Exam Available - There is now a CLA Practice Exam available...this is a great asset in CLA preparation that I highly recommend taking very seriously. There is also an exam solution available.
  • Requirements tracking - 30% of your CLA score is now determined by requirements tracking, the details of which are described in the practice exam and prep guide documents linked above. The exam graders will be using NI Requirements Gateway to verify requirement tracking in your VIs, so make sure you adhere to the [Covers: ] syntax described in the prep guide and sample exam. Note that knowledge of NI Requirements Gateway is *not* a requirement for the CLA.
Looking at the changes, I think they're probably for the best. I personally found the written portion of the CLA when I took it to be relatively easy and straightforward. But I know that written exams are notoriously hard to grade, and there might potentially be language barriers for some test takers. It looks like the test writers are expecting the additional requirements tracking to take roughly the same amount of time as the written portion of the exam, since the sample exam is very similar to the actual exam I took for my CLA, with the addition of the requirements tracking information.

Friday, July 16, 2010

Conclusions - The Diagram Cleanup Experiment

It's been just about a year since I embarked on The Diagram Cleanup Experiment. I attempted to use block diagram cleanup on just about every VI I wrote in LabVIEW 2009 this year. With such heavy use of the feature, I have come to the following conclusions:
  • Use it on moderately-sized diagrams - The majority of the VIs I write fit on one screen, and have relatively low levels of nesting. For these kinds of VIs, it is *way* faster to quickly write the VI, then press Ctrl-U to clean it up. The cleanup results in these cases, although not perfect, are acceptable. The diagrams are readable enough to avoid maintenance headaches down the road.
  • Don't use it on large diagrams - Diagram cleanup still has trouble with large diagrams. For top-level state machines, or similar architecture-level VIs, cleanup does not respect the arrangement of the diagram, which, for these VIs, is crucial to the understanding of the VI. I have decided that it is still best for me to arrange these diagrams myself.
  • Don't use it on heavily-nested diagrams - As I mentioned in my progress report, diagram cleanup completely explodes the diagram if there is heavy structure nesting. It simply can't figure out how to condense space in multiple frames simultaneously. Until it does, I will continue to arrange heavily-nested diagrams myself.
  • Mixed results with Tools > Options settings - I tried several times to tweak the settings in Tools > Options > Block Diagram > Block Diagram Cleanup to see if that would improve the cleanup arrangement, but I couldn't figure out a definitive collection of settings that always worked better. If anyone has any specific suggestions for settings that seem to improve the cleanup layout, let me know.
So what's next? I just posted the following idea on the LabVIEW Idea Exchange:

Tell Block Diagram Cleanup what "Clean" Looks Like

I think the easiest way to get diagram cleanup to arrange VIs to my liking is to show it VIs that I like. :) Please kudo the idea if you agree.

So that's pretty much it. In short--I like the feature, I will continue to use it because it helps me program faster, but it definitely has room for improvement.

Tuesday, June 8, 2010

I Like Global Variables

I think the title of this post alone will probably lose me some followers on this blog. :) Nevertheless, let's proceed...

Here's the deal...global variables are not evil. There are perfectly valid use cases in which a global variable is the best tool for the job, and doing something more complicated just to say, "I didn't use globals!" doesn't make sense. Now sure, globals can be abused, and there are certainly scenarios where they are contraindicated. But if you're aware of those situations, and you avoid them, you should feel free to use globals where needed. So let's look at some use cases where, in my opinion, globals can come in handy:
  • Static Data - Whenever I create a UI in LabVIEW in which user-visible strings are programmatically changed, I always store those user-visible strings in a global variable. Let's call this kind of global a Write Never, Read Many (WNRM) global. I am never writing new values into this global...I'm only reading from it in my code whenever I need to update a user-visible string. Another use case for WNRM globals is in scripting apps. When I'm doing code generation involving templates, I always store the identifying labels of panel/diagram objects within a global, and I always read those identifiers from a global in my scripting code. That way, if I need to change the identifiers in the templates, I know that I'll only need to change the identifier in one place (my global) in my scripting code. (Note: I believe Yair's Adding CONSTs to LabVIEW idea on the Idea Exchange is intended to facilitate a cleaner implementation of this use case.)
  • Debug Flags - If I've got parts of my code that I want to run differently when I'm debugging, I use a WNRM global. I'll open the global and change the debug flags before I run my code. Someone once suggested that I should instead use Conditional Disable Structures with conditional symbols in my project, but after I looked into it, I decided that the added complexity and configuration associated with conditional symbols didn't add enough value over the simple global solution.
  • Configuration Data - In this scenario, we are initializing configuration data (from an INI file, for instance) at some point in our code, and reading that data at later points. So here we have a Write Once, Read Many (WORM) global. (Note: The term WORM global was first coined by tbob on the NI Forums a few months back.) As long as there is one, single place where the global is written, it is perfectly fine to have many other places in your code that read that global.
Now one of the biggest reasons given as to why globals should be avoided is that new users could easily find themselves in race conditions if they're not careful. But with the use cases above, there should never be any worry about race conditions, as long as developers adhere to the WNRM/WORM constraints. I don't think we should hamstring ourselves with a blanket refusal to use a feature when it really can be a performant time-saver when utilized correctly.