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:
Generate code from a Stateflow chart that you can view as Ladder Diagram in your IDE.
Generate test bench for validation of the Ladder Diagram code in your IDE.
For the complete Ladder Diagram code generation workflow, see Ladder Diagram Generation Workflow.
The figure shows a Stateflow chart that implements three-aspect logic, a decision logic for many railway signaling applications.
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 can take place. For instance, the
state Red
becomes active in the following 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
, the conditions that allow transition
to Red
are true, and the 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 figure shows a portion of the Ladder Diagram code generated from the chart when viewed in the CODESYS 3.5 IDE. The Ladder Diagram consists of contacts (normally open and normally closed) and coils (normal, set, and reset).
You can map elements of the original Stateflow chart to these coils and contacts. For instance, the coil
Red_new
corresponds to the update of the state Red
in the
Stateflow chart. For the coil to receive power, one of 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 chart.
Transitions from other colors: The normally open
contact Green
must be closed and the following must be true:
The normally open contact T_2_3_trans
must be closed. This contact
corresponds to the chart condition that must be true for transition to the
Red
state.
The normally closed contacts T_2_1_trans
and
T_2_2_trans
must stay closed. These contacts correspond to the chart
condition that must be false for transition to the Red
state. If the
conditions are true, the contacts open and the coil
no longer receives power.
Staying red: The normally open contact
Red
must be closed, and the normally closed contacts
T_4_1_trans
and T_4_2_trans
must stay closed. These
contacts correspond to the chart conditions that must be false for the Red
state to continue to be active. If the conditions are true, the contacts open and the coil
no longer receives power.
To generate Ladder Diagram code from the model
plcdemo_ladder_three_aspect
:
Open the model.
Open the PLC Coder app. Click the PLC Code tab.
Specify the target IDE for which to generate the Ladder Diagram code.
Click Settings. Specify a supported IDE for the option Target IDE. See IDEs Supported for Ladder Diagram Code Generation. Click OK.
Right-click the chart and select PLC Code > Generate Ladder Logic for Chart.
If code generation is successful, in the subfolder
plcsrc
of the current working folder, you see the file
. You import this file to your IDE
and view the Ladder Diagram. For the CODESYS 3.5 IDE, see Import Ladder Diagram Code to CODESYS 3.5 IDE and Validate Diagram.ModelName
.xml
You can also use the function plcgenerateladder
to generate Ladder Diagram
code from a Stateflow chart.
You can generate a test bench to validate the generated Ladder Diagram code. You import the code together with the test bench in your IDE and validate the Ladder Diagram against the original Stateflow chart using the test bench. To generate test bench along with the Ladder Diagram code:
Open the PLC Coder app. Click the PLC Code tab.
Click Settings. Select the option Generate Testbench for Subsystem.
Right-click the chart and select PLC Code > Generate Ladder Logic for Chart.
The test benches use the inputs to the original Stateflow chart. Therefore, you can create test harnesses for the original chart and reuse them for validation of the Ladder Diagram code.
You can also use the function plcgenerateladder
to generate test
benches.
After generating the Ladder Diagram code and the test benches, you can import them to your IDE. For the CODESYS 3.5 IDE, see Import Ladder Diagram Code to CODESYS 3.5 IDE and Validate Diagram.