Help Sorting a Dynamic Array (elements) in Logix 5K

skyfox

Lifetime Supporting Member
Join Date
Nov 2006
Location
CA
Posts
276
Hello All,

Needing some help with finding Min max values in an array that can have a dynamic element count. The Sort function in Logix 5K only allows an immediate value for length. I am looking for a way to sort an Array with varying Element Count. 100 being minimum to 1000 being max elements.

Element count is known before sorting and always will be a value divisible by 100. Between runs array is cleared of previous data.

Short of having 10 Element count checks and then selecting between 1 of 10 sorting instructions..... wondering if there is a much elegant way of doing this.

For Example: (Array is sized for 1000 elements. Note: there is more than 1 array is involved but during each run, each array will have same number of elements or samples per run.)

Run 1 = 1000 Elements in Array
Run 2 = 100 Elements in Array
Run 3 = 600 Elements in Array
etc., etc.

Thanks-
 
If the element count is known you can MOV that value into your SORT Control.LEN and control what length of data is sorted. Of course, Array[0] is minimum and to find Array Maximum you only need use an indirect address of SORT Control.LEN -1
 
cwal61,

Thank you very much. This works great. I didn't know you could alter the length that way and assumed control used the immediate value that was entered in to length parameter of the instruction.
 
First RSLogix 5000 does not allow you to dynamically resize an array at runtime
You will have to size you array with enough elements to hold the max number of elements you expect
If it were me I would add 10% to number just to make sure I have enough

Just make sure the array index value is less than the number of elements in the array.
If you point an array index to an element that larger than the number of elements in the array processor will crash.

Create 3 tags Min Value, Max Value and stored elements count

Then populate the array from index 0 up to number you need for each run with the data you want to compare.
When you want to find the min and max values.
Move 64767 into the Min Value or any higher number that is above the highest value you expect to see in the data and 0 into Max Value ( assuming you are using an INT data type)
Then scan the array and look at each data element
For any element value > (greater) than 0
If the data value of the indexed element is less than the value of the Min Value Tag move the data into the Min Value Tag
If the data value of the indexed element is greater than the value of the Max Value Tag move the data into the Max Value Tag
If the data in the array is not needed then move 0 into the array element ( to clear the data)
Or you can wait and clear the array data whenever you want with a File Fill or a scan and move “0” to each element
After the scan the Min Value and Max Value Tags will contain the values you want

I hope that helps
Gary
 
GaryS,

Thanks for the detailed response. In the past I have used a similar approach when displaying min/max values in real time on to an HMI display. However in that instance, I was only monitoring a real time input and comparing it to the input from the previous scan and moving it to the appropriate min/max register. Correct me if I am wrong, by using the method you described above for a previously stored x number of elements, it would take x number of scans to arrive at the desired results.

For an example......

If element count is 1000, then it would take 1000 scans to arrive at the final results. Where as using the method cwal61 described, it only takes 1 scan to get at the final results. (I did confirm it with an 1000 element array and a scan counter variable) Am I missing something or did I misread your explanation? It is late and I've already had few shots of God's programming juice.......form Kentucky.
 
SkyFox
You create a scan loop within the program
And scan the array within the loop it all happens within 1 scan cycle of your program
Or if you want you can do it in a subprogram and only call the subprogram when you want to update the min Max Values.
The attached file should explain it better it should do what you want you may have to change tag names
Line 1 triggers the loop by moving values into the Min Max Tags and 0 into the Sc_index tag
As long as the value in Sc_Index is less than 1000 the loop runs. It will exit when Sc_Index value = 1000
After the scan the Min Max Values will be updated

I hope this clears it up for you

Gary

Array Scan.jpg
 
I need to correct my original ladder
After double checking it looked like it could get into a never ending loop
the attached is corrected

sorry about the confusion

Array Scan.jpg
 
Hi GaryS,

Now I understand what you were trying to say. However, I still would like to know if there is a performance advantage over your method vs. the method cwal61 presented. I am sorting 6 arrays at end of each run. Time is not a critical factor during this phase since the run has already ended and the next one won't be starting for several minutes.

Thanks

Min-Max Sorting.png
 
Skyfox
I think my program will run faster
with a sort have to get each element of the array and compare it to each element in the sort array and place the data in the location
you will have to make the loop several times to complete the sort

with my way you make 1 loop and compare the data to a single data tag not every tag in the array.

if you only need the values at the end of each run use a subroutine for each run and call the subroutine at the end of the run
a subroutine fro each run
each subroutine will be a loop and compare like is shown
you could also add a MOV 0 to each element in the array just after you do the compare and before you add 1 to thee index that way you will clear the array when the sub is finished and you are ready to load it again
with the subroutine they will only run once at the end of the run

in your case 6 subroutines
 
Thanks GaryS.

You make a valid point and I can see how yours might be considerably faster. I would love to find out if both methods can be timed and see how efficient each method is over several hundred iterations.

Cheers
 

Similar Topics

So I am working on a system where I need to change the priority running order of 4 vacuum pumps based 1. On demand 2. based on total run time. I...
Replies
9
Views
3,223
Hi All, Wondering if I could get a hand with why my bubble sort isn't working. I need to take 3 REAL inputs, and 3 BOOL enable inputs. Then...
Replies
10
Views
2,285
I am new to micrologix 850 cpu. Please help on sorting numbers in array. i want to sort in ascending order.
Replies
2
Views
2,085
My problem is a have two sensors one high and one low the convylor keeps moving. and i have three plungers only using two high part add a low part...
Replies
37
Views
17,388
Hi!! I'm looking for Temperature rise calculation software from Rockwell, I just download "Product selection toolbox 2022" but this software is...
Replies
1
Views
97
Back
Top Bottom