Programming the Sorting Station: Logic and Flow Control
Programming the Sorting Station: Logic and Flow Control
Today marked a significant milestone in the development of the Sorting Station, both in terms of logical control structure and physical layout. The original design included three slides for sorting workpieces by colour. However, the excess slide (Slide 3) has now been replaced with a conveyor system that transports extra workpieces to a de-assembly station, reflecting a more realistic and modular manufacturing workflow. This blog post outlines the complete behaviour of the station, explains the decision logic implemented in TIA Portal, and highlights the integration of sensors and simulation elements in Siemens NX MCD.
The control system is built around a step-based CASE structure using SCL, with each step representing a specific state in the sorting process. The logic was carefully structured to ensure reliable detection, sorting, and handling of all workpiece conditions.
Steps 0–20: Initialisation and Detection
All actuators are reset.
The stopper is extended to block the incoming part.
If a workpiece is detected by the workpiece sensor, both conveyor tracks (SORT_TS_1 and SORT_TS_2) are activated. (In future testing, if using the workpiece sensor to control the conveyor fails, it may be a better idea to use Lightgate at the end of the previous (Pick And Place) station. With that logic, when the workpiece leaves the Pick And Place station, it will activate the conveyors at the Sorting Station.)
If the workpiece detection sensor triggers, both conveyors (SORT_TS_1 and SORT_TS_2) are activated, and a delay is introduced to allow the part to reach the stopper. This delay helps synchronise the mechanical motion with logic flow and prevent premature sensor readings.
Step 30: Colour Assignment Logic
This is a crucial step. The colour of the incoming workpiece is identified using a combination of two sensors:
A Red sensor detects the presence of any red-colored surface.
An Inductive sensor detects metallic properties.
These sensors are configured with the Prevent Collision feature in NX MCD, which helps ensure accurate interaction without interfering with object motion.
The logic assigns an integer value to the #MyCurrentPuck variable:
If only the Red sensor is active → RED (2)
If both Red and Inductive sensors are active → SILVER (3)
If neither sensor detects → BLACK (1)
This allows for a reliable classification of the workpiece before it’s sorted.
IF "SORT_SENSOR_Red" AND NOT "SORT_SENSOR_Inductive" THEN
#MyCurrentPuck := 2;
ELSIF "SORT_SENSOR_Red" AND "SORT_SENSOR_Inductive" THEN
#MyCurrentPuck := 3;
ELSE
#MyCurrentPuck := 1;
END_IF;
Step 40: Order Evaluating and Routing
Once the colour is known, the system uses HMI-defined targets to decide which slide needs the part. These orders can be set manually by the operator for both Slide 1 and Slide 2 via the HMI.
If neither slide needs the current colour, the workpiece is passed forward to the conveyor belt for de-assembly. Each workpiece type has a separate evaluation sequence. For example:
IF #Slide1.Black < "HMIVars".Slide1_HMI_Selection.Black THEN
#Slide1.Black := #Slide1.Black + 1;
#Step := 100;
ELSIF #Slide2.Black < "HMIVars".Slide2_HMI_Selection.Black THEN
#Slide2.Black := #Slide2.Black + 1;
#Step := 200;
ELSE
#Slide3.Total := #Slide3.Total + 1;
#Step := 300;
END_IF;
Steps 100–210: Separator Engagement
Based on the assigned destination, the appropriate separator is engaged:
Step 100 extends Separator 1 for Slide 1.
Step 200 prepares and extends Separator 2 for Slide 2.
Additionally, to correctly route the workpiece to Slide 2 using Separator 2, it is essential to know the current position of Separator 1 to ensure it does not block the workpiece if extended. Each action is followed by sensor confirmation before releasing the part.
Step 300: Workpiece Release
Once the separator is ready, the stopper retracts, allowing the workpiece to roll into the correct slide or move further to the De-assemble Station.
Steps 310–320: Reflex Sensor Check
A reflex sensor checks whether the part has passed into a slide. A timer confirms its presence; if not detected, the system proceeds to Step 330.
Step 330: Reset State
All actuator states are cleared, and the system returns to the start of the sequence (Step := 0;).
Reset Function
The HMI Reset Button clears all order counters, system flags, and actuator states. This is useful during station reinitialization or after completing an order (to be moved to the HMI Function Block in future development).
After finalising the logic, I created corresponding HMI inputs for the user to define the number of red, black, and silver pucks required for both Slide 1 and 2. This added flexibility to the sorting operation and allowed complete user control over distribution targets.
At the current stage, the design of the HMI screen is very basic. I haven’t yet defined the final layout or visual design, as my main goal was ensuring the functionality works correctly. I plan to refine and enhance the HMI interface as the project progresses, making it more intuitive and visually straightforward for the operator.
After programming and logic testing, I moved to Siemens NX to simulate the Sorting Station. The goal was to verify actuator motion and validate that the workpieces could be sorted physically.
However, I quickly ran into serious problems. The moment I added collision bodies to guide the workpieces into slides, the entire system began to shake and behave erratically.
Probable Causes:
The assembly contains too many faces, particularly on STL or imported geometry.
There are likely too many rigid body definitions created at an early stage, many of which may now be conflicting.
Simply hiding components did not solve the issue, as the physical definitions remained active.
After evaluating the problem, I’ve decided on a complete rework of the simulation setup:
Delete all rigid and collision bodies currently defined.
Retain only necessary sensors and actuators.
Simplify the geometry by removing small faces or merging surfaces to reduce collision complexity.
Rebuild collision and rigid bodies from scratch with better-defined constraints and fewer interacting elements.
Although this will take some time, it’s the best path forward to achieve stable simulation behaviour.
Today marked an important milestone in programming the Sorting Station’s logic and HMI integration. The system now responds dynamically to user input and can make automated decisions based on the current distribution of workpieces.