LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

creating structures: state machine with controls

Solved!
Go to solution

Hello, I am wondering if someone can help me with something. I have a sequence structure, followed by event/while loop.

can someone help me. I want to create  A STATE MACHINE.

here is a basic diagram of the current structure i have. and here is what i want it to be. i wonder if its possible and how. 

i am using labview 7.0.

thanks

 

 

 

 

this is what i want?

 

 

Message Edited by krispiekream on 10-30-2008 04:01 PM
Best regards,
Krispiekream
Download All
0 Kudos
Message 1 of 17
(5,099 Views)

Is there going to be any kind of data passing between the event structure and the while loop?

How many events is the event structure going to handle? If you want the event structure to run continuously it must be inside a while loop. 

Message 2 of 17
(5,083 Views)

also, there is no data being passed from the event structure and the while loop.

 

yeah. sorry, i didnt see that. but yeah. i have two while loop running at the same time.

one has the event, and one does not. also, i need to handle it in a way where my controls needs to be taken care of too.

like if i PRESS ABORT. or whatever..

i am trying to run my wires instead of using local variables. 

i hope that doesnt confuses anyone.

thanks

 

Message Edited by krispiekream on 10-30-2008 05:00 PM
Best regards,
Krispiekream
0 Kudos
Message 3 of 17
(5,073 Views)

my previous program only uses stacked sequence and local variables.

i want to get rid of them using wires by bundle and unbundle. 

but i would also like to change it to STATE MACHINE.

 

 

Message Edited by krispiekream on 10-30-2008 05:12 PM
Best regards,
Krispiekream
0 Kudos
Message 4 of 17
(5,064 Views)

Are the cluster elements modified in both the loops? What function is the second while loop (with the stop 2 button) performing?

Can you post a copy of your old code using the stacked sequence?

Message 5 of 17
(5,055 Views)

take a look at this post. i finally get a chance to go back to this program and work on it

 

http://forums.ni.com/ni/board/message?board.id=170&thread.id=346811&view=by_date_ascending&page=3

if possible. i dont have labview 8.5 on my computer, can someone save it in previous version 7.0?

 

 

here is what i currently have again.

 

Message Edited by krispiekream on 10-30-2008 05:48 PM
Best regards,
Krispiekream
0 Kudos
Message 6 of 17
(5,054 Views)
Solution
Accepted by topic author krispiekream

I looked at your Executive VI.  I do not have any easy way to save back to LV 7 now so I am going to attempt to describe in words how I would approach this mess.

 

I think it can be done with two loops, one event structure, one case structure, and no local or global variables.  Actually there will probably be a few small case structures and for loops inside some of the case, but nothing like the ~12 layers of nesting you are now fighting.

 

Look at the Producer/Consumer (events) design pattern as the starting point.

 

The Producer loop (event loop) will have the event structure.  It will have a case for every control which requires immediate action.  The ones I could identify are: Get Telemetry, Print Front Page, Open Log, Abort, Press Char Mode(?), Temp Char Mode(?), Scan, Stop, End Wait, Single Step(?), Continue, and Execute. Most of the buttons should be set to Latching mechanical action and the terminals placed inside the appropriate event case. Then no locals are needed to reset them.  The ones with (?) may not need event cases.  You may want to have a timeout case to display your Date and Time, eliminating that (painful to my eyes) loop.  I am not sure about the other stuff in the timeout case, but most likely it belongs in the other loop.  If the Chamber 1, 2, .. buttons change during runs, they may need event cases also.  Inside each event case is exactly one item: the Enqueue function.  Create a Command enum and make it a typedef. Enqueue the value of that enum. The queue will be read in the Consumer loop where all action occurs.

 

The consumer loop is your state machine. It has a Dequeue function to get a new command from the event structure.  It has several shift registers to pass data from one case to another.  These eliminate your global and local variables.  I would have shift registers for each of the following: The State (typedef enum), error cluster, Temperature (array), Pressure (array), Results (array), Stat Results (array of clusters), and a big Indicators_Flags cluster (typedef).  The Indicators_Flags cluster would have everything which is in the cluster you initialize in the black frame sequence (and then never use again) plus all indicators Except the arrays in separate shift registers plus all your globals and any status flags you might need.

 

The case structure is the heart of the state machine.  All the shift registers pass through it.  It will have cases for everything the program does.  Everything in that black Initialization frame goes into about 15 Init cases.  The while loop at the right of the initialization frame goes away but the cases inside become some of the cases of the state machine.  The Temperature Cycling frame code goes into 1-3 cases. The Pump mode code and Pressure Read from your event timeout case move to 1-3 cases.  Telemetry code: 2 cases.  Print Front Page: 1 case.  Excel code.  Abort code.  Temperature and pressure cycling code from main program: 5-20 cases.  Graphing, saving to file: more cases.  Shutdown: 1-2 cases.

 

I would make a list on paper of all the cases you think you may need and use that to create a typedef enum to control the cases.  By making it a typedef, it is easy to change without chasing all over the code looking for the constant in every case to change.  The enum names will show up on the case structure like strings.  Create the loop, the shift registers, and the case structure.  Wire the shift registers through both of the original cases then duplicate the case until you have a case for each value of the state enum.  (Do not use the Make case for every value option because that will make empty cases that you will then need to wire through.)

 

Then copy code from the various places in your existing code to the appropriate cases in the new VI.  Try to avoid copying locals as that will create new controls.  Use unbundle by name and bundle by name to get data into and out of the big shift register cluster.  This provides self documentation of the data being used.

 

Check back in a month when you have other problems. This will not be a quick fix.  But it will be worth it to have a program which works and is maintainable.

 

Lynn 

Message 7 of 17
(5,010 Views)

thank you johnsold for taking the time to go over my code and giving me advices on fixing this. sounds complicated. i guess i'll try my best to do this because i really want to clean up the mess i have right now.

i'll be back if i have more problems. 

Best regards,
Krispiekream
0 Kudos
Message 8 of 17
(4,997 Views)

Your existing program is complicated.  It is easier to learn how to do things like state machines and parallel loops on smaller, simpler programs, but it appears that you really do not have that  option.

 

I suggested creating a new program and then copying and pasting pieces of the old one into it rather than modifying the original for two reasons:

 

1. The old one stays around to look at.  If something does not work in the new program you can go back to the original to see if it really works the way you thought it did.

 

2. As soon as you start changing things in the original program,  everything will break and it will be quite difficult to determine what should be changed next or why things are broken.

 

I have converted a few programs like this (including some of my own early programs) and have found that a re-write via a new program is almost always better than trying to "fix" the old one.

 

You have learned a lot and made some good progress since you first posted about this.  Please ask again when you get stuck on particular parts of your project.

 

Lynn 

Message 9 of 17
(4,961 Views)

thanks again johnsold! my idea right now is to replaced all stacked sequence with flat, and slowly gets rid of the local variables.

once i got the cold to still do whatever it used to do, then i'll go for the producer/consumer structure.. but it seems like a complicated task without breaking anything. but i will try becausei got nothing else to do anyways. might as well learn to program the right way. but thank you for your help. everyone here make learning LABVIEW FUN. i'll be back with specific question when i get stuck. thanks again!

Best regards,
Krispiekream
0 Kudos
Message 10 of 17
(4,940 Views)