LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Variable Initialization

Solved!
Go to solution

This is my first posting but I always searched this board every time when I need an help.

 

This is LabView beginner who is really interested in Data acquisition and analysis.

I am implementing real time parameter monitoring system (such as Temperature )

So the data come every 1 second from a daq device 

 

I want to ask about how to initialize the variables ( local or global or shared ones)

I have an experience with Visual C++.

When I program with Visual C++, I initialized variables in initdialog or PreCreateWindow functions.

 

Attached code is sub-vi  which check whether 6 continuous data series are strictly increasing  or decreasing.

 

Actually the data series (such as temperature ... ) come from DAQ device every 1 second.

 

I have an difficulty in initializing the variable. ( counterFBRule3  variable in attached code)

 

Currently I am using shared variable as counter, but I am going to change it into local variable later.  

 

Could you give me an advice about how to initialize variables?

Thank you in advance.

 

Chulsoon Park.

 

 

 

0 Kudos
Message 1 of 7
(3,649 Views)

If you want to initialize the first time the subVI is called, use the first call? primitive and a case structure.

 

Also note that you do way too many unnecessary things. For example most (or all) of your sequence structures are not needed if you use wires. For example in your inner sequence you could just wire to the comparison after the +1 and write to the global outside the case, leaving the FALSE case empty. The calculation of Numeric is a constant and never changes. Why is it inside the loop?

 

It is difficult to give more detailed advice without seeing the rest of the code.

0 Kudos
Message 2 of 7
(3,614 Views)

I'd imagine the general advice specific to LabVIEW would be to try and avoid use of large numbers of Local Variables or Global Variables.

 

In text-based languages (especially, for example, C++) we think about initializing and using named variables, but in LabVIEW it's perhaps better to consider the data that travels along the wires between nodes. We don't have to give names to these "variables".

When we read them from Controls or write them to Indicators, then they have a name of sorts, but these are I/O ports rather than ways to store data (typically).

 

Linking nodes together with wires prevents the need for labeling specific parts of the code and gives a clear visual indication of where our data comes from and goes to, if it is copied (the wire branches (technically this doesn't always produce a copy, but anyway...)), where we could see it (connected indicators or probes).


GCentral
0 Kudos
Message 3 of 7
(3,603 Views)
Solution
Accepted by topic author cspark66

You've already "discovered" that one way to "remember" a variable is to wire it to a Shift Register, where it will persist from one loop to the next.  You've also "discovered" that a Loop that runs exactly once can serve as a "memory" for all of its Shift Registers.  You used a For Loop with N = 1 -- what I learned years ago was to use a While Loop with "True" wired to the Stop indicator.  Basically the same thing ...

 

You have a value in a Global called CounterFBRule3.  Suppose you create another Shift Register, wire a Numeric Constant 0 (I32) to it from the left outside (which will turn it Blue), and connect it to the other (right) Shift Register.  Inside the Loop, create an I32 Indicator (the easiest way is to right-click the wire, choose Create, Indicator), and name it CounterFBRule3.  Every time the Loop runs, you can change the value (increment, reset to 0, something else) as your logic desires.  This allows you to elimininate Global Variables (generally a Good Thing, particularly for LabVIEW Beginners!), and don't even start to think about Local Variables!!  Once these are gone, you can get rid of the Frame Sequences (right-click Sequence, choose "Remove Sequence").

 

I notice in the "False" inner Case some text that I'm guessing shows how to implement the Rule3 algorithm, but I couldn't read it.  If you describe the algorithm, I'm sure one of us would be willing to show you the LabVIEW way to code it, without "variables", but with Controls and Indicators.

 

Coming from Text-based languages to LabVIEW, "unlearning" the use of Variables and getting comfortable with how data flows through wires can take a little time, particularly if you don't get a good explanation of the Principle of Data Flow, but it really all does make sense, and is generally a lot simpler -- many wires, particularly when "bundled" (in a Cluster) or gathered into an Array, can be much easier to visualize than trying to keep track of an equivalent number of "variables".

 

Bob Schor

 

P.S. -- A shift register that does not have something wired into it from outside the loop (on the left) is called an uninitialized Shift.  The "rule" for these is that they "initialize" themselves by taking the "default value" for the particular data type they contain.  Single-loop Shift Registers ("Memories", such as you've coded), generally get "initialized" the first time they are called, when a value gets put into the output Shift Register for the first time.  But you can initialize it yourself -- here's how to create an Array consisting of the first 10 Even Numbers.  I did it twice, once initializing the Shift Register to 0 and taking the values after I incremented it twice, the other initializing another Shift Register to 2 and taking the values before I incremented.  The code at the exit compares the two arrays and asks (using the upside-down A, logic for a Universal Quatifier, i.e. "Is Every?") if all the elements are the same.  If you run this, you should get the Indicator to light up.

Shift RegistersShift Registers

For Extra Credit, will anything change if you delete the I32 constant 0 initializing the first Shift Register?  [Try it].

 

 

 

0 Kudos
Message 4 of 7
(3,593 Views)

Thank you all for excellent advice.

I learned a lot and think I can solve my problem. 

 

Chulsoon.

0 Kudos
Message 5 of 7
(3,565 Views)

Well, at first, as I said I am a beginner in LabView programming. 

That is why my code looks little bit ambiguous and of poor quality..

 

What I am trying to do is as follows:

1) Data value continuously comes from DAQ device every 1 second,  not batch.

 

2) I want to implement Rule 3 algorithm of Nelson Rules as a sub-vi because I am going to use the same sub-vi  more than one time in my main vi for two different Temperatures and one Current RMS.

 

 3)  At first I tried to use Nelson Rule Checker vi  in SPC toolkit.

   However, the stocked Rule Checker vi is intended to use with batch data set (Array), but mine need to be checked with real-time data values from Daq device.

Oh.. I think the stocked Nelson Rule Checker vi is perfect and sufficient for its original purpose.  ( as you know in SPC,  they do sampling, saving them into array, one time batch rule checking, then plotting the result in Graph not Charts.....  )

 

4) But I thought mine is little different.   As you know the Rule 3 is very easy. When 6 continuous values are strictly increasing or decreasing, then the rule 3 is said violated.  So instead I decided to implement it by myself.

 

5) When the rule is violated, I want to plot my data series in Waveform Chart with original value with colored marker. And the counter reset to 0.

And when the rule is not violated, only original value is plotted without marker.

(that is why I am using NaN in False case to exclude marker.)

 

Any comments will be really appreciated.

Thank you.

 

Chulsoon Park.

 

0 Kudos
Message 6 of 7
(3,550 Views)

Thanks for mentioning Nelson Rules (which I can look up, and I have), which makes "Rule 3" mean something.  According to Wikipedia, Rule 3 asks "do six consecutive points all increase or all decrease", i.e. "is there a Trend"?  Hilariously, your "Point #4" says "As you know the Rule 3 is very easy".  But I am not a Process Control Engineer and have never read anything by Lloyd Nelson, and have never heard of the Journal of Quality Technology.  

 

So now that I understand what you are talking about, I have some suggestions about how to achieve it.  Making a "Rule 3" subVI is an excellent idea -- you can make it a "Clone" (set its Execution property to "Preallocated Clone Reentrant Execution"), and put one in each of the three loops that you need to return a Rule 3 decision.

 

So what is the "output" of a Rule 3 VI?  It is Boolean -- either Rule 3 has been met (i.e. the last six samples are all increasing or decreasing), or True, or not met, or False.

 

Each time you get a sample, you send it through the Rule 3 VI and it returns True or False.  I would code this as a State Machine, whose initial State is "Initialize" (meaning there are no numbers in it, yet), and there are similarly no previous "delta" representing the preceding two points (increase or decrease).

 

Ah, digression -- what if two values are exactly the same -- is that an Increase, or a Decrease?  Or maybe you need trivalent logic!

 

There's also a question in my mind about what happens after Rule 3 fires (recall I'm not a Control Engineer, and haven't read Nelson's paper).  I can think of three possibilities if successive samples keep trending in the same direction:

  1. Rule 3 "fires" on the 7th, 8th, etc. successive trend.
  2. You reset the count to 0, so Rule 3 doesn't fire until 6 more points come in.
  3. You go into a new State where you wait for the trend to reverse, and then restart Rule 3 with a count of 0.

Algorithm 1 can have Rule 3 fire for many successive samples.  Algorithm 2 guarantees a "pause" of at least 6, even if the trend doesn't reverse, while Algorithm 3 means a "pause" of at least 6 after the trend reverses.

 

You need to have some "state variables" -- you need the last value (so you can compute increase/decrease), you need the last "delta" (which might be trivalent, see above), and you need a counter, reset to 0 when delta changes, that causes Rule 3 to fire when it reaches 6.  Very simple (simpler than your algorithm, I think).

 

Writiing this as a Pre-allocated Reentrant Clone means you can have a Rule 3 inside each of the loops that collect each of the three samples, and they will "fire" for their respective sample (since they maintain the "State" inside themselves in shift registers).  Give it a try, and post the VIs (I hope you are using LabVIEW Project, and if so,, compress the Project Folder and post the Zip file of the Project).

 

I suggest you write a 2 or 3 channel "Data Simulator" that generates pairs or triples of points at some rate and place a Rule 3 clone in each of their loops to alllow you to test your Rule 3 algorithm without needing any DAQ or other hardware.

 

Bob Schor

Message 7 of 7
(3,488 Views)