LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Random Sample not updated

Solved!
Go to solution

I am working on a project has a VI similar to the attached one.

 

I have a “While loop” generates a random sample (k) every 500ms to function A and B. Function A does the following operation:

c=k+10

and function B does:

d=c+k

 

Currently, function B waits until function A completion to execute. However, Function B does not use the updated value of k -it uses the same one A uses. Is there a way to make Function B uses an updated sample of k different than the one Function A uses?

0 Kudos
Message 1 of 21
(3,638 Views)

Hi,

 

right now your VI does exactly what you describe. But it is to me not clear what you want it to do. What do you mean by "updated" sample?

 

Do you want another Sample e.g. "h"? Then just create a second random number and wire it to the second formula. Function B does then d=c+h.

Do you want the Sample from the previous loop iteration? Then store your value of "k" in a shift register and wire it to the second formula. Function B does then d=c+h[i-1]. Do not forget a reasonable initial value for the shift register.

ʍolɟɐʇɐp ʞuıɥʇ
0 Kudos
Message 2 of 21
(3,617 Views)

 I think the problem is that you (and your colleague whose code you posted) don't understand The Principle of Data Flow, a central concept of LabVIEW, that governs ideas about sequential or parallel processing, the time sequence of operations, and similar concepts.  The idea is if you have a Function or a Structure with inputs and outputs (like an Addition function or a While Loop), no processing occurs until all of the Inputs have data, and no data appears on the output(s) until all of the processing have finished.

 

This is the computation shown in your example, rewritten in LabVIEW (no need for those silly MathScript nodes, get rid of them!):

Simple FormulasSimple Formulas

Every 300 ms, a random number between 0 and 1 is added to a Numeric Control, "Number", giving "k", then Function A adds 10 to k to give "c", Function B adds (the same) k to c to give "d", the final value being displayed.  These computations take essentially no time, but happen sequentially (since the data flow in wires, and the Principle of Data Flow means that the last addition cannot start until the preceding ones finish and provide data on the input wires to the final additional), so the values displayed in d will update every 300 ms.

 

Inside this loop, there is no "updated" value of k.  k doesn't get updated until the loop repeats.  If you want to generate a new value of k (call it k-prime) for Function B, guess what? -- you have to generate a new value of k for Function B, which requires a second random number generation and addition to the input "Number".  That would be a (slightly) different program, left as an Exercise for the Reader (hint!).

 

Bob Schor

0 Kudos
Message 3 of 21
(3,606 Views)

@Hamdi30 wrote:

I am working on a project has a VI similar to the attached one.


  • Next time attach the actual VI, not something "similar". If they are not the same, you need to spell out all the differences. (Some differences I can see (1) No mention of the "Numeric 3" control, (2) 300ms instead of 500ms, (3) k is invariant during each iteration, (4), etc.)
  • There is probably no need to fire up Mathscript to add a few numbers, right?

 


@Hamdi30 wrote:

However, Function B does not use the updated value of k -it uses the same one A uses.


You are branching the k wire, so each branch gets the same value. If this surprises you, start with a few LabVIEW tutorials and learn about the magic of dataflow. 😄

 

 

0 Kudos
Message 4 of 21
(3,590 Views)

Thanks for your feedback. Please refer to the attached diagram that I am trying to implement in LabVIEW. 

0 Kudos
Message 5 of 21
(3,578 Views)

You can embed images to your post, no need to zip it. Don't use disallowed formats such as tiff, though.

 

diag.png

 

What does the code represent? What is "k"? What is "real time data"?

 

Code cannot look into the future, so k+1, k+5, etc. are typically not known yet. Maybe you want to rewrite it in terms of "k", "k-1", .... "k-5" Shift registers and feedback nodes allow you to look at historic values if done right. See how far you get.

0 Kudos
Message 6 of 21
(3,561 Views)

I don't think you "get it".  I'm going to make some assumptions (that make sense to me):

  • When you say "real time data X" and "X(k+5) ... X(k+1, X(k)", the k's are indexes into an array X
  • Thus if k=0, the above become X(5), ... X(1), X(0).
  • What is "strange" (to me) is that you seem to be talking about "data in the future", especially if we consider that k represents "now", the "k"urrent point.
  • LabVIEW is pretty good about dealing with "recent" data (one, two, three loop cycles ago, see Shift Registers), but does not (yet) allow us to work with future data.

So I'm going to restate Functions A and B, and see if this is what you want:

  • I'm going to use "i" as the "index" variable, and will assume X(i) is the latest point acquired, so we have a stream X(i), X(i-1), ... X(i-5).
  • Function A, Y(i) = X(i) + 8 (I think you have a Typo in your Function A).
  • Function B, Z(i) = X(i) + Y(i-5) = X(i) + X(i-5) + 8.

Note that if we don't need to use Y except to compute Z, we don't really even need to save it.  You can do this computation in a single step with a While Loop having a Shift Register with 5 "left edges" (look up Shift Registers and how they work if you don't understand what I'm saying).  You'll (of course) need to decide how to initialize the Shift Register to set values for X before you start running the loop.

 

Bob Schor

0 Kudos
Message 7 of 21
(3,554 Views)

@Bob_Schor wrote:

I don't think you "get it".  I'm going to make some assumptions (that make sense to me):

  • When you say "real time data X" and "X(k+5) ... X(k+1, X(k)", the k's are indexes into an array X
  • Thus if k=0, the above become X(5), ... X(1), X(0).
  • What is "strange" (to me) is that you seem to be talking about "data in the future", especially if we consider that k represents "now", the "k"urrent point.
  • LabVIEW is pretty good about dealing with "recent" data (one, two, three loop cycles ago, see Shift Registers), but does not (yet) allow us to work with future data.

So I'm going to restate Functions A and B, and see if this is what you want:

  • I'm going to use "i" as the "index" variable, and will assume X(i) is the latest point acquired, so we have a stream X(i), X(i-1), ... X(i-5).
  • Function A, Y(i) = X(i) + 8 (I think you have a Typo in your Function A).
  • Function B, Z(i) = X(i) + Y(i-5) = X(i) + X(i-5) + 8.

Note that if we don't need to use Y except to compute Z, we don't really even need to save it.  You can do this computation in a single step with a While Loop having a Shift Register with 5 "left edges" (look up Shift Registers and how they work if you don't understand what I'm saying).  You'll (of course) need to decide how to initialize the Shift Register to set values for X before you start running the loop.

 

Bob Schor



Bob Schor: thanks for your feedback again. 

I am talking about date in future but not how you understood it. What I meant by y(k+5) is that if function A uses sample y(k) (ie the sample at time k), I need Function B skips five sample or delayed for 5 time-unit, and then processes the the new sample “y(k+5)”. In other words, i need the two functions running in different sample rate where FunB still depends on FunA and both use the same source of the real time data.

FYI, I have to use the two functions because my my calculation is very complicated (includes some matrix multiplication and inversion ) than what I am presenting here. I am trying to make it simple as much as possible. 

I was able to do that in matlab simulink but it looks labview is more complicated plus I have a little information.

Hope that makes sense. 

Your feedback is highly appreciated 

0 Kudos
Message 8 of 21
(3,546 Views)

I regret to say it doesn't make sense (to me).  I wonder if a picture (I have to use small type because I complain all the time about posting pictures) of your Simulink routine will clarify things for us me ...

 

Bob Schor 

0 Kudos
Message 9 of 21
(3,543 Views)

 

“Inside this loop, there is no "updated" value of k.  k doesn't get updated until the loop repeats.  If you want to generate a new value of k (call it k-prime) for Function B, guess what? -- you have to generate a new value of k for Function B, which requires a second random number generation and addition to the input "Number.”

 

Given you example, I need k to be updated to k-prime for function B before the loop repeats, and using the same random number generator. Hope this makes clear what I am looking for. Thanks. 

 

0 Kudos
Message 10 of 21
(3,530 Views)