Complete Example: Weighing Control and Classification

Updated: 2025-10-20

In this example, we will see all the system elements in operation.

The model represents an automated production line where packages arrive one by one, are weighed on a scale, and based on their weight, are diverted to one path or another using a pneumatic piston.

Process Description

  1. Entry Detection
    A door sensor detects the arrival of a new package (symbolizing a product entering the zone).
    Each detection generates a new entity in GPSS-Plus.

  2. Unitary Flow Control
    A restroom acts as a unitary releaser, allowing packages to pass one at a time toward the scale.
    The next package only advances once the previous one has finished weighing.

  3. Weighing
    Upon reaching the scale, the entity activates the sensor (Device_Scale_Trigger = 1).
    When the weight stabilizes, the system assigns the read value to the entity.

  4. Classification
    If the weight exceeds a threshold (pesoLimite), the piston is activated (Device_Piston_Trigger = 1), diverting the package to the heavy zone.
    If not, it continues straight to the light zone.

  5. Synchronization with Physical Actuators
    The piston retracts automatically.
    GPSS-Plus subscribes to the piston state (Device_Piston_IsExtended) to know when it is ready for the next cycle.

System Architecture:

[Door Sensor]  -> generates entity
         ↓
 [Unitary Releaser]
         ↓
       [Scale]  <-> OPC-UA Device_Scale_Weight
         ↓
   [Separator / Piston]
         ↓
 [Light Exit] / [Heavy Exit]

Example

SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
SYSTEM {TYPE:OPTIONS, REAL_TIME:1}

Restroom {NAME:LiberadorUnitario,X:155,Y:332}

Queuer {NAME:zonaBascula,X:163,Y:32}
Graphic {NAME:lineZonaBascula,Type:LINE,color:#FF0000, X1:82,Y1:64,X2:260,Y2:65}

Restroom {NAME:RestroomBascula,X:226,Y:110}

initial pesoLimite,8

Initial options,{user:"user", pass:"1234"}

BRIDGER {NAME:bridge1,X:733,Y:556
	,SERVER:"wss://bridger.gpss-plus.com:3000"
	,CLIENT:"opc.tcp://opcua.gpss-plus.com:4840"
    ,OPTIONS:V$options
    ,on_error:bridge1_on_error
    }


POSITION {NAME:POS1,X:97,Y:437}
POSITION {NAME:POS2,X:95,Y:183}

POSITION {NAME:PosBascula,X:225,Y:199}
POSITION {NAME:PosSeparador,X:387,Y:201}
POSITION {NAME:PosPesados,X:513,Y:302}
POSITION {NAME:PosPesados2,X:594,Y:302}
POSITION {NAME:PosLigeros,X:511,Y:197}
POSITION {NAME:PosLigeros2,X:592,Y:197}

Graphic {NAME:Text1,Type:TEXT,X:167,Y:484,Text:"On OPEN"}
Graphic {NAME:TextError,Type:TEXT,X:424,Y:582,Text:"Error"}

Graphic {NAME:lineBascula,Type:LINE,color:#FF0000, X1:191,Y1:176,X2:263,Y2:176}
Graphic {NAME:txtBascula,Type:TEXT,X:226,Y:156,Text:"bascula"}

Graphic {NAME:lineSeparador,Type:LINE,color:#FF0000, X1:363,Y1:201,X2:432,Y2:201}
Graphic {NAME:txtSeparador,Type:TEXT,X:394,Y:143,Text:"separador"}

START 10

;*****************************************************

PROCEDURE PRE_RUN
    BRIDGE_SUBSCRIPTION { NAME: bridge1
	, VARIABLE: "Sensor_Door_IsOpen"
    , TRIGGER: on_open 
    }
savevalue pesoActual,0
savevalue pesoAnterior,X$pesoActual

	BRIDGE_SUBSCRIPTION { NAME: bridge1
        , VARIABLE: "Device_Scale_Weight"
        , TRIGGER: on_subscription_bascula 
        }
	BRIDGE_SUBSCRIPTION { NAME: bridge1
        , VARIABLE: "Device_Piston_IsExtended"
        , TRIGGER: on_subscription_piston 
        }

ASSIGN values, { Device_Scale_Trigger: 0 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
 
	TERMINATE_VE
ENDPROCEDURE

;*****************************************************
GENERATE 0,0,0,0 {NAME:GEN1,X:91,Y:547}

ADVANCE 5,0 {TO:POS1,flow:1}
ADVANCE 5,0 {TO:LiberadorUnitario,flow:1}
if (R$(zonaBascula,in)>0)
	rest LiberadorUnitario
endif
queue zonaBascula

ADVANCE 5,0 {TO:POS2,flow:1}
ADVANCE 5,0 {TO:PosBascula,flow:1}

ASSIGN values, { Device_Scale_Trigger: 1 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}

rest RestroomBascula
depart zonaBascula
call liberarRestroomBascula

ADVANCE 2
mod {subtitle:"P$peso Kg"}
if (P$peso > X$pesoLimite)
    mod {color:"red"}
else
    mod {color:"green"}
endif

ASSIGN values, { Device_Scale_Trigger: 0 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}

timeout moverPiston,4.3,P$peso

ADVANCE 5,0 {TO:PosSeparador,flow:1}

if (P$peso > X$pesoLimite)
    ADVANCE 3,0 {TO:PosPesados}
	ADVANCE 3,0 {TO:PosPesados2,flow:1}
else
    ADVANCE 3,0 {TO:PosLigeros}
 	ADVANCE 3,0 {TO:PosLigeros2,flow:1}
endif

ENDGENERATE 1
;----------------------------------------------------------------------------

procedure on_open
       move {name:Text1,TEXT:"DOOR P$(PARAM_A.VALUE)"}
       if (P$(PARAM_A.VALUE.EXIST))
       if (P$(PARAM_A.VALUE)==1)
    	NEW GEN1
    	endif
       endif
	TERMINATE_VE
endprocedure

procedure bridge1_on_error
    move {name:TextError,TEXT:"P$PARAM_A"}
	TERMINATE_VE
endprocedure
;-------------------------
procedure on_subscription_bascula
	savevalue pesoActual,P$(PARAM_A.VALUE)
    move {name:txtBascula,TEXT:"X$pesoActual Kg"}
    if (ABS(X$pesoAnterior-X$pesoActual)<=0.1 && X$pesoActual>1)
	    FOREACH entidad, IN_RESOURCE, RestroomBascula
	    	assign peso,round(X$pesoActual,1), P$entidad
		ENDFOREACH
    
	    wake RestroomBascula
    endif
	savevalue pesoAnterior,X$pesoActual

	TERMINATE_VE
endprocedure
;-------------------------
procedure on_write
	TERMINATE_VE
endprocedure

procedure on_subscription_piston
	if (P$(PARAM_A.VALUE)==1)
    move {name:lineSeparador,Y2:220}
    else
    move {name:lineSeparador,Y2:200}
    endif
	move {name:txtSeparador,text:"Piston P$(PARAM_A.VALUE)"}
	TERMINATE_VE
endprocedure

procedure liberarRestroomBascula
	assign hecho,0
	    FOREACH entidad, IN_RESOURCE, zonaBascula
            if (P$hecho==0)
	    	wake RestroomBascula,P$entidad
            endif
            assign hecho,1
		ENDFOREACH
endprocedure

procedure moverPiston
    if (P$PARAM_A>X$pesoLimite)
     ASSIGN values, { Device_Piston_Trigger: 1 }
     BRIDGE_WRITE { name:bridge1, VALUES:V$values, TRIGGER:on_write }
	assign tmp,"heavy"
    else 
    assign tmp,"light"
    endif
	TERMINATE_VE
endprocedure