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.

FIRST Robotics Competition Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview beginner vi

As a team mentor I am trying to get my programmers to write simple programs in LabVIEW. I have given them the challenge of using an array to sort six numbers in ascending order without using the sort array operator (the point is to familiarize ourselves with loops and case structures). Back in the day when I learned this in Basic, we used a conditional inside nested loops and switched two successive elements if they were in the wrong order.

Please see the attached file. So far we have created a control for a one-dimensional array with six elements- 6,5,4,3,2,1 and we want the program to to generate the array 1,2,3,4,5,6. Our block diagram uses a For loop to compare consecutive elements in the array and uses a case structure to switch the position of the elements if they are in the wrong (descending) order. The problem we ran into was that in the program we have written, once the for loop performs the sorting function once, it bases the next output off of the given control array and not off of the output array from the first time through. For example, when sorting the numbers 6,5,4,3,2,1, the first time the loop ran, you would get 5,6,4,3,2,1 which is good, but on the next iteration we get 6,4,5,3,2,1. How can we apply the second iteration on the previous iteration's output so that the second iteration would read 5,4,6,3,2,1? Thanks in advance for your help.

0 Kudos
Message 1 of 5
(8,115 Views)

I couldn' view you Vi because I have a different version of labview but my guess is that you are

accessing the original array data inside your loop every time.

So everytime through the loop it just starts over with the original data.

One of the simple ways to solve this is to read and write the data using a "local variable" interface to the array.

There are other ways as well using input and outputs to your loop.

0 Kudos
Message 2 of 5
(2,964 Views)

Dear tivomatic,

You can access data from previous iterations of a loop in LabVIEW with shift registers. Righ-click on the border of the loop and select "Add Shift-Register". The shift register transfers the data wired to the right-hand terminal to the left-hand terminal for access in the next iteration of the loop. If you want to access data from more just the last iteration (averaging the last X elements is a common application) you can add elements to the left-hand side of the shift register. Right-click on the left hand element and click "add element".

You may need to take a look at your sorting algorithm though. So say you start with

43210

and you iterate #elements-1.

you compare the first 2 elements and switch their locations if the 1st element is greater, so you end up with

34210

iterate again

32410

iterate again

32140

iterate for the last time

32104

You are basically moving the first element in your array to the back of your array.

Good luck with the array sorting, and post back if you run into any other problems.

~Nate

NI FIRST Support

0 Kudos
Message 3 of 5
(2,964 Views)

Right.. thats what they call'm... "shift registers".

It's actually a term borrowed from hardware design.

The idea is that you shift or store the results of each iteration and then it can be used as the input to your next iteration.

0 Kudos
Message 4 of 5
(2,964 Views)

One thing to watch for and detract your mentorees against is the expansion of the shift register in an unrestrained fashion.

Although you can expand an individual shift register down (hover over terminal and drag handle down) this is rarely needed and is usually a beginner mistake.

I don't think that this would happen with what you may be doing, but it's worth keeping an eye out for.

It is usually a sign that the program flow needs to be re-thunk.

-Norm

0 Kudos
Message 5 of 5
(2,964 Views)