finding greatest

ready961

Member
Join Date
Jan 2003
Posts
78
I'm trying to have my Direct Logic 06 plc find the greatest value of 4 variable timers. Basicly I have 4 timers for bottle fillers which are variable and all slightly different. I want the plc to take the value of the timer which is greatest and using it to stop something esle for that length of time.

I can figure this out. Any ideas?
 
Shouldn't be too hard. Off the top of my head I think this would do the trick...
Code:
-- TA0 > TA1 --- TA0 > TA2 --- TA0 > TA3 ----- SET C0
-- TA1 > TA0 --- TA1 > TA2 --- TA1 > TA3 ----- SET C1
-- TA2 > TA0 --- TA2 > TA1 --- TA2 > TA3 ----- SET C2
-- TA3 > TA0 --- TA3 > TA1 --- TA3 > TA2 ----- SET C3
-- C0 ---------------------------------------- LD TA0
                                               OUT V2000
                                               RST C0
-- C1 ---------------------------------------- LD TA1
                                               OUT V2000
                                               RST C1
-- C2 ---------------------------------------- LD TA2
                                               OUT V2000
                                               RST C2
-- C3 ---------------------------------------- LD TA3
                                               OUT V2000
                                               RST C3
-- SP1 --------------------------------------- T4
                                               V2000
-- NOT T4 ------------------------------------ YOUR OUTPUT
I don't know enough about your app to do more than that, but you get the idea. You'll have to reset T0, T1, T2, T3, etc and V2000 will be the value that T4 counts to. Hope that makes sense.

-Mark
 
Last edited:
Mark - your reply should work but another approach to finding the greatest of a group of numbers is:

1. Set a location to zero (V2001)
2. For each timer: If TA? > V2001 - LD TA?, OUT V2001
3. After all timers tested then move V2001 to V2000

The same logic applies for a 'least' test, just start the location (V2001) at max possible (9999) and do 'less than' tests.

This algorithm is more straightforward and only grows linearly in complexity with more timers while your would grow geometrically.
 
This calls for a "Bubble-Sort" routine. The specified condition ("greater than" or "less than") is "bubbled-up" to the surface in one pass.

The "Greatest Value" is found and "grabbed by V100 in one scan. Your "Check for Greatest" bit can be a SET or a one-shot. If it is a SET then the bit is RESET at the end of the routine.


Clear the target register.

Check +------+
For | LOAD |
Greatest | WORD |
------| |------+ V100 +-----( Dummy Bit )
| = 0 |
+------+

See if Tmr1 Acc is greater than the current value in V100.
(V100 = "0")
If Tmr1 Acc is greater, then over-write the current value in V100 with Tmr1 Acc.

Check +------+
For Tmr1 | MOVW |
Greatest Acc V100 | Tmr1 |
------| |------------| > |---------+ Acc +-----( Dummy Bit )
| to |
| V100 |
+------+

See if Tmr2 Acc is greater than the current value in V100
(V100 = "0" or Tmr1 Acc)
If Tmr2 is greater, then over-write the current value in V100 with Tmr2 Acc.

Check +------+
For Tmr2 | MOVW |
Greatest Acc V100 | Tmr2 |
------| |------------| > |---------+ Acc +-----( Dummy Bit )
| to |
| V100 |
+------+


See if Tmr3 Acc is greater than the current value in V100
(V100 = "0" or Tmr1 Acc or Tmr2 Acc)
If Tmr3 is greater, then over-write the current value in V100 with Tmr3 Acc.

Check +------+
For Tmr3 | MOVW |
Greatest Acc V100 | Tmr3 |
------| |------------| > |---------+ Acc +-----( Dummy Bit )
| to |
| V100 |
+------+


See if Tmr4 Acc is greater than the current value in V100.
(V100 = "0" or Tmr1 Acc or Tmr2 Acc or Tmr3 Acc)
If Tmr4 is greater, then over-write the current value in V100 with Tmr4 Acc.

Check +------+
For Tmr4 | MOVW |
Greatest Acc V100 | Tmr4 |
------| |------------| > |---------+ Acc +-----( Dummy Bit )
| to |
| V100 |
+------+

At this point, V100 contains the greatest timer accumulated value.

If the routine was called by a SET, then RESET the call.

Check
For
Greatest
------| |-------------------( RESET ) Check for Greatest




Damned! You beat me Bernie!

If you do a search in the archives, you'll see that I posted this about 4 maybe 5 years ago.
 
Hey thanks guys there all pretty trick! I think I'll try out the bubble sort. I'll have to set the v memory I use to 0 after each test, right?? Could I set this so it tested ever scan? Maybe every other scan would be easier using SP7?
 
Test the values as often as you feel the system needs it. Even if the procedure takes some CPU time the needs of the process will dictate how often it should be performed.
 
Uhhh... Peter? Are you upto date on your meds?

My solution and Bernie's solution are exactly the same... he just got there first.

And it most certainly IS called a "bubble-sort". It has been so for at least 50-years... long before PLC's.

The purpose of the "bubble-sort" is to bring one and only one target value to the surface. It does so by "sorting" (selecting) on an "instance basis".


Confusing the Rookies? Take a look at your PID stuff.

C'mon Peter... wazup?
 
Actually, Terry, the 'bubble sort' I remember runs through a list multiple items looking at adjacent pairs of values, and swapping them if they aren't in the required order. This running through the list continues until a pass yields no swaps. It was very efficient if only one item were out of place but incredibly inefficient if the list was initially random and worst if the list was initially sorted but backwards.

What we were doing in this application was just selecting the greatest value. In some applications we would have preserved the address of the greatest value also for other processing.
 
One more thing to muck up the works....

To truly make this flexible, I would do what Bernie and Terry are suggesting but I would use a for/next loop, a subroutine and indirect addressing. This way the program does not get any larger with greater ranges, just change the value of the for/next loop and increase the range being checked.

And for the record, this was taken from "MicroSOFT BASIC", Third Edition, Published 1991:

"Bubble Sort- A method used to sort numeric or alphanumeric data, where adjacent elements are continually compared and swapped until they are in the desired sequence."

Based on this, Bernie and Peter are right.

Bob
 
Bernie is right again.

Bernie knows the difference between finding a maximum and sorting. The difference in complexity between truely sorting and just finding a maximum or minimum valve are great. Searching for a maximum requires only one pass. It can't be optimized so keep it simple. Sorting requires multiple passes.
 
Bernie,

A full-blown bubble-sort can indeed be used to re-order a list into ascending or descending order. And, as you say, it can be terribly inefficient.

However, if you set the bubble-sorter to re-order into descending order, and then only allow one-pass through the bubble-sorter, then you have what we have produced... the greatest value.

If you set the bubble-sorter to re-order into ascending order, and then only allow one pass through the bubble-sorter, then you have the opposite effect... the least value.

Either of those cases is extremely efficient.

A bubble-sorter, cut-down in the prime of its life (after one-pass), was still a bubble-sorter.

And again, your solution and mine are exactly the same!

You wrote yours in terms of Timer-X. I wrote mine in terms of Timer-1, -2, -3 and -4.


And Peter... mine takes only one-pass.

And again, Bernie's solution and mine are exactly the same!

And I don't mean only in terms of the result... it is exactly the same in execution!
 
Last edited:
OK then - while I'm sure it has some other very technical sounding official name we'll call Terry and my method the Pre-Maturely Terminated Bubble Sort or PMTBS for short. Though it's not really a buble sort as we didn't modify any of the original list, just made a copy of the highest value, but otherwise PMTBS it is.
 
Last edited:

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
277
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
901
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,790
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