LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Idiom for initialising a sub VI?

Solved!
Go to solution

Idiom for initialising a sub VI?

 

I want to have some code which is run only one time at the first call to a sub VI.

 

My idea is to run a for loop one time (N = 1) at the first call and then zero time (N = 0). An example is shown in the attached code snippet.

 

When I am using a for loop with a loop tunnel, there is for me an unexpected result. The loop tunnel is NOT keeping its last value. See the attached picture.

 

My questions are:

 

1) Is it a better way of doing this?

 

2) What are the disadvantages with this idiom?

 

3) Where is it documented that an output loop tunnel is reinitialised to a default value when the for loop starts to execute?

 

Best regards

Bo Johansson

Download All
0 Kudos
Message 1 of 15
(3,532 Views)

Hi bojinlund,

 

when something is unexpected you should use highlight debugging to understand what's going on…

 

1) Yes, definitely. When code should run at a certain condition the case structure is the natural choice instead of fiddling with FOR loops…

 

2) Disadvantage: your unexpected outputs. The FOR loop will/must have an output regardless of their loop iteration count!

 

3) In the DATAFLOW.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 15
(3,515 Views)

Here's how I do it.  The false case simply passes the value from the Shift Register through.

 

Example_VI.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 3 of 15
(3,500 Views)

Hej Jim!

 

Thank you for the answer!

 

You need a While loop, a Case structure and you need to handle the “false case”. I need a For loop, two constants and a Select function.

 

/Bosse

0 Kudos
Message 4 of 15
(3,483 Views)

Hi Bosse,

 

you need 3 loops and two select functions to produce the wrong result.

Using just one loop with one case structure will give the correct result…

 

And Jim tried to show you a very similar use case, but it seems you didn't understand it.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 15
(3,481 Views)

@bojinlund wrote:

Hej Jim!

 

Thank you for the answer!

 

You need a While loop, a Case structure and you need to handle the “false case”. I need a For loop, two constants and a Select function.

 

/Bosse


Queue the links to beginner tutorials...

 

/Bosse.  You completely missed my point.  It wasn't my intention to write your sub-VI initialization code for you, but to show you how to do it by giving a very general example.

 

Example_VI.png

 

You need put the While loop/Case structure/First Call? construct in your sub-VI and it'll give you the same 2D array every time through.  The first time it'll generate the array, the rest of the times it'll pull it from the shift register.  Your code, however, is so simple that, unless you're doing something really, really quickly, I wouldn't worry about regenerating it every time through.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 6 of 15
(3,456 Views)

Hej Jim!

 

Thanks again! Sorry for being unclear! I have mixed two things in my question.

 

1) I am trying to understand the difference in “my” and “the common” way to initialise a sub VI.

 

The attached files shows the two ways I am comparing. I have used your “initialization code” in both.

 

2) I am quite new to LabVIEW and I expected a for loop tunnel to keep its last value.

Where is it documented that an output loop tunnel is reinitialised to a default value when the for loop starts to execute?

 

/Bosse

Download All
0 Kudos
Message 7 of 15
(3,433 Views)

Hej Bosse,

 

2) Why do you expect something different from DATAFLOW? A tunnel will not keep any previous values - a shift register may keep values…

 

A tunnel is NOT reinitialized to default when the loop starts.

But it is set to default when a (FOR) loop runs ZERO times! (THINK DATAFLOW!)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 15
(3,424 Views)

Hej GerdW!

 

Have you any reference to this in the documentation of LabVIEW?

 

/Bosse

0 Kudos
Message 9 of 15
(3,417 Views)

Hi Bosse,

 

no, couldn't find it in a quick search.

 

But this is basic knowledge thaught in beginner courses Core1 and probably also in the free online courses. And it is asked in each certification questionaire…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 15
(3,414 Views)