Sorting large amounts of sensor data

StopSignSteve

Member
Join Date
Jul 2017
Location
Toronto
Posts
5
Hey all,

Looking for some quick opinions on some sensor data collection with a S7-300

In the application we are testing a part for conformity. We will be reading 25 (4-20mA) values and recording the values every 100ms for 5 seconds for a total of around 50 values per sensor, and 1250 values per test.
We do this test 3 times per part, and each sensor and test needs to pass for the part to pass QC.

Currently we just do a rolling average of the data for each sensor, but our process and quality guys want to sort the values, then filter out a few of the top and bottom values per sensor, then average the rest. That way we can still pull any values that are way out of bounds.

Essentially the integrator wants to put in a PC to do that kind of work, but at a pretty big cost. I've floated some pseudo code around that should do the job using a comb sort when the test is done, but I don't have a rig to test it on right now.

https://en.wikipedia.org/wiki/Comb_sort

Has anyone done large amounts of data sorting like this before? Will it make my PLC cry, should I go with the integrator and spend a bunch of cash? Code is attached, hopefully it won't make anyone cry!

Thanks
 
Is it possible? Sure. I didn't dig too deep into your code though.

Partly depends how you do it, partly it depends on your CPU.

It is often a best practice in PLC programming to only do part of the loop each scan, to minimize the impact. As an example, if you want to loop between 1 and 1000, you might do 100 loops each scan. It takes 10 Plc scans to get the logic done, but it avoids maxing out your scan time. Usually the short delay in getting the data you need is irrelevant. It depends if you run your code every cycle, or only at the end of a batch/sequence.

If you have a 312, they are pretty slow. You'll need to be careful, but it's still possible. If you have a 319, you might only notice a small blip in your scan time even if you run the whole thing at once.

It's mostly just an optimization problem.
 
Here's your corrected SCL code tested using PLCSIM


Code:
//CALL THIS FUNCTION BLOCK TO GET AVERAGE VALUES AFTER EACH TEST
FUNCTION_BLOCK FB11
CONST
    SIZEOFDATA:=20;
    NUMBER_OF_SENSORS:=2;
    BOTTOM_TOP_DROP:=5;
END_CONST
    

//VARIABLE DECLARATION

VAR
     SENSOR_INDEX, VALUE_INDEX, SORT_INDEX, AVG_INDEX, i,j: INT;
    AVG_SUM, TEMPVALUE, SHRINKFACTOR, GAP: REAL;
    TEST_DONE, SORTED: BOOL;
    SENSOR_AVERAGE_VALUE:ARRAY[1..NUMBER_OF_SENSORS] OF REAL;  
    SORT_ARRAY, CURRENT_DATA :ARRAY[1..NUMBER_OF_SENSORS,1..SIZEOFDATA] OF REAL;
      
END_VAR

//SORTING AND AVERAGING, 1150 VALUES NEED SORTING PER TEST
BEGIN
//COPY TEST VALUES TO ARRAY FOR SORTING (OR SORT ARRAY DIRECTLY) THESE ARRAYS MUST BE THE SAME SIZE
TEST_DONE:=FALSE;
GAP:=10;
SHRINKFACTOR:=1.3;


//generate some test data in descending order
FOR i:=1 TO NUMBER_OF_SENSORS DO
  FOR j:=1 TO SIZEOFDATA DO
    CURRENT_DATA[i,j]:= SIZEOFDATA - (i+j);
  END_FOR;
END_FOR;  
  
SORT_ARRAY:=CURRENT_DATA;

//SORT ALL SENSORS, THEN SUM AND AVERAGE THEM, THIS WILL SORT AND AVERAGE ONE SENSOR AT A TIME WITH A COMB SORT
FOR SENSOR_INDEX:=1 TO NUMBER_OF_SENSORS BY 1 DO
    SORTED :=FALSE;
    WHILE SORTED=FALSE DO
        GAP:=GAP/SHRINKFACTOR;
        IF GAP >1 THEN
            SORTED:=FALSE; // We are never sorted as long as gap > 1
        ELSE
            GAP :=1;
            SORTED :=TRUE; // If there are no swaps this pass, we are done
        END_IF;
        VALUE_INDEX:=1;
        WHILE (VALUE_INDEX+ROUND(GAP))<=SIZEOFDATA DO
            IF (SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX] > SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX+ROUND(GAP)]) THEN
                TEMPVALUE:=SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX];
                SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX]:=SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX+ROUND(GAP)];
                SORT_ARRAY[SENSOR_INDEX,VALUE_INDEX+ROUND(GAP)]:=TEMPVALUE;
                SORTED:=FALSE;
            END_IF;
            VALUE_INDEX:=VALUE_INDEX+1;
        END_WHILE;
    END_WHILE;
    //SUM UP THE VALUES IN THE RANGE WE WANT (DROP BOTTOM AND TOP VALUES)
    AVG_SUM:=0.0;
    FOR AVG_INDEX:=1+BOTTOM_TOP_DROP TO SIZEOFDATA-BOTTOM_TOP_DROP BY 1 DO
        AVG_SUM:=AVG_SUM+SORT_ARRAY[SENSOR_INDEX,AVG_INDEX];
    END_FOR;
    //AVERAGE THOSE  VALUES
    SENSOR_AVERAGE_VALUE[SENSOR_INDEX]:=AVG_SUM/(SIZEOFDATA-(2*BOTTOM_TOP_DROP));
END_FOR;
TEST_DONE:=TRUE;
END_FUNCTION_BLOCK
 
Thanks a ton, I know its possible just wanted to know if it was good practice or not! I'll throw the integrator the code and let them know we want to try it first before going too overkill.
The PLC is a 317 so we should be just fine! I'm an AB guy so its just about learning the environment, I do miss the SRT block though!
Honestly this problem dropped pretty low on the priority list over the last couple hours. Lots of other fun stuff happening. Motors not connected to safety systems, or the PLC, and running a part through the tester process requires eight (8!) operator actions instead of 1 push button press.
China is amazing.
 
With 25 sensors and a data size of 50 with worst case sorting the scan time using the above code in a 317 is >300ms

315scantime.jpg
 

Similar Topics

I have worked on small projects using AB Micrologix but now we want to take a photo, process it online, and sort based on returned variables...
Replies
1
Views
94
Hi, I have just started with WinCC unified (v17) and there are alot of things I used on Comfort but now are not available. Currently I am finding...
Replies
3
Views
2,660
I'm trying to be smart about naming my tags so things automatically group together alphabetically, but for some reason it doesn't work like I...
Replies
15
Views
3,600
Hello friends. There is an int type, number that I got from the DataBlock. For example, the DataBlock address is Db1.dbw0. Here's what I want to...
Replies
32
Views
10,385
Good afternoon all, I am working on a college project where we have to sort 3 different color legos. Orange, Black, and Tan. I am using a color...
Replies
30
Views
6,726
Back
Top Bottom