ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Conveyor Sequencing with Multiple Gates and Sensors

PLC Structured Text

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM ConveyorSequencing
2
3VAR
4// System Mode
5SystemMode : INT := 0; // 0: Manual, 1: Automatic
6
7// Conveyor States
8Conveyor1_Run : BOOL := FALSE;
9Conveyor2_Run : BOOL := FALSE;
10Conveyor3_Run : BOOL := FALSE;
11
12// Gate States
13Gate1_Open : BOOL := FALSE;
14Gate2_Open : BOOL := FALSE;
15
16// Sensor Inputs
17Sensor_Entry : BOOL := FALSE; // Product at entry
18Sensor_Conveyor1_Mid : BOOL := FALSE; // Product on Conveyor 1 middle
19Sensor_Conveyor1_Exit : BOOL := FALSE; // Product at Conveyor 1 exit
20Sensor_Conveyor2_Entry : BOOL := FALSE; // Product at Conveyor 2 entry
21Sensor_Conveyor2_Exit : BOOL := FALSE; // Product at Conveyor 2 exit
22Sensor_Gate1_Position : BOOL := FALSE; // Gate 1 fully open
23Sensor_Gate2_Position : BOOL := FALSE; // Gate 2 fully open
24Sensor_Exit_Chute : BOOL := FALSE; // Product in exit chute
25
26// Manual Control Inputs
27Manual_StartConveyor1 : BOOL;
28Manual_StartConveyor2;
29Manual_StartConveyor3;
30Manual_StopConveyors;
31Manual_OpenGate1;
32Manual_CloseGate1;
33Manual_OpenGate2;
34Manual_CloseGate2;
35
36// Automatic Sequencing Logic Variables
37productDetectedAtEntry : BOOL := FALSE;
38productAtConveyor1Mid : BOOL := FALSE;
39productAtConveyor1Exit : BOOL := FALSE;
40productAtConveyor2Entry : BOOL := FALSE;
41productAtConveyor2Exit : BOOL := FALSE;
42productInExitChute : BOOL := FALSE;
43gate1FullyOpen : BOOL := FALSE;
44gate2FullyOpen : BOOL := FALSE;
45
46// Timers for sequencing and debouncing
47Timer_Conveyor1_StartDelay : TON;
48Timer_Gate1_OpenDelay : TON;
49Timer_Gate2_OpenDelay : TON;
50Timer_Product_Presence : TON;
51DebounceTimer : TON;
52DebounceTime : TIME := T#50MS;
53
54// State variables for automatic mode
55AutoState : INT := 0; // 0: Idle, 1: Waiting for Product, 2: Conveying to Gate1, 3: Waiting for Gate1 Open, 4: Through Gate1, 5: Conveying to Gate2, 6: Waiting for Gate2 Open, 7: Through Gate2, 8: Exit Chute Full, 9: Error
56
57// Error Handling
58SystemError : BOOL := FALSE;
59ErrorMessage : STRING := '';
60
61END_VAR
62
63// --- Helper Function Blocks (Conceptual - actual implementation would be separate FB instances) ---
64// FB_ConveyorControl(Input_Run, Output_Run, Input_Start, Input_Stop, Input_Overload)
65// FB_GateControl(Input_Open, Input_Close, Output_Open, Output_PositionSensor)
66// FB_SensorDebounce(Input_Raw, Output_Debounced, Timer_Instance, DebounceTime)
67
68// --- Sensor Debouncing ---
69// For simplicity, assuming direct use of sensors, but in a real system, each sensor would be debounced.
70// Example: Sensor_Entry_Debounced := FB_SensorDebounce(Input_Raw := Sensor_Entry, ...);
71
72// --- System Mode Logic ---
73IF SystemMode = 0 THEN // Manual Mode
74// Manual Conveyor Control
75IF Manual_StartConveyor1 THEN Conveyor1_Run := TRUE; END_IF;
76IF Manual_StartConveyor2 THEN Conveyor2_Run := TRUE; END_IF;
77IF Manual_StartConveyor3 THEN Conveyor3_Run := TRUE; END_IF;
78IF Manual_StopConveyors THEN
79Conveyor1_Run := FALSE;
80Conveyor2_Run := FALSE;
81Conveyor3_Run := FALSE;
82END_IF;
83
84// Manual Gate Control
85IF Manual_OpenGate1 THEN Gate1_Open := TRUE; END_IF;
86IF Manual_CloseGate1 THEN Gate1_Open := FALSE; END_IF;
87IF Manual_OpenGate2 THEN Gate2_Open := TRUE; END_IF;
88IF Manual_CloseGate2 THEN Gate2_Open := FALSE; END_IF;
89
90// Reset Automatic state variables if switching from Auto to Manual
91IF NOT Manual_StartConveyor1 AND NOT Manual_StartConveyor2 AND NOT Manual_StartConveyor3 AND NOT Manual_StopConveyors AND NOT Manual_OpenGate1 AND NOT Manual_CloseGate1 AND NOT Manual_OpenGate2 AND NOT Manual_CloseGate2 THEN
92// If no manual commands are active, potentially reset some auto vars if needed
93END_IF;
94
95ELSIF SystemMode = 1 THEN // Automatic Mode
96
97// Update internal sensor state variables
98productDetectedAtEntry := Sensor_Entry;
99productAtConveyor1Mid := Sensor_Conveyor1_Mid;
100productAtConveyor1Exit := Sensor_Conveyor1_Exit;
101productAtConveyor2Entry := Sensor_Conveyor2_Entry;
102productAtConveyor2Exit := Sensor_Conveyor2_Exit;
103gate1FullyOpen := Sensor_Gate1_Position;
104gate2FullyOpen := Sensor_Gate2_Position;
105productInExitChute := Sensor_Exit_Chute;
106
107// --- Automatic Sequencing State Machine ---
108CASE AutoState OF
1090: // Idle
110Conveyor1_Run := FALSE;
111Conveyor2_Run := FALSE;
112Conveyor3_Run := FALSE;
113Gate1_Open := FALSE;
114Gate2_Open := FALSE;
115IF productDetectedAtEntry THEN
116AutoState := 1;
117END_IF;
118
1191: // Waiting for Product at Entry
120Conveyor1_Run := TRUE; // Start conveyor to move product
121IF productAtConveyor1Mid THEN
122AutoState := 2;
123END_IF;
124IF productInExitChute THEN // Error condition: product stuck in exit
125SystemError := TRUE;
126ErrorMessage := 'Exit Chute Blocked';
127AutoState := 9;
128END_IF;
129
1302: // Conveying to Gate1
131Conveyor1_Run := TRUE;
132IF productAtConveyor1Exit THEN
133AutoState := 3;
134END_IF;
135IF productInExitChute THEN
136SystemError := TRUE;
137ErrorMessage := 'Exit Chute Blocked';
138AutoState := 9;
139END_IF;
140
1413: // Waiting for Gate1 Open
142Conveyor1_Run := FALSE; // Stop conveyor to allow gate to open
143Gate1_Open := TRUE;
144IF gate1FullyOpen THEN
145AutoState := 4;
146END_IF;
147// Timeout for gate opening
148Timer_Gate1_OpenDelay(IN := Gate1_Open, PT := T#5S);
149IF Timer_Gate1_OpenDelay.Q THEN
150SystemError := TRUE;
151ErrorMessage := 'Gate 1 Failed to Open';
152AutoState := 9;
153END_IF;
154
1554: // Through Gate1 (Product moving towards Conveyor 2)
156Conveyor1_Run := FALSE; // Keep Conveyor 1 stopped for now
157Gate1_Open := TRUE; // Keep gate open until product clears
158IF NOT productAtConveyor1Exit THEN // Product has passed exit sensor
159Gate1_Open := FALSE; // Close gate
160Timer_Gate1_OpenDelay(IN := FALSE); // Reset timer
161AutoState := 5;
162END_IF;
163IF productInExitChute THEN
164SystemError := TRUE;
165ErrorMessage := 'Exit Chute Blocked';
166AutoState := 9;
167END_IF;
168
1695: // Conveying to Gate2
170Conveyor2_Run := TRUE;
171IF productAtConveyor2Entry THEN // Product reached entry of second stage
172AutoState := 6;
173END_IF;
174IF productInExitChute THEN
175SystemError := TRUE;
176ErrorMessage := 'Exit Chute Blocked';
177AutoState := 9;
178END_IF;
179
1806: // Waiting for Gate2 Open
181Conveyor2_Run := FALSE; // Stop conveyor
182Gate2_Open := TRUE;
183IF gate2FullyOpen THEN
184AutoState := 7;
185END_IF;
186// Timeout for gate opening
187Timer_Gate2_OpenDelay(IN := Gate2_Open, PT := T#5S);
188IF Timer_Gate2_OpenDelay.Q THEN
189SystemError := TRUE;
190ErrorMessage := 'Gate 2 Failed to Open';
191AutoState := 9;
192END_IF;
193
1947: // Through Gate2 (Product moving to exit chute)
195Conveyor2_Run := FALSE; // Keep Conveyor 2 stopped
196Gate2_Open := TRUE; // Keep gate open
197IF NOT productAtConveyor2Exit THEN // Product has passed exit sensor
198Gate2_Open := FALSE; // Close gate
199Timer_Gate2_OpenDelay(IN := FALSE); // Reset timer
200Conveyor3_Run := TRUE; // Start final conveyor to exit chute
201AutoState := 0; // Return to Idle, ready for next product
202END_IF;
203IF productInExitChute THEN
204SystemError := TRUE;
205ErrorMessage := 'Exit Chute Blocked';
206AutoState := 9;
207END_IF;
208
2098: // Exit Chute Full (This state is now handled within other states for simplicity)
210// In a more complex system, this might involve stopping all upstream conveyors.
211
2129: // Error State
213Conveyor1_Run := FALSE;
214Conveyor2_Run := FALSE;
215Conveyor3_Run := FALSE;
216Gate1_Open := FALSE;
217Gate2_Open := FALSE;
218// SystemError flag is already set
219// Wait for manual reset or intervention
220
221END_CASE;
222
223// General Error Handling for Automatic Mode
224IF NOT SystemError THEN
225IF Timer_Product_Presence(IN := productDetectedAtEntry, PT := T#30S).Q THEN // Product stuck too long at entry
226SystemError := TRUE;
227ErrorMessage := 'Product Stuck at Entry';
228AutoState := 9;
229END_IF;
230END_IF;
231
232// Reset SystemError flag on manual intervention (e.g., a Reset button in manual mode)
233// IF Manual_ResetSystem THEN SystemError := FALSE; AutoState := 0; END_IF;
234
235END_IF;
236
237END_PROGRAM
Algorithm description viewbox

Conveyor Sequencing with Multiple Gates and Sensors

Algorithm description:

This program orchestrates a complex conveyor system with multiple belts, gates, and sensors. It supports both manual and automatic operational modes. In automatic mode, it implements a state machine to sequence product flow: detecting products, starting/stopping conveyors, opening/closing gates, and managing product movement through different stages. It includes error detection for blocked chutes, failed gate operations, and stalled products, transitioning to an error state for safety. This is typical in sortation systems, manufacturing lines, and material handling facilities.

Algorithm explanation:

The algorithm manages a conveyor system using a combination of manual and automatic modes. In manual mode, direct control signals are passed through. In automatic mode, a state machine (`AutoState`) governs the process. The system transitions through states like 'Idle', 'Waiting for Product', 'Conveying', 'Waiting for Gate Open', and 'Error'. Each state defines specific actions for conveyors and gates based on sensor inputs. Crucially, it incorporates timers for delays (e.g., gate opening) and timeouts to detect failures. Anti-windup is not directly applicable here, but robust error handling is paramount. The logic ensures that conveyors stop before gates open and restart only after products have passed. Time complexity is O(1) per scan cycle due to the fixed number of states and operations. Space complexity is O(1) as it uses a fixed set of variables.

Pseudocode:

PROGRAM ConveyorSequencing
VAR
    // System Mode
    SystemMode : INT // 0: Manual, 1: Automatic

    // Conveyor States
    Conveyor1_Run, Conveyor2_Run, Conveyor3_Run : BOOL

    // Gate States
    Gate1_Open, Gate2_Open : BOOL

    // Sensor Inputs
    Sensor_Entry, Sensor_Conveyor1_Mid, Sensor_Conveyor1_Exit, Sensor_Conveyor2_Entry, Sensor_Conveyor2_Exit, Sensor_Gate1_Position, Sensor_Gate2_Position, Sensor_Exit_Chute : BOOL

    // Manual Control Inputs
    Manual_StartConveyor1, Manual_StartConveyor2, Manual_StartConveyor3, Manual_StopConveyors, Manual_OpenGate1, Manual_CloseGate1, Manual_OpenGate2, Manual_CloseGate2 : BOOL

    // Automatic Sequencing Logic Variables
    productDetectedAtEntry, productAtConveyor1Mid, productAtConveyor1Exit, productAtConveyor2Entry, productAtConveyor2Exit, gate1FullyOpen, gate2FullyOpen, productInExitChute : BOOL

    // Timers
    Timer_Conveyor1_StartDelay, Timer_Gate1_OpenDelay, Timer_Gate2_OpenDelay, Timer_Product_Presence, DebounceTimer : TON
    DebounceTime : TIME

    // State variables for automatic mode
    AutoState : INT // 0: Idle, 1: Waiting for Product, 2: Conveying to Gate1, 3: Waiting for Gate1 Open, 4: Through Gate1, 5: Conveying to Gate2, 6: Waiting for Gate2 Open, 7: Through Gate2, 8: Exit Chute Full, 9: Error

    // Error Handling
    SystemError : BOOL
    ErrorMessage : STRING
END_VAR

IF SystemMode = Manual THEN
    // Handle manual controls for conveyors and gates
ELSE_IF SystemMode = Automatic THEN

    // Update internal sensor state variables
    productDetectedAtEntry := Sensor_Entry
    productAtConveyor1Mid := Sensor_Conveyor1_Mid
    // ... and so on for all sensors

    // --- Automatic Sequencing State Machine ---
    CASE AutoState OF
        Idle:
            Stop all conveyors and gates
            IF productDetectedAtEntry THEN
                AutoState := Waiting for Product
            END_IF
        Waiting for Product:
            Start Conveyor1
            IF productAtConveyor1Mid THEN
                AutoState := Conveying to Gate1
            END_IF
            IF productInExitChute THEN
                Set Error, AutoState := Error
            END_IF
        Conveying to Gate1:
            Run Conveyor1
            IF productAtConveyor1Exit THEN
                AutoState := Waiting for Gate1 Open
            END_IF
            // ... other error checks
        Waiting for Gate1 Open:
            Stop Conveyor1
            Open Gate1
            IF gate1FullyOpen THEN
                AutoState := Through Gate1
            END_IF
            IF Gate1 open timeout THEN
                Set Error, AutoState := Error
            END_IF
        Through Gate1:
            Keep Gate1 open
            IF NOT productAtConveyor1Exit THEN
                Close Gate1
                AutoState := Conveying to Gate2
            END_IF
            // ... other checks
        Conveying to Gate2:
            Start Conveyor2
            IF productAtConveyor2Entry THEN
                AutoState := Waiting for Gate2 Open
            END_IF
            // ... error checks
        Waiting for Gate2 Open:
            Stop Conveyor2
            Open Gate2
            IF gate2FullyOpen THEN
                AutoState := Through Gate2
            END_IF
            IF Gate2 open timeout THEN
                Set Error, AutoState := Error
            END_IF
        Through Gate2:
            Keep Gate2 open
            IF NOT productAtConveyor2Exit THEN
                Close Gate2
                Start Conveyor3
                AutoState := Idle
            END_IF
            // ... error checks
        Error:
            Stop all conveyors and gates
            // Wait for reset
    END_CASE

    // General error checks (e.g., product stuck too long)
    IF NOT SystemError THEN
        IF Product_Presence_Timer.Q THEN
            Set Error, AutoState := Error
        END_IF
    END_IF

END_IF

END_PROGRAM