Need some ideas as to how to do a "for each" loop to check what box is measured.

wtzll

Member
Join Date
Oct 2011
Location
Dublin
Posts
4
Need some ideas as to how to do a "for each" loop to check what box is measured.

Hello,

I need some help in understanding the appropriate approach needed to create a "for each loop" in a plc, or something along the lines (using data search?)

The idea is that I have 3 analog sensors that measure a box, compare it to preset dimensions, and tell me if the measurements are correct and what box it is from the list.

Here is a python code that does this.

Code:
x = (192, 332, 162) # "measured" box dimensions. 

q = [("BOX1", 250, 210, 120),  # predefined box dimension list
("BOX2", 210, 210, 460), 
("BOX3", 300, 350, 310), 
("BOX4", 300, 400, 310), 
("BOX5", 190, 330, 160), 
("BOX5", 160, 240, 100), 
("BOX6", 460, 460, 400), 
("BOX7", 330, 300, 310)]

t = []

for item in q:
    a = abs(item[1]-x[0])
    b = abs(item[2]-x[1])
    c = abs(item[3]-x[2])
    avg = (a + b + c)/3.0 
    t.append((avg, item)) # find the averages of the box sides and add them to empty list with measured average.


min_val = min(t)[1][0] # this gets the closest box from the predefined list to the "measured" box dimensions

d_a = abs(min(t)[1][1]-x[0]) # closest box dimensions,minus measured box dimensions  
d_b = abs(min(t)[1][2]-x[1])
d_c = abs(min(t)[1][3]-x[2])

limit_val = 3 # set the limit as to how much the measured box can creep past the predefined box
if d_a >= limit_val or d_b >= limit_val or d_c >= limit_val:
    print "Box dimensions out of bounds"
else:
    print "Box accepted:", min_val

PLCs in question - Omron CJ2M, or CompactLogix 5370 L33ER
 
You could write similar code in Structured Text.
"Similar" not exactly the same, because you cannot do "for item in q" in ST as in Python.
Make an array, something like this:
Code:
boxarray := ARRAY[1..10] OF STRUCT
     a_size, b_size, c_size : REAL ;
ENDSTRUCT ;

Then you can loop through the array like this:
Code:
FOR i:=1 TO 9 BY 1 DO
   a = ABS(boxarray.x_size[i+1] - boxarray.a_size[i])  ;
.....
END_FOR ;
This is just to give you an idea. the ST code should be portable between the Omron and AB controllers.
 
you ill need more checks as the testox can be in a diferent location
so the result will be 7 times 9 63 results the smallest is the best just like you do it in python.
 
Typically, you will be able to program a PLC in Structured Text or maybe C or even C++. In any case, you won't have access to a FOR EACH loop, only a FOR loop. You also won't be able to make dynamically sized arrays, you have to hard code their dimensions.

You can use a constant to define the size of your box list and thus the size of your loop for maximum flexibility. In structured text, you could do something like this:
Code:
VAR CONSTANT
    MY_ARRAY_SIZE    : INT:= 9;
END_VAR

TYPE
    MyStructure    : STRUCT
        MyElement1    : REAL;
        MyElement2    : REAL;
        MyElement3    : REAL;
    END_STRUCT;
END_TYPE

VAR
    i    : UINT;
    MyArray : ARRAY[0..MY_ARRAY_SIZE] OF MyStructure_typ;
END_VAR

FOR i:=0 TO MY_ARRAY_SIZE DO
    (*Your code*)
END_FOR
 
The answers provided so far look like good advice to me. I won't say all that again, but I will add one tidbit.

Your example used a multidimensional array (or possibly an array of arrays), and the sample code mentioned in the other posts were all single dimension arrays. It is possible on some PLC platforms to do multidimensional arrays, but not all of them. You would have to check, and I don't know the capabilities of the two specific systems you mentioned.

On rereading the thread, I see that JesperMP's suggestion was to use an array of a structure (or a custom data type) that includes the data points you desire. This is often one alternative, and may be simpler for the reader to understand than mapping a 2D array.
 

Similar Topics

Hi all. I am writing a program in RS Logix 5000. I have 8 pieces of support equipment that are exactly the same. They supply high pressure water...
Replies
4
Views
1,763
hi! Background on me: Self learned programmer mostly on LOGO, s7-1200, regular HMIs and WinCC Flexible 2008 Runtime. Only smaller projects. The...
Replies
20
Views
4,185
I have an application where we have a truck loading program written in Visual Basic. The truck loading program issues a permissive to a Reliance...
Replies
7
Views
1,786
Hey fellas, I'm looking for ideas. I've been asked to look into creating a system that will measure the length of panels cut by our flying cut...
Replies
14
Views
6,927
Hi all I'm after some ideas for some new PLC projects. We are running S5 95U and S7 313C plcs. I have classes doing everything from basic PLC's...
Replies
12
Views
8,878
Back
Top Bottom