MATLAB Examples

# Generate Ladder Logic Diagrams from Stateflow Charts and Validate Them

This example shows how to generate ladder logic diagrams from Stateflow charts and verify the generated ladder in the target PLC IDE using a test bench.

## Contents

Open up the "Subsys" subsystem. Open the Stateflow® chart named "3Aspect" chart inside it. This chart implements three-aspect logic, a decision logic that is used in many railway signalling applications.

## Stateflow Chart and Ladder Logic Diagram

The following figure shows the Stateflow chart logic.

The chart consists of five states: Init, Fault, Red, Yellow and Green. Based on the input to the chart, transitions to any of these states may take place. For example, the state Red becomes active in the scenarios:

• Initialization and power up : The previous state is Init and the condition Power_up is true.
• Fault rectification: The previous state is Fault and the condition VLDHealthy & FaultRectified is true.
• Transitions from other colors: The previous state is Green or Yellow and the conditions that allow transition to Red are true and conditions that allow transition to another color or to the Fault state are false.
• Staying Red: The previous state is Red and the conditions that allow transition to another state are false.

The following figure shows the ladder logic diagram generated from the chart and then imported to the CODESYS 3.5 IDE.

The ladder diagram is composed of contacts (normal and negated) and coils (normal, set and reset). For example, a negated contact like Power_Up is closed if the variable Power_up is true.

You can map elements of the Stateflow chart to coils and contacts in the ladder diagram above. The coil Red_new corresponds to the state Red in the Stateflow chart. For the coil to receive power, the following must be true:

• Initialization and power up : The normally-open contacts Init and Power_up must be closed.
• Fault rectification: The normally-open contacts Fault and T_1_1_trans must be closed. The contact T_1_1_trans represents the transition condition VLDHealthy & FaultRectified in the Stateflow chart.
• Transitions from other colors: The normally-open contact Green must be closed. The contact T_2_3_trans corresponds to the Stateflow chart condition that must be true for transition to the Red state. The contact is normally-open and must be closed. The contacts T_2_1_trans and T_2_2_trans corresponds to the Stateflow chart conditions that must be false for transition to the Red state. These contacts are normally-closed. If the conditions in the Stateflow chart are true, the contacts open and the coil no longer receives power.
• Staying Red: The normally-open contact Red must be closed. The contacts T_4_1_trans and T_4_2_trans corresponds to the Stateflow chart conditions that must be false for the Red state to continue to be active. These contacts are normally-closed. If the conditions in the Stateflow chart are true, the contacts open and the coil no longer receives power.

## Step 1: Generate Ladder Logic diagram from Stateflow Chart

Specify the target IDE for which the ladder diagram is to be generated. In the Simulink Editor, right-click the Stateflow chart and select PLC Code > Options. In the configuration window that opens up, specify the Target IDE as 3S Codesys 3.5.

Generate ladder logic diagram from the chart. The generated diagram file can be used for import to the target IDE that you specified in PLC Configuration for the model/chart.

`ans =`
`1x3 cell array`
`'plcsrc\plcdemo_la...'    'plcsrc\plcdemo_la...'    'plcsrc\plcdemo_la...'`

Alternatively, in the Simulink Editor, right-click the Stateflow chart and select PLC Code > Generate Ladder Logic for Chart.

## Step 2: Verify Generated Ladder Diagram Using Test Bench

It is also possible to incorporate a testbench in the generated ladder diagram file. You can then import the ladder diagram into your IDE and test the generated ladder logic against the automatically generated testbench. The testbench essentially tests the ladder diagram against the test inputs that you provide to the stateflow chart in Simulink.

`ans =`
`1x3 cell array`
`'plcsrc\plcdemo_la...'    'plcsrc\plcdemo_la...'    'plcsrc\plcdemo_la...'`