There are several approaches, one of which @JesperMP already described. The all involve a FIFO array of some kind, either of bits or of integers. In some PLC brands, only the integer form is called a FIFO.
OP mentioned neither the PLC make and model nor the language (Structured Text, Function Block Diagram, Ladder Logic, etc.); some PLCs have built-in FIFO and bit-shift instructions that make this easier, but it is also possible to build those tools from atomic instructions.
A FIFO is an array, either of integers or of bits;
FIFO is an acronym for
First In First Out. Normally values are appended or appended at one end of the FIFO array, and removed from and/or detected at the other end of the array; on one of those operations, or on a separate (e.g. encoder) event, all values in the array may be shifted by one position in the array.
The FIFO/bit-shift operation as described can be used as a
model of the conveyor with the bottles: The
integers or bits shifting through the array are
proxies for the
bottles moving along the conveyor. The exact details depend on
- whether FIFO instructions operating on an integer array, or bit shift instructions operating on an array of bits, are used,
- what sensors are available,
- what triggers the movement of the data through the FIFO.
Bit Shift Array Modeling a section of conveyor
Usually a bit shift is apropos the process when the array represents the status of increments of conveyor length, and the status of any increment is binary i.e. reject or not-reject, where not-reject could mean either no bottle is present or a good bottle is present that should not be rejected.
When the encoder signals a discrete distance move by the conveyor i.e. an edge, the bits in the array are shifted by one position, and a new bit (First In) is added at one end representing a new increment of conveyor at the upstream end of the monitored section, and the oldest bit (First Out) is removed from the other end. The
value of the new bit depends on the status of the bottle: 0 for accept (either no bottle present, or a good bottle); 1 for reject (bad bottle). How that new bit's value is determined is specific to the process. Note that, in this model,
- each bit's value models the "bottle status" of an [increment of the moving conveyor], i.e. not the bottle itself, and
- each bit's position in the array models the location of an [increment of the moving conveyor] between the entry sensor and reject stations
Since the length of the modeled section of conveyor is known,
in units of encoder events, the position in the array of the "exit" bit, which models the location of the reject station, is also known, so monitoring that exit bit, and activating the reject piston when that exit bit is 1, should be fairly simple. That exit bit could be the bit removed from by the bit shift instruction if the entire bit array length models exactly the same length as the distance between the entry and reject stations, or, if the entire bit array length models a length greater than the entry-to-reject distance, that exit bit will be at a location earlier in the bit array.
Integer FIFO modeling bottles
Usually an integer FIFO is apropos the process when the array models the individual bottles and only the entrance and exit of the conveyor, and there is a sensor at
both entrance
and exit of the conveyor section, and
the encoder is not used. I don't think this is the case for the current thread.
When a bottle is detected by the trigger sensor at the upstream entry station end of a section of the conveyor, a new integer value (First In), representing some measured property of the bottle e.g. accept/reject status, is appended to one end of the array. When a bottle is detected by a sensor at the downstream reject station end of a section of the conveyor, the oldest integer value (First Out) is removed from the other end of the array, and the value of that array is used to extend or retract the reject piston.
Caveats
- These approaches model conveyor/bottle movement on an edge of the the relevant input signal; typically built-in bit-shift and FIFO instructions execute only on those edges, so even if input sensors are active for multiple contiguous scans, the array will shift only one position on the first of those scans. However, if such built-in instructions are not available and the OP has to build the bit-shift or FIFO model from atomic instructions, then they would need to implement edge detection (one-shots) explicitly.
- The bit-shift model assumes that once a bottle enters the modeled section between entry station and reject station, the bottle is not moved relative to the moving conveyor surface; if that is not the case and there is adequate space between bottles, the code could assign two or more adjacent bits to 1 in the array when a bottle to be rejected enters the modeled section, and the array location of the reject station adjusted accordingly.
- The integer FIFO model cares less about the position of bottle relative to the conveyor, but it does assume, between the entry and reject stations, that
- bottles are not removed
- bottles are not added
- bottles are not re-ordered (I think this is what @bernie carlton is referring to)