LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Programming

Be careful duplicating code (your state machine) in multiple places. Create a subVI, if you must.

My preference is to use a typedef'd enum for the state. Then if you change or add a state name, all the constants will be updated at once.

Enums can be turned into a string with "Format Into String".

 

The attached VI runs through all states. You may need to relink the typedef'd ctl after downloading.

Download All
Message 11 of 18
(890 Views)

Thanks Todd. Grateful. Couldnt hold back my first Kudos on labview. It is well deserved.

 

LIke I was saying can I use two case structures in the while loop of a STATE MACHINE. Reason being that for me to run the sequence of state I describe, I have 3frequencies as DECIDERS. If my frequency falls in a range(4-8Hz) the sequence runs, if it falls within another range (15-50Hz) the sequence runs and if it falls within last range (25-30Hz) the sequence runs as well. A case of a case in a case structure.

0 Kudos
Message 12 of 18
(879 Views)

You don't need the two case structures. The outer code can be a state itself. The state would be select frequency. From what I can tell your internal "sequences" are all the same code simply copied into each "Frequency" state. Use some shift registers to contain information, such as the selected frequency, to pass through the other states as you process the code. Your "sequence" then simply becomes and execution of the other states. For example, let's say the first thing you need to do when you start is to select the frequency. (I'm using simplified names for teh states for the example) you state machine would look something like this:

 

Select Frequency->

Initialize->

Calibrate->

Move motor forward->

Check position->

Move motor forward->

Check position->

Take measurement->

Move motor backward->

Take measurement->

Complete task->

Select Frequency-> ....

 

Each state would contain the necessary logic to chose the next state. At teh appropriate state you would chose to either continue or end. You can invoke the states as many times as necessary. Having multiple copies of the same code in your program is a recipe for diaster. If you need to make a change you have to remember to change ALL of teh copies. If you forget one you have intriduced a bug. Using subVIs is one to prevent this. Properly using a state machine is another.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 13 of 18
(867 Views)

Thanks Mark,

I had to sit and write a code and test it to see it working before replying. After going through your advice I came out with this vi with the first part serving the purpose of acquisition of the signal while the other side is for the states.

My problem is how to trigger the state to happen for example looking at the first case structure. When the case is at 0 and the inner state is 1-Move motor. I already communicated with my motor using the vi inside the inner case, but please how do I triger it to happen.

 

Mark I couldn't do the one case structure thing, kindly assist if you can on this. I am attaching my vi. Its working well with the acquisition and the transition of the states but my problem now is how to trigger the states in the inner case structure to happen.

Thanks.

0 Kudos
Message 14 of 18
(838 Views)

After modifying the vi based on Mark suggestion, Attached is the modified (vi). I am afraid that to configure this vi to suit the purpose of my work might be difficult. The reason why I am separating the frequency is that though as Mark as rightly stated the sequence for the 3 frequency is same but the action are different. For example  if the frequency is between 4-8 or 15-20 or the last one 25-30Hz, thought the sequence will begin with initialize and stopped at end in all cases, but in between like "Move the Motor"; the motor will be moving to different positions and that is why I said maybe I need a case in a case structure. Because though the sequence is same but the actions in between are different for the 3 different cases of frequency.

Please kindly offer your advice if you feel I still don't need a case in a case structure?

0 Kudos
Message 15 of 18
(833 Views)

I think you are still missing the point on what a state machine is and how to use it. It the code you attached in your last post you read the data outside of the state machine. this should be a state within your state machine. As for duplicating the motor movement simply because you are moving it diffierent amounts is the wrong way to approach that. Create a state to move the motor and pass data to the sate to determine how much to move it. The mechanics of moving the motor are the same. The only thing that is changing is how much. This is a perfect case for a common state (or even a subVI) that requires an input to specify the distance/position to move the motor.

 

I would really recommend that you put th ecode aside for a bit and clearly define the states and teh transition between them.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 16 of 18
(814 Views)

Hi Mark,

I would be happy if you can pleaeeeeeeese show me the way. I sat with all examples I could get and attached is the vi I made. My issues are

1) Between the "Calibrate motor" state and "move motor state", how can I select the appropriate frequency which will then determine the course of the rest of actions to take. Like you said it is not that my remaining sequence after the selection of frequency is different it is just that they move to different values. Please show me the way......

2)If frequency comes in in the calibrate motor states that is not within the range I have specified, I find it add to take the system back to default case.

 

Sincerely I did sat to see all examples on case structure but the path I seek is hidden from me, please show me the way.Smiley Wink

0 Kudos
Message 17 of 18
(790 Views)

Hi Mark,

I would be happy if you can pleaeeeeeeese show me the way. I sat with all examples I could get and attached is the vi I made. My issues are

1) Between the "Calibrate motor" state and "move motor state", how can I select the appropriate frequency which will then determine the course of the rest of actions to take. Like you said it is not that my remaining sequence after the selection of frequency is different it is just that they move to different values. Please show me the way......

2)If frequency comes in in the calibrate motor states that is not within the range I have specified, I find it add to take the system back to default case.

 

Sincerely I did sat to see all examples on case structure but the path I seek is hidden from me, please show me the way.:smileywink:

0 Kudos
Message 18 of 18
(789 Views)