LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Repeating case structure

This program is supposed to recognize an input voltage of 5V and then proceed to start the vertical bar picture box moving either left or right, per the users choice. The user can also input the desired bar width, scroll speed, and cycle duration (time to get from left to right and back). I want the picture box to move left and right repeately. On frame #3 of the stacked sequence I have made a case stucture that would play the true program if the iteration for the larger for loop is even (or zero) and false if odd. When run, the program will only move left first then right and then stop completely. How do I get the case structure to consistently repeat between alternating the false and true cases?
Thank you!!
0 Kudos
Message 1 of 12
(5,311 Views)

You should consider a state machine.  There are many advantages to a state machine architecture over a simple sequence/case structure. 

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 2 of 12
(5,298 Views)

But to answer your question you can put the case structure in a loop and pass a boolean using a shift register ans inverting the valoe between itterations.

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 3 of 12
(5,296 Views)
Chip,
 
Please consider rethinking your code from scratch, the code is virtually unmanageable and full of race conditions due to overuse of local variables. It will never work as desired in its current form.
 
 
 
Unecessary code: Once you create the image in frame 0, its content never changes. There is no need to carry the wire and rewrite the same data to a local variable of the image in subsequent frames.
 
I would suggest NOT to use formula express VIs for simple operations sich as "divide by two", "2*x1*x2", etc. Since the formula is hidden it is impossible to see what is going on without clicking the express VI. Use plain wires and basic math nodes!
Don't use value property nodes if the actual wire is right there. Just use a plain wire, eliminating the property node (property nodes pose a sever performance penalty).
 
It is also very confusing to have multiple terminals with the same name (e.g. "Trial Duration (s)", etc.). You have four (!) instances of basically the same while loop, where the only difference is one diagram constant. They can all be combined into one and have the diagram constant be a function of the current "state".
 
Race Conditions!!!
As an example, lets have a look at your first FOR loop. Since several code elements are chopped up without any direct data dependency, there is no way to tell what occurs first. (LAbVIEW does NOT execute left to right!). For example, you cannot tell if "Number of Bars Drawn" (A) gets written first or one or two of its two locals (B,C) get read first.  Similarly, you cannot tell if D and E get updated before F and G get read. In a typical iteration, you'll probably get a random mix of stale and current values. There is no way to predict it.
 
The insert shows an alternative implementation. Dataflow exactly determines the sequence of events and no race condition can occur.
 
 
I am sure with very little work your code could be simplified to 10% of its current complexity. At this point we can start troubleshooting the problem you mentioned. Most likely, everything will fall into place. 😄
 
 

Message Edited by altenbach on 06-28-2006 01:09 PM

Message 4 of 12
(5,271 Views)

Thanks for your insightful message.

I am working on fixing the program. In reference to your diagram, where does the wire that leads to the top portion of the multiplication function lead to (as it leads to the left out of the picture). I tried to to connect it to the edge of the sequence frame but the wire returned an error (it needs a source). Thanks!

0 Kudos
Message 5 of 12
(5,223 Views)
Please disregard the previous question, I think I figured it out.
 
I got rid of some of local variables and property nodes.
 
How would I combine the while loops into one, with the diagram constant a function of the current state? (And what is the diagram constant you are referring to?) Combining the loops is probably the best way to simplify the program but I do not know what the most efficient way would be.
Thank you so much for your help!
 
0 Kudos
Message 6 of 12
(5,211 Views)
Hello,
 
I glanced at your newest code, and it still needs a lot of work.  In one place you have a case structure with two cases, and in each case you have nearly the exact same code - a while loop with some computation.  Now, the only differences in the computation seems to be the use of a property node and changing subtraction to addition.  You can use a single loop and put the case structure inside the loop, where you just have the property nodes and your subtraction and addition operations in the appropriate cases.
 
I hope this helps!
 
Best Regards,
 
JLS
Best,
JLS
Sixclear
0 Kudos
Message 7 of 12
(5,187 Views)

Thank you for your help.

Attached it my newest code. I decreased the number of loops from four to two. The program works fine after the first running (I'm not sure why that is). I need to somehow change the left starting position to start at the value where the last loop left off (to make the back and forth motion more fluid). I am not sure how to do this.

Thanks!

0 Kudos
Message 8 of 12
(5,184 Views)
As previous posters indicated a state machine will allow you to take care of this. Simply put the position into a shift register and the previous position is available to the next iteration. It is not clear exactly what you are doing, but I think you might only need one loop if the sequence were replaced by a state machine.

Also beware of doing equality comparisons on non-integers. Slight differences due to representation of the numbers in binary or due to calculation roundoff can cause the comparison to fail. 0.99999999999 is not equal to 1.0. Use In Range and Coerce or >= and <= comparisons, depending upon your needs.

Some comparisons and calculations which never change are still being done repeatedly inside the loops. I think this was pointed out by a previous poster also.

Lynn
0 Kudos
Message 9 of 12
(5,177 Views)

Lynn,

I do not know how to put the position into a shift register.because I need the left position for the first iteration to be a set value of zero. The position in the remaining iterations will depend on the position in the end of the previous iteration.

How would a state machine be different from what I have done?

Thank you!

0 Kudos
Message 10 of 12
(5,135 Views)