01-07-2010 11:00 PM
hi all,
plz see the attached VI
i have a state machine with 04 states .
state 1,
state 2,
state 3, and a
stop state.
i have already implemented all three states in VI . but the problem i cant figure out is how to stop a current state .. for example i have put a 5 sec delay in state 2 .. what i want to do is when i am in state 2 ... there shd be a botton which behaves as a intrupt function meaning as soon as that botton is pressed it shd stop that state or go to any required state. how can i implement this intrupt function.
thnx
01-07-2010 11:34 PM
Get rid of the wait function. There is no way you can make it end early.
Put in an Elapsed time express VI and set that for 5 seconds. If the elapsed time has not passed, then use a select statement that returns it to the same state through the shift register. If the time has elapsed, then move on to the next state.
I realize that your VI is just fleshing out a concept. But in reality you don't have an honest state machine. A state machine generally has a predefined path of states with the ability to jump to a different state if certain conditions are met. So 1 goes to 2, 2 to 3, 3 back to 1. If something else occurs, maybe 2 jumps back to 1. As you have it right now. All your states are basically doing the same thing, just waiting and seeing if a certain button is pressed to go to a different state. If no button is pressed, it just goes back to itself.
You really have more of an event driven structure without using an actual event structure.
All of the code at the bottom of each state is identical with the exception that the default state of the inner case structure has the current state wired. All of that code could be replaced with a subVI. Or moved out of the states completely. A flaw in your logic now is that it will probably take 10 seconds for the state to change because the control buttons are read before the state starts. So if you click on a button, it will occur in the middle of the delay in the state. All the buttons will have already been read read has false meaning it returns to the the same state, at which time the button press is read. But it will only change state after that current state has waited another 5 seconds.
01-07-2010 11:50 PM
HI RAVENS
the wait fuction is just there, in actual i whd put a code instead of the state function. what i need is only that is there any way to stop the processing of any state irrespective of what the code is in that state on the user request.
01-07-2010 11:59 PM - edited 01-07-2010 11:59 PM
Hi nolsqn,
you should change your statemachine to a queued state machine. You can use the timeout of the dequeue element as a "wait" time and a new "msg" as a stop condition.
Hope it helps.
Mike
01-08-2010 12:02 AM
01-08-2010 12:19 AM
nolsqn wrote:HI RAVENS
the wait fuction is just there, in actual i whd put a code instead of the state function. what i need is only that is there any way to stop the processing of any state irrespective of what the code is in that state on the user request.
It all depends on what you are doing in that state.
If you are doing something that is going to take a long time (writing a large file, complex math analysis operations, ...) there is no way you can shortcut those operations and make the state end earlier. A case structure can't end until all the code inside that state has completed.
If it is something that may take a long time, but is basically a quick but repeated process (such as slowly ramping an analog output by continually writing a new value to the DAQ functions, waiting for a condition to become true such as continually reading a temperature until it is greater than some value, ....) then you can put that code inside a while loop in the case, and have a means of stopping the while loop early (local variable, notifier, action engine). A 5 second wait could also fall into this category. If you had a while loop with a 100 millisecond wait that also reads a stop button then that loop will run relatively fast reading the stop button. You can stop that loop when the stop button becomes true OR the elapsed time has hit 5 seconds.
Other alternatives is to have other states, such as a Wait state, or Idle state. State 2 runs quickly setting up some initial conditions, then calls the Wait state, which does the waiting for 5 seconds and can be ended early under the right conditions. Basically take the while loop that is inside the case of that state, and expand it outwards so it essentially becomes its own state functioning as part of the outer overall state machine while loop.
01-08-2010 12:42 AM
hi RAVENS
thnx for the reply
" you can put that code inside a while loop in the case" ...... i want my code to excute once so i think i will use a for loop with conditional stop ....which is better ?
and also its much to ask but the last thing u said is a little unclear to me can u provide a vi for that concept
01-08-2010 01:43 AM
01-08-2010 03:11 AM
Hi nolsqn,
you still have the 5000 connected to the wait function. If you want shorter wait time, then connect a 50 to the wait function and a 100 to the for loop.
Mike
01-08-2010 03:36 AM