Results 1 to 7 of 7

Thread: Decoding button presses in LabVIEW

  1. #1
    Join Date
    Sep 2008
    Posts
    586

    Default Decoding button presses in LabVIEW

    As a veteran C programmer, but a LabVIEW newbie, I initially had some difficulty figuring out how to do something once when a button was pressed. In C I had used the following code a million times (where prevButton is a static variable)

    Code:
    if (button != prevButton && button == 1)
    {
    //do something
    }
    prevButton = button;
    I initially implemented the code exactly as written, using a feedback node to save the previous button value, but it seemed messy to me, and I made a few stupid errors in the process. Since I used it all over the place, I made a simple VI to do the same thing. To make it less error prone, I re-implemented it as a very simple state machine.

    I've attached my implementation of the state machine so that other new LabVIEW programmers can learn from my experiences.

    It takes a boolean input (True/False) and outputs not only True/False, but on a transition it outputs either TrueToFalse or FalseToTrue. You could then take that output into a case statement to implement your own code on the transition that you want.

    The VI execution style is reentrant. In LabVIEW, if it wasn't set as reentrant, the feedback node would act as a global variable and all instances would use that value. Since it is reentrant, the feedback node acts as a static local variable, and each instance of the VI has it's own value. This means you can use this same VI multiple times to decode multiple buttons.

    I've attached both the LabVIEW code and the HTML documentation. Hope this helps someone.
    Attached Files Attached Files
    Team 330 beta tester

  2. #2
    Join Date
    Sep 2008
    Location
    Tacoma, WA
    Posts
    60

    Default Re: Decoding button presses in LabVIEW

    Wouldn't you have to place the feedback node outside the subVI to make it reentrant? I've been having that trouble with a ramping VI, and had to convert it to deal with arrays of values because of that.

  3. #3
    Join Date
    Sep 2008
    Posts
    586

    Default Re: Decoding button presses in LabVIEW

    I had originally done that, but then it gets rid of most of the advantage of making a subvi. Then I found Reentrant execution.

    Open the VI and go to File->VI Properties. In the dialog box, choose the Execution category and you can choose Reentrant execution.
    Team 330 beta tester

  4. #4
    Join Date
    Sep 2008
    Location
    Tacoma, WA
    Posts
    60

    Default Re: Decoding button presses in LabVIEW

    Cool, thanks!
    I didn't know what that did before.

  5. #5
    Join Date
    Sep 2008
    Posts
    85

    Default Re: Decoding button presses in LabVIEW

    Just to be complete on what reentrancy does. A normal VI is not reentrant. This means that it is what is called a singleton. All calls to a singleton subVI foo() will wait on one another, taking turns for the single instance of foo() to perform for them. Also, if foo() has any state data stored internally, that state data will be shared between all callers.

    When you mark a subVI as reentrant, it is no longer a singleton. There is no queue, no waiting for the other calls to complete. Each caller in effect has their own subVI they can call. Any state data is isolated from the other calls. This is ideal for things like PID or filters. It means that the subVI can own the history data and the owning diagram doesn't need to provide it.

    Does that mean reentrant is faster?
    If you have multiple CPUs, it often does. For a single CPU, it doesn't really matter in terms of performance.

    Other than the state data, the other big functional difference is that nonreentrant subVIs can act as a critical section, a way to guarantee only a single thread is running this code at a time. This is used to protect HW state and other data where you need to read some stuff, calculate some stuff, then update some stuff. If two threads try to do this at the same time, they will horribly confuse one another and you can wind up with totally nonsense results. The classical solution is called a critical section, and can be implemented using mutexes and similar constructs. These exist in the LV palettes, but the simplest way to do this is the nonreentrant subVI.

    Greg McKaskle

    Greg McKaskle

  6. #6
    Join Date
    Nov 2008
    Posts
    20

    Default Re: Decoding button presses in LabVIEW

    I don't know much about the ctl's (yet) and I could only get the False and FalsetoTrue Case out of jross's code, so here's my solution, any thoughts?
    Attached Files Attached Files
    David Gitz
    FRC Team #2219 Lead Technical Advisor
    http://www.megahurts2219.com/
    davidgitz@gmail.com

  7. #7
    Join Date
    Sep 2008
    Posts
    586

    Default Re: Decoding button presses in LabVIEW

    Quote Originally Posted by DavidGitz View Post
    I don't know much about the ctl's (yet) and I could only get the False and FalsetoTrue Case out of jross's code, so here's my solution, any thoughts?
    If you right click on the selector label, you can choose "add case for every value", which will get you the other two cases.
    Team 330 beta tester

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I have a question about Labview
    By samcorp in forum Forum Help / Suggestions / Comments
    Replies: 0
    Last Post: 11-12-2008, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •