S7-1200 SCL Bubble Sort - I'm Stuck!

adfox

Member
Join Date
Jun 2006
Location
Poland
Posts
334
Hi, im creating compressor station project with S7-1200.

Project requires that 4 compressors should work in cascade - that's already done, and they should start always the one with smallest hour counter.

So i've done bubble sort in scl. Function takes time in hours from counter
and should output the number of compressor in descending hours order.

Code:
//------------------------------------------------------------------------------
//    Bubble sort 
//------------------------------------------------------------------------------

// --- Make an array from inputs
#Array1[1]:=#Input1;
#Array1[2]:=#Input2;
#Array1[3]:=#Input3;
#Array1[4]:=#Input4;

// --- Run bubble sort on array
REPEAT #swap:=false;
  
  FOR #i:=4 TO 2 BY -1 DO
    IF #Array1[#i-1] > #Array1[#i] THEN
    
        #aux:=#Array1[#i];
        #Array1[#i] :=#Array1[#i-1];
        #Array1[#i-1] :=#aux;
        #swap:=true;
        
    END_IF;
  END_FOR;
UNTIL NOT #swap END_REPEAT;

// --- Assign positions to values
FOR #i:=1 TO 4 BY 1 DO

  IF #Array1[#i] = #Input1 THEN #Array1[#i] := 1; END_IF;
  IF #Array1[#i] = #Input2 THEN #Array1[#i]  := 2; END_IF;
  IF #Array1[#i] = #Input3 THEN #Array1[#i]  := 3; END_IF;
  IF #Array1[#i] = #Input4 THEN #Array1[#i]  := 4; END_IF;

END_FOR;




#Out1 := #Array1[1];
#Out2 := #Array1[2];
#Out3 := #Array1[3];
#Out4 := #Array1[4];


Problem is that when if 2 or more Inputs got the same value for example i've got the same numbers on outputs.

For example 4 2 2 1

I've really stuck as it my first scl function - could You post some hints how to avoid duplicated output ?

Regards.
 
Problem starts when more compressors will be added - and still they must make cascade - or replace one of faulted with higher priority... it's confusing how customer may complicate things ;)
 
Use a second array initialised with the pump numbers and perform the same swap operations inside your bubble sort.
 
Problem starts when more compressors will be added - and still they must make cascade - or replace one of faulted with higher priority... it's confusing how customer may complicate things ;)

If there are N compressors you need N-1 compares to find the one with the minimum hours. One you find the compressor with the minimum hours there is no need to sort the rest. This becomes more important as N gets larger. Use one FOR..END_FOR loop.
 
Won't it give me the same result - duplicated numbers ?

No. As the PumpNo array is allocated the numbers 1,2,3,4 there cannot be duplicates.

Code:
#Out1 := #PumpNo[1];
#Out2 := #PumpNo[2];
#Out3 := #PumpNo[3];
#Out4 := #PumpNo[4];
 
quick edit to your code:

Code:
//------------------------------------------------------------------------------
//    Bubble sort 
//------------------------------------------------------------------------------
// --- Make an array from inputs
#Array1[1]:=#Input1;
#Array1[2]:=#Input2;
#Array1[3]:=#Input3;
#Array1[4]:=#Input4;
#PumpNo[1]:=1;
#PumpNo[2]:=2;
#PumpNo[3]:=3;
#PumpNo[4]:=4;
// --- Run bubble sort on array
REPEAT #swap:=false;
  
  FOR #i:=4 TO 2 BY -1 DO
    IF #Array1[#i-1] > #Array1[#i] THEN
    
        #aux:=#Array1[#i];
        #Array1[#i] :=#Array1[#i-1];
        #Array1[#i-1] :=#aux;
        #swap:=true;
        #aux:=#PumpNo[#i];
        #PumpNo[#i]:=#PumpNo[#i-1];
        #PumpNo[#i-1]:=#aux;          
    END_IF;
  END_FOR;
UNTIL NOT #swap END_REPEAT;
// --- Assign positions to values
#Out1 := #PumpNo[1];
#Out2 := #PumpNo[2];
#Out3 := #PumpNo[3];
#Out4 := #PumpNo[4];
 
Does it matter if 2 or more compressors have the same running hours? Just start the first one and then that compressor will accumulate more running hours than the second one so next time to run the second will have less hours than the first one.
 
Does it matter if 2 or more compressors have the same running hours? Just start the first one and then that compressor will accumulate more running hours than the second one so next time to run the second will have less hours than the first one.

For customer - it does :) He wants to see sorted list - don't ask why :)

L D[AR2,P#0.0] - thank You! I've been battling this issue for a 3rd day now.
Thanks again!
 
Hello friends,

I brought this matter back to the agenda :)

I have a question.

For example; I want 3 runs with the least running, 2 other runs to stop working.

How can I do it ?

Thank you.
 
For customer - it does :) He wants to see sorted list - don't ask why :)

I see a new PO in your future to change this to be done with a static list by ascending or descending order...

I don't have a crystal ball, but can see people servicing compressor 2 when it was compressor 3 which was the second listed from the top that had enough hours for service time. Particularly if the pumps are set up in a way for a while and then one day they change.

Why is a client defining this? Aren't you the expert? ;)
 

Similar Topics

Hello, I am currently forced to use s7-1200 (6ES7214-1AG40-0XB0) PLC for one small project. I do have some experience with S7-300/400 mostly in...
Replies
6
Views
5,081
Hi, I am new to Siemens and SCL. I have a datablock which is a global data block. I want to use some of the values in this DB in a FB block...
Replies
3
Views
3,543
Hi, I only know how to read one bit of e.g. an input port, using e.g. I0.7 or names from the standard table for such a single IO line. Is there...
Replies
11
Views
4,650
Hey, is it possible to know, within a program, the number of the FB, or OB, the code is currently executed in? Like, some function...
Replies
2
Views
2,049
Hi Guys! I'm preparing a short training for some colleagues on how to use the S7-1200 CPU and while explaining the 3 different programming...
Replies
2
Views
3,444
Back
Top Bottom