How to bubble sort in Do-more PLC

jdeleon

Member
Join Date
Oct 2021
Location
Texas
Posts
3
Hello,

I been trying to sort some values in the the Do-more PLC but still have some issues. I have 20 elements from the data view with random values that change every minute, would like to sort them in descending order to be able to see them in my HMI C-more screen. sort this values.jpg
 

(1) Assign 20 to the value of "N"
(2) Move value of 1 to "M"
(2.1) Add 1 to "M," put result into "K"
(2.1.1) If the value of DLV35[K] is less than value of DLV35[M], then swap values of DLV35[K] and DLV35[M](2.1.2) Move value of "K" into "M"
(2.1.3) If the value of "K" is less than value of "N," then jump to (2.1) above, otherwise drop through to (4) below
(2.2) Subtract 1 from "N," put result back into "N"
(2.3) If the value of "N" is greater than 1, then jump to (2) above, otherwise drop through to (3) below
(3) Done i.e. DLV3501 though DLV3520 are sorted in descending order

On the first pass of the nested loops of steps (2) through (2.3), the smallest value will end up in DLV3516. On the second pass, the second-smallest value will end up in DLV3515. Etc.

N.B. I don't know how to do indirect addressing in Do-More, but the syntax DLV35[K] or DLV35[N]means

  • DLV3501 when the value of "M" is 1
  • DLV3502 when the value of "K" or "M" is 2
  • ...
  • DLV3519 when the value of "K" or "M" is 19
  • DLV3520 when "K" is 20
 
Here is an extra-inefficient bubble sort, using a FIFO. If anyone submits this as a homework assignment, they should not get a very good grade.

xxx.png
 
dlv35[k] ?

what do you mean by:

(2.1.1) If the value of DLV35[K] is less than value of DLV35[M], then swap values of DLV35[K] and DLV35[M](2.1.2) Move value of "K" into "M"

is it:

dlv3501+k < dlv35+M then swap them?

thank you.
 
what do you mean by:

(2.1.1) If the value of DLV35[K] is less than value of DLV35[M], then swap values of DLV35[K] and DLV35[M]

Say K was 8 and M was 7, then DLV35[K] and DLV35[M] mean DLV3508 and DLV3507, respectively.

I don't know how Do-More handles indirect addressing, so that was my shorthand; apparently it was not as intuitive as I had hoped.

(2.1.2) Move value of "K" into "M"

This should be a separate bullet point; it's a cheap way of incrementing M, since the value of K is (M+1).

Sorry for the confusion.
 
Originally posted by drbitboy:

...apparently it was not as intuitive as I had hoped.


It most certainly was not, Doc. I don't know of any plc that would let you do a partial address index like you show. It would have been more clear if the had said:

(1) Assign 3520 to the value of "N"
(2) Move value of 3501 to "M"

and explicitly stated that K, M and N take on the address values so that DLV[M] would be equivalent to DLV3501.

Also:
(2.1.3) If the value of "K" is less than value of "N," then jump to (2.1) above, otherwise drop through to (4) below

There is no (4) to drop through to. I think you meant (2.2).

A friend of mine told me a long time ago that words matter so choose them carefully. He also said that making a peanut butter and jelly sandwich should take no less than about 35-40 steps if you are careful about listing them all.

Keith
 
and this question will be asked again.
Is there anyway to package the bubble sort code into a subroutine that can be called with the address of the data and the length of the data?


BTW, if the 20 cells change randomly every minute then a bubble sort is good. If the cell change one at a time then an insertion sort will be better.


I have been on this forum for 20+ years now. I really hate to see previous answer/work being ignored/lost.
 
...I have 20 elements from the data view with random values that change every minute, would like to sort them in descending order to be able to see them in my HMI C-more screen.

Summary

See attached PDF.

It's more or less a naive implementation of bubble sort cf. this link.

I wrote it for, and tested it in, the Allen-Bradley/CCW/Micro820 environment. There are no complex instructions I would expect to be missing from a Do-More; the algorithm itself is six rungs and about a dozen and a half instructions, so porting it should not take much effort.

Details

Rung 3 has the [Sort_loop] label that is jumped to from Rung 7; see image below.

Rung 8 (not fully shown) has the [Bypass_sort] label that is jumped to from Rung 2 or Rung 3.

On a press of the Normally-Closed [arm_sort_button], Rung 1 initializes the sort by making two assignments:

  1. the value 19 to the current index ([icurrent] DINT) which is the index of the last element in the list of 20 numbers to be sorted;
  2. the value 1 to the [swapped] BOOL,
After Rung 1 does this Rung 3 will "think" at least one bubble-swap occurred (because [swapped] is 1) during a just-completed sweep (because the current index is 19) through the list, which Rung 3 will interpret as meaning that at least one more sweep will be required to sort the list.

Rung 3 acts as the outer loop guard of bubble sort, determining whether the sort is complete i.e. when a complete sweep of the list has been made with no swaps i.e. all the numbers are in decreasing order; if the sort is indeed complete, then the code makes a jump to the [Bypass_sort] label; jumps look like this:

ccw_jump.png

Rung 4 initializes the next inner loop of bubble sort i.e. the next sweep, assigning a 0 to the current index when it is at a maximum, and resetting [swapped] to 0.

Rung 5 calculates the next index ([inext] DINT) of the next item after index icurrent.

Rung 6 does the compare, and optionally swaps the values of the next and current indices if the former is greater than the latter.

Rung 7 increments the current index and jumps back to the top of the Sort loop.

Rung 2 is a superfluous guard to allow me to review the unsorted values and trigger the start of the sort.

The second PDF (*_ugly.pdf) is from the "Document Generator" of CCW, in case someone wants to see the full program, but it is very poor quality; is that A-B's fault, or is the summat lacking in my PDF driver?

This effort reinforces my opinion that CCW and Siemens TIA are roughly tied for the Most Awkward IDE award.
 

Similar Topics

Hello all, I was looking into different sorting techniques and actually found a bubble/insertion sort sample code file on Rockwell's site. Only...
Replies
20
Views
5,260
Hi, im creating compressor station project with S7-1200. Project requires that 4 compressors should work in cascade - that's already done, and...
Replies
13
Views
11,620
Hey, I have written a function in SCL for doing the bubble sort thing. The code is taken from wikipedia. But, question I have is: When you...
Replies
20
Views
6,157
Hi all, I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation...
Replies
1
Views
7,827
Good afternoon guys, I have a basic question on the bubble level in the picture attached. This is probably really easy, but I don't get it, haha...
Replies
11
Views
3,355
Back
Top Bottom