LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement State Machine

Hello!

I'm trying now to implement simple state machine in LabView, and as I'm newbie in LabView, I'm not sure I think right way. I would appreciate any comments.

The state machine implements pump control.
The pump is controlled remotely, so besides two base states: stopped & running, we have transition states also: starting & stopping, that should be time-controlled.
Yet we have two common states such as init & failed.
The pump has 1 input (on/off), and 1 output (to run/stop) and can be in the following states:
init, stopped, starting, running, stopping, failed.

Besides that the pump has 3 commands:
- reset;
- run;
- stop.
Reset is used when the pump is failed and operator tries to recover the state machine to correct state.
Note: we can do so that the state machine recover automatically from the failed state by monitoring inputs & outputs until their relation is correct, but it doesn't matter now.

Now the scenario: when the operator starts VI, state machine is set to init state. In init state it reads input & output and goes to appropriate state (say, stopped). Operator pushes button RUN, issuing the command "run" to state machine. The state machine sets output = 1, starts timer and goes to "starting" state. In "starting" state the state machine waits for input = 1, or timeout. If input goes to 1, the state machine goes to "running" state, otherwise (in case of timeout) it goes to "failed" state. The state machine stays in failed state until operator presses "reset" button on front panel. If reset, the state machine goes to init state and tries to define state of the pump.

Now the questions:
1) is it right to use local variables as I did or there's more elegant way?
2) this VI uses "time elapsed" structure to control time of transitions, is there more correct way to do that?
3) this VI uses polling paradigm, maybe it can be done in event-driven way for some reasons?

Besides, I'll be thankful to everyone for any comments from language-specific to philosophic ones.

Best wishes.
Rashid.
Download All
0 Kudos
Message 1 of 11
(13,112 Views)
As I can see, you put two kind of state machine one inside of the other.
I'm sure that is a bether way to do it. Try to use the queue VI. By doing that, you will be able to change the priority of the next queue to be executed.
 
for the time control, use the next multiple of milisec. In your state machine, in the while loop, put that VI. By doing that, you will be able to controle the elapsed time for one loop. Be sure that the time you put is larger than the longest state.
 
For the user interface, use anothe loop with local variable that is read from the state machine. The user interface loop should run with delay more than 100ms.
 
Benoit
Benoit Séguin
Software Designer
0 Kudos
Message 2 of 11
(13,070 Views)
Hi, Benoit!

What do you mean by the 2nd state machine?

I thought, I have only one state machine, inside which
I have case structure for every state to process events differently.
In other words, depending on the state my machine acts differently -
stays in the same state or goes to another state.

As to controlling time: do you propose to replace the elapsed time structure by
fixed time execution of the loop? or you're just telling about how to make VI
more efficient (I mean yielding control to CPU)?

As to user interface I'm agreed that it would be more efficient to separate program
flow into 2 threads: GUI and control. As GUI can be updated slower then control
thread. And if I get it right, the data from the control thread are passed by means of
local variables to GUI thread. (I'll try to change my VI).

Best regards,
Rashid.

Using LV 8.0 under WinXP.
0 Kudos
Message 3 of 11
(13,047 Views)
The thing is the your State machine have a lots of code that is repeating. in each step you define the next step to execute. Example, the default state is in more than one place. instead of putting it in the case in the state machine, include it in the state machine. and do it for all the other.
 
I suggest you to use the queue fonction. It seams to be a bit complicated, but when you will understand how it's work, you will love it. The biggest advantage on queue, is the possibility to have more tnan one state witing to be executed. You can change the order when the queue will be executed. So the more important step will always be executed first. example the safty feature code...
 
 
About the time execution, the wait function is not realy acurate. the wait for next multiple of milisecond function, will make your application to do one loop every x ms. This advantage is see in control loop and in save loop. If you want to save data every 10 min, it will be every 10min not 10,01 or 10,05. If it's not needed, I suggest you to use it anyways. just to take the habit to use it.
 
Benoit
Benoit Séguin
Software Designer
Message 4 of 11
(13,030 Views)
Thanks, Benoit for your comments.

I'm looking now for posts about queue-driven state machines.

It seems to me now that I misused local variables in
my state machine and that it would be much better
to use shift registers (I found the State Machine Framework
in LV I missed before).

Best wishes,
Rashid.
0 Kudos
Message 5 of 11
(13,002 Views)
Take also the habit to note the answer of people that help you in the forum.
thanks
Benoit
Benoit Séguin
Software Designer
0 Kudos
Message 6 of 11
(13,001 Views)

@Benoit wrote:
Take also the habit to note the answer of people that help you in the forum.




Benoit, do you mean rate the answer?
If yes, no problem, I'll do, and thank you for remark.

I read about queue-driven state machines (QDSM).

Did I get it right, that QDSM allows me to run concurrent states in the same time?
And yet if I use a complex state machine with concurrent states then the QDSM is
the best way to solve this problem, but if my state machine has no parallel states
I can use the ordinary shift registers as in standard state machine framework from LV.

Thanks in advance.
Rashid.

P.S. If I'm right about concurrent states then I'm agreed with you that queues are really very elegant solution.

Message Edited by Rashid on 01-31-2006 11:33 AM

0 Kudos
Message 7 of 11
(12,980 Views)
Just curious, why is the Output actually a control rather than an indicator?
0 Kudos
Message 8 of 11
(12,950 Views)
Though it's just a sketch, the point is that
I have outputs & inputs arrays that will be read from the controller upon VI start. My VI must define current states of all objects by their input-output states so that not to impact on the system upon the start. That's why output is also the input for my state machine. When initializing my state machine needs to read output, but when controlling the object it will write to output.

If to tell more generally the main point is that my system has 2 levels of control - PC & controller. If PC hangs and reboots the VI should be able to start as though nothing happened while the controller holds the outputs of all objects (the possibility that controller will hang is much less).

A little scheme:

VI: starting -> reading inputs & outputs from controller via network (or RS-232) -> defining states (converting inputs & outputs to states of state machines)
-> waiting for events (operator command, input change, etc.) -> changing outputs if appropriate event happened according to control algorithms.

Best regards,
Rashid.
0 Kudos
Message 9 of 11
(12,935 Views)

hi Rashid,

can U post Ur VIs saved in LV 7.1? I think it ll be helpful for me to go thro' it. I think U must be knowing how to save for previous version.

Regards,

Partha.

- Partha ( CLD until Oct 2027 🙂 )
0 Kudos
Message 10 of 11
(12,931 Views)