From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Variables precedence in shift registers used in For loops

Solved!
Go to solution

In this simple program, I would like to subtract two double constants 45 and 8.9. Case structures 1 and 2 is used to define the variables, case struct 3 is used to subtract them. The values are passes using a shift register and I expect them to follow the order as per indexing (as shown in Array indicator). However, they seem to have swapped positions resulting in negative answer.

 

I am trying to pull in the numbers generated in case struct 1 and 2 in case struct #3. By using Add Element to shift registers I assume I am indexing 0th and 1st element in the shift register line pointing to the variables 45 and 8.9.

 

Why does this happen? What is the logic behind this?

 

 

LabVIEW_2019-04-09_14-21-52.png

LabVIEW_2019-04-09_14-23-09.pngLabVIEW_2019-04-09_14-23-21.png

NI System Configuration:
- NI PXIe-1071, 4-Slot 3U PXI Express Chassis , 1 GB/Slot throughput, Part Number: 781368-01
- NI PXIe-PCIe8381,x8 Gen2 MXI-Express for PXI Express Interface,3m, Part Number: 782522-01
- PXIe-5160 PXI Oscilloscope, 500 MHz, 10 bits, 2.5 GS/s, 2 Channels, 64 MB, Part Number: 782621-01
- Astronics PXIe-1209 2-Channel, 100 MHz PXI Pulse Generator, Part Number: 785033-01
0 Kudos
Message 1 of 8
(2,359 Views)

You use an expanded shift register and a is i-1 and b is i-2. Looks correct.

Message 2 of 8
(2,345 Views)

On the left set of shift register terminals, the upper terminal outputs the previous value.  The lower terminal outputs the value before that.

"If you weren't supposed to push it, it wouldn't be a button."
Message 3 of 8
(2,342 Views)
Solution
Accepted by topic author asukumari

The most typical use of shift registers (SR) is to have a single SR on the left side of the loop which will be vertically aligned with the SR on the right side.  Whatever you write to the right side on iteration i will come out of the aligned left-side SR on iteration i+1.  It is the *most* recent value.

 

When you expand the left-side SR, it grows in a downward direction.  The top SR retains its meaning as the most recently written value.  The ones below are values from prior iterations.  Basically, this is a visual metaphor for a fixed-size lossy "stack", with the added bonus that you're allowed simultaneous read access to anything in the stack.

 

So yes, the vertical ordering of the data is opposite to what you'd get from Array indexing, but it's intentional, not an "oops". 

 

 

-Kevin P

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 4 of 8
(2,341 Views)

Some notes:

  1. First of all, you don't need to initialize the shift register, because you never use the init value here.
  2. If you want to keep both values, use two shift registers, one for [a] and one for [b] Now they persist, even if other cases execute in random order as long as they have the values wired across (see attached quick mod).
  3. You could also initialize the shift register with a 1D array of three elements and replace each value in the respective case using "replace array" (second image below).
  4. Of course your current code is pure Rube Goldberg for that specific task, so I imagine that your real application is a bit more complicated.
  5. You don't have "variables" just wires and indicators.

 

 

 

ShiftRube.png

 

ShiftRube2.png

Message 5 of 8
(2,332 Views)
the cases will always occur sequentially since its wired to the iterator of the FOR loop. And yes, I want to retain both the values to do calculations at a later stage. Yes the entire application is complex.

Your MWEs (minimal working examples) are really useful and I am adding them to my collection of reference VIs.
NI System Configuration:
- NI PXIe-1071, 4-Slot 3U PXI Express Chassis , 1 GB/Slot throughput, Part Number: 781368-01
- NI PXIe-PCIe8381,x8 Gen2 MXI-Express for PXI Express Interface,3m, Part Number: 782522-01
- PXIe-5160 PXI Oscilloscope, 500 MHz, 10 bits, 2.5 GS/s, 2 Channels, 64 MB, Part Number: 782621-01
- Astronics PXIe-1209 2-Channel, 100 MHz PXI Pulse Generator, Part Number: 785033-01
0 Kudos
Message 6 of 8
(2,316 Views)

My first example was flawed because you don't get the correct array unless you branch inside the case structure. (I would use the array example anyway). Not sure if you need that array.

 

ShiftRube3.png

0 Kudos
Message 7 of 8
(2,313 Views)

@altenbach wrote:

... (I would use the array example anyway). Not sure if you need that array.

 

ShiftRube3.png


Agreed.

 

They scale well on the fly.

 

It has been years since I used a resized SR. When I see one in code they "feel dirty" and I find myself wondering why the developer went that route.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 8
(2,295 Views)