From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Loop counter in state machine

Solved!
Go to solution

Hi all,

 

I have a state machine, and one particular state is pretty busy.  I want to loop that state a few times.  I thought of the following, but I don't like them.  Let me know if you have a better idea.

 

1. Add a shift register as a counter for the state.  The state will keep looping to itself, while decrementing the counter.  If the counter is zero, it will go to the next state.  If not, it will keep looping.  I don't like this, because I will have to add a shift register.  If I have a few more state that need different number of iteration, I will have to add a counter for such state.  My main VI will have way too many shift registers.

 

2. Add a for loop around the code in my case for iteration.  I don't like it, since too many wires will be going in and out of the loop.  I don't want to make cluster either.

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 8
(4,837 Views)
Try a feedback node instead of the Shift Register, nicely self contained.
0 Kudos
Message 2 of 8
(4,835 Views)

One feedback node per counter that I need?

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 3 of 8
(4,820 Views)
Solution
Accepted by topic author jyang72211

jyang72211 wrote:

One feedback node per counter that I need?

 

Yik


Precisely.  See this example, each state loops 5 times before moving on.  Beats having 5 shift registers and all of those wires passing through the other cases.

 

 

Message 4 of 8
(4,814 Views)
I didn't mean for all of the states to be the same, forgot to save on my way out the door.  I wanted to show you two uses, the iteration counter (perform state n times) and the total counter (how many times a state has been called).  If all you are doing is iterating n times, you can probably get away with a single SR.  I still like FN in most cases, especially when I need to save some data in a single case of a large state machine.
0 Kudos
Message 5 of 8
(4,805 Views)

Yik,

 

this depends a lot on the question:

Do you want to split the state into several iterations because you want to have a chance to interact inbetween?

If yes, the logic to determine the next state has to take into account:

Iterations finished? || Stop Iterations?

 

If the Iterations finished?-criteria depends on pure "amount of iterations", a loopcounter using a shift register or feedback node should work out. If you do not plan to "resume the iterations anytime", a single shiftregister/feedback node will work for any amount of split states.

If i would have to implement something like this, i would consider to "hide" that counting mechanism in an action engine. This will remove SR/FN from the state machine iteself but will provide the desired functionality.

 

If the Iterations finished?- criteria depends on some other stuff....well, it could get arbitrarily complicated (is this really an english expression???). But even in this case, moving the logic into an action engine could be a good idea....

 

hope this helps,

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 6 of 8
(4,790 Views)

Hi Darin,

 

I really like the counter that you attached.  I like the fact that it reset itself when it is done.  I also learned an interesting fact about the FB node from you code.  It seems like when it is uninitialized, the initial value is assumed to be zero?  I am right? 

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 7 of 8
(4,768 Views)

Hey jyang,

 

The default initialized value of a feedback node is 0. However, you can change this by initializing the feedback node yourself. To do this, just wire a constant to the Initialization Terminal of the feedback node. This is the node  coming out of the "DOT" on the feedback node.

 

 

 Cheers

 

Logan

 

National Instruments
Applications Engineer
Message 8 of 8
(4,755 Views)