LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

StateMachine in SubVI, Restart shift registers

Solved!
Go to solution

Hello,

 

I am having a problem with a reentrant SubVI which implements a State Machine. The SubVI is only called once in the code, so problems with reentrant/not reentrant execution is not the problem.

 

The SubVI implements a State Machine which output is a position reference for a motor drive. The State Machine is implemented with while loop with shift registers. The subVI is called once every control cycle (cycle time being about some miliseconds), so the State Machine while loop is executed only once every control cycle (the stop terminal is connected to "true"). And the shift registers are used to save data between executions.

 

When I open the principal VI it works perfectly. When I stop the VI and restart it, the subVI starts using the last values it had from the previous principal VI execution. I want my position reference to restart from 0. I suppose that my problem is that I should restart the SubVIs shift registers.

 

Is there a way of doing this without adding additional code? (Maybe an invoke node fucntionality for the subvi or something like this??)

 

Thank you for your help in advance,

Best regards,

 

Jesus Pestana

 

0 Kudos
Message 1 of 7
(3,219 Views)
Solution
Accepted by topic author jespestana

Hi jespestana,

i recommend to add an initialization case.

 

Mike

0 Kudos
Message 2 of 7
(3,213 Views)

As Mike said, an initialization case should be part of your state machine.

 

I do not understand how you are operating this VI.  You say it is only called once in main program, yet you say the loop stops after the first iteration.

 

Is there some reason that you do not implement the state machine directly in the control cycle loop?  Perhaps you can just remove the while loop from your state machine subVI.  Pease post some code or images.

 

Lynn

0 Kudos
Message 3 of 7
(3,204 Views)

Hello,

 

In fact I added the initialization state using the "First Call" function (Programming>>Shyncronization>>First Call?) to call the initialization code of the State Machine. It has worked really well, thank you.

 

Best regards,

 

Jesus Pestana

0 Kudos
Message 4 of 7
(3,195 Views)

Hello Lynn,

 

What I mean about the "not reentrant problem" is that the subVI is placed only once in my principal VI code (so that LabView only works with one instance of the reentrant subVI).

 

I would not like to put all the subVI code in my principal VI, just beacuse the subVI makes the code more clear.

 

My problem was that my shift registers where not working quite as I expected..

 

Thank you for your post,

 

Jesus Pestana

0 Kudos
Message 5 of 7
(3,190 Views)

You can certainly put the "working code" of the state machine in a subVI.  I would not put a loop inside the subVI.  Just use the outer loop in the main VI.  Often a state machine has the while loop and the case structure in the main VI and within each case of the state machine is a subVI which performs the work of that state.  This keeps diagrams small and the subVIs are logically coherent, meaning that each subVI does just one thing.  A state machine implemented this way is quite easy to understand, to maintain, and to modify when needed.

 

Lynn

Message 6 of 7
(3,184 Views)

Ok. I think I will also try that.. It is not much work to remove the while loop anyway..

 

Thanks,

 

Jesus Pestana

0 Kudos
Message 7 of 7
(3,179 Views)