LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how do you wire state machine

Solved!
Go to solution

I am trying to learn how to wire state machine.  I created this simple vi.  The intention is take the input, and if it is less than 5, then, the value is squared.  If it is greater than 5, then the value should be cubed.  However, I get only squared values.  What is missing here?  For values greater than 5, I get the value "2" showing up in my squared output box...where does the 2 come from????

Message 1 of 12
(3,457 Views)

There's alot wrong with your attached vi unfortunately.  

Its only giving you squared values because you haven't even wired a cubed value for the false case, you've wired the selector value (case1) for the false? Plus, your selector for the case statement is ALWAYS case1 because you are shifting the same thing back every time.

 

I suggest you read some (more) tutorials to help you from here with your Labview programming.  

 

Anyway, here's a few methods that you could use in the attached vi.  The problem doesn't really lend itself to a state machine, but for illustrative purposes I've put something in to help you...I hope this helps.

 

 

Message 2 of 12
(3,444 Views)

Thanks a lot for the feedback.  You are right, I am struggling to understand the basic architecture of the state machine.  Searching the tutorials has not been successful for me.   I have been searching for a tutorial on state machines, and the only one I can find is one that merely switches from state to state, but, it does not offer any explanation of how you include instructions or executable code for each state.  Have you seen the tutorial about the nickels?  It helps to give a clear idea about how the cases can be selected and moved from one to another, but, it doesn't really explain how you issue instructions from each case. 

 

Thanks again,

Dave

0 Kudos
Message 3 of 12
(3,415 Views)

Start LabVIEW -> VI for Template -> VI -> From Template -> Frameworks -> Design Patterns -> Standard State Machine

 

A new VI will open with NI's standard State Machine Template.  It simple to understand, well commented and ready for your modifications.  Try it out.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 4 of 12
(3,402 Views)

 

BTW:  Concerning what to run in any particular state:  The reason it's not specified in tutorials is because it's totally up to you, the programmer, to decide.  The state machine is just a sequencing architecture.  You can put anything you want in each state depending on your needs.  I suggest you start with a simple message dialog that pops up and says " State **** is running!" (you replace the **** with whatever you named your state).  After that you can move on to states that do more meaningful sequences such as INIT DAQ - CONFIG DAQ - READ DAQ - WRITE FILE -SHUTDOWN - ETC.  Then add more advanced sequence control like looping READ DAQ - WRITE FILE over and over until some condition is met.  You'll be surprised how powerful the SM can be.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 5 of 12
(3,395 Views)
Solution
Accepted by topic author dav2010

NIquist beat me to the punch.  I made this moification an got tied up before I could post it for you.

 

Unfortunatly for you, these examples only show two states which don't help drive home the point of a state machine.  You can have multiple states to process your data or move you around your application.


I've expanded your VI a bit to show a couple things.  There are essentially 3 separate state machines running in the loop.  The bottom one is a simple example of where to put your code depending on what you need to do.  Then you determine which state your data is telling you to go to next, and you could have many states here, not just two.

Message 6 of 12
(3,381 Views)

Thank you very much, Nilquist and Clampy.  I will spend the weekend studying the material you suggested.  I can already see how the state machine architecture is going to help me write a very powerful application.  I just need some help understanding how things are supposed to be wired.  Hopefully, the code you guys wrote for me, and the example in the Labview porgram will be very illustrative.

 

THANKS AGAIN!!!

 

Dave

0 Kudos
Message 7 of 12
(3,357 Views)

Hi Nilquist,

Thanks for the suggestion for the message dialog exercise. It took me a while, but, I think I figured out how to make it work. I created a three_cases.vi that issues messages as you described, "Lower than LOW CASE", "Between LOW and HIGH" and "GREATER THAN HIGH" etc.  based on inputting a value, and choosing the LOW and HIGH values.  Is this structure OK in your eyes?  Would you offer any comments on this one?  I realize that if the user enters a LOW value that is higher than the HIGH value, then, things don't work, so, just ignore that aspect for now.  I am most interested in your thoughts about the state machine and the transition among the states.  What do you think?

 

Dave

Message 8 of 12
(3,327 Views)

Very nice.  You have the general concept of a state machine under control.  I would suggest the next step for you is to work on an SM that "wakes up" every XX number of seconds, performs a process (display a message) and then goes back to the IDLE state to wait again.  This is a very common use for SMs.  I often create test programs that collect data in regular time frames and the SM is the goto architecture. The usual states for this type of App are:  INIT TIMER, IDLE, SOME PROCESS, EXIT.  Play around with the idea.  I have some more advanced SM templates I've posted before that I will share with you but try some things yourself and see how you get along.

 

One problem with your HiLo code:  You didn't put any wait function in your loop so the program is consuming 100% of CPU power.  You always want to add a throttle so put the Wait.vi with a 10mSec delay inside your loop but outside the case structure so it delays the loop each iteration.  There are better ways to control loop timing but a simple Wait.vi is fine for now.

 

 

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 9 of 12
(3,293 Views)

Thank you, Nilquist.  I appreciate the comment about the WAIT function.  I will be aware of that in the future.

 

Thanks again,

David

0 Kudos
Message 10 of 12
(3,289 Views)