LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple poll case structures to event help


@johnsold wrote:

Matt,

 

I am going to confuse you even more!  You might be better off with three loops. One loop would handle all the User events - when the user presses a button or enters a value on the front panel. This is a Producer (Events) loop. Another loop might be the one which communicates with the Arduino. This is a Producer (Data) loop.  The third loop is the Consumer and it gets commands from the User Interface loop and it gets data from the Arduino loop. It updates displays and saves to files.

 

The basic idea of the Producer/Consumer concept is to separate functions which might need to operate at different speeds. The user interface needs to "feel" responsive to users - which typically means that it should respond within about 100 ms to user actions. With an even structure and a queue being the only code inside the loop, the response is very fast with almost no CPU overhead. Suppose the Arduino needs to have a response within 10 ms. Having a loop which only communicates with the Arduino and passes the data to the consumer via a (different) queue means that it can run much faster than the other loops. Meanwhile the save to file loop can run once every few seconds. If the OS decides to defragment or reallocate the files and takes hundreds of milliseconds to do it, the other loops continue to run at their usual rates.  The queues act as buffers so data is not lost.

 

Lynn


I have built an application that uses Lynn's 3-loop construction. A first WHILE loop handles front panel actions with an event structure that sends information to the other parts of the program using notifiers and queues. A second WHILE loop dequeues state "commands" from the event structure and performs the state machine functionality that also includes state transitions that come from within the various states as well. The function of the program are to measure parameters from various UUT's. The state machine queues data to the third WHILE loop that processes the data by writing it to text files (that I process and and analyze in Excel). I have recently added a bit of state machine functionality to this third data loop that pushes the data into Excel and runs a macro that generates pivot graphs of the results.

Jeff

 

Jeffrey Zola
0 Kudos
Message 21 of 34
(1,018 Views)

So I'm writing a simple VI which starts a num at 0 and has 5 states - do nothing, add1, add2, sub1, stop.  I'm trying to follow Figure 3c here as my selector option.  I created a cluster of boolean arrays, converted to a n array, and looked for which boolean is true.  Take that index num+1 and put it to my constant enum.   However, I'm getting an error when I try to use the "Index Array Function" with the n-dimension array being the constant enum.  Attached is a picture of the code and the error message.  

 

My enum values are

Init - 0

add1 - 1

add2 - 2

sub1 - 3

stop - 4

 

I thought it would be good to have the +1 after the search array.  If none are true, it gives me a -1, add 1, 0.  at 0, it just waits for a command.  If add1 is true, it returns 0, add 1, 1.  

 

Any advice?

 

Matt

 

Edit:

 

I took out the "Index Array Function" and just wired the +1 to the case structure's output terminal and everythin worked fine.  Question, why does the code from Figure 3c from above have that block in?  is it to make things works on stings instead of numbers....?

0 Kudos
Message 22 of 34
(1,002 Views)

Maybe i am missing something but can't you just add an event structure as one of your states?  Then your event structure will handle the button presses.

 

Capture.PNG

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 23 of 34
(983 Views)

That'd be an event-driven state machine, which is also a solution that's ok assuming the states or events doesn't take too long.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 24 of 34
(957 Views)

@Uke88 wrote:

 

 

I took out the "Index Array Function" and just wired the +1 to the case structure's output terminal and everythin worked fine.  Question, why does the code from Figure 3c from above have that block in?  is it to make things works on stings instead of numbers....?


Just typecast the number to the enum, no index array needed.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 25 of 34
(953 Views)

I have not started with the P-C design yet, I am trying to get my state machine correct first, then switch to the P-C design.  

 

Is there another type of state machine out there?  I thought the state machine had to be in a case loop and the cases were determined by either a time out or a change in variable by the producer.  

 

What do you mean by typecasting? I found this when I googled type casting and that looks like it just changes the variable from one type to another.  From that link, would the output state (num) be x and type be enum, and then everything is fine with the case loop?

 

Matt

0 Kudos
Message 26 of 34
(926 Views)

Hi Matt,

 

A state machine is just an event structure within a while loop.  That event structure is consists of an enum of cases that can do anything such as data collection, analysis, initialization, error handling, stopping, and starting.  Based on the how the current state acts, you can change what the next state will be using a Shift Register.  This is a great example for how they work:

 

https://decibel.ni.com/content/docs/DOC-4594

 

 

You mentioned using a Producer/Consumer design as well, so I will throw in an example for that as well:

 

https://decibel.ni.com/content/docs/DOC-30005

 

I am a little confused on your last question, could you clarify?  You make a Control (http://zone.ni.com/reference/en-XX/help/371361H-01/lvconcepts/custom_cont_ind_type/) for an enum and you can add different conditions.  From there, you can drop the control anywhere you need to change the current state. Does that make sense?

 

Let me know if this helps.

 

Nick

Applications Engineer
National Instruments
0 Kudos
Message 27 of 34
(900 Views)

Nick,

 

Did you mean to say the a state machine is a CASE structure in a while loop, NOT an EVENT structure?

 

Lynn

Message 28 of 34
(890 Views)

/Y and Nick,

 

I'm having trouble figuring out the whole enum value thing.  The way I think of things, its a number that looks like a string.  By this, I mean that when you click on the enum value, a whole bunch of words drop down and when you select one, it gives a number associated with that word.  My reference is the LIFA code.  When choosing a command to send to the Arduino, it uses a typedef enum with ~50 actions.  Instead of calling all these actions "1", "2", or "3", they call them "sync", "digital write", "digital read".  However, Labview passes them as a 1, 2, or 3 because its easier to deal with numbers and not strings.  Right?  Attached is an image which shows what I mean with words and drop down


Thanks,

Matt

0 Kudos
Message 29 of 34
(885 Views)

Uke88 wrote:

I'm having trouble figuring out the whole enum value thing.  The way I think of things, its a number that looks like a string.


Well, you could have started with the wikipedia entry instead of thinking on your own. 😄

Then there is the LabVIEW specific entry in the LabVIEW help.


@Uke88 wrote:

 Instead of calling all these actions "1", "2", or "3", they call them "sync", "digital write", "digital read".  However, Labview passes them as a 1, 2, or 3 because its easier to deal with numbers and not strings.  Right?


LabVIEW does not pass them as numbers, it passes them as an enum of a specific type and having a specific value. If you wire it to a simple numeric terminal, you'll get a coercion dot or worse. There are many more advanges for using enums. Have you ever wired one to a case structure? Makes code easy to read and self-documenting. When incrementing or decrementing an enum, it will automatically wrap around if you reach one of the ends.

 

 

0 Kudos
Message 30 of 34
(877 Views)