LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Programming with Events and General Advice on Code Structure

Hi!

I have a feeling that I am painting myself into a corner, or digging a hole under my own feet in my current vi project, and I was hoping that someone here could take a look at the structure (or lack thereof?) of my code and set me back on the "right" path!
Some of my issues is probably related to the fact that this is one of my first vi's to interact heavily with a user.

What it is I want to do:
* When VI runs, wait for valid configuration to be entered.
-Pass config data to my producer and consumer loops

* Loop my producers and consumers until Exit is activated, and wait for events on my "Send Command" tab.

Issues with the code as it is now:
* Exit button does not work in an efficient manner, sometimes the application does not exit at all.

* Readability and scaleability
As it is, I used to have my Data Handler (graph and led indicators) in a sub-vi, but it caused me all sorts of problems, so I moved it out into the main vi, which is getting crowded.
Also, I will add at least 5 xy-charts (inside case structure for appropriate decoding of incomming data) and...

basically, I just feel like I am missing some thing here, because normally my vi's dont look this messy! When I try to program efficiently, say with events, I run into all sorts of problems, and the same if I try to move stuff into sub-vi's, which leads me to believe that I am missing something fundamental here.

Since I am standing in the middle of this, I find it a little hard to see alternative ways to layout and structure my code to avoid the problems I now face, which is where I hope someone here can step in and offer a guiding hand!

Thanks!
The attached zip-archice contains my LabVIEW 8 project. I dont know how to make a "screen capture" of the code since it spans more than my resolution, hence no pictures available at this time.
---------------------------------------------------

Project Engineer
LabVIEW 2009
Run LabVIEW on WinXP and Vista system.
Used LabVIEW since May 2005

Certifications: CLD and CPI certified
Currently employed.
0 Kudos
Message 1 of 7
(3,309 Views)
You are getting close!

I have a very similar program (structurally) that is in the debugging stage now.

Set up the main loop as a state machine with the event structure. Things not directly derived from user control operations on the front panel run as substates in the timeout case (which is currently empty in your progam). Set the timeout to perhaps 10-100 ms. The states might include: Idle (default) - watches for status reports from the producer/consumer loops. Set Config - handles everything in your set configuration loop - No other action states can be entered until config is valid. Init - sets up Buffer and Queues. Then enable Producer and Consumer loops - They may already be running but in an idle state until receiving a Start or Run command from the main loop. Exit - Sends Halt commands to Producer and Consumer loops and waits for "stopped" status reports from all of them before stopping the main loop. This assures that all parts of the program have stopped before exitng the man loop. Other cases if needed to update indicators and flags.

Note that the Producer and Consumer loops run as independent loops (I make them subVIs) parallel with the main loop.

I have no local or global variables and one Value (signalling) property node. All other data is passed via wires and shift registers or queues.

Lynn
Message 2 of 7
(3,301 Views)
My gosh! -That was a fast reply!! Thank you so much sir!

I was hoping to re-structure my code this weekend, with or without any replies from the forum, and you just ensured that I will be able to finish!

The idle thing in the event structure... I never thought of that! 🙂

Again, Thanks!

I'll post back in this thread if I stumble on any more specific problems regarding this!

Kjell
---------------------------------------------------

Project Engineer
LabVIEW 2009
Run LabVIEW on WinXP and Vista system.
Used LabVIEW since May 2005

Certifications: CLD and CPI certified
Currently employed.
0 Kudos
Message 3 of 7
(3,288 Views)
Okay. I guess I'm not as clever as I thought! 🙂

I have several things that, after having spent quite some time working on my VI, I still dont understand key points of your answer!

For one, you talk about using the event structure to build a state machine, but I am not quite sure I understand how to do this.

You also say you do not use any local or global variables and only one signalling event?

Does this mean that you send the "Go" or "Wait" flag to your producers and consumers using a queue? -If these run outside the main loop in their own loops, you cant use wires to pass the values, at least as far as I understand it. (Since then the value would not be passed until the main loop exits.)
And how do you pass your configuration data (such as file and queue references, serial port settings etc.)? Using queues for this too? (Message queue maybe?)

I've looked over the state machine examples on VI (those I could find) but none of these use the event structure, and since you can not set "default" cases in an event structure, I am not sure how to implement my "Idle" state that you mention, since by your description, the "Idle" state is not equal to the "Timed out" event/state.

I realize I might have partially answered some or all of my questions here (using a value (signal) to go between states in my event structure, using a message queue to pass messages, commands and settings to my producers and consumers), but I would be immensly gratefull if someone could shoe me a skelleton outline of how this should look. (E.g. a main loop that waits in the "config" state, moves on to "init" stage while passing out info to producers/consumers etc.)

Also, if I use queues to pass information between my state-machine and my consumer/producers, how can I make this scenario work:

The state-machine has intercepted an exit condition, sent the exit message to the producers/consumers. Now it needs to "wait" for "exit clean-up compelte" messages from the other two loops, but how can it wait for such a message? If I run a while loop inside my exit-state, I will "freeze" my program...

Sorry for not quite understanding how to implement your suggestions Lynn, I've really tried, but though I get the concept for the most part, I feel very uncertain as to how to actually implement them!

Gratefull for any input!
Kjell
---------------------------------------------------

Project Engineer
LabVIEW 2009
Run LabVIEW on WinXP and Vista system.
Used LabVIEW since May 2005

Certifications: CLD and CPI certified
Currently employed.
0 Kudos
Message 4 of 7
(3,236 Views)

Hi Kjell,

If you'd like an example of using Queues and an Event Structure together, you might consider looking at a template. Templates are like regular VIs, but when you open one up, you only open an instance of that template file rather than the template itself.

You can browse to the templates by selecting File >> New from LabVIEW (note: not File >> New VI). From the template dialog that pops up, browse to VI >> From Template >> Frameworks >> Design Patterns >> Producer Consumer Design Pattern (Events). The template can run when you open it, but it won't really do anything special. It does show an example of setting up Events and Queues together, though.

Jarrod S.
National Instruments
0 Kudos
Message 5 of 7
(3,206 Views)
Hi!

You are unfortunately sort of missing the point. I already looked at those, and I am using queues in that fashion. What has me confused is when I try to create a state-machine using the event structure and queues. (having separate queues for real data and one "event message" queue.)

Message Edited by Kjell-Edmund on 03-15-2006 01:57 PM

---------------------------------------------------

Project Engineer
LabVIEW 2009
Run LabVIEW on WinXP and Vista system.
Used LabVIEW since May 2005

Certifications: CLD and CPI certified
Currently employed.
0 Kudos
Message 6 of 7
(3,184 Views)

Here's a method that has worked reliably for me, though it frankly doesn't rank very high on my "elegance meter."

Your UI Event-handling loop catches an exit condition and puts an "exit" message in the producer-consumer message queue.

Your consumer loop receives the "exit" message and manages a graceful shutdown of DAQ tasks, etc.

After all done, it then Releases it's message queue with the "force destroy?" input set to TRUE, then stops execution.

The next time the UI Event-handling loop checks a status on the message queue, the error cluster will have an error asserted because the queue has been destroyed.  This error can cause your Event loop to exit.

If you have additional queues for data-passing, you can apply similar reasoning to handle them gracefully.

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 7 of 7
(3,174 Views)