Finding MIN/MAX values in SLC 500 PLC

papaya72

Member
Join Date
Apr 2013
Location
Vancouver, WA
Posts
2
Looking at 6 Integer values...say N7:0 - N7:5. Need a clever way to find the Minimum and Maximum values from this group. Better yet would like to rank them in descending order. So a sorting routine would be nice...lol.
 
Back in the days of pascal programming this ability to rank values in a list used to be called a bubble sort. It's easier than you think. Simply compare the first 2 registers, N7:0 and N7:1, if N7:0 is less than N7:0 then move the value of N7:0 into a temporary register (like N7:10), then move N7:1 into N7:0, then move the temporary register into N7:1. The next rung of code should compare N7:1 to N7:2, then N7:2 to N7:3 and so on. All of your integers should line up.
 
Thanks, and yes, a bubble sort is one sorting method that works good with a short list (not so well on a really long list). Normally I would offload this type of list/table functions to my HMI (Intouch). Unfortunately, this system has no HMI with enough intelligence to sort a list and pass values back to the PLC. I've written the logic to do ta bubble sort and it actually works. Problem is, that only sorts the values in the Registers. once done, I need to know which register contains the Max value and which contains the min. Example. N7:0 - N7:5 represent piece counts per Machines 1-6. I want to know which Machine has the highest piece count and which has the lowest then I can direct my diverter gates to send to Machine with lowest piece count. Gets a little more complicated when you have to sort ordered pairs...lol
 
OK, guess I misunderstood the question. Use GRT instructions to find the highest value. Use 5 instuctions in series on the same rung and do a simple comparison. The first GRT would test if N7:0 was greater than N7:1, the second instruction would test if N7:0 was greater than N7:2, and so on, to energize an output coil.
The next rung would be the same as the first except the GRT instruction would test if N7:1 was greater than N7:0, N7:2, ect, then energize that output coil. Do this with all the other registers. Which ever rung had all 5 GRT instructions true, that would be the register with the highest value. Use LES instructions to find the lowest value.
 
I bet you don't need to sort the whole list each scan. I would have the counts in 6 consecutive registers for the 6 machines. I would use indirect addressing and sort the indexes based on the count. I would only make one pass per scan because I bet the counts will not change that fast or by that much.
 
Welcome to the forum.

I read your post earlier and after thinking about it I threw together a bit of SLC500 code to show how you might tackle the problem with a sort. I only used five machines/lines in the example, you can expand the code to however many you really need.

First lets address your problem of knowing what machine has the highest and lowest count. One very simple way is to use two files. One file contains the counts, the other file contains the line number. So you copy the count for line 1 into N10:0, then you put a 1 in N11:0. Then you copy the count for line 2 into N10:1 and put a 2 in N11:2, etc. In the process of sorting, if you swap a count value you also swap the line numbers. After you have sorted then N10:0 has the lowest count and N11:0 has the number of the line with the lowest count.

First I wrote a routine for an optimized bubble sort. See ladder 3 in the attached example. In an bubble sort, after each pass there is one less item to sort. In the sort in ladder 3 after the first pass the greatest value is at the end of the array, so there is no need to to a compare on it again. To optimize it each pass makes one less comparison than the previous pass. This sort uses indirect addressing and two nested for/next loops to sort N10 and N11 in a single scan. Use ladder 3 if you absolutely have to have an up to date sort every scan, but it is heavy on overhead and has a high "WTF?" coefficient for future viewers.

After I programmed ladder 3 I read Peter's post and added a second example. See Ladder 4 in the attached. This one is a multiscan sort. It uses no indirect addressing and no looping so its low on overhead. Every scan it compares each successive pair of values in file N10, and if necessary swaps the values and the matching line numbers in N11. When a pass is completed with no swaps then we know that N10 is sorted lowest to highest and N11 gives us the matching line numbers. N10 and N11 are then copied out to N20 and N21. N20 and N21 will always contain the last sorted results, and then the process is repeated. Most of the time since only one or two counts are likely to change on any given scan, the sorted results will lag by only a few scans and never by more than N scans where N is the number of lines. If that works then go with Ladder 4, it is much simpler and I like it better. It is far less likely to result in a 3:00 am phone call from Bubba who doesn't understand loops and indirect addressing. Even if the code doesn't seem optimized, at execution time on PLC it will be more efficient than Ladder 3.

I haven't tested this code and someone may yet find something wrong with it, but hopefully it helps you. There are better ways to sort than a bubble sort but coding them on a PLC, esp. a SLC500, is a PITA and you aren't sorting a large amount of information anyways.
 

Similar Topics

Hi all, I'm in the process of upgrading a PanelView1200 HMI program to be developed in FactroyTalk View Studio. The filetype for PanelView 1200...
Replies
7
Views
279
Hey all, pretty new to PLC and got a question regarding finding the MSB or the last non-zero bit in a SINT array in studio5000... I am reading...
Replies
2
Views
830
Having an issue connecting to my Micro820 PLC. I don't have an IP Explorer and I know its MAC Address is 5C:88:16:D8:E6:65. I'm connected to the...
Replies
5
Views
910
I have reached a dead end trying to find an EDS file. Manufacturer says to contact third party tech support. Clueless. RSLINX can see it, just...
Replies
9
Views
1,791
Hello, I have an array of 300 of UDT. In each UDT is an array of 3 DINT and another array of 3 REAL. I have 10 controllers that pull data from...
Replies
7
Views
1,160
Back
Top Bottom