07-24-2015 02:50 PM
Hey Everyone-
Be gentle. I'm not a programmer (took a class 18 years ago) and just picked up Labview 2 months ago.
Let me start off by telling you what I'm doing. I have a setup which I have 7 switches. One of these switches is a "kill switch or a Big Red Button (BRB)". Toggling this switch sends the VI into a mode which terminates or closes all other switches and stops the VI. Another switch is acting as a toggle to allow the other switches to be used in manual mode to control certain outputs like valves. A third switch executes an Auto Sequence which I have currently mapped out as a flat sequence inside the case structure. Now I realize there might be more eloquent ways but I have used DAQ assistant to receive my inputs and DAQ assistant to execute my outputs (reference the attached VI).
So I have 4 options on my case structure which is controlled by the output of the switches. Everything works well for the most part with one exception. When I flip the switch to run “Auto Sequence” it works very well and the VI steps through the flat sequence just like I want. My problem is when something happens to the test (bad day scenario) and I need to execute an emergency shutdown by pressing the BRB. As I have studied the problem it makes sense that the BRB didn’t do anything because the VI is waiting for the sequence to be completed before it can flip the case structure to the “stop” case. Anyone have an idea how I can interrupt the flat sequence at any point and stop the VI? I have studied a bit on state machines but I’m really shaky and not sure how it can still solve my specific problem.
I would appreciate any other advice but understand I might have a hard time following you. I need to get this up and running for the short term and I can improve and clean it in the long term. Also note that the VI also has a small while loop all by itself. It is just set up right now as a bit of a place holder. I will be incorporating a VI I setup to record data during the test.
07-24-2015 04:20 PM
A sequence structure must execute each frame, one at a time, and it cannot skip any frames.
You definately should be using a state machine here. In fact, a good state machine can clean up a lot of your code here. The beauty of a state machine is that you can go from one state to any other state based on whatever criteria you want. Therefore, it makes it so you can interrupt your sequence when a TRUE is read on the BRB.
I recommend creating a flow diagram first. This will show you how you should transition from one state to another based on various inputs.
07-24-2015 04:59 PM
Anyone have an idea how I can interrupt the flat sequence at any point and stop the VI?
As CrossRulz said, the sequence is what you asked for, so that's what you get. It will do frame 0 and then frame 1, and then frame 2...
In programming, when you get to a question like this, it's a clue that your design is wrong.
The need to stop (BRB) is not something you can tack on after the fact, it's something you have to consider from the beginning.
Imagine a WHILE loop, with a CASE statement inside it, and the "i" of the while loop tied to the selector of the case, and a TRUE out of the default case wired to the STOP of the WHILE loop:
That is exactly the same as a SEQUENCE. (the only TRUE output in that pic is the DEFAULT case, as shown. Other cases emit a FALSE.).
It will execute frame 0, then frame 1, then frame 2, however many frames you have. When it runs out of frames, it stops.
But look what you can do with it: You can STOP at any frame, simply by wiring a TRUE output of any case.
That's an interruptible sequence.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
07-24-2015 05:06 PM - edited 07-24-2015 05:07 PM
Now, take that picture one step further: What determines which case to execute?
Well, it's the "i" variable. But does it have to be?
Of course not.
What if you used strings instead?
(
(Bad picture-taking - under the menu is a string "RECV".)
You've removed the dependence on "i" and made each case depend on whichever case executed before.
(You have to start somewhere, that's why the INIT is there.
What would you call this? It's sort of a sequence, except that you don't have to know the order ahead of time.
It's called a State Machine.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
07-24-2015 05:13 PM
I wrote an article about this some years ago.State of the Machine. if you care to peruse it.
Basically, here are MY rules about state machines. Not everybody subscribes to these, but they work for me.
A State Machine is a piece of code, with three distinguishing characteristics:
Some people disagree about #3. While it is possible to include WAITING functions in the state machine itself, doing so means that it's hard to use the same code for more than one job at one time.
Some people prefer using strings for the selectors, for flexibility.
Some people (me included) prefer using ENUMS, for speed and typo prevention.
But that's the gist of it.
State machines are not hard.
Hopefully you can see the path from sequence to interruptible sequence to state machine above, and see that it's really pretty simple.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
07-24-2015 05:27 PM
I will take a look at that article when I get home. I'm company appears to not like your website and it is blocked. You have given me a lot to chew on. I have also looked at the other article that was posted about state machines and some others that i have found in doing my own "homework". Am I wrong to think that the meat of my code is in near form of a state machine and my biggest problem is case 6 (Auto Sequence start)? Would I or could I create a machine structure out of that one case? Perhaps I'm way off base here. I will give it some more thought.
I actually have another set of questions but I will chew on them a bit to see if I can get them straight in my head.
Thanks!
07-24-2015 05:53 PM
Scott,
You wouldn't have to create an entirely new machine structure out of your Auto Sequence Start, you can just add it as additional state in your already existing state machine. At the very least you will only transition to this state once but will then be able to check for stop conditions between states. You may also need to go back to one of those states in particular in which case you already have it in place.
I will also make the plug for Mikeporter who has a two part blog post about state machines.
http://www.notatamelion.com/2015/02/23/building-a-proper-labview-state-machine-design-pattern-pt-1/
07-24-2015 06:42 PM
Would I or could I create a machine structure out of that one case?
I haven't looked at your code. I just wanted to point out that the transition from sequence to state machine is not as gigantic as you might think.
Consider each frame of your existing sequence as one state. Move to the interruptible sequence if that helps. It's identical in function, but now you have a way of stopping at any point.
After that, you can see how to move to a state machine.
Blog for (mostly LabVIEW) programmers: Tips And Tricks