Main Content

Simulink PLC Coder Ladder Diagram Code Generation

Note

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

Ladder Diagram (LD) is a graphical programming language used to develop software for programmable logic controllers (PLCs). It is one of the languages that the IEC 61131 Standard specifies for use with PLCs.

A program in Ladder Diagram notation is a circuit diagram that emulates circuits of relay logic hardware. The underlying program uses Boolean expressions that translate readily to switches and relays. When you program complex applications directly in Ladder Diagram notation, it is challenging because you must write the programs with only Boolean variables and expressions.

Using Simulink PLC Coder™, you can generate Ladder Diagram code for your applications from a Stateflow chart (Stateflow). The benefits are:

  • You can design your application by using states and transitions in a Stateflow chart. Once you complete the design, you can generate Ladder Diagram code in XML or another format. You then import the generated code to an IDE such as CODESYS 3.5 or RSLogix™ AOI 5000 and view the Ladder Diagram.

  • When you test your Stateflow chart by using a set of inputs, you can reuse these inputs to create a test bench for the Ladder Diagram code. You import the test bench to your PLC IDE and compare the results of simulation with the results of running the Ladder Diagram. If the results agree, the original Stateflow chart is equivalent to the generated Ladder Diagram code.

The figure shows a simple Stateflow chart with three states and two transitions. Based on the transition conditions, the chart transitions from one state to another.

The state State1 is active as long transitionCondition1 and transitionCondition2 are not true. This means, State1 is active in one of these two cases:

  • The chart has started execution through the default transition.

  • The previous active state is also State1 and the conditions transitionCondition1 and transitionCondition2 are false.

State3 is active in one of these two cases:

  • The previous active state is State1, transitionCondition1 is false, and transitionCondition2 is true.

  • The previous active state is also State3. State3 is a terminating state.

You can import the generated Ladder Diagram code to CODESYS 3.5 and view the diagram. A portion of the Ladder Diagram is shown.

In the preceding Ladder Diagram, each rung of the ladder ends in a coil. The coil corresponds to a state of the original chart. The contacts before the coil determine if the coil receives power. You can compare the Ladder Diagram visually with the Stateflow chart. For instance, the coil State1_new receives power in one of these two cases:

  • The normally open contact State1 is closed and the normally closed contacts transitionCondition1 and transitionCondition2 are open.

  • The normally open contact stateflow_init is closed. This contact corresponds to the default transition.

Once the coil State1_new receives power, the contact State1_new further down in the ladder is then closed and the coil State1 receives power.

The Ladder Diagram executes from top to bottom and from left to right.

Ladder Diagram Generation Workflow

  1. Before generating Ladder Diagram code from your Stateflow chart, confirm that your chart is ready for code generation.

    See Prepare Chart for Simulink PLC Coder Ladder Diagram Code Generation.

  2. Generate Ladder Diagram code from the Stateflow chart. The code is in a format suitable for import to an IDE.

    Generate a test bench along with the code. The test bench is in the Structured Text language. You can later import the code along with the test bench to your IDE. The test bench invokes the Ladder Diagram code and compares the output against the expected outputs from the original Stateflow chart.

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

  3. Import the generated Ladder Diagram code to your CODESYS 3.5 IDE. Validate the diagram in the IDE by using the generated test bench.

    See Import Ladder Diagram Code to CODESYS 3.5 IDE and Validate Diagram.