Determining priority

cornbread

Lifetime Supporting Member
Join Date
Feb 2007
Location
madison, indiana
Posts
407
I have three controllers with operator set points. I’ve been tasked with determining a priority based on the set point. Lowest set point has the highest priority, highest set point the lowest priority. I’ve banged out some code using a **** load of less than comparisons to do this but I’m sure there is an elegant way to do this… Any help or suggestions would be appreciated.:confused:
 
Create a For Loop.
Just prior to jumping into the loop, make a buffer variable to house your value and set it to zero.

Make one comparator that checks the value of the the Nth setpoint to your buffer. If your Nth setpoint is less than the buffer, then write the Nth setpoint into the buffer. Once the For Loop has completed you should remain with the smallest value in your buffer. Store that to your command setpoint.

This of course is code that is geared towards minimizing the code you have to write. It is not more efficient for the PLC to process. It will also be a little more difficult for your average Joe to understand. Unless you have five plus setpoints there is little point and in bothering.

If for some reason you need to put all setpoints in order and keep track of which is where in that order, then that is a whole other ballgame.
 
What I've done in ladder is something like this

If (SP1 < SP2) & (SP2 < SP3) then
Case1 = true; (Priority is ABC)
endif;

IF (SP1 < SP3) & (SP3 < SP2) then
Case2 = true; (Priority is ACB)
endif;

IF (SP1 = SP2) & (SP1 < SP3) then
Case3 = true; (Priority is A=B C)
endif;

.......
 
This might work? I haven't tried it, but if you wanted to do it in ladder, it might be a good starting point (I think :confused:).

quick.jpg
 
A couple of different methods

If you are using a Logix-class PLC, go with the SRT (sort) instruction.
It will rearrange an array of values (one-dimension) in ascending order. NOTE: If your setpoints are in an array, and the order needs to remain unchanged, then you will have to copy the array to a buffer array prior to sorting.

Once your values are arranged, you can compare each value against the array, and the position where the match is found + 1 is the priority. For example, if Controller A has a SP of 300, B is 250, and C is 500, then after a SRT, a FSC would find A at My_Sort[1], which is actually 2nd position, so A would be 2nd priority...

Attached is a sample program I worked up for RS5k which contains 3 different Sort routines. The first is done in ladder, without using the built-in SRT instruction, to give an idea of how to implement the function on another processor lacking a built-in sort instruction. Use at your own discretion ;).
The second routine is performing the same function as the first, only with the SRT instruction.
The third routine is basically the same as the first, but done in ST.

Hope this helps. Happy coding!

🍻
 
I can't read the .ACD file so I will post my own

Start with SPs A,B and C with random SP values and sort.
The lowest SP will be in A and the 3 SP are sorted A<=B<=C
Code:
IF A > B THEN
   TEMP:=A;
   A:=B;
   B:=TEMP;
END_IF
IF B > C THEN
   TEMP:=B;
   B:=C;
   C:=TEMP;
END_IF
IF A > B THEN
   TEMP:=A;
   A:=B;
   B:=TEMP;
END_IF

IF only the lowest SP is needed in A then use two compares. A<=B AND A<=C
Code:
IF B > C THEN
   TEMP:=B;
   B:=C;
   C:=TEMP;
END_IF
IF A > B THEN
   TEMP:=A;
   A:=B;
   B:=TEMP;
END_IF
 
I'm not very good with the pdf creator, so to get all 3 routines of the .ACD file onto one pdf, I had to print the entire project. The ST is on page 27 or 28 I believe. The scope is a little broader than the OP's original question. It allows for a variable # of array values to be sorted, but the principle is basically the same: If Value[n] < Value[n-1] then Temp = Value[n]; Value[n] = Value[n-1]; Value[n-1] = Temp; where n is the pointer moving through the array.

But what if A = 5, B = 3, and C = 1?
If we just do a series of compares, then we end up with A = 3, B =1, and C = 5, unless we loop again. As the size of the array increases, then the amount of compares necessary dramatically. I think that for an array with size = n, n - 1 loops would be required, right?
Im not sure if what i posted is more efficient, since it involves nested loops, but it does only require one pass through the source array.
Makes me curious to know how the SRT instruction in Logix is accomplished, or in Excel for that matter.
 
But what if A = 5, B = 3, and C = 1?
The first example will handle that. The second example will just find the lowest set point.

If we just do a series of compares, then we end up with A = 3, B =1, and C = 5, unless we loop again.
That is why the 3rd IF THEN in the first example is the same as the first IF THEN.

As the size of the array increases, then the amount of compares necessary dramatically. I think that for an array with size = n, n - 1 loops would be required, right?
Yes, and it would take multiple scans.

[/quote]
Im not sure if what i posted is more efficient, since it involves nested loops, but it does only require one pass through the source array.
[/quote]
Yes

Makes me curious to know how the SRT instruction in Logix is accomplished, or in Excel for that matter.
I would use either a quick sort, heap sort or a shell sort. The method depends on how much stack space, the number of items I need to sort, andwhether I must sort in place.

Computer science classes will spend a whole semester on sorting and searching. Most of the sorting algorithms suggested on this forum use the bubble sort which is very inefficient but easy to implement for sort a small number of items.

This can be a long and very detailed topic.
 

Similar Topics

We are in the process of installing a new DC drive to replace a 1373 series Speedpak drive. There are 8 leads coming out of the motor -- Two for...
Replies
5
Views
845
Good morning! I am looking for a clever method to determine the number of occurrences in a series using ladder logic. What I need to determine is...
Replies
6
Views
1,260
I have my PLC and HMI connected to my local network over DHCP just like my laptop. I can upload and download including VNC to the HMI over wifi...
Replies
2
Views
1,138
We have a DC Drive (ABB DCS800) which is running a load (initially) at a fixed speed. For testing purposes, we want to simulate a different...
Replies
20
Views
6,265
ive been tasked, due to a restructure of our inventory, to find out how big our SLC programs are. specifically, we have several dozen 5/05s at...
Replies
4
Views
1,710
Back
Top Bottom