LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Generating single random number with a boolean signal on a RT Target

Solved!
Go to solution

Hello, this is my first time posting a question. I apologize if I'm posting this question in the wrong board.

 

[What I'm trying to do]

I'm testing a heating and cooling equipment in an experimental space that I have (with cRio9024).

And I'm trying to set a different experiemental condition in the space based on "random number" function.

"Different experimental condition" meaning different combinations of Tind, Twat_s and mdot (refer to the attached VI).

 

For example, if I get boolean TRUE signal,

I should have a set of [Tind,Twat_s,mdot] = [21,14,0.05], and REMAIN this set of numbers until this boolean signal changes to FALSE.

And when the signal changes to TRUE again,

I should have different set of [Tind,Twat_s,mdot] = [24,16,0.15] as an example.

 

[Where I'm at]

Problem that I'm having with my code is that,

when the VI is running, the random number does NOT remain the same while the boolean signal is TRUE. 

 

Based on what I've searched so far for the past couple days, using "event structure" was the most relevant post that I've found,

however, LabView help tells me that "event structure" on RT targets do not support events associated with user interface such as VI front panels or controls. 

 

So I'd like to ask for an quick help, since my knowledge in LabView has been exhausted at this point.

 

Case.jpg

 

so this is the part where I want to generate random numbers, WHEN this case structure is TRUE.

convert random numbers into integers,

use them as an array indices,

so that I can pick radom conditions from the given set of different conditions.

 

Once again, what I want to do is,

generate SINGLE set of random numbers when the above case structure is TRUE.

 

Hope I've elaborated clear enough.

I'll appreciated any kind of advice.

Thank you! 

0 Kudos
Message 1 of 14
(5,545 Views)
Solution
Accepted by topic author geekshock

You just need to store the boolean value from the previous run. Then the logic would be (I'll call previous boolean B_prev, and current boolean B) :

 

if B AND B_prev >> don't  generate new values

else if B >> do generate new values

 

Edit: to store your boolean value you can use a feedback node, or a for loop with N=1 and a shift register.

Message 2 of 14
(5,541 Views)

Let me rephrase your requirement.  You have a Boolean signal, and you want to do something (generate random values) when the Boolean goes from False to True.

 

That's easy in LabVIEW, once you realize the nature of your condition -- you need to know the current value of the Boolean (e.g. True) and the value just before this one (e.g. False).

 

Do you know about Shift Registers and While Loops?  Normally, a While Loop is used to do something over-and-over again, but there's a special use of a While Loop to be a sort of "memory" (I argued that there should be yet-another structure for this, precisely to make this special use stand out, but the LabVIEW Gurus turned it down).  Here's what it could look like:

Do Once.png

Notice I've wired True to the Stop indicator, so this is a "Do Once" loop.  What does it do?  Well, not much, except that the Boolean value on the input is wired to the output Shift Register, which means that the next time this is called, the input Shift Register will hold the previous value of Boolean.  So now you can (by using Boolean functions) develop a value that is True only if the current value of Boolean is True and the previous value was False -- wire that to a Case Statement and put the computations you want to do in the True case, leaving False empty.

 

Bob Schor

 

Message 3 of 14
(5,536 Views)

Thank you for the comment, gregoryj. But I don't think I'm understanding your solution fully enough.

Let me put this another way.

 

The boolean signal "Experiment ON?" (in VI) is controlled by another equipment which is controlled under a separate logic.

What I want is,

when and while this signal is TRUE (while the "Dehum. ON?" is FALSE),

a SINGLE set of random numbers should be generated.

 

What's happening now with my current VI is that, when "Experiment ON?" is TRUE and "Dehum. ON?" is FALSE,

"random number" generates NUMEROUS numbers WHILE the signal is TRUE.

 

 

0 Kudos
Message 4 of 14
(5,514 Views)

Thank you also for the comment, Bob_Schor. I don't think I'm understanding your solution fully enough as well.

I can't connect the idea of "storing the boolean signal" to "generating single set of random numbers".

Once again the problem that I'm having is,

"random number" generates numerous numbers while the case structure is TRUE ("Experiment ON?" in VI),

but what I want is,

"random number" generating just one random number while the case structure is TRUE ("Experiment ON?" in VI).

 

Let me know if I'm missing anything from your comment.

 

0 Kudos
Message 5 of 14
(5,511 Views)

Here I show you how to store the previous value of the trigger (Experiment On?) in a feedback node. The compound arithmetic is set up to be True when the Experiment On? value is True, and when its previous value was False.

 

Run it and flip the switch to give it a try.

Download All
0 Kudos
Message 6 of 14
(5,502 Views)

Yes, you certainly are "missing something".  I'm attempting to teach you, not simply "give you the answer".

 

Boolean is your Control.  Let's suppose, for the moment, it is off or False.  Let us also assume, for the moment, that the value in the Shift Register is False.  Execute this While Loop.  A False (the current value of the control) comes in, and the Shift Register (the former value of the control, but you don't know that yet) is false, so what output do you want?  Well, False (you don't want to generate a Random Number if the Boolean Input is False).  I did not provide the Output for this function -- that's something for you to do.  But for now, we know that in this situation, the output needs to be False.

 

Notice that the False "current" value gets saved in the Shift Register.  Now let's run this again, but this time with the Boolean = True.  We come into the loop, the Current Value is True, the Former Value (in the Shift Register) is False -- what do we want the output to be?  Why, True (we want a True only on transition from False to True, i.e. former = False, current = True).  We save the current True in the Shift Register.

 

Third time, still True.  Current = True, previous (Shift Register) = True, we want the output to be False (as this was not a "rising edge"), and save current True in Shift Register.

 

Finally, Input = False, previous = True (falling edge) -- again, output is False, and we save False in Shift Register.

 

So now make a Truth Table with two inputs, Current (wire coming from Boolean Tunnel) and Previous (wire coming from Shift Register).  Figure out how to make True if Current = True and Previous = False, and False in all other cases (it takes two Boolean functions to do this).  Wire the output to an output Tunnel and call it "Generate Random Number".  This is the value you wire into your Case statement that generates the Random numbers -- it will only generate when Boolean goes from False to True, exactly what you want.

 

Bob Schor (the "underscore" is silent) 

0 Kudos
Message 7 of 14
(5,499 Views)

P.S. -- Feedback Nodes are very similar to my "Do Once" structure.  I learned LabVIEW before Feedback nodes were present, and find (for me) that the Do Once structure is easier (for me) to understand.

 

BS

0 Kudos
Message 8 of 14
(5,494 Views)

In the spirit of Bob's comment, I've uploaded a "Broken" version of my example. Please attempt to connect it up yourself, and if it is not working as you expect, post your attempt.

 

Bob - I hardly every use feedback nodes as well. My preference for Functional Globals is a For loop with 1 iteration.

0 Kudos
Message 9 of 14
(5,492 Views)

Gregory,

 

     You know what they say, "Six of one, two-pi half-a-dozen of another".

 

Bob Schor

Message 10 of 14
(5,479 Views)