Tutorial 11 of 11
Control (PLC) – run a PLC program inside the factory simulation
Advanced 15 min
Bind a Structured Text, ladder or state-machine program to stations and buffers, run it in lock-step with the simulation and compare the line with and without control.
Real lines are not only machines and buffers. A PLC decides when a station may start, when the infeed releases parts and where a diverter sends them. The Control (PLC) module adds that layer to a scenario. A small PLC program reads sensors from the model, such as part present, buffer full or machine busy, and switches actuators, such as a station's run permit, a source's release or a split's divert. It runs inside the discrete-event simulation: every scan cycle is an event, so the program and the factory move in lock-step.
This is a simulation only. Nothing connects to a real controller.
Open the example
Open the template PLC-controlled line from the start screen, then choose Analyze → Control (PLC)…. The scenario With PLC buffer-level control already holds a program:
IF Level >= High THEN
Feed := FALSE;
ELSIF Level <= Low THEN
Feed := TRUE;
END_IF;
Level is a VAR_INPUT bound to the Count (number) of the transfer buffer B1. Feed is a VAR_OUTPUT bound to Release parts of the infeed S1. With High = 4 and Low = 1 the infeed stops releasing parts when four parts wait in B1, and resumes when only one is left.
The check below the bindings says The program compiles and lists the number of inputs, outputs and bindings. Press Test run. Twinworks simulates one replication with the program and one without it. The table With and without the PLC shows the same number of good parts, but far fewer parts waiting in B1 (in our run an average of 1.66 instead of 3.71). The parts wait at the infeed instead, which is often what you want in front of a sensitive process. The PLC trace shows Level and Feed over time. Choose a Trace window of Whole run to see the full shift.
Settings
- Run this program during simulations: when this is on, every run of the scenario uses the program, including experiments, sweeps, the API and MCP. The template's second scenario, Without PLC control, has it switched off.
- Language: Structured Text, Ladder (text rungs) or State machine. Ladder and state machines are compiled to Structured Text. Open Generated Structured Text and ladder diagram to see the code and the rung drawing.
- Scan cycle time (ms of simulated time): how often the program runs. Shorter cycles react faster but make long runs slower.
Bindings
Each row binds one program variable to one object:
- Sensor → input feeds a
VAR_INPUT. Process steps offer Busy, Idle, Blocked, Down, Part present and Count. Buffers offer Part present, Empty, Full and Count. Sources offer Parts waiting to be released and Count. - Output → actuator takes a
VAR_OUTPUT. Run permit (may start new parts) holds a station: it finishes its current part but accepts no new one. Release parts holds parts at a source. Divert to connection (0, 1, …) forces a split onto one outgoing connection.
Suggest from program adds one row for every unbound input and output, and you then choose the object and the signal. Problems are listed in plain words, for example a variable that is not declared or a signal that the chosen object does not have.
More examples
Choose one under Examples and press Load example:
- Start/stop with interlock (seal-in rung): one ladder rung,
(Start | Run) & !Stop & !BufferFull -> Run. Station M2 runs until B2 is full or M3 breaks down, and starts again only when B2 is empty. - Two-station handshake (state machine): states Wait, Transfer and Process. M2 loads one part at a time, and M1 only starts while B1 is empty. Look at the State lane in the trace.
Apply saves the program in the scenario as one undo step. You can test a program on its own, with an input timeline instead of the factory, in the twinworks-plc-simulator tool. It runs the same compiler and the same scan cycle.
Ready to try it?