Main Content

Generate Structured Text Code for Shuttle and Robot Control

This example shows you how to generate and verify structured text code for the control system of a virtual assembly line that has two robotic work cells and a shuttle that goes between the robotic cells. The assembly line is based on a modular industrial framework created by ITQ GmbH known as Smart4i. This system consists of two robotic work cells that are connected by a shuttle track and a conveyor belt. One robot places cups onto the shuttle, and the other robot places balls in the cups. The control system is modeled using Stateflow®.

Download Support Files

This example uses the assembly line CAD data, robot RBT files, and other supporting files. Download and unzip the data set.

downloadFolder = matlab.internal.examples.downloadSupportFile("R2023a/sl3d/examples/",...

Open the Model

To open the model, run:


The model has six main parts:

  • Scene Creation & Configuration - Loads, configures, and sets up the environment for the Unreal Engine™.

  • System Control Logic - Push buttons and constants to control the process.

  • Actor Motion Control - Stateflow® charts with sequences to control the robot cells and the shuttles.

  • Sensor Input - Uses Simulink 3D Animation™ to take the camera image at the robot arm's end and create a bounding box.

  • Robot Kinematics - Uses the Robotics System Toolbox™ to compute robot kinematics using their rigid body model, and the outputs are used to animate the robots in Unreal Engine™.

  • Animation Control - Uses the outputs from the Actor Motion Control, Sensor Input, and Robot Kinematics blocks to animate the entire virtual automation line.

Motion Control

This block contains Stateflow® charts that control the robot cells and the shuttles that travel between the robot cells. Inside the Stateflow® chart, there are three main sections:


  • Robot Control Logic - The charts in this section control the motion of the two robot manipulators. the charts output three-dimensional vectors that contain the end-effector translations. The chart for Robot 2 also controls the creation of new ball instances so that the balls may be picked up.

  • Cup Instance Logic - This chart controls the destruction of old cups after the conveyor belt drops the cups into the container.

  • Shuttle Control Logic - the charts in this section control the motion of each of the four shuttles. The charts output three-dimensional vectors containing the specified shuttle XY position and the rotation about the Z-axis.

Generate Structured Text Code

To generate structured text code select the Stateflow chart Chart and then,

  1. In the Apps tab, click PLC Coder.

  2. In the PLC Code tab, click Settings > PLC Code Generation Settings. Change the Target IDE to 3S CoDeSys 2.3. Click OK.

  3. In the PLC Code tab, click Generate PLC Code.

Alternatively, to generate structured text code from the MATLAB command line, use the plcgeneratecode function.

### Generating PLC code for 'Smart4i_PLC/Chart'.
### Using model settings from 'Smart4i_PLC' for PLC code generation parameters.
### Begin code generation for IDE codesys23.
### Emit PLC code to file.
### Creating PLC code generation report Smart4i_PLC_codegen_rpt.html.
### PLC code generation successful for 'Smart4i_PLC/Chart'.
### Generated files:

Verify Structured Text Code

Before you verify the generated structured text code set the PLC_AbsTimeTemporalLogic to counter to use the target independent counter.


To verify the structured text code, select the Stateflow chart Chart and then:

  1. In the Apps tab, click PLC Coder.

  2. In the PLC Code tab, click Settings > PLC Code Generation Settings. Enable Generate testbench for subsystem.

  3. Select the Settings button and click Import Code into IDE and Verify Code in IDE.

  4. Click Generate PLC Code.

When Simulink PLC Coder™ completes the code generation it imports the generated code into the 3S CoDeSys 2.3 IDE, runs the testbench and displays the results.

This option to verify the generated code in the IDE is available only for the 3S CoDeSys 2.3 IDE.

See Also


Related Examples

More About