LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Having different conditions for loop start and stop

Solved!
Go to solution

I'm making a VI where a circle moves on a picture control and it's movement is decided programatically but also by user input. I programmed the circle's speed as a vector that rotates (using a rotation matrix) when a certain condition is met. This condition is dependent on circle position and direction as well as other input. The tricky thing is that I want that when the rotation condition is met (true), the rotation continues until the angle of the vector is equal to original angle (when the condition became true) +/- variable x.

 

So my question is how can I program a piece of code that once activated, will run until a stop condition (dependent on vector state at the begining of the run) is reached. Since this needs to happen while the VI is running, I can't put a while loop inside another while loop. The online help also mentions conditional for loops, but this is not available in my version of LV (8.2)

0 Kudos
Message 1 of 9
(4,506 Views)

@julmich wrote:

I can't put a while loop inside another while loop. 


Why not?

 

loops.png

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 2 of 9
(4,475 Views)
Solution
Accepted by julmich

The correct solution is a state machine with a case structure inside a single while loop. Based on the need use one of the various cases and switch to a different case as needed using a state variable (e.g. an enum) in a shift register. One of the cases should be the idle case and not do much except maybe poll controls.

 

There are plenty of templates.

 

Feel free to attach a simplified example of your code and will help you.

Message 3 of 9
(4,460 Views)

Because both while loop need to be running simultaneously. The outer one contains the code to draw my circle on the picture control and handles user input. If I use a while loop for vector rotation, it will perform the vector rotation without drawing the iterations of the circle or checking for user input.

0 Kudos
Message 4 of 9
(4,427 Views)

Please re-read Altenbach's message.  Right now your while loops do not run "simultaneously".  One is nested inside the other so it will run to completion, then only then does the outer one iterate again, and the inner one  begins again.

 

As Altenbach said a state machine can swap between a state or case that checks the controls, then the case that draws them.  HIstory of all past iterations can be stored in a shift register.

Message 5 of 9
(4,407 Views)

Thanks altenbach. I didn't know about state machines, so it's a great opportunity to learn about and try to make one. I'm looking up tutorials right now and this looks like what I need so I'll put your post as the solution.

0 Kudos
Message 6 of 9
(4,400 Views)

Good luck!  In all cases, a single flat loop for the user interface code is sufficient. Stacking intereactive loops just complicates things, because whenever you climb down into a series of stacked loops, you need to somehow climb out again until the outer loops are allowed to continue again.

 

There is only one user interface, so it should not rely on deeply stacked layers of loops because they force ordering of user interactions. Notice that in the code given above, pressing "STOP" will have no visible effect unless "START" has been pressed. If you run the program and decide not to start the process, there is no way to end the program. If you press stop first, pressing start will actually stop. Try to explan all that in the user manual for your software! 😄

 

Inner loops are perfeclty fine, but they are typically not interactive and complete in finite time on their own. Examples would be a Newton-Raphson implementation to numerically find roots of a function, but there are an infinite number of uses.

0 Kudos
Message 7 of 9
(4,373 Views)

Hi altenbach,

 

I'm still designing my state machine. One difficulty I have is that I need to take the angle value (a) of my vector when I enter a new state (a0). I use a0 to calculate the exit angle (a') which would be the condition to return to default state. ex : if a' = a0-x then switch state to default

 

a is constantly changing as the VI runs, so I don't know how to get a0 when a state switch happens.

0 Kudos
Message 8 of 9
(4,352 Views)

@julmich wrote:

Hi altenbach,

 

I'm still designing my state machine. One difficulty I have is that I need to take the angle value (a) of my vector when I enter a new state (a0). I use a0 to calculate the exit angle (a') which would be the condition to return to default state. a is constantly changing as the VI runs, so I don't know how to get a0 when a state switch happens.


Use another shift register to hold your a0.  You only write the updated value to it when you detect that the state needs to change.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 9 of 9
(4,347 Views)