Main Content

Prepare Chart for Simulink PLC Coder Ladder Diagram Code Generation


Ladder diagram generation from Stateflow® charts will be removed in a future release. To generate ladder diagrams, use Simulink® models instead. To create Simulink models compatible with ladder logic generation, do one of the following:

  • Use the blocks from the PLC Ladder library to create a model that is compatible with ladder diagram generation. To open the PLC Ladder library, type plcladderlib at the MATLAB command prompt.

  • Import ladder logic from a L5X file with the plcimportladder function.

To generate ladder logic from the Simulink models, use these functions: plcgeneratecode and plcgeneraterunnertb

This example shows how to prepare your Stateflow chart for Ladder Diagram code generation. Once your chart is ready, you can generate Ladder Diagram code from the chart.

For the complete Ladder Diagram code generation workflow, see Ladder Diagram Generation Workflow.

Design PLC Application with Stateflow

Use Stateflow to design state machines that model PLC controllers. Your Stateflow chart must have these properties:

  • The inputs and outputs to the chart must be Boolean. They correspond to the input and output terminals of your PLC.

  • Each state in the chart must correspond to an output. The output is true if the state is active.

    To ensure that each state in the chart is mapped to an output, in the Properties dialog box of each state, select Create output for monitoring. Then, select Self activity.

  • The transition conditions must involve only Boolean operations such as ~, &, and | between the inputs.

For instance, in the following chart, transitionCondition1, and transitionCondition2 are Boolean inputs to the model. State1, State2, and State3 correspond to Boolean outputs from the model.

Some advanced Stateflow features are not supported because of inherent restrictions in Ladder Diagram semantics. You can use the function plccheckforladder to check if the chart has the required properties. You can also use the function plcprepareforladder to change certain chart properties so that the chart is ready for Ladder Diagram code generation.

You can start generating Ladder Diagram code from the chart. See the example in Generate Simulink PLC Coder Ladder Diagram Code from Stateflow Chart.

Create Test Harness for Chart

If you want to generate a test bench for validation of the Ladder Diagram code, create a test harness for the Stateflow chart. The test harness can consist of multiple test cases. Using the test harness, Simulink PLC Coder™ can generate test benches for validation of the Ladder Diagram code.

You can manually create a test harness by using the Signal Builder block or autogenerate a test harness by using Simulink Design Verifier™. To autogenerate the test harness:

  1. Right-click the chart or a subsystem containing the chart. Select Design Verifier > Generate Tests for Subsystem.

  2. After test creation, select Create harness model.

The harness model is created. The model consists of the original subsystem coupled with inputs from a Signal Builder block. The block consists of multiple test cases, so that the states and transitions in your model are covered at least once.

You can also create tests by using other blocks from the Simulink library. However, you must ensure that the inputs to the chart are Boolean.

You can now generate Ladder Diagram code from the chart and validate the diagram.

  • To generate Ladder Diagram code only, use the original Stateflow chart.

  • To generate Ladder Diagram code with test bench, use the Stateflow chart coupled with the Boolean inputs from the test cases. For instance, if you create a harness model with Simulink Design Verifier, use the harness model for the Ladder Diagram code and test bench generation instead of the original chart.

See Generate Simulink PLC Coder Ladder Diagram Code from Stateflow Chart.