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.
PLCs in question - Omron CJ2M, or CompactLogix 5370 L33ER
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