Problem
A customer wanted to add an inspection camera to a bag production line to catch bad seals. When the camera sees a seal that wasn’t pressed correctly, it sends a single bit. The bad bag can’t be pulled at that point, though: it has to travel the rest of the way through the machine, and only at the end of the line can a chute open to divert it away from the good bags. The PLC has to remember every flagged bag and divert exactly that bag, possibly several at once, while the line keeps running.
Approach
I wrote the Allen-Bradley routine in Studio 5000 that connects the camera’s bit to the chute.
- The machine’s motor position is a counter that keeps climbing and wraps around as the line runs. The distance from the camera to the end of the machine is fixed, so I converted it into motor counts. When a bad seal is flagged, the routine records the motor position at which that bag will reach the chute.
- Pending rejects go into a FIFO queue of target positions. When the motor reaches the oldest target, that bag is at the chute.
- The chute opens and closes at positions calculated from each recorded target, and stays open through consecutive bad bags instead of cycling between them.
How it came together
The hard part was making the timing trustworthy on a machine that runs continuously.
- Every target is recorded relative to the motor position at the moment of detection, so it always stays ahead of the current position, like two points chasing each other around a circle. Nothing depends on counting bags since startup, so the counter wrapping around or resetting to zero doesn’t make it reject the wrong bag; I tested both in simulation. In principle it also survives a power cycle, since the motor keeps its position value and the offsets are relative to it, though that wasn’t tested on a machine.
- I didn’t have the customer’s line to test on, so I recreated its behavior in its own subroutine. A trigger bit stands in for the camera, and a counter generates a simulated motor encoder count that climbs and wraps like the real one. Watching the Studio 5000 trend plots, I could see the motor value climb and check that the reject and chute signals fired at the right positions, including back-to-back bad bags and rollover.
Key decisions
- A FIFO queue instead of a bit shift register (BSR). A BSR shifts one bit per bag, which assumes the PLC steps once per bag. Each entry here is a motor position (a number), so a FIFO of positions fits the data and lets several bad bags be in flight at once.
- Relative targets instead of a fixed reference. Recording each target from the position at detection is what makes the routine immune to the counter wrapping or resetting.
- Build a test harness instead of waiting for the machine. A simulated encoder in its own subroutine made it possible to test the timing, edge cases included, without the customer’s machine.