LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to call elements of an array at a specified frequency?

Solved!
Go to solution

I'm wondering if anyone could figure out how to solve it.

 

As seen below, I want to call elements of an array at a frequency of 1s. It could be done if the indicator is in the loop, but I need to input the element to another structure, and the data doesn't arrive at the outside indicator until the loop is completely done. So I try to use local variable, which I find also won't work until the loop is done. Is there any method to fix this problem?

 

I apologize in advance if my explanations are not clear enough.

 

Thanks!!!

Capture.PNG

0 Kudos
Message 1 of 6
(3,196 Views)

Your numeric indicator IS getting a value from the array at each iteration of the timed While Loop.

Your numeric2 indicator will get a value a single time, and whether it is the value in Numeric from when the program starts, or after some iteration of the while loop, who knows because you have a race condition.

 

You seem to not understand the principle of dataflow in LabVIEW.  I would recommend you learn more about LabVIEW from here. How to Learn LV

Message 2 of 6
(3,174 Views)

Thanks for your advice!

 

I have browsed more about dataflow, but haven't figure out how to realize the function.Smiley Sad

As seen below, the numeric control can input data to the Bundle by Name function. Now I want to input elements of an array at a specified frequency to Bundle by Name. I plan to use a loop to input the element and set the event to the value change of the element. But I couldn't place the loop in this event structure because this will block the structure from moving to the next case, and I need to process the element (and start motion) one by one. So maybe I should consider dataflow from outside.

Is there any method to input elements of an array at a specified frequency to Bundle by Name?Smiley Embarassed

 

I apologize for my explanations in advance.

Thanks a lot!!! 

Capture.PNG

0 Kudos
Message 3 of 6
(3,154 Views)
Solution
Accepted by topic author Kate97

I agree with RavensFan that you really need to learn (a lot) more about LabVIEW, particularly the Principle of Data Flow, before you are ready to tackle a problem by yourself such as the one you posed.

 

Here's the basic problem -- you have a means of generating data at the rate of one per second, and now you want to (somewhere else) use it at the same rate, i.e. use it as it is produced.  So what structure would you think would do that?  Why, a loop, since you want to do the same thing "as often as requested".  Where should this loop live?  It should be obvious that it has to live outside the Timed Loop.  So your basic task is going to have two loops, one a Timed Loop (or the equivalent) producing data at one per second, and the other consuming this data as it is produced.

 

This is called a Producer/Consumer Design Pattern.  I'm going to "skip ahead" several chapters and show you a nifty way to do this that you won't find in the Introduction to LabVIEW Tutorials, as it uses a new feature that made its Debut in LabVIEW 2016.

 

So here are two loops -- a Loop on the left that produces data from an Array, and a Loop on the right that could do something, but doesn't, yet.

Stream minus Channels.png

The "usual" way to send data between parallel loops, particularly with the Producer/Consumer pattern, is through a Queue, but we are going to use something newer, and (I think) more intuitive.  LabVIEW 2016 introduced Asynchronous Channel Wires, a Data Path that is "Asynchronous" (meaning, among other things, it can "jump over" the boundaries of Structures and appear to violate the Principle of Data Flow).  If I right-click on the Indexing Tunnel in the Producer Loop and choose "Create" from the Dropdown menu, and then choose Channel Writer, and choose Stream Write, it will create the beginning of a Stream Channel, which takes data presented to its input and "streams" it asychronously (meaning "right now") to a corresponding Channel Reader attached to the other end of the Channel.  Notice the Channel does not go through the "walls" of the While Loop, it goes over the top of the wall.  Now right-click the Channel in the Consumer Loop, choose Create Channel Reader, and wire the output to your Indicator.

 

But, Wait, There's More!  The Producer Loop will stop when all of the Array Elements have been processed (we are using a While Loop, so we need to "count" the elements as we generate them).  What stops the Consumer Loop, particularly if we don't know how many elements are coming?  A very nice addition to the Stream Channel is the ability to designate a "Last Element?" input which will also be transmitted and can be used to shut down the Channel and stop the Consumer.

 

Here is the final product:

Use Once per Second.png

 

Bob "Channel" Schor

 

P.S. -- for Extra Credit -- What Can Go Wrong with this design?  Under what (not uncommon) circumstance will it "hang" and never exit?  How can you prevent this from happening?  Hint -- when dealing with Data, always consider the "end" cases ...

Message 4 of 6
(3,147 Views)

Thank you for your reply! I have to admit that you are 100% right. I will learn more about the Principle of Data Flow. Besides, Asynchronous Channel Wires is really a new thing for me, and I will find more examples to understand it.Smiley Happy

0 Kudos
Message 5 of 6
(3,130 Views)

You can try using producer consumer loop.

Producer_Consumer_20120919143003.png


CLD Using LabVIEW since 2013
0 Kudos
Message 6 of 6
(3,120 Views)