LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

state machine vs. event driven programming

I have recently been trying to create an event driven state machine with the help of some subscribers on this forum (this effort is cross listed at http://forums.ni.com/ni/board/message?board.id=170&message.id=189750).  The goal is to have a button cause a change between two states, however this change is done with a series of roughly timed steps.  It has been suggested that I use a state machine within an event structure, and I have come up with the attached program.  I am familiar with the state machine method from LabVIEW Intermediate I and II, but I am less familiar with the event structure programming technique.  My VI seems very close to working, but I can't get it to initialize as per the numeric constant and array control on the left side of the control loop.  Does anyone have any suggestions for this program, or an comments about this technique?

Thank you,
Brad

It is posted in both 8.0 and 7.1

Download All
0 Kudos
Message 1 of 11
(7,756 Views)
Looks like your Toggle Trap button has to be pressed before anything can happen, including initialization.  Is this what you intended?  Or do you want to initialize and then wait for the button to be pressed?
- tbob

Inventor of the WORM Global
0 Kudos
Message 2 of 11
(7,747 Views)
Well, you could do something like in the attached VI.
 
I prefer NOT to have loops inside event cases, because they block the event structure. You can use the outer loop for everything and manipulate the timeouts.
 
I would strongly suggest NOT to toggle booleans but to hardwire the desired states using diagram constants. Toggling must rely on the existing state, which can be unexpected unders certain conditions, especially if the program gets much bigger and you add features.
 
Also, I think you are missing a state, because valve 5 gets closed once and never opens again. You should also initialize the toggle to a known state using a local variable at the start of the program, else things are out of sync if you would accidentally change the toggle when the VI is not running.
 
You probably should add some logic on the other events to discard them if the state is not zero.
 
 
Message 3 of 11
(7,740 Views)
tbob,

I want to have the machine initialized before the toggle trap button is pressed.


0 Kudos
Message 4 of 11
(7,727 Views)
I don't have LV8 to open Altenbach's version, but here is a 7.1 version that employs a state machine with the event structure.  Your opening state is to initialize, so put your code to initialize here.  Then next state is your vi as you have it now.  If you don't need to initialize everytime the button is pressed, modify your code to remove the init stuff from the Run state.
- tbob

Inventor of the WORM Global
Message 5 of 11
(7,717 Views)
Here's a slightly improved version of my example in LabVIEW 7.1.
Message 6 of 11
(7,711 Views)
Very clever way of using Timeout event to do the timing.
- tbob

Inventor of the WORM Global
0 Kudos
Message 7 of 11
(7,698 Views)

Nice example!

I didn't knew that the 'set busy.vi' block's the frontpanel. The help just talked about the mouse (buttons), but even with tab/enter the buttons are disabled.

But it seems to me that in a critical process the close panel needs an extra event case or needs to be disabled since the set busy still allow it.

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


0 Kudos
Message 8 of 11
(7,680 Views)
Thanks for the examples and tips.  This was a fun little introduction to event based programming in LabVIEW. 

Technical question - is this still considered a state machine without the while loop?  Or is it an event machine...

Brad
0 Kudos
Message 9 of 11
(7,652 Views)


@CFAMS Brad wrote:
Technical question - is this still considered a state machine without the while loop?  Or is it an event machine...

It is still a state machine and there is still a  while loop, right?. The while loop has a shift register that keeps track of the "state".

Henrik is of course right that the current program is not finished, it needs quite a bit more code to e.g.

  1. Set the valves and cursors to a defined state when stop is pressed or the panel closed.
  2. Define proper actions if the system is toggled while it is still in the middle of a previous change sequence.
  3. Make the code more readable by changing the state constant e.g. to an enum with descriptive state names.
  4. I really dislike the current toggle switch implementation because the actual boolean value is not used, just implied from the dataflow. This can be dangerous.
  5. I probably would use a latch action toggle button together with LED indicators that show the current state (right or left).
  6. ...

I only made a quick "proof of concept" demo here. Your turn! 😉

Message 10 of 11
(7,623 Views)